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: 82/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
Uninitialized variables by default contain a value equivalent to 0: uint
s are initialized to 0; bool
s to false; address
es to address(0)
.
Explicitly assigning these values to variables when they are declared increases gas costs while providing no funciton.
e.g. change this code:
uint256 var = 0;
to
uint256 var;
For more information, please consult the following resources:
Tips and Tricks to Save Gas and Reduce Bytecode Size
The following lines of code are affected:
code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsGuild.sol:24: uint256 public numMinted = 0; code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:162: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:220: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:259: for (uint256 i = 0; i < count; i++) {
Newer versions of the Solidity compiler will check for integer overflows and underflows automatically. This provides safety but increases gas costs.
When an unsigned integer is guaranteed to never overflow, the unchecked
feature of Solidity can be used to save gas costs.
A common case for this is for-loops using a strictly-less-than comparision in their conditional statement, e.g.:
uint256 length = someArray.length; for (uint256 i; i < length; ++i) { }
In cases like this, the maximum value for length
is 2**256 - 1
. Therefore, the maximum value of i
is 2**256 - 2
as it will always be strictly less than length
.
This example can be replaced with the following construction to reduce gas costs:
for (uint i = 0; i < length; i = unchecked_inc(i)) { // do something that doesn't change the value of i } function unchecked_inc(uint i) returns (uint) { unchecked { return i + 1; } }
For more information, consult the following resources:
Solidity docs: underflows, overflows, and unchecked
The following lines of code are affected:
code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:162: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:220: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:259: for (uint256 i = 0; i < count; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:355: for (uint256 i = startIdx; i < endIdx + 1; i++) {
Using ++i
costs less gas than using i++
. In the context of a for-loop, gas is saved on each iteration.
The following lines of code are affected:
When checking whether a value is equal to zero, using the construction var != 0
is costs less gas than using var > 0
. Note that this is true only when the comparison occurs in a conditional context and the Solidity compiler is using the Optimizer.
For more information, please consult the following resources:
Twitter discussion detailing the gas costs of != 0 vs > 0 in require() calls
Solidity Compiler: Optimizer options
The following lines of code are affected:
code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:162: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:220: for (uint256 i = 0; i < numWarriors; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:259: for (uint256 i = 0; i < count; i++) { code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:355: for (uint256 i = startIdx; i < endIdx + 1; i++) {
Revert message strings included in calls to require()
are stored in 32-byte chunks when contracts are deployed. To optimize gas costs, consider using revert messages that are less than 32 bytes in length.
For more information, see the following resource: Gas optimization tips: Shorter revert strings
The following lines of code are affected:
code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsGuild.sol:70: 'ERC721Metadata: URI query for nonexistent token' code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:142: 'You can summon no more than 20 Warriors at a time' code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:148: 'Ether value sent is not sufficient' code4rena/2022-05-runes/contracts/ForgottenRunesWarriorsMinter.sol:212: 'You can summon no more than 20 Warriors at a time'