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

6 stars 4 forks source link

Gas Optimizations #183

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Optimizations

++i use less gas than i++:

++i costs less gas compared to i++. about 5 gas per iteration.

BkdLocker.sol
  310,45:             for (uint256 i = 0; i < length; i++) {

Controller.sol
  117,45:         for (uint256 i = 0; i < numActions; i++) {

StakerVault.sol
  260,49:         for (uint256 i = 0; i < actions.length; i++) {

RoleManager.sol
  80,47:         for (uint256 i = 0; i < roles.length; i++) {

TopUpAction.sol
  188,51:         for (uint256 i = 0; i < protocols.length; i++) {
  456,41:         for (uint256 i = 0; i < length; i++) {
  479,41:         for (uint256 i = 0; i < length; i++) {
  506,42:         for (uint256 i = 0; i < howMany; i++) {
  891,41:         for (uint256 i = 0; i < length; i++) {

TopUpKeeperHelper.sol
  43,51:             for (uint256 i = 0; i < users.length; i++) {
  72,46:         for (uint256 i = 0; i < keys.length; i++) {
  93,41:         for (uint256 i = 0; i < length; i++) {
  165,41:         for (uint256 i = 0; i < length; i++) {

CompoundHandler.sol
  135,48:         for (uint256 i = 0; i < assets.length; i++) {

CTokenRegistry.sol
  61,49:         for (uint256 i = 0; i < ctokens.length; i++) {

ConvexStrategyBase.sol
  313,57:         for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  380,57:         for (uint256 i = 0; i < _rewardTokens.length(); i++) {

MockStableSwap.sol
  30,36:         for (uint256 i = 0; i < 3; i++) {
  42,36:         for (uint256 i = 0; i < 2; i++) {
  70,36:         for (uint256 i = 0; i < 3; i++) {

InflationManager.sol
  91,41:         for (uint256 i = 0; i < length; i++) {
  105,41:         for (uint256 i = 0; i < length; i++) {
  109,54:         for (uint256 i = 0; i < stakerVaults.length; i++) {
  114,41:         for (uint256 i = 0; i < length; i++) {
  166,41:         for (uint256 i = 0; i < length; i++) {
  191,41:         for (uint256 i = 0; i < length; i++) {
  259,41:         for (uint256 i = 0; i < length; i++) {
  283,41:         for (uint256 i = 0; i < length; i++) {
  357,41:         for (uint256 i = 0; i < length; i++) {
  381,41:         for (uint256 i = 0; i < length; i++) {
  404,41:         for (uint256 i = 0; i < length; i++) {
  445,41:         for (uint256 i = 0; i < length; i++) {

KeeperGauge.sol
  155,52:         for (uint256 i = startEpoch; i < endEpoch; i++) {

VestedEscrow.sol
  93,49:         for (uint256 i = 0; i < amounts.length; i++) {

EnumerableExtensions.sol
  21,38:         for (uint256 i = 0; i < len; i++) {
  34,38:         for (uint256 i = 0; i < len; i++) {
  92,38:         for (uint256 i = 0; i < len; i++) {
  105,38:         for (uint256 i = 0; i < len; i++) {
  118,38:         for (uint256 i = 0; i < len; i++) {
  131,38:         for (uint256 i = 0; i < len; i++) {

!= 0 use less gas than > 0 for unsigned ints in require():

BkdLocker.sol
  90,24:         require(amount > 0, Error.INVALID_AMOUNT);
  91,36:         require(totalLockedBoosted > 0, Error.NOT_ENOUGH_FUNDS);
  136,24:         require(length > 0, "No entries");

TopUpAction.sol
  210,42:         require(record.singleTopUpAmount > 0, Error.INVALID_AMOUNT);
  554,43:         require(position.totalTopUpAmount > 0, Error.INSUFFICIENT_BALANCE);

TopUpActionFeeHandler.sol
  123,32:         require(totalClaimable > 0, Error.NOTHING_TO_CLAIM);

LiquidityPool.sol
  401,29:         require(_depositCap > 0, Error.INVALID_AMOUNT);
  471,34:         require(underlyingAmount > 0, Error.INVALID_AMOUNT);
  473,45:         require(lpToken_.balanceOf(account) > 0, Error.INSUFFICIENT_BALANCE);
  549,32:         require(redeemLpTokens > 0, Error.INVALID_AMOUNT);

StrategySwapper.sol
  111,36:         require(slippageTolerance_ > 0.8e18, Error.INVALID_SLIPPAGE_TOLERANCE);

MockErc20Strategy.sol
  65,32:         require(currentBalance > 0, "Invalid amount to withdraw");
  75,24:         require(amount > 0, "Invalid amount to transfer");

MockEthStrategy.sol
  66,32:         require(currentBalance > 0, "Invalid amount to withdraw");

MockVotingEscrow.sol
  35,39:         require(_balances[msg.sender] > 0, "a lock needs to first be created");

AmmConvexGauge.sol
  159,24:         require(amount > 0, Error.INVALID_AMOUNT);
  172,24:         require(amount > 0, Error.INVALID_AMOUNT);

AmmGauge.sol
  104,24:         require(amount > 0, Error.INVALID_AMOUNT);
  125,24:         require(amount > 0, Error.INVALID_AMOUNT);

KeeperGauge.sol
  138,32:         require(totalClaimable > 0, Error.ZERO_TRANSFER_NOT_ALLOWED);

VestedEscrow.sol
  83,35:         require(unallocatedSupply > 0, "No reward tokens in contract");

Vault.sol
  164,24:         require(amount > 0, Error.INVALID_AMOUNT);

Assign 0 to uint256:

Default value of uint256 is 0. you can delete = 0 for saving some gas.

BkdLocker.sol
  133,42:         uint256 totalAvailableToWithdraw = 0;

Controller.sol
  114,34:         uint256 totalEthRequired = 0;

StakerVault.sol
  144,35:         uint256 startingAllowance = 0;

TopUpAction.sol
  452,34:         uint256 totalEthRequired = 0;

LiquidityPool.sol
  483,33:         uint256 currentFeeRatio = 0;

KeeperGauge.sol
  154,32:         uint256 totalClaimable = 0;

VestedEscrow.sol
  92,29:         uint256 totalAmount = 0;

Vault.sol
  135,41:             uint256 allocatedUnderlying = 0;
  583,33:         uint256 strategistShare = 0;

Use Custom Errors to save Gas:

Custom errors from Solidity 0.8.4 are cheaper than require messages. https://blog.soliditylang.org/2021/04/21/custom-errors/