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: 51/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
During the audit, 3 gas issues were found.
Total savings ~20,245.
â„– | Title | Instance Count | Saved |
---|---|---|---|
G-1 | Optimization for isOpen | 1 | 20,000 |
G-2 | Use unchecked blocks for incrementing i | 3 | 105 |
G-3 | Use unchecked blocks for subtractions where underflow is impossible | 4 | 140 |
isOpen
For state variable isOpen
, it is better to use uint256(1) and uint256(2) to avoid spending 20,000 gas when changing from '0' to '1' (when opening after closing).
Link:
function open() external onlyOwner { isOpen = 1; emit Opened(); } function close() external onlyOwner { isOpen = 0; emit Closed(); }
This saves ~20,000.
In Solidity 0.8+, there’s a default overflow and underflow check on unsigned integers. In the loops, "i" will not overflow because the loop will run out of gas before that.
Change:
for (uint256 i; i < n; ++i) { // ... }
to:
for (uint256 i; i < n;) { // ... unchecked { ++i; } }
This saves ~30-40 gas per iteration.
So, ~35*3 = 105
In Solidity 0.8+, there’s a default overflow and underflow check on unsigned integers. When an overflow or underflow isn’t possible (after require or if-statement), some gas can be saved by using unchecked blocks.
This saves ~35.
So, ~35*4 = 140
#0 - c4-judge
2022-11-17T12:55:41Z
berndartmueller marked the issue as grade-b