code-423n4 / 2022-07-swivel-findings

0 stars 1 forks source link

Gas Optimizations #27

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

State variables that could be set immutable

In the following files there are state variables that could be set immutable to save gas.

Code instance:

    feenominators in Swivel.sol

Unnecessary array boundaries check when loading an array element twice

There are places in the code (especially in for-each loops) that loads the same array element more than once. 
In such cases, only one array boundaries check should take place, and the rest are unnecessary.
Therefore, this array element should be cached in a local variable and then be loaded
again using this local variable, skipping the redundant second array boundaries check: 

Code instance:

    Swivel.sol.setFee - double load of d[x]

Storage double reading. Could save SLOAD

Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the overall gas uses. The following is a list of functions and the storage variables that you read twice:

Code instances:

    ZcToken.sol: maturity is read twice in redeem
    ZcToken.sol: underlying is read twice in redeem
    ZcToken.sol: maturity is read twice in withdraw
    ZcToken.sol: underlying is read twice in withdraw

Rearrange state variables

You can change the order of the storage variables to decrease memory uses.

Code instance:

In VaultTracker.sol,rearranging the storage fields can optimize to: 5 slots from: 6 slots. The new order of types (you choose the actual variables):

  1. uint256
  2. uint256
  3. address
  4. uint8
  5. address
  6. address

Use != 0 instead of > 0

Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)

Code instances:

    VaultTracker.sol, 123: change 'maturityRate > 0' to 'maturityRate != 0'
    VaultTracker.sol, 59: change 'maturityRate > 0' to 'maturityRate != 0'
    VaultTracker.sol, 165: change 'maturityRate > 0' to 'maturityRate != 0'
    VaultTracker.sol, 184: change 'maturityRate > 0' to 'maturityRate != 0'
    VaultTracker.sol, 93: change 'maturityRate > 0' to 'maturityRate != 0'
    VaultTracker.sol, 222: change 'maturityRate > 0' to 'maturityRate != 0'

Unnecessary cast

Code instance:

    int256 FixedPointMathLib.sol.lnWad - unnecessary casting int256(x)

Use unchecked to save gas for certain additive calculations that cannot overflow

You can use unchecked in the following calculations since there is no risk to overflow:

Code instances:

    Swivel.sol (L#437) - uint256 when = block.timestamp + HOLD;
    Swivel.sol (L#523) - uint256 when = block.timestamp + HOLD;
    Swivel.sol (L#474) - uint256 when = block.timestamp + HOLD;

Consider inline the following functions to save gas

You can inline the following functions instead of writing a specific function to save gas.
(see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)

Code instances

    LibCompound.sol, viewUnderlyingBalanceOf, { return cToken.balanceOf(user).mulWadDown(viewExchangeRate(cToken)); }
    LibFuse.sol, viewUnderlyingBalanceOf, { return cToken.balanceOf(user).mulWadDown(viewExchangeRate(cToken)); }
    FixedPointMathLib.sol, mulWadUp, { return mulDivUp(x, y, WAD); // Equivalent to (x * y) / WAD rounded up. }
    FixedPointMathLib.sol, divWadUp, { return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up. }
    FixedPointMathLib.sol, mulWadDown, { return mulDivDown(x, y, WAD); // Equivalent to (x * y) / WAD rounded down. }
    FixedPointMathLib.sol, divWadDown, { return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down. }
    Hash.sol, order, { return keccak256(abi.encode( ORDER_TYPEHASH, o.key, o.protocol, o.maker, o.underlying, o.vault, o.exit, o.principal, o.premium, o.maturity, o.expiry )); }

Gas Optimization On The 2^256-1

Some projects (e.g. Uniswap - https://github.com/Uniswap/interface/blob/main/src/hooks/useApproveCallback.ts#L88) set the default value of the user's allowance to 2^256 - 1. Since the value 2^256 - 1 can also be represented in hex as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative choice could be now 0x8000000000000000000000000000000000000000000000000000000000000000 or 2^255 to represent "infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter costs 45'888 gas. If you accept that infinity can also be represented via 2^255 (instead of 2^256-1), which almost all projects can - you can already save about 4% gas leveraging this optimisation trick on those calculations.

Code instance:

    Swivel.sol (L#549): uint256 max = 2**256 - 1;)

Do not cache msg.sender

We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.

Code instances:

    https://github.com/code-423n4/2022-07-swivel/tree/main/Creator/Creator.sol#L19
    https://github.com/code-423n4/2022-07-swivel/tree/main/Marketplace/MarketPlace.sol#L40
    https://github.com/code-423n4/2022-07-swivel/tree/main/Swivel/Swivel.sol#L70
    https://github.com/code-423n4/2022-07-swivel/tree/main/VaultTracker/VaultTracker.sol#L33
    https://github.com/code-423n4/2022-07-swivel/tree/main/Creator/VaultTracker.sol#L33