code-423n4 / 2022-04-backd-findings

6 stars 4 forks source link

Gas Optimizations #204

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

2022-04-backd optimization

1 use calldata instead of memory.

The following input for function can be calldata instead of memory to save gas.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/pool/Erc20Pool.sol#L15 https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/pool/EthPool.sol#L13 https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/pool/LiquidityPool.sol#L702 https://github.com/code-423n4/2022-04-backd/blob/main/backd/interfaces/pool/IEthPool.sol#L6 https://github.com/code-423n4/2022-04-backd/blob/main/backd/interfaces/pool/IErc20Pool.sol#L6

string calldata name_,

2 check the following validation at the beginning of updateDepositCap.

The other require statements need to read the state variable. So you can save gas if the input _depositCap is 0 and the execution is reverted.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/pool/LiquidityPool.sol#L401

Place the require statement at the beginning of updateDepositCap.

3 use unchecked for the following calculation.

The underflow must happen never because the calculation tries to subtract after balance from before balance.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/StakerVault.sol#L384

uint256 unstaked; unchecked { unstaked = oldBal - IERC20(token).balanceOf(address(this)); }

4 delete the following local variables that are used only one time in transferFrom.

The local variables allowanceNew, srcTokensNew, and dstTokensNew are used only one time in transferFrom, so you can delete them and set these calculations directly for state variables.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/StakerVault.sol#L163-L165

balances[src] = srcTokens - amount; balances[dst] = balances[dst] + amount;

if (startingAllowance != type(uint256).max) { _allowances[src][spender] = startingAllowance - amount; }

5 use unchecked for the calculation in decreaseActionLockedBalance.

The underflow is checked already in if sentence, so you can use unchecked to save gas costs.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/StakerVault.sol#L231

unchecked { actionLockedBalances[account] -= amount; }

6 use the initial value, prefix and unchecked in loop

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/StakerVault.sol#L260 https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/BkdLocker.sol#L310

for (uint256 i; i < length;) { // do something unchecked { ++i; } }

7 use unchecked for the calculation in _withdrawFrom.

The underflow for the following calculation is already checked in the next lines, so you can use unchecked to save gas costs.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/GasBank.sol#L87

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/GasBank.sol#L68 https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/GasBank.sol#L76

In withdrawUnused the underflow is also checked because the unused amount will be transferred.

unchecked { _balances[account] = currentBalance - amount; }