You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are a few variables throughout the code base that is only assigned in the constructor, and then never assigned a new value - these can be marked as immutable to save some gas every time the variables are used.
As shown in the code example above, the state variable is read from storage twice, meaning two gas expensive SLOAD operations. Instead, we can cache the variable in memory, such that the 2 SLOAD operations will be converted into 1 SLOAD, 1 MSTORE and 2 MLOAD operations, which is cheaper.
[G-03] Expressions in constants should be immutables
Using an expression like keccak256() in a constant will make the expression be evaluated each time the constant is used, which is the opposite of what we try to accomplish with a constant, and thus ends up costing more gas than needed.
Instead the variable should be marked as an immutable and assigned in the constructor, because then the expression is only evaluated once.
When declaring a uint it will have the value of 0 by default. This means that declaring a uint variable uint256 var = 0; will just use unnecessary gas and should instead just be uint256 var;.
Removing the assignment of 0 will save 8 gas and since this is done for almost every uint in the code base, the total amount of gas savings for this can be decent. A few examples:
InfinityExchange L148:
Gas optimization report
[G-01] Make variables immutable
There are a few variables throughout the code base that is only assigned in the constructor, and then never assigned a new value - these can be marked as immutable to save some gas every time the variables are used.
File InifityToken.sol L31:
File InfinityStaker.sol L25:
[G-02] Cache variable to save gas
The state variable
userMinOrderNonce[msg.sender]
in the contractcancelAllOrders()
can be cached to save gas on the expensiveSLOAD
operations.File InfinityExchange.sol at L380-L381:
As shown in the code example above, the state variable is read from storage twice, meaning two gas expensive
SLOAD
operations. Instead, we can cache the variable in memory, such that the 2SLOAD
operations will be converted into 1SLOAD
, 1MSTORE
and 2MLOAD
operations, which is cheaper.This means the code can be changed into:
This will approximately save
152 gas
[G-03] Expressions in constants should be immutables
Using an expression like
keccak256()
in a constant will make the expression be evaluated each time the constant is used, which is the opposite of what we try to accomplish with a constant, and thus ends up costing more gas than needed.Instead the variable should be marked as an immutable and assigned in the constructor, because then the expression is only evaluated once.
File InfinityToken.sol L25-L28:
[G-04] Don’t initialize uint with 0
When declaring a
uint
it will have the value of 0 by default. This means that declaring auint
variableuint256 var = 0;
will just use unnecessary gas and should instead just beuint256 var;
.Removing the assignment of 0 will save
8 gas
and since this is done for almost everyuint
in the code base, the total amount of gas savings for this can be decent. A few examples:InfinityExchange L148:
InfinityExchange L200:
InfinityExchange L219:
InfinityExchange L272:
InfinityExchange L308:
The text was updated successfully, but these errors were encountered: