code-423n4 / 2022-03-lifinance-findings

6 stars 4 forks source link

Gas Optimizations #70

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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

unchecked {
    ++i;
}

}

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();

    // Do CBridge stuff
    require(s.cBridgeChainId != _cBridgeData.dstChainId, "Cannot bridge to the same network.");

    if (LibAsset.isNativeAsset(_cBridgeData.token)) {
        ICBridge(s.cBridge).sendNative(
            _cBridgeData.receiver,
            _cBridgeData.amount,
            _cBridgeData.dstChainId,
            _cBridgeData.nonce,
            _cBridgeData.maxSlippage
        );
    } else {
        // Give CBridge approval to bridge tokens
        LibAsset.approveERC20(IERC20(_cBridgeData.token), s.cBridge, _cBridgeData.amount);
        // solhint-disable check-send-result
        ICBridge(s.cBridge).send(
            _cBridgeData.receiver,
            _cBridgeData.token,
            _cBridgeData.amount,
            _cBridgeData.dstChainId,
            _cBridgeData.nonce,
            _cBridgeData.maxSlippage
        );
    }

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)

H3xept commented 2 years ago

All issues have been tackled in other commits

H3xept commented 2 years ago

Re Redundant literal boolean comparisons

Duplicate of #39

H3xept commented 2 years ago

Re s.cBridge instead of calling the _bridge()

Duplicate of #39

H3xept commented 2 years ago

Re unchecked operations

We internally decided to avoid unchecked operations for now.

H3xept commented 2 years ago

Re prefix increments

We internally decided to avoid previx increments for now.