Open code423n4 opened 2 years ago
Re For uint use != 0 instead of > 0: -- Fixed in previous commit.
Fixed in lifinance/lifi-contracts@f35ed79a266a69b363d72332b7861d15d18b98cb
Removed in previous commit.
We internally decided to avoid prefix increments for now.
Re Cache _postBalance earlier -- Fixed in lifinance/lifi-contracts@87a27cee2fbde337c4ab873971f37573d2240994
We internally decided to avoid previx increments for now.
Gas optimization report
For uint use != 0 instead of > 0
For the type
uint
it is cheaper to use the logical operator!=
instead of the>
when checking the value is not0
. The value can never be below0
since it is unsigned and hence would underflow instead.The relevant code:
Keep revert strings below 32 bytes
Strings are stored in slots of 32 bytes, and hence the length of the revert string should be at max 32 bytes to fit inside 1 slot. If the string is just 33 bytes it will occupy 2 slots (64 bytes). Keeping the string size at 32 bytes or below will save gas on both deployment and when the revert condition is met.
Since the used version of Solidity is
>=0.8.4
it would also be worth considering using Custom Errors which is both more gas efficient and allows error descriptions using NatSpec.The relevant code:
Unused variable MAX_INT in LibSwap.sol
The constant variable
MAX_INT
is never used inLibSwap.sol
and can hence be removed.The relevant code:
For loop gas optimization
It is cheaper to prefix increment (
++i
) a loop counter rather than postfix increment it (i++
). Furthermore, there is no reason to use checked arithmetic on auint
/uint256
loop counter, since there is little to no risk of it overflowing. In newer versions of Solidity there is a built-in overflow check for increments, which is not necessary in this case and is hence a waste of gas. Instead it is possible to skip the overflow check by using unchecked arithmetic likeunchecked{++i}
This means that a loop looking like this
Should be rewritten into this
These optimizations will save gas for each iteration and hence the total amount of saved gas depends on the size of the element that is looped over.
The relevant code:
Unused library import
The library
LibDiamond
is imported twice inAnySwapFacet.sol
and the library is not used in the file and hence should be removed.The relevant code:
Cache _postSwapBalance earlier
In
AnySwapFacet.sol
the_postSwapBalance
is cached in line 103, however, the exact same operation is used in the line above the caching (line 101). Therefore, it is possible to save aBALANCE
instruction by swapping the two lines such that:Becomes this: