code-423n4 / 2022-02-concur-findings

2 stars 0 forks source link

Gas Optimizations #258

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Do not recalculate constant results

Since results are constant, the code should not calculate them over and over again.

https://github.com/code-423n4/2022-02-concur/blob/main/contracts/ConvexStakingWrapper.sol#L116-L117


registeredRewards[_pid][crv] = CRV_INDEX + 1; //mark registered at index+1
registeredRewards[_pid][cvx] = CVX_INDEX + 1; //mark registered at index+1

StakingRewards.sol cache variables to save gas

By caching variables, loads from storage can be avoided to save gas.


rewardPerToken() — rewardPerTokenStored

notifyRewardAmount() — rewardsDuration, periodFinish

updateReward() — rewardPerTokenStored (cache the new value)

USDMPegRecovery.sol cache variables to save gas

By caching variables, loads from storage can be avoided to save gas.


provide() -- step, usdm3crv

At setters emit input rather than from storage to save gas

In StakingRewards.sol:


setRewardsDuration

setRewardsDistribution
GalloDaSballo commented 2 years ago

Do not recalculate constant results 6 gas

StakingRewards.sol rewardPerToken() — rewardPerTokenStored -> Disagree it's used as either or notifyRewardAmount() — rewardsDuration, periodFinish - Agree for both, using a cached version would save 94 * 2 = 188 gas updateReward() — rewardPerTokenStored (cache the new value) - Agree it would save another 94 gas

USDMPegRecovery.sol Caching would save another 94 gas * 2 = 188 (not using storage would save way more bug warden didn't provide that as a recommendation)

StakingRewards.sol

97 gas * 2 = 188

Total: 664 gas from these recommendation