Fraxlend (Frax Finance) contest - 0xNazgul's results

Fraxlend: A permissionless lending platform and the final piece of the Frax Finance Defi Trinity.

General Information

Platform: Code4rena

Start Date: 12/08/2022

Pot Size: $50,000 USDC

Total HM: 15

Participants: 120

Period: 5 days

Judge: Justin Goro

Total Solo HM: 6

Id: 153

League: ETH

Frax Finance

Findings Distribution

Researcher Performance

Rank: 29/120

Findings: 2

Award: $99.85

๐ŸŒŸ Selected for report: 0

๐Ÿš€ Solo Findings: 0

[NAZ-L1] Value Range Validity

Severity Low Context: FraxlendPairCore.sol#L193, FraxlendPairCore.sol#L235-L236, FraxlendPair.sol#L215

Description: These functions doesn't have any checks to ensure that the variables being set is within some kind of value range.

Recommendation: Each variable input parameter updated should have it's own value range checks to ensure their validity.

[NAZ-L2] Missing Equivalence Checks in Setters

Severity: Low Context: FraxlendWhitelist.sol#L50, FraxlendWhitelist.sol#L65, FraxlendWhitelist.sol#L80, FraxlendPair.sol#L204, FraxlendPair.sol#L215

Description: Setter functions are missing checks to validate if the new value being set is the same as the current value already set in the contract. Such checks will showcase mismatches between on-chain and off-chain states.

Recommendation: This may hinder detecting discrepancies between on-chain and off-chain states leading to flawed assumptions of on-chain state and protocol behavior.

[NAZ-L3] Missing Zero-address Validation

Severity: Low Context: FraxlendPairCore.sol#L172-L175, FraxlendPairCore.sol#L190-L191, FraxlendPairCore.sol#L225, FraxlendPair.sol#L204, FraxlendPair.sol#L274, FraxlendPairDeployer.sol#L104-L107

Description: Lack of zero-address validation on address parameters may lead to transaction reverts, waste gas, require resubmission of transactions and may even force contract redeployments in certain cases within the protocol.

Recommendation: Consider adding explicit zero-address validation on input parameters of address type.

[NAZ-L4] Missing Events In Initialize Functions

Severity: Low Context: FraxlendPairCore.sol#L245

Description: None of the initialize functions emit emit init-specific events. They all however have the initializer modifier (from Initializable) so that they can be called only once. Off-chain monitoring of calls to these critical functions is not possible.

Recommendation: It is recommended to emit events in your initialization functions.

[NAZ-N5] Unintuitive Variable Name

Severity: Informational Context: FraxlendPairCore.sol#L602 (mint => lendAssets)

Description: The variable name is either not as it describes or can be more descriptive.

Recommendation: Consider changing all occurrences of these variables to be more intuitive.

[NAZ-N6] Function && Variable Naming Convention

Severity Informational Context: VaultAccount.sol#L16, VaultAccount.sol#L33, FraxlendPairCore.sol#L81-L89, FraxlendPair.sol#L160-L167, FraxlendPairDeployer.sol#L49-L51, FraxlendPairDeployer.sol#L57-L62

Description: The linked variables do not conform to the standard naming convention of Solidity whereby functions and variable names(local and state) utilize the mixedCase format unless variables are declared as constant in which case they utilize the UPPER_CASE_WITH_UNDERSCORES format. Internal functions should lead with an underscore.

Recommendation: Consider naming conventions utilized by the linked statements are adjusted to reflect the correct type of declaration according to the Solidity style guide.

[NAZ-N7] Code Contains Empty Blocks

Severity: Informational Context: VariableInterestRate.sol#L57, FraxlendWhitelist.sol#L40, FraxlendPair.sol#L68, FraxlendPairDeployer.sol#L406

Description: It's best practice that when there is an empty block, to add a comment in the block explaining why it's empty.

Recommendation: Consider adding /* Comment on why */ to the empty blocks.

[NAZ-N8] Code Structure Deviates From Best-Practice

Severity: Informational Context: All Contracts

Description: The best-practice layout for a contract should follow the following order: state variables, events, modifiers, constructor and functions. Function ordering helps readers identify which functions they can call and find constructor and fallback functions easier. Functions should be grouped according to their visibility and ordered as: constructor, receive function (if exists), fallback function (if exists), external, public, internal, private. Some constructs deviate from this recommended best-practice: Events are in the middle of contracts. External/public functions are mixed with internal/private ones.

Recommendation: Consider adopting recommended best-practice for code structure and layout.

[NAZ-N9] Unindexed Event Parameters

Severity Informational Context: FraxlendPairCore.sol#L376-L382, FraxlendPairCore.sol#L389, FraxlendPair.sol#L200, FraxlendPair.sol#L228, FraxlendPair.sol#L268

Description: Parameters of certain events are expected to be indexed so that theyโ€™re included in the blockโ€™s bloom filter for faster access. Failure to do so might confuse off-chain tooling looking for such indexed events.

Recommendation: Consider adding the indexed keyword to event parameters that should include it.

[NAZ-N10] Use Underscores for Number Literals

Severity: Informational Context: LinearInterestRate.sol#L34, VariableInterestRate.sol#L35-L36, VariableInterestRate.sol#L40-L42, FraxlendPairConstants.sol#L41, FraxlendPairCore.sol#L194, FraxlendPairDeployer.sol#L49, FraxlendPairDeployer.sol#L51, FraxlendPairDeployer.sol#L171, FraxlendPairDeployer.sol#L173-L174

Description: There are multiple occasions where certain numbers have been hardcoded, either in variables or in the code itself. Large numbers can become hard to read.

Recommendation: Consider using underscores for number literals to improve its readability.

[NAZ-N11] Spelling Errors

Severity: Informational Context: VariableInterestRate.sol#L32 (calulcating => calculating), FraxlendWhitelist.sol#L47 (oralce => oracle), FraxlendPairCore.sol#L231 (whitelist => whitlist), FraxlendPairCore.sol#L893 (occured => occurred), FraxlendPairCore.sol#L896 (liabilites => liabilities), FraxlendPairCore.sol#L945 (Liquidate => LiquidateClean), FraxlendPairCore.sol#L992 (calced => Calculated), FraxlendPairCore.sol#L992 (calcs => calculations), FraxlendPairCore.sol#L1010 (stuct => struct), FraxlendPair.sol#L286 (whos => who's), FraxlendPair.sol#L287 (approcal => approval), FraxlendPair.sol#L305 (whos => who's), FraxlendPair.sol#L306 (approcal => approval), FraxlendPairDeployer.sol#L168 (accomodate => accommodate)

Description: Spelling errors in comments can cause confusion to both users and developers.

Recommendation: Consider checking all misspellings to ensure they are corrected.

[NAZ-N12] Floating Pragma

Severity: Informational Context: All Contracts

Description: Contracts should be deployed with the same compiler version and flags that they have been tested with thoroughly. Locking the pragma helps to ensure that contracts do not accidentally get deployed using, for example, an outdated compiler version that might introduce bugs that affect the contract system negatively.

Recommendation: Consider locking the pragma version.

[NAZ-G1] Use ++index instead of index++ to increment a loop counter

Context: SafeERC20.sol#L27, FraxlendWhitelist.sol#L51, FraxlendWhitelist.sol#L66, FraxlendWhitelist.sol#L81, FraxlendPair.sol#L289, FraxlendPair.sol#L308

Description: Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation: Use ++index to increment a loop counter.

[NAZ-G2] The Increment In For Loop Post Condition Can Be Made Unchecked

Context: SafeERC20.sol#L27, FraxlendWhitelist.sol#L51, FraxlendWhitelist.sol#L66, FraxlendWhitelist.sol#L81, FraxlendPairCore.sol#L265, FraxlendPairCore.sol#L270, FraxlendPair.sol#L289, FraxlendPair.sol#L308

Description: (This is only relevant if you are using the default solidity checked arithmetic). i++ involves checked arithmetic, which is not required. This is because the value of i is always strictly less than length <= 2**256 - 1. Therefore, the theoretical maximum value of i to enter the for-loop body is 2**256 - 2. This means that the i++ in the for loop can never overflow. Regardless, the overflow checks are performed by the compiler.

Unfortunately, the Solidity optimizer is not smart enough to detect this and remove the checks. One can manually do this by:

for (uint i = 0; i < length; ) {
    // do something that doesn't change the value of i
    unchecked {
        ++i;
    }
}

Note that itโ€™s important that the call to unchecked_inc is inlined. This is only possible for solidity versions starting from 0.8.2.

Recommendation: Consider doing the increment in the for loop post condition in an unchecked block.

[NAZ-G3] Catching The Array Length Prior To Loop

Context: FraxlendWhitelist.sol#L51, FraxlendWhitelist.sol#L66, FraxlendWhitelist.sol#L81, FraxlendPairCore.sol#L265, FraxlendPairCore.sol#L270, FraxlendPair.sol#L289, FraxlendPair.sol#L308

Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.

Recommendation: Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.

[NAZ-G4] Setting The Constructor To Payable

Context: All Contracts

Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.

Recommendation: Set the constructor to payable.

[NAZ-G5] Use of Custom Errors Instead of String

Context: LinearInterestRate.sol, FraxlendPairDeployer.sol

Description: To save some gas the use of custom errors leads to cheaper deploy time cost and run time cost. The run time cost is only relevant when the revert condition is met.

Recommendation: Use Custom Errors instead of strings.

[NAZ-G6] Function Ordering via Method ID

Context: All Contracts

Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation: Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax ยฉ 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter