Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider to upgrade pragma to at least 0.8.10.
C4-004 : Non-strict inequalities are cheaper than strict ones
Impact
Strict inequalities add a check of non equality which costs around 3 gas.
C4-005 : Check if amount > 0 before token transfer can save gas
Impact
Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.
C4-006 : Use Shift Right/Left instead of Division/Multiplication if possible
Impact
A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.
While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.
C4-001 : ++i is more gas efficient than i++ in loops forwarding
Impact
++i is more gas efficient than i++ in loops forwarding.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
It is recommend to use unchecked{++i} and change i declaration to uint256.
C4-002 : There is no need to assign default values to variables
Impact - Gas Optimization
When a variable is declared solidity assigns the default value. In case the contract assigns the value again, it costs extra gas.
Example: uint x = 0 costs more gas than uint x without having any different functionality.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
uint x = 0 costs more gas than uint x without having any different functionality.
C4-003 : Free gas savings for using solidity 0.8.10+
Impact
Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.
Proof of Concept
Solidity 0.8.10 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/
Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider to upgrade pragma to at least 0.8.10.
C4-004 : Non-strict inequalities are cheaper than strict ones
Impact
Strict inequalities add a check of non equality which costs around 3 gas.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Use >= or <= instead of > and < when possible.
C4-005 : Check if amount > 0 before token transfer can save gas
Impact
Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.
Proof of Concept
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider checking amount != 0.
C4-006 : Use Shift Right/Left instead of Division/Multiplication if possible
Impact
A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.
While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.
Code Location
https://github.com/fei-protocol/ERC4626/blob/643cd044fac34bcbf64e1c3790a5126fec0dbec1/src/xERC4626.sol#L89
Tools Used
None
Recommended Mitigation Steps
A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.