Skip to content

Commit

Permalink
add external methods to RewardManager to get unclaimed rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
JCSanPedro committed Aug 5, 2024
1 parent b8b19ba commit 50e0fb6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions contracts/payments/IRewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ interface IRewardsManager {
uint256[] calldata cycles
) external view returns (uint256[] memory);

function getClaim(address node, address user, uint256 cycle) external view returns (uint256);

function getUnclaimedReward(
address node,
address user,
uint256 cycle
) external view returns (uint256);

function getUnclaimedRewards(
address node,
address user,
uint256[] calldata cycles
) external view returns (uint256[] memory);

function getUnclaimedNodeCommission(address node) external view returns (uint256);

function incrementRewardPool(address node, uint256 amount) external;
Expand Down
22 changes: 22 additions & 0 deletions contracts/payments/RewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ contract RewardsManager is IRewardsManager, Initializable, AccessControl {
return rewardPools[node][cycle];
}

function getUnclaimedReward(address node, address user, uint256 cycle) external view returns (uint256) {
return _getUnclaimedReward(node, user, cycle);
}

function _getUnclaimedReward(address node, address user, uint256 cycle) internal view returns (uint256) {
if (claims[node][user][cycle]) {
return 0;
}

return _getClaim(node, user, cycle);
}

function getUnclaimedRewards(address node, address user, uint256[] calldata cycles) external view returns (uint256[] memory) {
uint256 [] memory unclaimedRewards = new uint256[](cycles.length);

for (uint256 i = 0; i < cycles.length; i++) {
unclaimedRewards[i] = _getUnclaimedReward(node, user, cycles[i]);
}

return unclaimedRewards;
}

function getClaim(address node, address user, uint256 cycle) external view returns (uint256) {
return _getClaim(node, user, cycle);
}
Expand Down
21 changes: 20 additions & 1 deletion test/paymets/rewardsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ describe('Rewards Manager', () => {
'function getUnclaimedNodeCommission(address node) external view returns (uint256)',
'function incrementRewardPool(address node, uint256 amount) external',
'function claim(address node, uint256 cycle) external',
'function getClaim(address node, address user, uint256 cycle) external view returns (uint256)',
'function getUnclaimedReward(address node, address user, uint256 cycle) external view returns (uint256)',
'function getUnclaimedRewards(address node, address user, uint256[] cycles) external view returns (uint256[])',
];

const interfaceId = getInterfaceId(abi);
Expand Down Expand Up @@ -308,6 +311,16 @@ describe('Rewards Manager', () => {
await setTimeSinceStart(cycle * 1000);
}

const unclaimedRewards = await rewardsManager.getUnclaimedRewards(
node1.getAddress(),
user.getAddress(),
cycles,
);

for (const [i, r] of unclaimedRewards.entries()) {
expect(r).to.be.eq(BigInt(i + 1) * rewardAmount);
}

for (const cycle of cycles) {
await testClaim(
node1.getAddress(),
Expand Down Expand Up @@ -501,9 +514,15 @@ describe('Rewards Manager', () => {
const balance = await contracts.syloToken.balanceOf(user);

const claim = await rewardsManager.getClaim(node, user.getAddress(), cycle);

expect(claim).to.equal(expectedIncrease);

const unclaimed = await rewardsManager.getUnclaimedReward(
node,
user.getAddress(),
cycle,
);
expect(unclaimed).to.equal(expectedIncrease);

await rewardsManager.connect(user).claim(node, cycle);

const balanceAfter = await contracts.syloToken.balanceOf(user);
Expand Down

0 comments on commit 50e0fb6

Please sign in to comment.