Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max at available balance in previewRewards #413

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions contracts/FixedRateRewardsSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ contract FixedRateRewardsSource is Governable, Initializable {

/// @dev Compute pending rewards since last collect
/// @return rewardAmount Amount of reward that'll be distributed if collected now
function previewRewards() public view returns (uint256) {
function previewRewards() public view returns (uint256 rewardAmount) {
RewardConfig memory _config = rewardConfig;

if (_config.lastCollect == 0) {
return 0;
}
return (block.timestamp - _config.lastCollect) * _config.rewardsPerSecond;

rewardAmount = (block.timestamp - _config.lastCollect) * _config.rewardsPerSecond;
uint256 balance = IERC20(rewardToken).balanceOf(address(this));
if (rewardAmount > balance) {
rewardAmount = balance;
}
}

/// @dev Set address of the strategist
Expand Down
12 changes: 12 additions & 0 deletions tests/staking/FixedRateRewardsSource.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ contract FixedRateRewardsSourceTest is Test {
assertEq(rewards.previewRewards(), 0 ether, "Pending reward mismatch");
}

function testLowBalanceCollection() public {
// Should also allow disabling rewards
vm.prank(strategist);
rewards.setRewardsPerSecond(2000000 ether);

// Should never show more than balance
vm.warp(block.number + 10);
assertEq(rewards.previewRewards(), 1000000 ether, "Pending reward mismatch");
vm.warp(block.number + 123);
assertEq(rewards.previewRewards(), 1000000 ether, "Pending reward mismatch");
}

function testRewardRatePermission() public {
// Should allow Strategist to change
vm.prank(strategist);
Expand Down
Loading