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
2. use prefix increment (++i) instead of postfix increment(i++) to save gas
Gas can be saved by using prefix increment instead of postfix increment which is very tiny, but since this is used in several places especially the loop increments, it is called several times in single transaction.
Recommended solution
// Beforefor(uint i =0; i < limit; i++)
// Afterfor(uint i =0; i < limit; ++i)
3. Loop increment can never overflow since it starts from 0, so it can be unchecked
The loops counters usually start from 0 and usually only incremented by 1 on each loop iterations. So, they can never overflow/underflow. So these kinds of operations can be kept in unchecked block to save gas.
Recommended solution
// Beforefor(uint i =0; i < limit; i++) {
...
}
// Afterfor(uint i =0; i < limit; ) {
...
unchecked {
++i;
}
}
4. Array length can be cached in iteration of loops
The array length is computed in each iteration which can be cached to avoid computing in each loop iteration. This will save gas.
Recommended solution
// Beforefor(uint i =0; i < array.length; i++) {
...
}
// Afteruint256 len = arr.len;
for(uint i =0; i < len; i++) {
}
usage
./contracts/AuraLocker.sol:696: for (uint256 i = nextUnlockIndex; i < locks.length; i++) {
./contracts/AuraClaimZap.sol:143: for (uint256 i =0; i < rewardContracts.length; i++) {
./contracts/AuraClaimZap.sol:147: for (uint256 i =0; i < extraRewardContracts.length; i++) {
./contracts/AuraClaimZap.sol:151: for (uint256 i =0; i < tokenRewardContracts.length; i++) {
./contracts/AuraVestedEscrow.sol:100: for (uint256 i =0; i < _recipient.length; i++) {
5. Incorrect type used for emitting a event which increases gas cost
in contracts/AuraLocker.sol#L295 event Staked takes lockAmount as its second argument of type uint112
But the event Staked takes uint256 as its argument. The use of incorrect variable types cause extra gas.
6. uint variables are initialized to 0 by default, extra initialazation requires extra gas
When declaring state variables, uint types are set to zero by default. by, initializing them to zero, it costs extra gas on deployment. So to save gas it is better to avoid initializing them to zero.
1. Use solidity custom errors to save gas
solidity 0.8.4 introduces custom errors which are cheaper than using revert strings in terms of gas
Use the custom error patterns to reduce gas cost.
for eg.
more details can be found here
2. use prefix increment (++i) instead of postfix increment(i++) to save gas
Gas can be saved by using prefix increment instead of postfix increment which is very tiny, but since this is used in several places especially the loop increments, it is called several times in single transaction.
Recommended solution
3. Loop increment can never overflow since it starts from 0, so it can be unchecked
The loops counters usually start from 0 and usually only incremented by 1 on each loop iterations. So, they can never overflow/underflow. So these kinds of operations can be kept in unchecked block to save gas.
Recommended solution
4. Array length can be cached in iteration of loops
The array length is computed in each iteration which can be cached to avoid computing in each loop iteration. This will save gas.
Recommended solution
usage
5. Incorrect type used for emitting a event which increases gas cost
in contracts/AuraLocker.sol#L295 event Staked takes lockAmount as its second argument of type
uint112
But the event
Staked
takesuint256
as its argument. The use of incorrect variable types cause extra gas.Recommended Solution
Cast the variable to
uint256
before emitting the event6. uint variables are initialized to 0 by default, extra initialazation requires extra gas
When declaring state variables, uint types are set to zero by default. by, initializing them to zero, it costs extra gas on deployment. So to save gas it is better to avoid initializing them to zero.
eg.
in contracts/AuraBalRewardPool.sol#L35-L39
in contracts/AuraLocker.sol#L72
Recommended solution
The text was updated successfully, but these errors were encountered: