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
Here are gas improvements findings which are commonly seen throughout codes.
[Gas-1] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the gas cost.
If 0 is set on uint variable like this uint256 unlocksSinceLatestCkpt = 0;, it can rewrite to uint256 unlocksSinceLatestCkpt;.
In for loop, if uint is used like this for (uint256 i = 0; i < userRewardsLength; i++), it can omit setting = 0 and will be for (uint256 i; i < userRewardsLength; i++).
uint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.
If the codebase uses > 0 on uint variables like this:
require(_amount > 0, "Must mint something");
it can rewrite like this:
require(_amount != 0, "Must mint something");
> 0 is used throughout the codebase, so it will list links per file where codes can use != 0 instead of > 0.
There are 103 callsites of require check with inline error message. Using the custom errors instead of the inline error messages can reduce the gas cost.
Here are gas improvements findings per file.
AuraBalRewardPool.sol
[Gas-4] Use _totalSupply private state variable instead of calling totalSupply() function
_totalSupply private state variable is defined at AuraBalRewardPool contract. There are 2 places calling totalSupply() function as follows.
while (currentLock.unlockTime > upcomingEpoch) {
....
if (i > 0) {
i--;
currentLock = locks[i];
} else {
break;
}
}
i is uint256 and it checks if (i > 0) so it is guaranteed that i-- will not be less than 0. Therefore, this part can add unchecked wrapping i-- like this:
while (currentLock.unlockTime > upcomingEpoch) {
....
if (i > 0) {
unchecked {
i--;
}
currentLock = locks[i];
} else {
break;
}
}
[Gas-8] Using unchecked in for loop at totalSupplyAtEpoch and balanceAtEpochOf functions
It is certain that i will not be underflown since locksLength or epochIndex + 1 are uint256, and having i > 0 makes sure that i-- will not be underflown. Therefore, they can be rewritten like this:
for (uint256 i = locksLength; i > 0;) {
uint256 lockEpoch = uint256(locks[i - 1].unlockTime).sub(lockDuration);
//lock epoch must be less or equal to the epoch we're basing from.
//also not include the current epoch
if (lockEpoch < epochStart) {
if (lockEpoch > cutoffEpoch) {
amount = amount.add(locks[i - 1].amount);
} else {
//stop now as no futher checks matter
break;
}
}
unchecked {
i--;
}
}
AuraVestedEscrow.sol
[Gas-9] false is not needed on initialised state variable
The default value of bool type is false. It sets false at initialised state variable, but this is not needed.
Since it checks require(endtime_ > starttime_, "end must be greater"), endTime - startTime will not be underflown. Therefore, unchecked can be used on endTime - startTime like this to reduce the gas cost.
unchecked {
totalTime = endTime - startTime;
}
[Gas-11] vested variable is not needed at remaining and available functions
for (uint256 i = epochIndex; i < tokenEpochs; i++) {
//only claimable after rewards are "locked in"
if (rewardEpochs[_token][i] < latestEpoch) {
claimableTokens += _claimableRewards(_account, _token, rewardEpochs[_token][i]);
//return index user claims should be set to
epochIndex = i + 1;
}
}
The range of i is from epochIndex to tokenEpochs - 1. Both epochIndex to tokenEpochs - 1 are in uint256 so epochIndex = i + 1 and i++ will not be overflown. Therefore, it can write like this:
for (uint256 i = epochIndex; i < tokenEpochs;) {
//only claimable after rewards are "locked in"
if (rewardEpochs[_token][i] < latestEpoch) {
claimableTokens += _claimableRewards(_account, _token, rewardEpochs[_token][i]);
//return index user claims should be set to
unchecked {
epochIndex = i + 1;
i++;
}
}
}
The text was updated successfully, but these errors were encountered:
Here are gas improvements findings which are commonly seen throughout codes.
[Gas-1] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the gas cost.
If 0 is set on uint variable like this
uint256 unlocksSinceLatestCkpt = 0;
, it can rewrite touint256 unlocksSinceLatestCkpt;
.In for loop, if uint is used like this
for (uint256 i = 0; i < userRewardsLength; i++)
, it can omit setting= 0
and will befor (uint256 i; i < userRewardsLength; i++)
.AuraBalRewardPool.sol: 2 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L35
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L38
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L39
AuraClaimZap.sol: 3 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L143
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L147
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L151
AuraLocker.sol: 7 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L72
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L174
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L381
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L485
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L540
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L630
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L773
AuraMerkleDrop.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraMerkleDrop.sol#L29
AuraVestedEscrow.sol: 2 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L99
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L100
BalLiquidityProvider.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/BalLiquidityProvider.sol#L51
ExtraRewardsDistributor.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/ExtraRewardsDistributor.sol#L231
[Gas-2] Use
!= 0
instead of> 0
on uint variablesuint variables will never be lower than 0. Therefore,
> 0
and!= 0
have same meanings. Using!= 0
can reduce the gas deployment cost, so it is worth using!= 0
wherever possible.If the codebase uses
> 0
on uint variables like this:it can rewrite like this:
> 0
is used throughout the codebase, so it will list links per file where codes can use!= 0
instead of> 0
.Aura.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/Aura.sol#L68
AuraBalRewardPool.sol: 5 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L121
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L139
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L157
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L178
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L210
AuraClaimZap.sol: 4 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L196
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L200
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L218
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraClaimZap.sol#L221
AuraLocker.sol: 15 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L210
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L259
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L309
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L359
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L385
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L400
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L419
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L431
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L443
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L471
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L520
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L822
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L851
Following 2 call sites use
i > 0
in for loopfor (uint256 i = locksLength; i > 0; i--)
.Technically, it can write like this
for (uint256 i = locksLength; i != 0; i--)
.https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L664
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L726
AuraMerkleDrop.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraMerkleDrop.sol#L122
AuraPenaltyForwarder.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraPenaltyForwarder.sol#L52
AuraStakingProxy.sol: 3 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraStakingProxy.sol#L177
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraStakingProxy.sol#L185
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraStakingProxy.sol#L207
BalLiquidityProvider.sol: 2 call sites
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/BalLiquidityProvider.sol#L57
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/BalLiquidityProvider.sol#L70
ExtraRewardsDistributor.sol: 1 call site
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/ExtraRewardsDistributor.sol#L171
[Gas-3] Use custom errors
Using custom errors can reduce the gas cost.
There are 103 callsites of require check with inline error message. Using the custom errors instead of the inline error messages can reduce the gas cost.
Here are gas improvements findings per file.
AuraBalRewardPool.sol
[Gas-4] Use
_totalSupply
private state variable instead of callingtotalSupply()
function_totalSupply
private state variable is defined at AuraBalRewardPool contract. There are 2 places callingtotalSupply()
function as follows.https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L104
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L109
It is worth considering using
_totalSupply
private state variable to reduce the gas cost.They can be written like followings:
Confirmed that both deployements and methods gas costs decreased when using
_totalSupply
private state variable.[Gas-5]
balance
variable does not need to be definedhttps://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraBalRewardPool.sol#L133-L134
It can omit defining balance like this to reduce the gas cost:
AuraLocker.sol
[Gas-6] false is not needed on
isShutdown
state variableThe default value of bool type is false. It sets false at
isShutdown
state variable, but this is not needed.https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L114
Removing setting false can reduce the gas cost.
[Gas-7] Using unchecked in while loop at delegate function
In the while loop at delegate function,
i--
is used.https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L488-L502
i
is uint256 and it checksif (i > 0)
so it is guaranteed thati--
will not be less than 0. Therefore, this part can add unchecked wrappingi--
like this:[Gas-8] Using unchecked in for loop at totalSupplyAtEpoch and balanceAtEpochOf functions
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L664
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraLocker.sol#L726
It is certain that
i
will not be underflown sincelocksLength
orepochIndex + 1
are uint256, and havingi > 0
makes sure thati--
will not be underflown. Therefore, they can be rewritten like this:AuraVestedEscrow.sol
[Gas-9] false is not needed on
initialised
state variableThe default value of bool type is false. It sets false at
initialised
state variable, but this is not needed.https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L33
It can be written like this to reduce the gas cost:
[Gas-10] Using unchecked on
endTime - startTime
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L65
Since it checks
require(endtime_ > starttime_, "end must be greater")
,endTime - startTime
will not be underflown. Therefore,unchecked
can be used onendTime - startTime
like this to reduce the gas cost.[Gas-11]
vested
variable is not needed at remaining and available functionshttps://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L139-L140
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/AuraVestedEscrow.sol#L148-L149
This is a definition of remaining function.
vested
variable is defined, but it is not needed. It can write like this:Same workaround can be applied to available function.
BalLiquidityProvider.sol
[Gas-12] unchecked can be used in the for loop at provideLiquidity function
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/BalLiquidityProvider.sol#L51
The range of
i
variable is from 0 to 1. So it can write like this by using unchecked, and improve gas cost.CrvDepositorWrapper.sol
[Gas-13]
minOut
variable is not neededhttps://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/CrvDepositorWrapper.sol#L73-L74
It can write as follow to reduce the gas cost.
ExtraRewardsDistributor.sol
[Gas-14]
latestEpoch
variable is not neededhttps://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/ExtraRewardsDistributor.sol#L50-L51
It can write as follow to reduce the gas cost.
[Gas-15]
balance
variable is not neededhttps://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/ExtraRewardsDistributor.sol#L256-L257
It can write as follow to reduce the gas cost.
[Gas-16] unchecked can be used in the for loop
https://github.com/code-423n4/2022-05-aura/blob/4989a2077546a5394e3650bf3c224669a0f7e690/contracts/ExtraRewardsDistributor.sol#L233-L240
The range of
i
is fromepochIndex
totokenEpochs - 1
. BothepochIndex
totokenEpochs - 1
are in uint256 soepochIndex = i + 1
andi++
will not be overflown. Therefore, it can write like this:The text was updated successfully, but these errors were encountered: