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

6 stars 4 forks source link

Gas Optimizations #39

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

G01: Redundant literal boolean comparisons

Line References

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

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

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

Description

Since the referenced lines are expressions that evaluate to booleans, there isn’t a need to check against the boolean literals true and false

Recommended Mitigation Steps

Replace if (condition == true) and if (condition == false) to if (condition) and if (!condition) respectively.

G02: Unused constant MAX_INT

Line References

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibSwap.sol#L8

Description

MAX_INT is declared but not used in the LibSwap contract. It can thus be removed.

G03: Replace _bridge() with s.cBridge

Line References

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/CBridgeFacet.sol#L143-L144

Description

The storage variable s has been retrieved in the line above. The bridge address can therefore be directly fetched from it instead of making another internal function call.

Recommended Mitigation Steps

Storage storage s = getStorage();
address bridge = s.cBridge;

G04: Redundant non-zero address check on _anyswapData.token

Line References

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

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

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

Description

If _anyswapData.token is the zero address, calling IAnyswapToken(_anyswapData.token).underlying(); would revert with a call to a non-contract account. Hence the check _anyswapData.token != address(0) is not necessary. Alternatively, shift the check to be before the call to fetch the underlying token address IAnyswapToken(_anyswapData.token).underlying();.

Recommended Mitigation Steps

require(_anyswapData.token != address(0), '0 anyswap token address');
address underlyingToken = IAnyswapToken(_anyswapData.token).underlying();

or remove the checks.

G05: Duplicate condition checks in LibSwap#swap() can be combined

Line References

https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibSwap.sol#L33-L39

Description

The condition if(!LibAsset.isNativeAsset(fromAssetId)) is checked twice. They should be combined to save gas.

Recommended Mitigation Steps

if (!LibAsset.isNativeAsset(fromAssetId)) {
  LibAsset.approveERC20(IERC20(fromAssetId), _swapData.approveTo, fromAmount);
  if (LibAsset.getOwnBalance(fromAssetId) < fromAmount) {
    LibAsset.transferFromERC20(fromAssetId, msg.sender, address(this), fromAmount);
  }
}
H3xept commented 2 years ago

Re Unused variable MAX_INT

Duplicate of #100