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

6 stars 4 forks source link

Gas Optimizations #69

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas#1: Unecessary read from storage prior to a possible revert in initNXTP.

L33-37

        Storage storage s = getStorage();
        LibDiamond.enforceIsContractOwner();
        s.nxtpTxManager = _txMgrAddr;
    }

Storage storage s = getStorage(); reads from storage even though the function can revert at LibDiamond.enforceIsContractOwner(); causing unnecessary usage of gas.

Recommend implementing LibDiamond.enforceIsContractOwner(); prior to Storage storage s = getStorage();

Example:

        LibDiamond.enforceIsContractOwner();
        Storage storage s = getStorage();
        s.nxtpTxManager = _txMgrAddr;
    }

Gas#2: Unnecessary else statement in swapAndStartBridgeTokensViaCBridge

L92-119

The use of the else statement below is not necessary and can be avoided by implementing swapAndStartBridgeTokensViaCBridge in the same manner swapAndStartBridgeTokensViaHop or swapAndStartBridgeTokensViaNXTP have been implemented. Reference for swapAndStartBridgeTokensViaHop below.

swapAndStartBridgeTokensViaCBridge:

function swapAndStartBridgeTokensViaCBridge(
        LiFiData memory _lifiData,
        LibSwap.SwapData[] calldata _swapData,
        CBridgeData memory _cBridgeData
    ) public payable {
        if (_cBridgeData.token != address(0)) {
            uint256 _fromTokenBalance = LibAsset.getOwnBalance(_cBridgeData.token);

            // Swap
            _executeSwaps(_lifiData, _swapData);

            uint256 _postSwapBalance = LibAsset.getOwnBalance(_cBridgeData.token) - _fromTokenBalance;

            require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");

            _cBridgeData.amount = _postSwapBalance;
        } else {
            uint256 _fromBalance = address(this).balance;

            // Swap
            _executeSwaps(_lifiData, _swapData);

            uint256 _postSwapBalance = address(this).balance - _fromBalance;

            require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");

            _cBridgeData.amount = _postSwapBalance;
        }

swapAndStartBridgeTokensViaHop swapAndStartBridgeTokensViaHop:

function swapAndStartBridgeTokensViaHop(
        LiFiData memory _lifiData,
        LibSwap.SwapData[] calldata _swapData,
        HopData memory _hopData
    ) public payable {
        address sendingAssetId = _bridge(_hopData.asset).token;

        uint256 _sendingAssetIdBalance = LibAsset.getOwnBalance(sendingAssetId);

        // Swap
        _executeSwaps(_lifiData, _swapData);

        uint256 _postSwapBalance = LibAsset.getOwnBalance(sendingAssetId) - _sendingAssetIdBalance;

        require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");

        _hopData.amount = _postSwapBalance;

Note: Same issue present in AnyswapFacet.swapAndStartBridgeTokensViaAnyswapL74-108

H3xept commented 2 years ago
  1. Fixed in lifinance/lifi-contracts@36039dd114fc23acebb60cd195e1175a076e71b8
H3xept commented 2 years ago
  1. Refactored in lifinance/lifi-contracts@87a27cee2fbde337c4ab873971f37573d2240994