Gas Optimizations #204
Labels
bug
Something isn't working
duplicate
This issue or pull request already exists
G (Gas Optimization)
Redundant initialisation with default value
Some variables are initialised with their default values which cause unnecessary gas consumption
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L35
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L38-L39
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L72
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L114
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L380
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L484
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L539
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L629
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraMerkleDrop.sol#L29
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L33
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L99
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BaseRewardPool.sol#L71-L72
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BaseRewardPool.sol#L75-L77
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/Booster.sol#L29
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/ConvexMasterChef.sol#L63
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/CrvDepositor.sol#L36
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/ExtraRewardsDistributor.sol#L231
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/VirtualBalanceRewardPool.sol#L89-L90
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/VirtualBalanceRewardPool.sol#L93-L95
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/VoterProxy.sol#L308
Some public constants and immutables can be made private
There are many public constants and immutables which can be converted to private. Some of them are below.
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/Aura.sol#L26-L30
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L29
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L73
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L81
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L83
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L107
for loops can be optimized
1- For loop index increments can be made unchecked for solidity versions > 0.8.0. There is no risk of overflowing the index of the for loops. Therefore, they can be changed to unchecked to save gas. This would save more gas as iterations in the loop increases.
2- Postfix increments can be changed as prefix as it is cheaper.
3- There is no need to set uint256 variable to 0 since default value is already 0.
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraClaimZap.sol#L143
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraClaimZap.sol#L147
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraClaimZap.sol#L151
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L174
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L305
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L409
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L663
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L695
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L725
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L772
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L100
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/BalLiquidityProvider.sol#L51
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/ConvexMasterChef.sol#L180
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/ExtraRewardsDistributor.sol#L233
Recommended Mitigation Steps
I suggest to change the original code from this
for (uint256 i = 0; i < rewardContracts.length; i++) {
IBasicRewards(rewardContracts[i]).getReward(msg.sender, true);
}
to this
for (uint256 i; i < rewardContracts.length; ) {
IBasicRewards(rewardContracts[i]).getReward(msg.sender, true);
unchecked { ++i; }
}
Using != 0 is cheaper than > 0 when used on a uint in a require() statement
Lines of code
There are many instances, some of which are listed below:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/Aura.sol#L68
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L121
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L139
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L157
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L210
Error string longer than 32 characters
Error strings take space in the deployed bytecode. Every reason string takes at least 32 bytes so make sure your string fits in 32 bytes or it will become more expensive.
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L197
Missing non-zero amount checks
It is a good practice to apply non-zero amount checks for token transactions to avoid unnecessary executions. This has been applied in most of the transactions, but still there are some missing instances.
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L198
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L251
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraMerkleDrop.sol#L100
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraStakingProxy.sol#L162
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L123
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BaseRewardPool.sol#L179
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BaseRewardPool.sol#L198
It is better to use 1-2 for binary variables rather than 0-1
SSTORE from 1 to 2 is cheaper than SSTORE from 0 to 1. Therefore, some booleans such as isShutdown, isForceTimerStarted, isSealed, etc can be defined as uint and their values can be switched between 1-2.
code-423n4/2022-01-yield-findings#102
Lines of code
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/Booster.sol#L54
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BoosterOwner.sol#L49
https://github.com/code-423n4/2022-05-aura/blob/main/convex-platform/contracts/contracts/BoosterOwner.sol#L53
Booleans are more expensive than uint256
It is more expensive to operate using booleans because before every write operation an SLOAD is executed to read the contents of the slot. Therefore, it is cheaper to use uint256 instead of bool. On the other hand, using bool is better for the code readability. Hence, it is a tradeoff to be decided by the developers.
The text was updated successfully, but these errors were encountered: