Alchemix contest - 0xNazgul's results

A protocol for self-repaying loans with no liquidation risk.

General Information

Platform: Code4rena

Start Date: 05/05/2022

Pot Size: $125,000 DAI

Total HM: 17

Participants: 62

Period: 14 days

Judge: leastwood

Total Solo HM: 15

Id: 120

League: ETH

Alchemix

Findings Distribution

Researcher Performance

Rank: 25/62

Findings: 2

Award: $305.02

🌟 Selected for report: 0

🚀 Solo Findings: 0

Lack of Event Emission For Critical Functions

Severity: Low Context: gALCX.sol#L39-L41, gALCX.sol#L46-L59

Description: Several functions update critical parameters that are missing event emission. These should be performed to ensure tracking of changes of such critical parameters.

Recommendation: Add events to functions that change critical parameters.

Floating Solidity Pragma

Severity: Informational Context: AlchemicTokenV2.sol, CrossChainCanonicalAlchemicTokenV2.sol, gALCX.sol, AlchemistV2.sol, TransmuterBuffer.sol, TransmuterV2.sol, YearnTokenAdapter.sol, StakingPools.sol, WETHGateway.sol, AutoleverageCurveFactoryethpool.sol, AutoleverageCurveMetapool.sol, Whitelist.sol, FixedPointMath.sol, LiquidityMath.sol, Limiters.sol, TokenUtils.sol, Tick.sol, Sets.sol, SafeCast.sol

Description: Contracts should be deployed using the same compiler version/flags with which they have been tested. Locking the pragma (for e.g. by not using ^ in pragma solidity 0.5.10) ensures that contracts do not accidentally get deployed using an older compiler version with unfixed bugs.

Recommendation: Remove ^ from in front of your pragma version.

Multiple Solidity Pragma

Severity: Informational Context: FuseTokenAdapterV1.sol, WstETHAdapterV1.sol, RETHAdapterV1.sol, VesperAdapterV1.sol, TransmuterConduit.sol, EthAssetManager.sol, ThreePoolAssetManager.sol

Description: It is better to use one Solidity compiler version across all contracts instead of different versions with different bugs and security checks.

Recommendation: Ensure all pragma versions are the same one.

Too recent of a Pragma

Severity Informational Context: FuseTokenAdapterV1.sol, WstETHAdapterV1.sol, RETHAdapterV1.sol, VesperAdapterV1.sol, TransmuterConduit.sol, EthAssetManager.sol, ThreePoolAssetManager.sol

Description: Using too recent of a pragma is risky since they are not battle tested. A rise of a bug that wasn't known on release would cause either a hack or a need to secure funds and redeploy.

Recommendation: Use a Pragma version that has been used for sometime. I would suggest 0.8.4 for the decrease of risk and still has the gas optimizations implemented.

Use ++index instead of index++ to increment a loop counter

Context: AlchemistV2.sol#L988-L993, AlchemistV2.sol#L1280-L1285, AlchemistV2.sol#L1353-L1358, AlchemistV2.sol#L1457-L1471, AlchemistV2.sol#L1520-L1546, TransmuterBuffer.sol#L165-L175, TransmuterBuffer.sol#L178-L209, TransmuterBuffer.sol#L230-L248 (For both), TransmuterBuffer.sol#L263-L286, TransmuterBuffer.sol#L371-L397 (For both), TransmuterBuffer.sol#L471-L484, EthAssetManager.sol#L200-L226, EthAssetManager.sol#L561-L595, ThreePoolAssetManager.sol#L235-L264 (For both), ThreePoolAssetManager.sol#L330-L402, ThreePoolAssetManager.sol#L763-L805, ThreePoolAssetManager.sol#L896-L922, StakingPools.sol#L182-L203, StakingPools.sol#L362-L367

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

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

Catching The Array Length Prior To Loop

Context: AlchemistV2.sol#L988-L993, AlchemistV2.sol#L1280-L1285, AlchemistV2.sol#L1353-L1358, AlchemistV2.sol#L1457-L1471, AlchemistV2.sol#L1520-L1546, TransmuterBuffer.sol#L165-L175, TransmuterBuffer.sol#L178-L209, TransmuterBuffer.sol#L230-L248 (For both), TransmuterBuffer.sol#L263-L286, TransmuterBuffer.sol#L371-L397 (For L382), TransmuterBuffer.sol#L471-L484, StakingPools.sol#L182-L203, StakingPools.sol#L362-L367

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.

Use calldata Instead of memory For Function Parameters

Context: TransmuterBuffer.sol#L178-L209 (For both)

Description: The dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient.

Recommendation: Use calldata instead of memory for function parameters to avoid using memory with array values whena function is getting called externally.

State Variables That Can Be Set To Immutable

Context: gALCX.sol#L12, gALCX.sol#L13, gALCX.sol#L17

Description: Solidity 0.6.5 introduced immutable as a major feature. It allows setting contract-level variables at construction time which gets stored in code rather than storage. Each call to it reads from storage, using a sload costing 2100 gas cold or 100 gas warm. Setting it to immutable will have each storage read of the state variable to be replaced by the instruction push32 value, where value is set during contract construction time and this costs only 3 gas.

Recommendation: Set the state variable to immutable

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.

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.

#0 - 0xfoobar

2022-05-30T07:09:11Z

Good point on calldata vs memory for array parameters

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