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

6 stars 4 forks source link

Gas Optimizations #183

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. > 0 is less efficient than != 0

Proof of Concept: 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/NXTPFacet.sol#L98

Recommended Mitigation Steps: Change to:

require(_postSwapBalance != 0, "ERR_INVALID_AMOUNT");

========================================================================

  1. Use of uint8 in for loop increases gas costs

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

Recommended Mitigation Steps: Change uint8 to uint256

========================================================================

  1. changing i++ to ++i can save gas

Proof of Concept: https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/HopFacet.sol#L48 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DiamondLoupeFacet.sol#L24 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/DexManagerFacet.sol#L33

Recommended Mitigation Steps: Change i++ to ++i

========================================================================

  1. != 0 is a cheaper operation compared to > 0, when dealing with uint

Proof of Concept: 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

Recommended Mitigation Steps: Replace > with ! =

========================================================================

  1. the best way to use SafeERC20.function for gas opt

Proof of Concept: https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/WithdrawFacet.sol#L9

Recommended Mitigation Steps: by not declaring:

using SafeERC20 for IERC20;

and use:

SafeERC20.safeTransfer(IERC20(_assetAddress), sendTo, _amount);

========================================================================

H3xept commented 2 years ago
  1. fixed by lifinance/lifi-contracts@6da5fff8550d5b1fd46c08a9133422e8e5cd4b6d
H3xept commented 2 years ago
  1. We discussed this internally and decided not to implement prefix increments for now.
H3xept commented 2 years ago
  1. Fixed by lifinance/lifi-contracts@3c1558ef50a19cfbbdd6d616d18322dae0bef6ba
H3xept commented 2 years ago

Re > 0 is less efficient than != 0

Duplicate of #100

H3xept commented 2 years ago

Re uintx to uint256

Duplicate of #196

H3xept commented 2 years ago

Re prefix increments

We internally decided to avoid previx increments for now.