code-423n4 / 2021-11-badgerzaps-findings

0 stars 0 forks source link

Use else if can save gas #40

Closed code423n4 closed 2 years ago

code423n4 commented 3 years ago

Handle

WatchPug

Vulnerability details

https://github.com/Badger-Finance/ibbtc/blob/49c9564724a0283e6ee0293621902819cacb861a/contracts/Zap.sol#L126-L142

if (numTokens == 2) {
    uint[2] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
}

if (numTokens == 3) {
    uint[3] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
}

if (numTokens == 4) {
    uint[4] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
}

Change to else if can save gas:

if (numTokens == 2) {
    uint[2] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
} else if (numTokens == 3) {
    uint[3] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
} else if (numTokens == 4) {
    uint[4] memory amounts;
    amounts[idx] = amount;
    pool.add_liquidity(amounts, 0);
}
GalloDaSballo commented 3 years ago

I would like an explanation

You're stil comparing a condition and you're still making a jump, why is else if cheaper?

0xleastwood commented 2 years ago

duplicate of #76 which contains explanation.