code-423n4 / 2022-06-badger-findings

0 stars 0 forks source link

Gas Optimizations #44

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[G-01] Unnecessary variable initialization of default value

When variable is not initialized, it will have its default values. Example: 0 for uint, false for bool and address(0) for address By removing this default values you will save about 5 gas.

I suggest removing default value initialization for following variables.

MyStrategy.sol
118:        for(uint i = 0; i < length; i++){
300:        for (uint256 i = 0; i < _claims.length; i++) {
317:        for (uint256 i = 0; i < _claims.length; i++) {

For example these can change to:

[G-02] Save Gas in For-Loops by storing array's length as a variable

3 gas per iteration can be saved by storing an array's length as a variable before the for-loop.

Issue found at:

MyStrategy.sol
300:        for (uint256 i = 0; i < _claims.length; i++) {
317:        for (uint256 i = 0; i < _claims.length; i++) {

For example, I suggest changing it to:

length = _claims.length
for (uint i; i < length; i++) {

[G-03] ++i costs less gas than i++

It is better to use ++i than i++ when possible since it costs less gas. It saves about 5 gas per iteration.

Issue found at:

MyStrategy.sol
118:        for(uint i = 0; i < length; i++){
300:        for (uint256 i = 0; i < _claims.length; i++) {
317:        for (uint256 i = 0; i < _claims.length; i++) {

[G-04] Not Defining Variables to Reduce Gas

Certain variables is defined even though they are used only once. Remove these unnecessary variables to save gas. It saves about 10 gas for each variable (Total 100gas). For cases where it will reduce the readability, one can use comments to help describe what the code is doing.

Issues found at MyStrategy.sol

  1. Remove toSend variable of sweepRewardToken function
    111:        uint256 toSend = IERC20Upgradeable(token).balanceOf(address(this));
    112:        _handleRewardTransfer(token, toSend);
  2. Remove balances variable of balanceOfPool function
    143:        IAuraLocker.Balances memory balances = LOCKER.balances(address(this));
    144:        return balances.locked;
  3. Remove auraBalBalanceBefore variable of _harvest function
    220:        uint256 auraBalBalanceBefore = AURABAL.balanceOf(address(this));
    228:        uint256 auraBalEarned = AURABAL.balanceOf(address(this)).sub(auraBalBalanceBefore);
  4. Remove balEthBptEarned variable of _harvest function
    249:            uint256 balEthBptEarned = BALANCER_VAULT.swap(singleSwap, fundManagement, 0, type(uint256).max);
    260:                userData: abi.encode(ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, balEthBptEarned, BPT_WETH_INDEX),
  5. Remove wethBalanceBefore variable of _harvest function
    252:            uint256 wethBalanceBefore = WETH.balanceOf(address(this));
    266:            uint256 wethEarned = WETH.balanceOf(address(this)).sub(wethBalanceBefore);
  6. Remove exitPoolRequest variable of _harvest function
    257:            IBalancerVault.ExitPoolRequest memory exitPoolRequest = IBalancerVault.ExitPoolRequest({
    263:            BALANCER_VAULT.exitPool(BAL_ETH_POOL_ID, address(this), payable(address(this)), exitPoolRequest);
  7. Remove wethEarned variable of _harvest function
    266:            uint256 wethEarned = WETH.balanceOf(address(this)).sub(wethBalanceBefore);
    272:                amount: wethEarned,
  8. Remove beforeVaultBalance variable of claimBribesFromHiddenHand function
    292:        uint256 beforeVaultBalance = _getBalance();
    341:        require(beforeVaultBalance == _getBalance(), "Balance can't change");
  9. Remove beforePricePerFullShare variable of claimBribesFromHiddenHand function
    293:        uint256 beforePricePerFullShare = _getPricePerFullShare();
    342:        require(beforePricePerFullShare == _getPricePerFullShare(), "Ppfs can't change");
  10. Remove auraAmount variable of manualSendAuraToVault function
    381:        uint256 auraAmount = balanceOfWant();
    382:        _transferToVault(auraAmount);

For mitigation, simply don't define variable that is used only once. Below is mitigation example of above 1.

        _handleRewardTransfer(token, IERC20Upgradeable(token).balanceOf(address(this)));

[G-05] Reduce the long revert strings of error messages

By keeping the revert strings within 32 bytes will save you gas since each slot is 32 bytes and cost about 20k gas.

Following are revert strings that are more than 32 bytes.

MyStrategy.sol
184:        require(
185:            balanceOfPool() == 0 && LOCKER.balanceOf(address(this)) == 0,
186:            "You have to wait for unlock or have to manually rebalance out of it"

[G-06] Use require instead of &&

When there are multiple conditions in require statement, break down the require statement into multiple require statements instead of using && can save gas. (saves about 50 gas)

Issue found at

184:        require(
185:            balanceOfPool() == 0 && LOCKER.balanceOf(address(this)) == 0,
186:            "You have to wait for unlock or have to manually rebalance out of it"
187:        );

For example these can be changed to

        require(balanceOfPool() == 0);
        require(LOCKER.balanceOf(address(this)) == 0, "You have to wait for unlock or have to manually rebalance out of it");
GalloDaSballo commented 2 years ago

Ack, note that assigning a value costs 3 gas, not 10