Gas Optimizations #131
Labels
bug
Something isn't working
G (Gas Optimization)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Gas optimization report
[G-01] No need to initialize uint to 0
There is no need to initialize a uint to 0, since this will be done by default when declaring the variable. In fact it will cost a small amount of gas to initialize the value “twice”. An example can be found in the contract at
AuraLocker.sol
L72.The initialization should instead look like this:
Variables initialized to 0 can also be found several times in
AuraBalRewardPool.sol
andAuraMerkleDrop.sol
.[G-02] Optimize for loops
The loops found in e.g.
AuraLocker.sol
can be optimized to save gas. They could look like this found at L173-L185:3 modifications can be done to this loop to safe gas:
i
to 0, since this is automatically done when declaring auint
(the same argument as G-01).++i
is cheaper than postincrementi++
, since the latter will make use of a temporary variable to store the returned value in before incrementing it. This can be saved by doing a preincrement, which increments the value and then returns it.i
.With the above suggestions the for-loop above can be rewritten into:
[G-03] Redundant division followed by multiplication
Throughout the contract
AuraLocker.sol
there are multiple places where the block timestamp is divided and multiplied by the same constant. Division immediately followed by a multiplication (or vice versa) with the same number equals each other out, and hence both operations are redundant. An example can be seen at L162:Where the div() and mul() will be a waste of gas since they use the same constant, and thus can be rewritten to:
[G-04] Variable minterMinted initialized with unused value
The state variable
minterMinted
in the contractAura.sol
is initialized with the value oftype(uint256).max
however, the variable is set to 0 in the contract’sinit()
function. The variable is used in two other places, which required theinit()
method to have been called first. Thus the initial value oftype(uint256).max
is never used, and is hence a waste of gas. The variable is declared at L33 and should instead be declared as:This also means that the assignment of 0 to the variable in the
init()
method can be removed to save further gas. Hence the line L74 can be deleted, so theinit()
method looks like this:[G-05] Cache merkleRoot in AuraMerkleDrop
In the method
claim()
in the contractAuraMerkleDrop.sol
the variablemerkleRoot
can be cached to save some gas. The state variable is read 2 times in the method, and by caching it in memory it is possible to switch one of the expensiveSLOAD
operations to aMLOAD
/MSTORE
operation which is way cheaper.The code L119-L126:
Can be changed into:
[G-06] Cache auraLocker AuraMerkleDrop
In the method
claim()
in the contractAuraMerkleDrop.sol
the state variableauraLocker
is read 2 times inside the if clause, and by caching it in memory it is possible to switch one of the expensiveSLOAD
operations to aMLOAD
/MSTORE
operation which is way cheaper.There is no reason to cache
auraLocker
outside of the if-statement, since the else clause only reads the variable once. If we cache it outside the if-statement we would make the else part a bit more expensive.The code L130-L139:
Can be changed into:
The text was updated successfully, but these errors were encountered: