Platform: Code4rena
Start Date: 11/11/2022
Pot Size: $36,500 USDC
Total HM: 5
Participants: 62
Period: 3 days
Judge: berndartmueller
Id: 181
League: ETH
Rank: 57/62
Findings: 1
Award: $22.22
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: ReyAdmirado
Also found by: 0x4non, 0xRoxas, 0xab00, Awesome, Aymen0909, Bnke0x0, Deivitto, Diana, IllIllI, Rahoz, RaymondFam, Rolezn, Sathish9098, ajtra, aphak5010, aviggiano, c3phas, carlitox477, ch0bu, cryptostellar5, erictee, lukris02, martin, rotcivegaf, saian, shark, trustindistrust, zaskoh
22.2155 USDC - $22.22
The Gas savings are calculated using the yarn test
command.
The same tests were used that are provided in the contest repository.
Issue | Title | File | Instances | Estimated Gas Savings (Deployments) | Estimated Gas Savings (Method calls) |
---|---|---|---|---|---|
G-00 | Use values 1 and 2 for isOpen instead of 0 and 1 | Exchange.sol | 5 | not calculated | not calculated |
G-01 | Don't initialize variables with default value (Not in automated findings) | Exchange.sol | 1 | 2440 | 0 |
G-02 | Use unchecked{++i} in loops | Exchange.sol | 3 | not calculated | not calculated |
G-03 | Use functions instead of modifiers | Exchange.sol | 3 modifiers | not calculated | not calculated |
isOpen
instead of 0 and 1isOpen=1
should be used instead of isOpen=0
and isOpen=2
should be used instead of isOpen=1
.
This is because setting storage to a non-zero value from a zero value costs 20000 Gas and setting storage to zero from a non-zero value refunds only 4800 Gas.
Leaving isOpen
at a non-zero value continously is significantly cheaper.
The exact values of saved Gas depend of course on how often Exchange.open
and Exchange.close
are called.
There are 5 instances of isOpen
that must be adapted accordingly:
There is 1 instance of a variable that is initialized with its default value and that is not already mentioned in the C4audit automated findings.
Deployment Gas saved: 2440
Method call Gas saved: 0
unchecked{++i}
in loopsSo far the automated C4audit findings only suggest that you should use ++i
instead of i++
.
However it is also safe to use unchecked{++i}
which saves even more Gas since there are no under-/overflow checks.
There are 3 instances of this:
https://github.com/code-423n4/2022-11-non-fungible/blob/e9451f78069999cfd8fdd126b87241eb94e93078/contracts/Exchange.sol#L184-L208
Fix:
for (uint8 i = 0; i < executionsLength;) { ... unchecked{++i}; }
for (uint8 i = 0; i < orders.length;) { ... unchecked{++i}; }
for (uint8 i = 0; i < fees.length;) { ... unchecked{++i}; }
Modifiers make code more elegant and readable but cost more Gas than functions.
Arguably, the additional Gas cost outweighs the readability benefit.
There are 3 instances where modifiers are defined.
#0 - c4-judge
2022-11-17T14:18:53Z
berndartmueller marked the issue as grade-b