Platform: Code4rena
Start Date: 21/04/2022
Pot Size: $100,000 USDC
Total HM: 18
Participants: 60
Period: 7 days
Judge: gzeon
Total Solo HM: 10
Id: 112
League: ETH
Rank: 55/60
Findings: 1
Award: $89.35
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: joestakey
Also found by: 0v3rf10w, 0x1f8b, 0x4non, 0xDjango, 0xNazgul, 0xkatana, 0xmint, Dravee, Funen, IllIllI, MaratCerby, NoamYakov, Tadashi, TerrierLover, Tomio, WatchPug, catchup, defsec, fatherOfBlocks, hake, horsefacts, kenta, oyc_109, pauliax, rayn, rfa, robee, saian, securerodd, simon135, slywaters, sorrynotsorry, tin537, z3s
89.3504 USDC - $89.35
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.
++index
instead of index++
to increment a loop counterContext: TopUpAction.sol#L180-L193
, TopUpAction.sol#L451-L463
, TopUpAction.sol#L476-L483
, TopUpAction.sol#L493-L511
, TopUpAction.sol#L884-L899
, StakerVault.sol#L257-L264
, RoleManager.sol#L73-L86
, CompoundHandler.sol#L128-L173
, TopUpKeeperHelper.sol#L32-L57 (For both loops)
, TopUpKeeperHelper.sol#L65-L77
, TopUpKeeperHelper.sol#L84-L100
, TopUpKeeperHelper.sol#L159-L169
, Controller.sol#L112-L121
, CTokenRegistry.sol#L59-L74
, ConvexStrategyBase.sol#L301-L320
, ConvexStrategyBase.sol#L363-L390
Description:
Due to reduced stack operations, using ++index
saves 5 gas per iteration.
Recommendation:
Use ++index
to increment a loop counter.
Context: TopUpAction.sol#L180-L193
, StakerVault.sol#L257-L264
, RoleManager.sol#L73-L86
, CompoundHandler.sol#L128-L173
, TopUpKeeperHelper.sol#L32-L57 (For both loops)
, TopUpKeeperHelper.sol#L65-L77
, CTokenRegistry.sol#L59-L74
, ConvexStrategyBase.sol#L301-L320
, ConvexStrategyBase.sol#L363-L390
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.
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.
arr[i] = arr[i] + 1
is cheaper than arr[i] += 1
Context: StakerVault.sol#L105-L123 (For both L112 and L113)
, StakerVault.sol#L198-L211
, StakerVault.sol#L219-L236 (For L231)
, StakerVault.sol#L323-L350 (For L341)
, StakerVault.sol#L360-L399 (For both L388 and L390)
, TopUpActionFeeHandler.sol#L81-L109 (For both L101 and L102)
, GasBank.sol#L25-L28
, VaultReserve.sol#L25-L28
Description: Due to stack operations this is 25 gas cheaper when dealing with arrays in storage, and 4 gas cheaper for memory arrays.
Recommendation:
Use arr[i] = arr[i] + 1
instead of arr[i] += 1
when dealing with arrays
require()
, Use != 0
Instead of > 0
With Uint ValuesContext: TopUpActionFeeHandler.sol#L117-L131
, LiquidityPool.sol#L398-L405 (For L401)
, LiquidityPool.sol#L465-L495 (For both L471 and L473)
, LiquidityPool.sol#L544-L570 (For L549)
, Vault.sol#L163-L169 (For L164)
, TopUpAction.sol#L203-L254 (For L210)
, TopUpAction.sol#L539-L733 (For L554)
Description:
In a require, when checking a uint, using != 0
instead of > 0
saves 6 gas. This will jump over or avoid an extra ISZERO
opcode.
Recommendation:
Use != 0
instead of > 0
with uint values but only in require()
statements.
calldata
Instead of memory
For Function ParametersContext: RoleManager.sol#L64-L86
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.