Platform: Code4rena
Start Date: 23/05/2022
Pot Size: $75,000 USDC
Total HM: 23
Participants: 75
Period: 7 days
Judge: GalloDaSballo
Total Solo HM: 13
Id: 130
League: ETH
Rank: 68/75
Findings: 1
Award: $50.15
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: IllIllI
Also found by: 0x1f8b, 0x4non, 0xNazgul, 0xf15ers, 0xkatana, Chom, ControlCplusControlV, DavidGialdi, Deivitto, Dravee, ElKu, Fitraldys, Funen, GimelSec, MadWookie, MaratCerby, MiloTruck, Picodes, Randyyy, TerrierLover, TomJ, Tomio, UnusualTurtle, WatchPug, Waze, _Adam, asutorufos, c3phas, catchup, csanuragjain, delfin454000, djxploit, fatherOfBlocks, gzeon, hake, hansfriese, horsefacts, orion, oyc_109, pauliax, reassor, rfa, rotcivegaf, sach1r0, saian, sashik_eth, simon135, supernova, teddav, z3s
50.1536 USDC - $50.15
++i
is cheaper than i++
Use prefix increment instead of postfix increment.
The i++
part of for loops can be surrounded by the unchecked
keyword to save gas. For example:
for (uint256 i = 0; i < 10; i++) { ... }
can be optimized to:
for (uint256 i = 0; i < 10;) { ... unchecked { i++; } }
!= 0
is cheaper than > 0
(unsigned integers)When dealing with unsigned integers the != 0
comparison is equivalent to > 0
comparison. It is also cheaper, therefore you should use it instead.
You can optimize for loops that iterate over array elements by caching the array length in a local variable. This will reduce the number of MLOADs and will save gas. For example:
for (uint256 i = 0; i < array.length; i++) { ... }
can be optimized to:
uint256 length = array.length; for (uint256 i = 0; i < length; i++) { ... }
Change those for:
Gauge:353: for (uint i = 0; i < tokens.length; i++)
Minter:57: for (uint i = 0; i < claimants.length; i++)
Pair:257: for (uint i = 0; i < _prices.length; i++)
etc...
Many for loops that iterate over array elements access the current element (array[i]
) more than once in each iteration. This element can be accessed only once (and be cached in a local variable) in order to avoid the additional array-boundaries check that Solidity performs with each array access, and save gas.
For example in Gauge.getReward function
for (uint i = 0; i < tokens.length; i++) { (rewardPerTokenStored[tokens[i]], lastUpdateTime[tokens[i]]) = _updateRewardPerToken(tokens[i]); uint _reward = earned(tokens[i], account); lastEarn[tokens[i]][account] = block.timestamp; userRewardPerTokenStored[tokens[i]][account] = rewardPerTokenStored[tokens[i]]; if (_reward > 0) _safeTransfer(tokens[i], account, _reward); emit ClaimRewards(msg.sender, tokens[i], _reward); }
Instead you can cached out the tokens[i] in a local variable.
#0 - GalloDaSballo
2022-06-30T00:00:45Z
Would save Hundreds of gas, and seems to be from c4udit
#1 - GalloDaSballo
2022-06-30T02:09:38Z
100 / 500