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

6 stars 4 forks source link

Gas Optimizations #201

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS OPTIMIZATION for LIFI by PeritoFlores

Avoid using the tautology ==true and change ==false for !

Some part of the code you are using == true and ==false. You can remove ==true and change ==false for ! to save gas

Swapper.sol#LOC16

            ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true,

DDexManagerFacet.sol#LOC20

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

DexManagerFacer.sol#LOC34

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

DexManagerFacer.sol#LOC47

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

DexManagerFacer.sol#LOC66

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

Using uint8 instead of uint256 cost more gas and can even cause an overflow.

You are using uint8 for some loops

for (uint8 i; i < _swapData.length; i++) {               Swaper.sol#L14


​ for (uint8 i; i < _tokens.length; i++) { HopFacets.sol#L48

First of all, in case that you send an array of more than 256 the transaction will revert with an overflow . Suppose that this case is unlikely.

EVM works with 256 bits word so every operation is on a uint256, unless you are packing variables using a shorter variable cost more gas. So I believe that using uint8 have no benefits.

Sugestion

change uint8 for uint.
H3xept commented 2 years ago

Re: Avoid using the tautology ==true and change ==false for !

Fixed by lifinance/lifi-contracts@4651609d65add358a2b9edf8f393f18735163139

H3xept commented 2 years ago

Re Redundant literal boolean comparisons

Duplicate of #39

H3xept commented 2 years ago

Re uintx to uint256

Duplicate of #196