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

0 stars 1 forks source link

Gas Optimizations #133

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

G01 - Caching storage variables

Since reading from memory is much cheaper than reading from storage, state variables that are called more than 1 SLOAD inside the function should be cached.

Marketplace/MarketPlace.sol:77  (address zct, address tracker) = ICreator(creator).create(p, underAddr, m, c, swivel, n, s, IErc20(underAddr).decimals()) ; // swivel 2nd SLOAD

Swivel/Swivel.sol:502 if (block.timestamp < feeChange) { revert Exception(17, block.timestamp, feeChange, address(0), address(0)); }  // feeChange 2nd SLOAD

G02 - Variable that could be immutables

It's possible to avoid storage access and save gas using immutable keyword for the following variables:

Swivel/Swivel.sol:33  address public aaveAddr;

https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L33

G03 - unchecked block can be used for gas efficiency of the expression that can't overflow/underflow

L363 could be unchecked since due to L362 fee <= premiumFilled

    Swivel/Swivel.sol:362 uint256 fee = premiumFilled / feenominators[3];
    Swivel/Swivel.sol:363 Safe.transfer(uToken, msg.sender, premiumFilled - fee);  

https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L363

Lines with adding block.timestamp value to constant value could be unchecked since their overflow would appear only in the extremely far future:

Swivel/Swivel.sol:437 uint256 when = block.timestamp + HOLD;
Swivel/Swivel.sol:474 uint256 when = block.timestamp + HOLD;  
Swivel/Swivel.sol:523 uint256 when = block.timestamp + HOLD; 

G04 - Using prefix increment

Prefix increment is cheaper than postfix increment. Consider using ++i in next lines:

Swivel/Swivel.sol:100 unchecked {i++;} 

Swivel/Swivel.sol:269 unchecked {i++;}  

Swivel/Swivel.sol:418 i++;  

Swivel/Swivel.sol:511 x++;  

Swivel/Swivel.sol:564 i++;  

G05 - No needed computation

Line 121 could be filled[hash] = amount without repeated computation from line 116:

    Swivel/Swivel.sol:116    uint256 amount = a + filled[hash];
        ...
    Swivel/Swivel.sol:121    filled[hash] += a; 

https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L116-L121 Same here: https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L158 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L193 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L222 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L287 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L318 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L348 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L383

robrobbins commented 2 years ago

some addressed in other tickets

rest dupes