Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #97

Open
code423n4 opened this issue Jun 17, 2022 · 1 comment
Open

Gas Optimizations #97

code423n4 opened this issue Jun 17, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

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:

uint256 public currentEpochTimestamp;

File InfinityStaker.sol L25:

address public INFINITY_TOKEN;

[G-02] Cache variable to save gas

The state variable userMinOrderNonce[msg.sender] in the contract cancelAllOrders() can be cached to save gas on the expensive SLOAD operations.

File InfinityExchange.sol at L380-L381:

require(minNonce > userMinOrderNonce[msg.sender], 'nonce too low');
require(minNonce < userMinOrderNonce[msg.sender] + 1000000, 'too many');

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.

This means the code can be changed into:

uint256 cachedNonce = userMinOrderNonce[msg.sender];
require(minNonce > cachedNonce, 'nonce too low');
require(minNonce < cachedNonce + 1000000, 'too many');

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:

bytes32 public constant EPOCH_INFLATION = keccak256('Inflation');
bytes32 public constant EPOCH_DURATION = keccak256('EpochDuration');
bytes32 public constant EPOCH_CLIFF = keccak256('Cliff');
bytes32 public constant MAX_EPOCHS = keccak256('MaxEpochs');

[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 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:

for (uint256 i = 0; i < numMakerOrders; ) {

InfinityExchange L200:

for (uint256 i = 0; i < ordersLength; ) {

InfinityExchange L219:

for (uint256 i = 0; i < ordersLength; ) {

InfinityExchange L272:

for (uint256 i = 0; i < numSells; ) {

InfinityExchange L308:

for (uint256 i = 0; i < numMakerOrders; ) {
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 17, 2022
code423n4 added a commit that referenced this issue Jun 17, 2022
@nneverlander
Copy link
Collaborator

Duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants