Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking if lpProfitCut > 0 can save gas #310

Open
code423n4 opened this issue Dec 1, 2021 · 1 comment
Open

Checking if lpProfitCut > 0 can save gas #310

code423n4 opened this issue Dec 1, 2021 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

https://github.com/code-423n4/2021-11-malt/blob/c3a204a2c0f7c653c6c2dda9f4563fd1dc1cecf3/src/contracts/SwingTrader.sol#L136-L141

if (profit > 0) {
    uint256 lpCut = profit.mul(lpProfitCut).div(1000);

    collateralToken.safeTransfer(address(rewardThrottle), lpCut);
    rewardThrottle.handleReward();
  }

Given that lpProfitCut can be 0, checking if lpProfitCut > 0 can avoid unnecessary code execution (including external calls) and save some gas.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 1, 2021
code423n4 added a commit that referenced this issue Dec 1, 2021
@0xScotch 0xScotch added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 10, 2021
@GalloDaSballo
Copy link
Collaborator

Agree with finding, however, notice that lpProfitCut is a storage variable, so if you're going to read it more than once you'll also want to cache it's value.
It may be be ideal to rewrite as

uint256 cut = lpProfitCut
if (profit > 0 && cut > 0) {
    uint256 lpCut = profit.mul(cut).div(1000);

Also noticed that you're using 1k instead of the more usual 10_000 (BPS math)
Lastly you may want to store the max value in a constant (probably some other warden also flagged it), to avoid magic values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants