code-423n4 / 2022-09-frax-findings

2 stars 1 forks source link

Gas Optimizations #261

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Report

Gas Optimizations

[G-01]: X += Y (X -= Y) costs more gas than X = X + Y (X = X - Y)

Context:

Recommendation:

Change X += Y (X -= Y) to X = X + Y (X = X - Y).

[G-02]: Don't initialize variable with its default value

Context:

  1. https://github.com/code-423n4/2022-09-frax/blob/main/src/ERC20/ERC20PermitPermissionedMint.sol#L84
  2. https://github.com/code-423n4/2022-09-frax/blob/main/src/frxETHMinter.sol#L129
  3. https://github.com/code-423n4/2022-09-frax/blob/main/src/OperatorRegistry.sol#L63
  4. https://github.com/code-423n4/2022-09-frax/blob/main/src/OperatorRegistry.sol#L84
  5. https://github.com/code-423n4/2022-09-frax/blob/main/src/OperatorRegistry.sol#L114
  6. https://github.com/code-423n4/2022-09-frax/blob/main/src/frxETHMinter.sol#L63
  7. https://github.com/code-423n4/2022-09-frax/blob/main/src/frxETHMinter.sol#L64
  8. https://github.com/code-423n4/2022-09-frax/blob/main/src/frxETHMinter.sol#L94

Description:

Default value of uint is 0. It's unnecessary and costs more gas to initialize uint variavles to 0.

Recommendation:

1-5. Change "uint256 i = 0;" to "uint256 i;".

6-7. Remove the line.

  1. Change "uint256 withheld_amt = 0;" to "uint256 withheld_amt".

[G-03]: >0 costs more gas than !=0

Context:

Description:

uint256 type will never be less than 0.

Recommendation:

Change > 0 to !=0.

[G-04]: i++ costs more gas than ++i

Context:

for (uint i = 0; i < minters_array.length; i++){ 

https://github.com/code-423n4/2022-09-frax/blob/main/src/ERC20/ERC20PermitPermissionedMint.sol#L84

Recommendation:

Change i++ to ++i.

[G-05]: Unchecked arithmetic

Context:

https://github.com/code-423n4/2022-09-frax/blob/main/src/ERC20/ERC20PermitPermissionedMint.sol#L34

Description:

Some gas can be saved by using an unchecked {} block if an overflow/underflow isn't possible because of a previous require() or if-statement.

Recommendation:

Place the arithmetic operations in an unchecked block.

[G-06]: Use new variable instead of reading array length in every loop of a for-loop

Context:

Description:

If you read the length of the array at each iteration of the loop, this consumes a lot of gas.

Recommendation:

Store the array’s length in a variable before the for-loop, and use this new variable in the loop.

[G-07]: Use custom errors instead of revert strings

Custom errors are more gas efficient than using require with a string explanation.