Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #20

Open
code423n4 opened this issue Mar 26, 2022 · 2 comments
Open

Gas Optimizations #20

code423n4 opened this issue Mar 26, 2022 · 2 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix)

Comments

@code423n4
Copy link
Contributor

Use uint256 instead of uint8 at for loop at Swapper.sol and HopFacet.sol

Target codebase

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L14

for (uint8 i; i < _swapData.length; i++) {
    require(
        ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true,
        "Contract call not allowed!"
    );

    LibSwap.swap(_lifiData.transactionId, _swapData[i]);
}

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/HopFacet.sol#L48-L50

for (uint8 i; i < _tokens.length; i++) {
    s.hopBridges[_tokens[i]] = _bridgeConfigs[i];
}

Usage of uint8 increases the gas fee. If switching this to uint256 is possible for the product, it can reduce the gas fee.

Proposed implementations

Just use uint256 instead of uint8.

for (uint256 i; i < _swapData.length; i++) {
    require(
        ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true,
        "Contract call not allowed!"
    );

    LibSwap.swap(_lifiData.transactionId, _swapData[i]);
}
for (uint256 i; i < _tokens.length; i++) {
    s.hopBridges[_tokens[i]] = _bridgeConfigs[i];
}

Gas improvements

Gas fees of methods and deployments are decreased by using uint256 instead of uint8.

Methods - average gas change

Contract Methods Before After Change
AnyswapFacet swapAndStartBridgeTokensViaAnyswap 238904 238872 -32
CBridgeFacet swapAndStartBridgeTokensViaCBridge 315598 315557 -41
DiamondCutFacet diamondCut 252812 252800 -12
GenericSwapFacet swapTokensGeneric 254149 254108 -41
HopFacet startBridgeTokensViaHop 215293 215289 -4
HopFacet swapAndStartBridgeTokensViaHop 357016 356975 -41
NXTPFacet swapAndStartBridgeTokensViaNXTP 357455 357414 -

Deployments - average gas change

Contract Before After Change
HopFacet 1482624 1476996 -5628
NXTPFacet 1754544 1750853 -3691

== true check is not needed at _executeSwaps function in Swapper.sol

Target codebase

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L16

require(
    ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true,
    "Contract call not allowed!"
);

When checking boolean value, it does not need to check == true.

Proposed implementations

Following code has same meaning.

require(
    ls.dexWhitelist[_swapData[i].approveTo] && ls.dexWhitelist[_swapData[i].callTo],
    "Contract call not allowed!"
);

Gas improvements

Confirmed that the gas fees of deployments and methods decreased.


Usage of != 0 instead of > 0 can reduce the gas fee slightly

Target codebase

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/HopFacet.sol#L109
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
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L105
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L116
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/NXTPFacet.sol#L98
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibAsset.sol#L67
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L84
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L102
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L121
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L189
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L196
https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L212

Proposed implementations

Use != 0 instead of > 0. Example code is as follows:

require(contractSize != 0, _errorMessage);

Gas improvements

Confirmed that the gas fees of deployments and methods decreased.


Avoid using == true or == false at DexManagerFacet.sol

Target codebase

Where == true is used

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L20

if (s.dexWhitelist[_dex] == true) {
    return;
}

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L34

if (s.dexWhitelist[_dexs[i]] == true) {
    continue;
}

Where == false is used

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L47

if (s.dexWhitelist[_dex] == false) {
    return;
}

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L66

if (s.dexWhitelist[_dexs[i]] == false) {
    continue;
}

Proposed implementations

Where == true is used

if (s.dexWhitelist[_dex]) {
    return;
}
if (s.dexWhitelist[_dexs[i]]) {
    continue;
}

Where == false is used

if (!s.dexWhitelist[_dex]) {
    return;
}
if (!s.dexWhitelist[_dexs[i]]) {
    continue;
}

Gas improvements

Confirmed that the gas fees of deployments and methods slightly decreased.


@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Mar 26, 2022
code423n4 added a commit that referenced this issue Mar 26, 2022
@H3xept H3xept self-assigned this Apr 5, 2022
@H3xept
Copy link
Collaborator

H3xept commented Apr 5, 2022

Re: uint8 -> uint256

Fixed in lifinance/lifi-contracts@3c1558ef50a19cfbbdd6d616d18322dae0bef6ba

@ezynda3 ezynda3 closed this as completed Apr 5, 2022
@H3xept
Copy link
Collaborator

H3xept commented Apr 11, 2022

Re uintx to uint256

Duplicate of #196

@H3xept H3xept added the resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) label Apr 11, 2022
@H3xept H3xept reopened this Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix)
Projects
None yet
Development

No branches or pull requests

2 participants