Platform: Code4rena
Start Date: 03/05/2022
Pot Size: $30,000 USDC
Total HM: 6
Participants: 93
Period: 3 days
Judge: gzeon
Id: 118
League: ETH
Rank: 81/93
Findings: 1
Award: $15.49
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: BowTiedWardens
Also found by: 0v3rf10w, 0x1f8b, 0x4non, 0xDjango, 0xNazgul, 0xProf, 0xc0ffEE, 0xf15ers, 0xkatana, 0xliumin, ACai, AlleyCat, CertoraInc, Cityscape, Cr4ckM3, DavidGialdi, Dinddle, FSchmoede, Funen, GimelSec, Hawkeye, IllIllI, Kulk0, M0ndoHEHE, MaratCerby, MiloTruck, Picodes, RoiEvenHaim, Tadashi, TerrierLover, TrungOre, VAD37, WatchPug, antonttc, catchup, defsec, delfin454000, dirk_y, eccentricexit, ellahi, fatherOfBlocks, gzeon, hake, hansfriese, hickuphh3, horsefacts, ilan, joestakey, kebabsec, kenta, kenzo, marximimus, minhquanym, noobie, oyc_109, p4st13r4, pauliax, rajatbeladiya, reassor, rfa, robee, rotcivegaf, saian, samruna, shenwilly, shung, simon135, slywaters, sorrynotsorry, throttle, unforgiven, z3s
15.491 USDC - $15.49
++index
instead of index++
to increment a loop counterContext: ForgottenRunesWarriorsMinter.sol#L130-L165
, ForgottenRunesWarriorsMinter.sol#L201-L223
, ForgottenRunesWarriorsMinter.sol#L257-L262
, ForgottenRunesWarriorsMinter.sol#L350-L358
Description:
Due to reduced stack operations, using ++index
saves 5 gas per iteration.
Recommendation:
Use ++index
to increment a loop counter.
require()
, Use != 0
Instead of > 0
With Uint ValuesContext: ForgottenRunesWarriorsMinter.sol#L30-L165 (For L140)
, ForgottenRunesWarriorsMinter.sol#L201-L223 (For L210)
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: ForgottenRunesWarriorsGuild.sol#L129-L131
, ForgottenRunesWarriorsGuild.sol#L145-L147
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.
Context: ForgottenRunesWarriorsGuild.sol#L61-L74
, ForgottenRunesWarriorsGuild.sol#L85-L87
, ForgottenRunesWarriorsGuild.sol#L94-L106
, ForgottenRunesWarriorsGuild.sol#L113-L119
, ForgottenRunesWarriorsGuild.sol#L145-L147
, ForgottenRunesWarriorsGuild.sol#L163-L165
, ForgottenRunesWarriorsGuild.sol#L173-L176
, ForgottenRunesWarriorsMinter.sol#L337-L339
, ForgottenRunesWarriorsMinter.sol#L350-L358
, ForgottenRunesWarriorsMinter.sol#L364-L366
, ForgottenRunesWarriorsMinter.sol#L371-L374
, ForgottenRunesWarriorsMinter.sol#L427-L429
, ForgottenRunesWarriorsMinter.sol#L434-L436
, ForgottenRunesWarriorsMinter.sol#L469-L471
, ForgottenRunesWarriorsMinter.sol#L480-L500
, ForgottenRunesWarriorsMinter.sol#L505-L507
, ForgottenRunesWarriorsMinter.sol#L513-L515
, ForgottenRunesWarriorsMinter.sol#L520-L522
, ForgottenRunesWarriorsMinter.sol#L550-L552
, ForgottenRunesWarriorsMinter.sol#L557-L559
, ForgottenRunesWarriorsMinter.sol#L564-L566
, ForgottenRunesWarriorsMinter.sol#L571-L573
, ForgottenRunesWarriorsMinter.sol#L586-L588
, ForgottenRunesWarriorsMinter.sol#L593-L595
, ForgottenRunesWarriorsMinter.sol#L600-L602
, ForgottenRunesWarriorsMinter.sol#L616-L619
, ForgottenRunesWarriorsMinter.sol#L627-L630
,
Description: Several functions across multiple contracts have a public visibility and can be marked with external visibility to save gas.
Recommendation: Change the functions visibility to external to save gas.
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.
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.
Context: All Contracts
Description: Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks for free!
The advantages of versions =0.8.*= over =<0.8.0= are:
Low level inliner
from =0.8.2=, leads to cheaper runtime gas.
Especially relevant when the contract has small functions. For
example, OpenZeppelin libraries typically have a lot of small
helper functions and if they are not inlined, they cost an
additional 20 to 40 gas because of 2 extra =jump= instructions and
additional stack operations needed for function calls.Optimizer improvements in packed structs
: Before =0.8.3=, storing
packed structs, in some cases used an additional storage read
operation. After EIP-2929
, if the slot was already cold, this
means unnecessary stack operations and extra deploy time costs.
However, if the slot was already warm, this means additional cost
of =100= gas alongside the same unnecessary stack operations and
extra deploy time costs.Custom errors
from =0.8.4=, leads to cheaper deploy time cost and
run time cost. Note: the run time cost is only relevant when the
revert condition is met. In short, replace revert strings by
custom errors.Recommendation: Upgrade to at least 0.8.4 for the additional benefits.