code-423n4 / 2021-06-gro-findings

0 stars 1 forks source link

Gas optimization by avoiding array copying #124

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

shw

Vulnerability details

Impact

Some functions copy the given array-type parameters to local variables first and provide the local variables to other functions as parameters afterward. The array copying operation can be omitted to save gas.

Proof of Concept

For example, consider the _stableToLp function in Buoy3Pool. A more gas-efficient implementation is to directly provide tokenAmounts to curvePool, as follows:

function _stableToLp(uint256[N_COINS] memory tokenAmounts, bool deposit) internal view returns (uint256) {
    require(tokenAmounts.length == N_COINS, "deposit: !length");
    return curvePool.calc_token_amount(tokenAmounts, deposit);
}

Similarly, we can also optimize the following functions to save gas.

Referenced code: Buoy3Pool.sol#L184-L191 Buoy3Pool.sol#L174-L182 Buoy3Pool.sol#L153-L163 Insurance.sol#L160-L169 Insurance.sol#L316-L320

Recommended Mitigation Steps

It is shown as above.

kitty-the-kat commented 2 years ago

27

ghoul-sol commented 2 years ago

Duplicate of #27