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: 53/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
You can have further gas savings by using named return values as a temporary local variable.
For example:
File: Pool.sol
Line 83-85
function totalSupply() public view returns (uint256) { return address(this).balance; }
Instead of the above code, you can return a named variable like so:
function totalSupply() public view returns (uint256 supply) { supply = address(this).balance; }
Here are the rest of the instances found:
File: Exchange.sol
Line 369
File: Exchange.sol
Line 390
File: Exchange.sol
Line 448
File: Exchange.sol
Line 476
File: Exchange.sol
Line 522
File: Exchange.sol
Line 540
File: Exchange.sol
Line 596
File: Pool.sol
Line 60
x = x + y
is more efficient than x += y
(same for subtracting)Using +
instead of +=
saves around 113 gas.
There are 7 instances of this issue:
File: Exchange.sol
(Line 316, Line 574, Line 601)
File: contracts/Exchange.sol 316: nonces[msg.sender] += 1; 574: remainingETH -= price; 601: totalFee += fee;
File: Pool.sol
(Line 36, Line 46, Line 73, Line 74)
File: contracts/Pool.sol 36: _balances[msg.sender] += msg.value; 46: _balances[msg.sender] -= amount; 73: _balances[from] -= amount; 74: _balances[to] += amount;
The following instance of state variable cache is unnecessary because remainingETH
is referenced only once in the function.
File: Exchange.sol
Line 213
212 function _returnDust() private { 213 uint256 _remainingETH = remainingETH; 214 assembly { 215 if gt(_remainingETH, 0) { 216 let callStatus := call( 217 gas(), 218 caller(), 219 selfbalance(), 220 0, 221 0, 222 0, 223 0 224 ) 225 } 226 } 227 }
The order of the functions will have an impact on gas consumption. The reason why is that the order is dependent on the Method ID. Each function position will use up an extra 22 gas.
For more info visit this medium article
#0 - c4-judge
2022-11-17T14:14:18Z
berndartmueller marked the issue as grade-b