Open code423n4 opened 2 years ago
Context:
Recommendation:
Change X += Y (X -= Y) to X = X + Y (X = X - Y).
Description:
Default value of uint is 0. It's unnecessary and costs more gas to initialize uint variavles to 0.
1-5. Change "uint256 i = 0;" to "uint256 i;".
6-7. Remove the line.
uint256 type will never be less than 0.
Change > 0 to !=0.
for (uint i = 0; i < minters_array.length; i++){
https://github.com/code-423n4/2022-09-frax/blob/main/src/ERC20/ERC20PermitPermissionedMint.sol#L84
Change i++ to ++i.
https://github.com/code-423n4/2022-09-frax/blob/main/src/ERC20/ERC20PermitPermissionedMint.sol#L34
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.
Place the arithmetic operations in an unchecked block.
If you read the length of the array at each iteration of the loop, this consumes a lot of gas.
Store the array’s length in a variable before the for-loop, and use this new variable in the loop.
Custom errors are more gas efficient than using require with a string explanation.
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:
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.
[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:
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.