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 read of array length in for loops can save gas #36

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

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.

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

for (uint i = 0; i < pools.length; i++) {
    Pool memory pool = pools[i];
    pool.lpToken.safeApprove(address(pool.sett), uint(-1));
    if (i < 3) {
        ren.safeApprove(address(pool.deposit), uint(-1));
        wbtc.safeApprove(address(pool.deposit), uint(-1));
        IERC20(address(pool.sett)).safeApprove(address(settPeak), uint(-1));
    } else {
        IERC20(address(pool.sett)).safeApprove(address(byvWbtcPeak), uint(-1));
    }
}

As pools.length must be 4, it can be replaced with a literal 4.

@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

Agree with the finding as the warden actually explained the logic

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