Skip to content

Commit

Permalink
Implement and test claim reward method
Browse files Browse the repository at this point in the history
  • Loading branch information
apbendi authored and alexkeating committed Jan 24, 2024
1 parent af7b4dd commit 87283a8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/UniStaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ contract UniStaker is ReentrancyGuard {
_stakeTokenSafeTransferFrom(address(surrogates[deposit.delegatee]), deposit.owner, _amount);
}

function claimReward() external nonReentrant {
_updateReward(msg.sender);

uint256 _rewards = rewards[msg.sender];
if (_rewards == 0) return;
rewards[msg.sender] = 0;

SafeERC20.safeTransfer(REWARDS_TOKEN, msg.sender, _rewards);
}

function notifyRewardsAmount(uint256 _amount) external {
if (msg.sender != REWARDS_NOTIFIER) revert UniStaker__Unauthorized("not notifier", msg.sender);
// TODO: It looks like the only thing we actually need to do here is update the
Expand Down
50 changes: 50 additions & 0 deletions test/UniStaker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1750,3 +1750,53 @@ contract Earned is UniStakerRewardsTest {
assertLteWithinOnePercent(uniStaker.earned(_depositor2), _depositor2ExpectedEarnings);
}
}

contract ClaimReward is UniStakerRewardsTest {
function testFuzz_SendsRewardsEarnedToTheUser(
address _depositor,
address _delegatee,
uint256 _stakeAmount,
uint256 _rewardAmount,
uint256 _durationPercent
) public {
(_stakeAmount, _rewardAmount) = _boundToRealisticStakeAndReward(_stakeAmount, _rewardAmount);
_durationPercent = bound(_durationPercent, 0, 100);

// A user deposits staking tokens
_boundMintAndStake(_depositor, _stakeAmount, _delegatee);
// The contract is notified of a reward
_mintTransferAndNotifyReward(_rewardAmount);
// A portion of the duration passes
_jumpAheadByPercentOfRewardDuration(_durationPercent);

uint256 _earned = uniStaker.earned(_depositor);

vm.prank(_depositor);
uniStaker.claimReward();

assertEq(rewardToken.balanceOf(_depositor), _earned);
}

function testFuzz_ResetsTheRewardsEarnedByTheUser(
address _depositor,
address _delegatee,
uint256 _stakeAmount,
uint256 _rewardAmount,
uint256 _durationPercent
) public {
(_stakeAmount, _rewardAmount) = _boundToRealisticStakeAndReward(_stakeAmount, _rewardAmount);
_durationPercent = bound(_durationPercent, 0, 100);

// A user deposits staking tokens
_boundMintAndStake(_depositor, _stakeAmount, _delegatee);
// The contract is notified of a reward
_mintTransferAndNotifyReward(_rewardAmount);
// A portion of the duration passes
_jumpAheadByPercentOfRewardDuration(_durationPercent);

vm.prank(_depositor);
uniStaker.claimReward();

assertEq(uniStaker.earned(_depositor), 0);
}
}

0 comments on commit 87283a8

Please sign in to comment.