code-423n4 / 2022-02-concur-findings

2 stars 0 forks source link

Gas Optimizations #73

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

General issues

  1. Change the incremental logic from i++ to ++i in order to save some opcodes:

  2. Use delete instead of set to default value (false or 0)

Contracts specific

Contract contracts\MasterChef.sol

  1. The following variables could be declared as constant:

    • concurPerBlock
    • _concurShareMultiplier
    • _perMille
  2. The following variables could be declared as immutable:

    • startBlock
    • endBlock
    • concur
  3. There was an empty pool in lines L63-L70, pushed during the constructor that never is used.

  4. The following struct could be optimized moving depositFeeBP close to depositToken in order to save one storage slot:

    struct PoolInfo {
        IERC20 depositToken; // <- Move here
        uint allocPoint;
        uint lastRewardBlock;
        uint accConcurPerShare;
        uint16 depositFeeBP; // <- Move this
    }
  5. On line 204, the transferSuccess variable is init false, it is better to create the variable with the return to avoid this initialization.

Contract contracts\VoteProxy.sol

  1. In line 21 it could be removed the == true in order to save some opcodes.

Contract contracts\USDMPegRecovery.sol

  1. The variable startLiquidity is never used and can be removed or changed to immutable.
  2. The variable step coud be constant.
  3. On lines 74-76 usdm.balanceOf(address(this)) was called twice without any change of balance, it's cheaper to cache it.

Contract contracts\ConvexStakingWrapper.sol

  1. In line 235 it's better to move the asignation of deposits[_pid][msg.sender].amount inside the if if (_amount > 0) {
    deposits[_pid][msg.sender].amount -= uint192(_amount);
        if (_amount > 0) {
GalloDaSballo commented 2 years ago

1) 9 gas 2) Would like to see proof that it actually saves gas, per the docs this won't save gas

MastChef.sol 1) -> 2.1k per variable per read -> 6.1k 2) -> 2.1k per variable per read -> 6.1k 3) Not sure if we can remove this as the sponsor may want to always use non-zero indexes 4) Saves a SLOT, let's say 2k gas (one less Cold Slot) 5) Disagree as syntactic sugar won't save gas

VoteProxy.sol 1) Would like to see proof but I agree, will give it an ISZERO, 3 gas

USDMPegRecovery.sol 1) Agree, 2.1k 2) Agree, 2.1k 3) Agree, 200 gas (STATICCALL + Hot Slot Read)

ConvexStakingWrapper.sol 1) Would save 200 gas (Hot SLOAD + Same Value SSTORE)

18812 Gas Saved by implementing these findings