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

Avoid unnecessary code execution can save gas #41

Open
code423n4 opened this issue Nov 16, 2021 · 1 comment
Open

Avoid unnecessary code execution can save gas #41

code423n4 opened this issue Nov 16, 2021 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

https://github.com/Badger-Finance/ibbtc/blob/d8b95e8d145eb196ba20033267a9ba43a17be02c/contracts/Zap.sol#L304-L318

function ibbtcToCurveLP(uint poolId, uint bBtc) public view returns(uint lp, uint fee) {
    uint sett;
    uint max;
    (sett,fee,max) = settPeak.calcRedeem(poolId, bBtc);
    Pool memory pool = pools[poolId];
    if (bBtc > max) {
        return (0,fee);
    } else {
        // pesimistically charge 0.5% on the withdrawal.
        // Actual fee might be lesser if the vault keeps keeps a buffer
        uint strategyFee = sett.mul(controller.strategies(pool.lpToken).withdrawalFee()).div(10000);
        lp = sett.sub(strategyFee).mul(pool.sett.getPricePerFullShare()).div(1e18);
        fee = fee.add(strategyFee);
    }
}

L309-311 is necessary as they won't affect the storage or the returns anyway.

Recommendation

Change to:

if (bBtc <= max) {
    // pesimistically charge 0.5% on the withdrawal.
    // Actual fee might be lesser if the vault keeps keeps a buffer
    uint strategyFee = sett.mul(controller.strategies(pool.lpToken).withdrawalFee()).div(10000);
    lp = sett.sub(strategyFee).mul(pool.sett.getPricePerFullShare()).div(1e18);
    fee = fee.add(strategyFee);
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 16, 2021
code423n4 added a commit that referenced this issue Nov 16, 2021
@GalloDaSballo
Copy link
Collaborator

Personally I like the return early, also like the idea presented by the warden, ambivalent on applying the improvement

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)
Projects
None yet
Development

No branches or pull requests

2 participants