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

6 stars 4 forks source link

Gas Optimizations #170

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Title: Cache array length in for loops can save gas

Impact

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack. Caching the array length in the stack saves around 3 gas per iteration.

Recommended Mitigation Steps

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


Title: Prefix increaments are cheaper than postfix increaments

Impact

Prefix increaments are cheaper than postfix increaments

Recommended Mitigation Steps

Use ++i in place of i++: https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/Swapper.sol#L14


Title: Redundant constructs

Impact

Redundant code and comments can be confusing and should be removed (or changed appropriately) after careful evaluation. This will not only reduce gas costs but improve readability and maintainability of the code.

Recommended Mitigation Steps

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

From: ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true To: ls.dexWhitelist[_swapData[i].approveTo] && ls.dexWhitelist[_swapData[i].callTo]


Title: Functions can be external

Impact

Public functions that are never called by the contract should be declared external to save gas.

Recommended Mitigation Steps

Change below functions to external: AnyswapFacet.startBridgeTokensViaAnyswap() https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L35

AnyswapFacet.swapAndStartBridgeTokensViaAnyswap() https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/AnyswapFacet.sol#L74

H3xept commented 2 years ago

Re Cache array length in for loops can save gas

Duplicate of #44

H3xept commented 2 years ago

Re Functions can be external

Duplicate of #197

H3xept commented 2 years ago

Re prefix increments

We internally decided to avoid previx increments for now.