Shorten the revert strings to fit in 32 bytes. That will affect gas optimization.
C4-002 : Adding unchecked directive can save gas
Impact
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
Unchecked{i++};
C4-003 : 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-004 : There is no need to assign default values to variables
Impact - Gas Optimization
Boolean is default initialized to false. There is no need assign false to variable.
Proof of Concept
2022-06-infinity/contracts/core/InfinityExchange.sol::148 => for (uint256 i = 0; i < numMakerOrders; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::200 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::219 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::272 => for (uint256 i = 0; i < numSells; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::308 => for (uint256 i = 0; i < numMakerOrders; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::349 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::393 => for (uint256 i = 0; i < numNonces; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1048 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1086 => for (uint256 i = 0; i < numTokens; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1109 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1190 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1206 => for (uint256 i = 0; i < numTokens; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::76 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::82 => for (uint256 j = 0; j < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::197 => uint256 numConstructedItems = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::199 => for (uint256 i = 0; i < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::214 => uint256 numTakerItems = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::216 => for (uint256 i = 0; i < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::244 => uint256 numCollsMatched = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::246 => for (uint256 i = 0; i < order2NftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::247 => for (uint256 j = 0; j < order1NftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::289 => uint256 numTokenIdsPerCollMatched = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::290 => for (uint256 k = 0; k < item2TokensLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::291 => for (uint256 l = 0; l < item1TokensLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::318 => uint256 sum = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::320 => for (uint256 i = 0; i < ordersLength; ) {
Tools Used
Code Review
Recommended Mitigation Steps
bool x = false costs more gas than bool x without having any different functionality.
C4-005 : Using operator && used more gas
Impact
Using double require instead of operator && can save more gas.
using &&:
function check(uint x)public view{
require(x == 0 && x < 1 );
}
// gas cost 21630
using double require:
require(x == 0 );
require( x < 1);
}
}
// gas cost 21622
C4-006 : 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
2022-06-infinity/contracts/MockERC721.sol::11 => for (uint256 i = 0; i < 100; i++) {
2022-06-infinity/contracts/core/InfinityExchange.sol::148 => for (uint256 i = 0; i < numMakerOrders; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::200 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::219 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::272 => for (uint256 i = 0; i < numSells; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::308 => for (uint256 i = 0; i < numMakerOrders; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::349 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::393 => for (uint256 i = 0; i < numNonces; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1048 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1086 => for (uint256 i = 0; i < numTokens; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1109 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1190 => for (uint256 i = 0; i < numNfts; ) {
2022-06-infinity/contracts/core/InfinityExchange.sol::1206 => for (uint256 i = 0; i < numTokens; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::76 => for (uint256 i = 0; i < ordersLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::82 => for (uint256 j = 0; j < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::197 => uint256 numConstructedItems = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::199 => for (uint256 i = 0; i < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::214 => uint256 numTakerItems = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::216 => for (uint256 i = 0; i < nftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::244 => uint256 numCollsMatched = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::246 => for (uint256 i = 0; i < order2NftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::247 => for (uint256 j = 0; j < order1NftsLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::289 => uint256 numTokenIdsPerCollMatched = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::290 => for (uint256 k = 0; k < item2TokensLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::291 => for (uint256 l = 0; l < item1TokensLength; ) {
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::318 => uint256 sum = 0;
2022-06-infinity/contracts/core/InfinityOrderBookComplication.sol::320 => for (uint256 i = 0; i < ordersLength; ) {
Tools Used
Code Review
Recommended Mitigation Steps
Use >= or <= instead of > and < when possible.
C4-007 : Use Custom Errors instead of Revert Strings to save Gas
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)
Source Custom Errors in Solidity:
Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.
Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).
Instances include:
All require Statements
Tools Used
Code Review
Recommended Mitigation Steps
Recommended to replace revert strings with custom errors.
C4-008 : 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.
Proof of Concept
2022-06-infinity/contracts/staking/InfinityStaker.sol::39 => /// the user will get back 100/4 tokens.
2022-06-infinity/contracts/staking/InfinityStaker.sol::235 => (userstakedAmounts[user][Duration.THREE_MONTHS].amount * 2) +
2022-06-infinity/contracts/staking/InfinityStaker.sol::237 => (userstakedAmounts[user][Duration.TWELVE_MONTHS].amount * 4)) / (10**18);
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.
C4-009 : State Variables that can be changed to immutable
Impact
Solidity 0.6.5
introduced immutable as a major feature. It allows setting
contract-level variables at construction time which gets stored in code
rather than storage.
Consider the following generic example:
contract C {
/// The owner is set during contruction time, and never changed afterwards.
address public owner = msg.sender;
}
In the above example, each call to the function owner() reads from
storage, using a sload. After
EIP-2929, this costs 2100 gas
cold or 100 gas warm. However, the following snippet is more gas
efficient:
contract C {
/// The owner is set during contruction time, and never changed afterwards.
address public immutable owner = msg.sender;
}
In the above example, each storage read of the owner state variable is
replaced by the instruction push32 value, where value is set during
contract construction time. Unlike the last example, this costs only 3
gas.
The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
ISSUE LIST
C4-001: Revert String Size Optimization - [S]
C4-002 : Adding unchecked directive can save gas - [S]
C4-003 : Check if amount > 0 before token transfer can save gas - [S]
C4-004 : There is no need to assign default values to variables - [N]
C4-005 : Using operator && used more gas - [M]
C4-006 : Non-strict inequalities are cheaper than strict ones - [N]
C4-007 : Use Custom Errors instead of Revert Strings to save Gas - [S]
C4-008 : Use Shift Right/Left instead of Division/Multiplication if possible [N]
C4-009 : State Variables that can be changed to immutable - [N]
C4-010 : Keccak functions in constants waste gas - [M]
C4-011 :
> 0
can be replaced with!= 0
for gas optimization - [S]C4-001: Revert String Size Optimization
Impact
Shortening revert strings to fit in 32 bytes will decrease deploy time gas and will decrease runtime gas when the revert condition has been met.
Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.
Proof of Concept
Revert strings > 32 bytes are here:
Tools Used
Manual Review
Recommended Mitigation Steps
Shorten the revert strings to fit in 32 bytes. That will affect gas optimization.
C4-002 : Adding unchecked directive can save gas
Impact
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
C4-003 : 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-004 : There is no need to assign default values to variables
Impact - Gas Optimization
Boolean is default initialized to false. There is no need assign false to variable.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
bool x = false costs more gas than bool x without having any different functionality.
C4-005 : Using operator && used more gas
Impact
Using double require instead of operator && can save more gas.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Example
C4-006 : 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-007 : Use Custom Errors instead of Revert Strings to save Gas
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)
Source Custom Errors in Solidity:
Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.
Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).
Instances include:
All require Statements
Tools Used
Code Review
Recommended Mitigation Steps
Recommended to replace revert strings with custom errors.
C4-008 : 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.
Proof of Concept
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.
C4-009 : State Variables that can be changed to immutable
Impact
Solidity 0.6.5 introduced immutable as a major feature. It allows setting contract-level variables at construction time which gets stored in code rather than storage.
Consider the following generic example:
In the above example, each call to the function owner() reads from storage, using a sload. After EIP-2929, this costs 2100 gas cold or 100 gas warm. However, the following snippet is more gas efficient:
In the above example, each storage read of the owner state variable is replaced by the instruction push32 value, where value is set during contract construction time. Unlike the last example, this costs only 3 gas.
Code Location
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L25
Tools Used
None
Recommended Mitigation Steps
Consider using immutable variable.
C4-010 : Keccak functions in constants waste gas
Impact
The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.
See this issue for more context: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232)
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Replace the constant directive with immutable, or assign the already hashed value to the constants
C4-011 :
> 0
can be replaced with!= 0
for gas optimizationImpact
!= 0
is a cheaper operation compared to> 0
, when dealing with uint.Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider to replace
> 0
with!= 0
for gas optimization.