diff --git a/src/UniStaker.sol b/src/UniStaker.sol index 6630d3c..6d8c747 100644 --- a/src/UniStaker.sol +++ b/src/UniStaker.sol @@ -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 diff --git a/test/UniStaker.t.sol b/test/UniStaker.t.sol index f1bf899..1b8228c 100644 --- a/test/UniStaker.t.sol +++ b/test/UniStaker.t.sol @@ -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); + } +}