code-423n4 / 2022-06-connext-findings

1 stars 0 forks source link

Gas Optimizations #230

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. ++I COSTS LESS GAS THAN ++I, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\SwapUtils.sol (Line 205)

  2. File: contracts\contracts\core\connext\helpers\StableSwap.sol (Line 81)


2. X = X + Y IS CHEAPER THAN X += Y

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\facets\BridgeFacet.sol (Line 796)

  2. File: contracts\contracts\core\connext\facets\BridgeFacet.sol (Line 1003)


3. REQUIRE() OR REVERT() STATEMENTS THAT CHECK INPUT ARGUMENTS SHOULD BE AT THE TOP OF THE FUNCTION

Example of this issue in the codebase:

  1. File: contracts\contracts\core\connext\facets\BaseConnextFacet.sol (Line 125)

4. ++I/I++ SHOULD BE UNCHECKED{++I}/UNCHECKED{++I} WHEN IT IS NOT POSSIBLE FOR THEM TO OVERFLOW, AS IS THE CASE WHEN USED IN FOR- AND WHILE-LOOPS

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\helpers\ConnextPriceOracle.sol (Line 176)

  2. File: contracts\contracts\core\connext\helpers\Multicall.sol (Line 16)


5. USING > 0 COSTS MORE GAS THAN != 0 WHEN USED ON A UINT IN A REQUIRE() STATEMENT

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\helpers\ConnextPriceOracle.sol (Line 150)

  2. File: contracts\contracts\core\connext\libraries\AmplificationUtils.sol (Line 86)


6. SPLITTING REQUIRE() STATEMENTS THAT USE && SAVES GAS

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\AmplificationUtils.sol (Line 86)

  2. File: contracts\contracts\core\connext\libraries\SwapUtils.sol (Line 397)

  3. File: contracts\contracts\core\connext\libraries\SwapUtils.sol (Line 493)


7. IT COSTS MORE GAS TO INITIALIZE VARIABLES TO ZERO THAN TO LET THE DEFAULT OF ZERO BE APPLIED

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\SwapUtils.sol (Line 289)

  2. File: contracts\contracts\core\connext\libraries\SwapUtils.sol (Line 300)


8. SafeMath IS NO LONGER NEEDED STARTING WITH SOLIDITY 0.8

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\helpers\ConnextPriceOracle.sol (Line 4)

  2. File: contracts\core\connext\helpers\OZERC20.sol (Line 10)


9. EXPRESSIONS FOR CONSTANT VALUES SUCH AS A CALL TO KECCAK256(), SHOULD USE IMMUTABLE RATHER THAN CONSTANT

See this issue for a detail description of the issue Example of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\LibDiamond.sol (Line 14)

10. USE CallData INSTEAD OF MEMORY

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\SwapUtils.sol. balances can be changed to calldata (Line 336)

  2. File: contracts\contracts\core\connext\libraries\SwapUtils.sol. xp can be changed to calldata (Line 286)


11. USE CUSTOM ERRORS RATHER THAN REVERT()/REQUIRE() STRINGS TO SAVE DEPLOYMENT GAS

Examples of this issue in the codebase:

  1. File: contracts\contracts\core\connext\libraries\LibDiamond.sol (Line 113)

  2. File: contracts\contracts\core\connext\libraries\LibDiamond.sol (Line 121)


12. USE string.concat() INSTEAD OF abi.encodePacked(<str>,<str>)

Example of this issue in the codebase:

  1. File: contracts\contracts\core\connext\helpers\TokenRegistry.sol (Line 330)

13. STATE VARIABLES ONLY SET IN THE CONSTRUCTOR SHOULD BE DECLARED IMMUTABLE

Example of this issue in the codebase:

  1. File: contracts\contracts\core\connext\helpers\ConnextPriceOracle.sol (Line 49)