Gas Optimizations #167
Labels
bug
Something isn't working
G (Gas Optimization)
resolved
Finding has been patched by sponsor (sponsor pls link to PR containing fix)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Gas opt
#1 Using
== true
cost more gashttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L16
Using
== true
to validate bool variable is unnecessary:#2 Using && in require() can cost more execution gas fee
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L15-L18
Using multiple
require()
can save execution gas:#3 Using
unchecked
and prefix increment for loop incrementhttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L14
Using unchecked can save gas:
#4 Relocate the
toAmount
var declaration locationhttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibSwap.sol#L31
Declare the var at L48, then put the value
LibAsset.getOwnBalance(_swapData.receivingAssetId)
replacingtoAmount
. Remove L31#5 Using parameter to emit event
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L47
Instead of reading from storage to get
s.cBridge
ands.cBridgeChainId
value, using_cBridge
and_chainId
to emit can save gas#6 Unnecessary
_sendingAssetIdBalance
var declarationhttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/NXTPFacet.sol#L54-L57
_sendingAssetIdBalance
was only called once in the contract. CachingLibAsset.getOwnBalance(sendingAssetId)
into memory is consuming gas. I recommend just put it directly at L57Same case at:
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/NXTPFacet.sol#L91
#7 Using
calldata
on struct parameterhttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L12
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L35
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L75
Using
calldata
to store struct data type (LifiData) can save gas#8 != more effective than <
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L92
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L105
Using != to validate than value is not 0 is gas saving
#9 Using unchecked on calculating
finalBalance
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/NXTPFacet.sol#L166
The calculation of
finalBalance
won't underflow because of the if condition at L165. Usingunchecked
will save gas:#10 Unnecessary
receivingAssetIdBalance
MSTOREhttps://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/GenericSwapFacet.sol#L23
receivingAssetIdBalance
var was merely called once in the function. Just passLibAsset.getOwnBalance(_lifiData.receivingAssetId)
value directly to L28:#11 Custom errors from Solidity 0.8.4 are cheaper than revert strings
By defined using
error
statement, and usingif(condition)revert()
to check the condition. It can be implemented for allrequire()
check#12 Function can set external
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L35
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L78
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L57
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L96
Some function can set external to save gas
The text was updated successfully, but these errors were encountered: