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

2 stars 0 forks source link

Gas Optimizations #265

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1) Use != instead of > for uint256 in StakingReward.sol:

Both amount and reward are of type uin256 comparing, checking for inequality instead of a greater than relation saves gas. The comparisons can be found in the lines below

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L94

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L119

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L119

Cache length before for loop ConcurRewardPool.sol:

2) The length can be cached before the loop avoid calling length many times to save gas.

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/ConcurRewardPool.sol#L35

3) Assignment to default value

In L51 uint totalAllocPoint is being declared with the default value of 0. Just totalAllocPoint; will save gas. The same thing can also be observed in StakingRewards.sol L21 and L22. Also in ConvexStakingWrapper.sol L36

Similarly bool transferSuccess is also being assigned False in L204

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/MasterChef.sol#L51

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/MasterChef.sol#L204

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L21

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L22

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/ConvexStakingWrapper.sol#L36

4) Resturcture if-else statement to remove else in StakingRewards.sol L142:

if (block.timestamp >= periodFinish) { rewardRate = reward / rewardsDuration; } else { uint256 remaining = periodFinish - block.timestamp; uint256 leftover = remaining * rewardRate; rewardRate = (reward + leftover) / rewardsDuration; }

can be reformulated as:

rewardRate = reward / rewardsDuration; if (block.timestamp < periodFinish) { uint256 remaining = periodFinish - block.timestamp; uint256 leftover = remaining * rewardRate; rewardRate += (leftover / rewardsDuration); }

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/StakingRewards.sol#L142

5) use unchecked{...} to save gas

uint256 is way too big to realistically lead to overflows, unchecked can be used to save gas in the following situations.

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/ConvexStakingWrapper.sol#L121

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/ConvexStakingWrapper.sol#L219

https://github.com/code-423n4/2022-02-concur/blob/72b5216bfeaa7c52983060ebfc56e72e0aa8e3b0/contracts/ConcurRewardPool.sol#L34

GalloDaSballo commented 2 years ago

1) Use != instead of > for uint256 in StakingReward.sol: 3 gas * 3 = 9

2)The length can be cached before the loop avoids calling length many times to save gas. Caching length saves the length check which I believe is another 3 gas per loop 3

3) Assignment to default value For the 3 storage values let's say another 100 gas (evm.codes, from research I believe is more but will judge consistently, if any warden has different data please share) For constant is not relevant as it's inlined, no cost there 300

4)Resturcture if-else statement to remove else in StakingRewards.sol L142: This "could" save 15 gas on the optimistic case, let's say 15

5)Use unchecked This should save about 20 gas per each refactoring 60

Total gas savings 372

Formatting here was underwhelming, recommend the warden to spend an extra couple minutes with a markdown editor to make the report higher quality