Platform: Code4rena
Start Date: 12/12/2022
Pot Size: $36,500 USDC
Total HM: 8
Participants: 103
Period: 7 days
Judge: berndartmueller
Id: 193
League: ETH
Rank: 35/103
Findings: 1
Award: $179.23
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: Rolezn
Also found by: 0x1f8b, 0xAgro, 0xSmartContract, 0xab00, 0xhacksmithh, Aymen0909, Bnke0x0, Breeje, Diana, HardlyCodeMan, IllIllI, JC, JrNet, Madalad, NoamYakov, RaymondFam, ReyAdmirado, SleepingBugs, UdarTeam, c3phas, carlitox477, cryptonue, gz627, lukris02, millersplanet, oyc_109, pavankv, ret2basic, saneryee, tnevler
179.2341 USDC - $179.23
The instances below point to the second+ access of a state variable within a function. Caching of a state variable replace each Gwarmaccess (100 gas) with a much cheaper stack read. Other less obvious fixes/optimizations include having local memory caches of state variable structs, or having local caches of state variable contracts/addresses.
high chance of saving 100 gas for a low risk of losing 3, cache closeTimestamp
before the second require (only if withdraw is not initiated we will lose 3 gas otherwise we will save 100)
unchecked {}
for subtractions where the operands cannot underflow because of a previous require()
or if
statementrequire(a <= b); x = b - a => require(a <= b); unchecked { x = b - a } if(a <= b); x = b - a => if(a <= b); unchecked { x = b - a } this will stop the check for overflow and underflow so it will save gas
can not underflow
checked in require L157
<x> += <y>
costs more gas than <x> = <x> + <y>
Using the addition operator instead of plus-equals saves gas
make the variable before the for loop and only give the value to it inside the loop
isValid
char
++i/i++
should be unchecked{++i}/unchecked{i++}
when it is not possible for them to overflow, as is the case when used in for-loop and while-loopsIn Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.
&&
saves gasthis will have a large deployment gas cost but with enough runtime calls the split require version will be 3 gas cheaper
Not inlining costs 20 to 40 gas because of two extra JUMP instructions and additional stack operations needed for function calls.
_validateTokenIds
Contracts are allowed to override their parents’ functions and change the visibility from external to public and can save gas by doing so.
destroy
create
mint
burn
price
withdraw
close
nftSell
nftBuy
nftRemove
nftAdd
before transfer we should check for amount being 0 so the function doesnt run when its not gonna do anything. varaiables below have possibility of being 0
fractionalTokenOutputAmount
baseTokenOutputAmount
outputAmount
inputAmount
just pre calculate 160 - 4 * 4
and use it inside the code instead of using 2 operations
closeTimestamp
costs 100 gas instead just use the value block.timestamp + CLOSE_GRACE_PERIOD
given to it some line above or cache the value of this operation before to make it even cheaper
#0 - c4-judge
2022-12-30T13:38:34Z
berndartmueller marked the issue as grade-a