code-423n4 / 2023-01-numoen-findings

0 stars 0 forks source link

Gas Optimizations #204

Open code423n4 opened 1 year ago

code423n4 commented 1 year ago

See the markdown file with the details of this report here.

c4-judge commented 1 year ago

berndartmueller marked the issue as grade-a

c4-judge commented 1 year ago

berndartmueller marked the issue as selected for report

noamyakov commented 1 year ago

Some of the instances described in this gas report are false and the report saves less gas than claimed.

[G‑02] Using storage instead of memory for structs/arrays saves gas

File: src/core/libraries/Position.sol

47:       Position.Info memory _positionInfo = positionInfo;

All of _positionInfo's fields are read, so no gas will be saved. size is read on line 50, rewardPerPositionPaid is read on line 70 (inside newTokensOwed()) and tokensOwed is read on line 64.

File: src/periphery/LiquidityManager.sol

211:      Position memory position = positions[msg.sender][lendgine]; // SLOAD

All of position's fields are read, so no gas will be saved. size is read on line 214, rewardPerPositionPaid is read on line 214 and tokensOwed is read on line 214.

Total gas actually saved: 4200

[G‑03] Avoid contract existence checks by using low level calls

File: src/core/Pair.sol

/// @audit swapCallback()
127:      ISwapCallback(msg.sender).swapCallback(amount0Out, amount1Out, data);

swapCallback() doesn't have a return value so the optimization doesn't apply here and no gas will be saved.

File: src/periphery/Payment.sol

/// @audit withdraw()
30:         IWETH9(weth).withdraw(balanceWETH);

withdraw() doesn't have a return value so the optimization doesn't apply here and no gas will be saved.

Total gas actually saved: 1800


Overall, this gas report actually saves a total of 6730 gas. Therefore, I think report #98 is better and should be selected for the report.

berndartmueller commented 1 year ago

I invite @IllIllI000 to add feedback here.

IllIllI000 commented 1 year ago

noamyakov is right about the second G-02, and both of the G-03s (the G-03 bugs I've fixed). I haven't looked at his report so I can't vouch for correctness, but if his numbers are right, then yes, he should be selected

berndartmueller commented 1 year ago

I will reconsider my decision to select this gas submission for the report. Thanks @noamyakov, for pointing out the inaccuracies, and big thanks to @IllIllI000 for being such a collegial fellow warden.

G-02: No possible gas savings in both mentioned cases by Noam

G-03: Due to safety precautions, the EXTCODESIZE check done by Solidity is valid in the case of swapCallback() and withdraw to ensure a contract exists at the target address. Hence, the gas optimization recommendations are invalid.

c4-judge commented 1 year ago

berndartmueller marked the issue as not selected for report