Skip to content

Commit

Permalink
Make the reward duration a constant value
Browse files Browse the repository at this point in the history
  • Loading branch information
apbendi committed Jan 24, 2024
1 parent 15f93fc commit af7b4dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/UniStaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract UniStaker is ReentrancyGuard {
IERC20 public immutable REWARDS_TOKEN;
IERC20Delegates public immutable STAKE_TOKEN;
address public immutable REWARDS_NOTIFIER;
uint256 public constant REWARD_DURATION = 7 days;
uint256 private constant SCALE_FACTOR = 1e24;

DepositIdentifier private nextDepositId;
Expand All @@ -39,7 +40,6 @@ contract UniStaker is ReentrancyGuard {

mapping(address delegatee => DelegationSurrogate surrogate) public surrogates;

uint256 public rewardDuration = 7 days;
uint256 public finishAt;
uint256 public updatedAt;
uint256 public rewardRate;
Expand Down Expand Up @@ -151,18 +151,18 @@ contract UniStaker is ReentrancyGuard {
if (block.timestamp >= finishAt) {
// TODO: Can we move the scale factor into the rewardRate? This should reduce rounding errors
// introduced here when truncating on this division.
rewardRate = _amount / rewardDuration;
rewardRate = _amount / REWARD_DURATION;
} else {
uint256 remainingRewards = rewardRate * (finishAt - block.timestamp);
rewardRate = (remainingRewards + _amount) / rewardDuration;
rewardRate = (remainingRewards + _amount) / REWARD_DURATION;
}

if (rewardRate == 0) revert UniStaker__InvalidRewardRate();
if ((rewardRate * rewardDuration) > REWARDS_TOKEN.balanceOf(address(this))) {
if ((rewardRate * REWARD_DURATION) > REWARDS_TOKEN.balanceOf(address(this))) {
revert UniStaker__InsufficientRewardBalance();
}

finishAt = block.timestamp + rewardDuration;
finishAt = block.timestamp + REWARD_DURATION;
updatedAt = block.timestamp;
}

Expand Down
14 changes: 7 additions & 7 deletions test/UniStaker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ contract UniStakerRewardsTest is UniStakerTest {
console2.log("reward balance");
console2.log(rewardToken.balanceOf(address(uniStaker)));
console2.log("rewardDuration");
console2.log(uniStaker.rewardDuration());
console2.log(uniStaker.REWARD_DURATION());
console2.log("finishAt");
console2.log(uniStaker.finishAt());
console2.log("updatedAt");
Expand Down Expand Up @@ -1241,7 +1241,7 @@ contract UniStakerRewardsTest is UniStakerTest {
}

function _jumpAheadByPercentOfRewardDuration(uint256 _percent) public {
uint256 _seconds = (_percent * uniStaker.rewardDuration()) / 100;
uint256 _seconds = (_percent * uniStaker.REWARD_DURATION()) / 100;
_jumpAhead(_seconds);
}

Expand Down Expand Up @@ -1277,7 +1277,7 @@ contract NotifyRewardsAmount is UniStakerRewardsTest {
_amount = _boundToRealisticReward(_amount);
_mintTransferAndNotifyReward(_amount);

uint256 _expectedRewardRate = _amount / uniStaker.rewardDuration();
uint256 _expectedRewardRate = _amount / uniStaker.REWARD_DURATION();
assertEq(uniStaker.rewardRate(), _expectedRewardRate);
}

Expand All @@ -1286,11 +1286,11 @@ contract NotifyRewardsAmount is UniStakerRewardsTest {
_amount2 = _boundToRealisticReward(_amount2);

_mintTransferAndNotifyReward(_amount1);
uint256 _expectedRewardRate = _amount1 / uniStaker.rewardDuration();
uint256 _expectedRewardRate = _amount1 / uniStaker.REWARD_DURATION();
assertEq(uniStaker.rewardRate(), _expectedRewardRate);

_mintTransferAndNotifyReward(_amount2);
_expectedRewardRate = (_amount1 + _amount2) / uniStaker.rewardDuration();
_expectedRewardRate = (_amount1 + _amount2) / uniStaker.REWARD_DURATION();
assertLteWithinOneUnit(uniStaker.rewardRate(), _expectedRewardRate);
}

Expand All @@ -1301,7 +1301,7 @@ contract NotifyRewardsAmount is UniStakerRewardsTest {
_jumpAhead(_jumpTime);

_mintTransferAndNotifyReward(_amount);
uint256 _expectedFinishTimestamp = _futureTimestamp + uniStaker.rewardDuration();
uint256 _expectedFinishTimestamp = _futureTimestamp + uniStaker.REWARD_DURATION();

assertEq(uniStaker.updatedAt(), _futureTimestamp);
assertEq(uniStaker.finishAt(), _expectedFinishTimestamp);
Expand Down Expand Up @@ -1359,7 +1359,7 @@ contract NotifyRewardsAmount is UniStakerRewardsTest {

function testFuzz_RevertIf_RewardAmountIsTooSmall(uint256 _amount) public {
// If the amount is less than the rewards duration the reward rate will be truncated to 0
_amount = bound(_amount, 0, uniStaker.rewardDuration() - 1);
_amount = bound(_amount, 0, uniStaker.REWARD_DURATION() - 1);
rewardToken.mint(rewardsNotifier, _amount);

vm.startPrank(rewardsNotifier);
Expand Down

0 comments on commit af7b4dd

Please sign in to comment.