4 use unchecked. Underflow is already checked in if statement before the following line.
Since Solidity 0.8.0, all arithmetic operations revert on over- and underflow by default, thus making the use of these libraries unnecessary. To obtain the previous behaviour, an unchecked block can be used and this is cheaper from the view of gas.
5 use memory instead of storage and you don’t need to call getStorage() to get s.cBridge, so you don’t use _bridge()any more. You can delete this function and save deployment costs.
2022-03-Li.finance gas optimization
1 use unchecked and prefix for loop
https://github.com/code-423n4/2022-03-Li.finance/blob/main/src/Facets/Swapper.sol#L14 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L33 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L52 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L65 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L70 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DiamondLoupeFacet.sol#L24 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/HopFacet.sol#L48 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L67 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L92 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L110 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L125
For example,
for (uint8 i; i < _swapData.length;) { // some executions
}
2 == true is unnecessary in require.
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L16
require( ls.dexWhitelist[_swapData[i].approveTo] && ls.dexWhitelist[_swapData[i].callTo], "Contract call not allowed!" )
3 use unchecked. Underflow will not happen in the following part because of an increase in the balance.
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibSwap.sol#L48
unchecked { toAmount = LibAsset.getOwnBalance(_swapData.receivingAssetId) - toAmount; }
4 use unchecked. Underflow is already checked in if statement before the following line. Since Solidity 0.8.0, all arithmetic operations revert on over- and underflow by default, thus making the use of these libraries unnecessary. To obtain the previous behaviour, an unchecked block can be used and this is cheaper from the view of gas.
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/NXTPFacet.sol#L166
Unchecked { finalBalance = postSwapBalance - startingBalance; }
5 use memory instead of storage and you don’t need to call getStorage() to get s.cBridge, so you don’t use _bridge()any more. You can delete this function and save deployment costs.
https://github.com/code-423n4/2022-03-lifinance/blob/699c2305fcfb6fe8862b75b26d1d8a2f46a551e6/src/Facets/CBridgeFacet.sol#L143-L144
Storage memory s = getStorage();
and delete _bridge()in CBridgeFacet.sol.
6 Don’t use == (equality) to save gas. The following lines use == (equality) for boolean. You don’t need this syntax to check boolean.
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L20 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L34
For example,
if (s.dexWhitelist[_dex]){}
7 Use ! instead of == false. ! must be cheaper than == false.
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L47 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L66
if (!s.dexWhitelist[_dex)