Open code423n4 opened 2 years ago
Would save 100 for the call + 100 for SLOAD = 200 - 6 = 194
Saves 8 gas (JUMP)
2500 (1 message shown)
7 * 2100 = 14700
Valid but not at runtime
194 gas saved
Saves 100 for SLOAD + 3 for check 103
Saves 9 gas
94 + 94 - 6 = 182 94 94 Total = 370
200
Saves 100 gas
7 * 20 = 140 gas saved
Valid but only for "bad path"
91 gas saved (97 -6)
191 (100 call 97 - 6 SLOAD)
Only for bad path
Total Gas Saved: 18800
Repeated external call to usdm.balanceOf(address(this):
Don't need to call owner() here, you can just use msg.sender:
Long revert messages, e.g.: "Previous rewards period must be complete before changing the duration for the new period" Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition has been met.
Variables that are set in the constructor and can't be changed can be marked as immutable:
Should be constant:
Variables and events that are not used in any meaningful way can be removed or used where they were intended:
Would be cheapier to use local variables when emitting events, e.g.:
Usually under normal conditions, the first check is not neccessary, as GRACE_PERIOD > block.timestamp should always revert:
This could be inlined to optimize for gas usage, from:
to:
Repeated access of storage variables should be cached, e.g. convexBooster is accessed 3 times:
masterChef twice:
convexPool[_pid] twice:
registeredRewards[_pid] twice:
The initialization to default variables is not neccessary:
If the same MasterChef implementation will be used, then the pid of the token can't change, so you should cache it once (in the constructor call), and do not make the external calls again and again in stake and withdraw functions of StakingRewards:
Contract MasterChef has declared a compiler version of ^0.8.11 but it still uses the SafeMath library:
Overflow/underflow protection is built-in starting from version 0.8, so you don't need to use SafeMath here.
Would be cheaper if you checked not the storage but the _amount paramater against 0 in the first sentence:
currentEpoch() is called twice, should cache after the first call:
You already have the address of rewardToken() here, no need to fetch it again:
In _calcRewardIntegral I think these statements:
should be placed right after this:
or you can even return and skip all the heavy calculations and 0 value transfers if the balance hasn't changed.