Skip to content

Commit

Permalink
Add getNextCycleRewards tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kphed committed Nov 2, 2023
1 parent b0ff04a commit 8cf9045
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/DynamicRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ contract DynamicRewards is FlywheelDynamicRewards {

/**
* @notice Retrieves next cycle's rewards from the store contract to ensure proper accounting.
* @dev For the sake of simplicity, we're not making use of the `strategy` param (assumption
* is that stakedBRR is the only strategy - if this changes later, can update FlywheelRewards).
* FlywheelCore also adds a layer of protection by checking whether the strategy exists
* before calling `FlywheelDynamicRewards.getAccruedRewards`.
*/
function getNextCycleRewards(ERC20) internal override returns (uint192) {
return rewardsStore.transferNextCycleRewards().toUint192();
Expand Down
77 changes: 77 additions & 0 deletions test/DynamicRewards.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;

import "forge-std/Test.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {RewardsStore} from "src/RewardsStore.sol";
import {Helper} from "test/Helper.sol";

contract DynamicRewardsTest is Test, Helper {
using SafeTransferLib for address;

/*//////////////////////////////////////////////////////////////
getNextCycleRewards
//////////////////////////////////////////////////////////////*/

function testGetNextCycleRewards() external {
uint256 amount = 1e18;

deal(WETH, address(dynamicRewardsStore), amount);

assertEq(
address(dynamicRewards),
dynamicRewardsStore.flywheelRewards()
);

ERC20 strategy = ERC20(address(stakedBRR));
(, uint32 lastUpdatedTimestamp) = flywheel.strategyState(strategy);
uint256 dynamicRewardsWETHBalance = WETH.balanceOf(
address(dynamicRewards)
);

// The `getAccruedRewards` is only callable by the FlywheelCore contract.
vm.prank(address(flywheel));

uint256 accruedAmount = dynamicRewards.getAccruedRewards(
strategy,
lastUpdatedTimestamp
);

// Zero since it's the beginning of a new reward dist. cycle.
assertEq(0, accruedAmount);

assertEq(
dynamicRewardsWETHBalance + amount,
WETH.balanceOf(address(dynamicRewards))
);
}

function testGetNextCycleRewardsFuzz(uint192 amount) external {
deal(WETH, address(dynamicRewardsStore), amount);

assertEq(
address(dynamicRewards),
dynamicRewardsStore.flywheelRewards()
);

ERC20 strategy = ERC20(address(stakedBRR));
(, uint32 lastUpdatedTimestamp) = flywheel.strategyState(strategy);
uint256 dynamicRewardsWETHBalance = WETH.balanceOf(
address(dynamicRewards)
);

vm.prank(address(flywheel));

uint256 accruedAmount = dynamicRewards.getAccruedRewards(
strategy,
lastUpdatedTimestamp
);

assertEq(0, accruedAmount);
assertEq(
dynamicRewardsWETHBalance + uint256(amount),
WETH.balanceOf(address(dynamicRewards))
);
}
}
30 changes: 14 additions & 16 deletions test/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,41 @@ contract Helper is Test {
address public constant WETH = 0x4200000000000000000000000000000000000006;
uint32 public constant REWARDS_CYCLE_LENGTH = 1 weeks;
address public immutable owner = address(this);
FlywheelCore public immutable distributor;
FlywheelCore public immutable flywheel;
StakedBRR public immutable stakedBRR;
DynamicRewards public immutable dynamicRewards;
RewardsStore public immutable dynamicRewardsStore;

constructor() {
distributor = new FlywheelCore(
flywheel = new FlywheelCore(
ERC20(WETH),
IFlywheelRewards(address(0)),
IFlywheelBooster(address(0)),
owner,
Authority(address(0))
);
stakedBRR = new StakedBRR(address(distributor));
stakedBRR = new StakedBRR(address(flywheel));
dynamicRewards = new DynamicRewards(
WETH,
distributor,
flywheel,
REWARDS_CYCLE_LENGTH
);
dynamicRewardsStore = dynamicRewards.rewardsStore();

distributor.setFlywheelRewards(dynamicRewards);
distributor.addStrategyForRewards(ERC20(address(stakedBRR)));
flywheel.setFlywheelRewards(dynamicRewards);
flywheel.addStrategyForRewards(ERC20(address(stakedBRR)));

assertEq(
address(dynamicRewards),
address(distributor.flywheelRewards())
);
assertEq(owner, distributor.owner());
assertEq(address(dynamicRewards), address(flywheel.flywheelRewards()));
assertEq(owner, flywheel.owner());

(uint224 index, uint32 lastUpdatedTimestamp) = distributor
.strategyState(ERC20(address(stakedBRR)));
(uint224 index, uint32 lastUpdatedTimestamp) = flywheel.strategyState(
ERC20(address(stakedBRR))
);

assertEq(index, distributor.ONE());
assertEq(index, flywheel.ONE());
assertEq(lastUpdatedTimestamp, block.timestamp);
assertEq(address(distributor), address(stakedBRR.flywheel()));
assertEq(address(distributor), address(dynamicRewards.flywheel()));
assertEq(address(flywheel), address(stakedBRR.flywheel()));
assertEq(address(flywheel), address(dynamicRewards.flywheel()));
assertEq(REWARDS_CYCLE_LENGTH, dynamicRewards.rewardsCycleLength());
assertEq(WETH, dynamicRewardsStore.rewardToken());
assertEq(
Expand Down

0 comments on commit 8cf9045

Please sign in to comment.