Open code423n4 opened 2 years ago
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.
memory
storage
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
It's possible to avoid storage access and save gas using immutable keyword for the following variables:
immutable
Swivel/Swivel.sol:33 address public aaveAddr;
https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L33
unchecked
L363 could be unchecked since due to L362 fee <= premiumFilled
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:
block.timestamp
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;
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++;
Line 121 could be filled[hash] = amount without repeated computation from line 116:
filled[hash] = amount
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
some addressed in other tickets
rest dupes
G01 - Caching storage variables
Since reading from
memory
is much cheaper than reading fromstorage
, state variables that are called more than 1 SLOAD inside the function should be cached.G02 - Variable that could be immutables
It's possible to avoid storage access and save gas using
immutable
keyword for the following variables: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/underflowL363 could be
unchecked
since due to L362fee <= premiumFilled
https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L363
Lines with adding
block.timestamp
value to constant value could beunchecked
since their overflow would appear only in the extremely far future:G04 - Using prefix increment
Prefix increment is cheaper than postfix increment. Consider using ++i in next lines:
G05 - No needed computation
Line 121 could be
filled[hash] = amount
without repeated computation from line 116: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