Skip to content

Commit

Permalink
remove tester function in rewards manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jul 4, 2024
1 parent 788b836 commit e5af4a3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 102 deletions.
5 changes: 5 additions & 0 deletions contracts/payments/IRewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ interface IRewardsManager {

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

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

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

function claim(address node, uint256 cycle) external;
Expand Down
7 changes: 3 additions & 4 deletions contracts/payments/RewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity ^0.8.18;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

import "hardhat/console.sol";

Check failure on line 7 in contracts/payments/RewardsManager.sol

View workflow job for this annotation

GitHub Actions / build

Unexpected import of console file

Check failure on line 7 in contracts/payments/RewardsManager.sol

View workflow job for this annotation

GitHub Actions / build

Unexpected import of console file

import "./IRewardsManager.sol";
import "../Registries.sol";
import "./Ticketing.sol";
Expand Down Expand Up @@ -80,9 +82,6 @@ contract RewardsManager is IRewardsManager, Initializable, AccessControl {
uint256 cycle,
uint256 amount
) external onlyRole(onlyTicketing) {
if (address(0) == node) {
revert CannotIncrementRewardPoolWithZeroNodeAddress();
}
if (amount == 0) {
revert CannotIncrementRewardPoolWithZeroAmount();
}
Expand Down Expand Up @@ -117,7 +116,7 @@ contract RewardsManager is IRewardsManager, Initializable, AccessControl {
) external view returns (uint256[] memory) {
uint256[] memory rewards = new uint256[](cycles.length);
for (uint i = 0; i < cycles.length; i++) {
rewards[i] = rewardPools[node][i];
rewards[i] = rewardPools[node][cycles[i]];
}
return rewards;
}
Expand Down
4 changes: 0 additions & 4 deletions contracts/payments/Ticketing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,4 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
_deposits.spendEscrow(sender, amount);
_rewardsManager.incrementRewardPool(stakee, cycle, amount);
}

function testerIncrementRewardPool(address node, uint256 cycle, uint256 amount) external {
_rewardsManager.incrementRewardPool(node, cycle, amount);
}
}
126 changes: 71 additions & 55 deletions test/paymets/rewardsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import { SyloContracts } from '../../common/contracts';
import { deployContracts, getInterfaceId } from '../utils';
import { Signer } from 'ethers';
import { expect, assert } from 'chai';
import { Registries, RewardsManager, Ticketing } from '../../typechain-types';
import {
Registries,
RewardsManager,
Ticketing,
Deposits,
} from '../../typechain-types';
import { redeemTicket } from './ticketing.test';

describe('Rewards Manager', () => {
let accounts: Signer[];
let contracts: SyloContracts;
let rewardsManager: RewardsManager;
let registries: Registries;
let ticketing: Ticketing;
let deposits: Deposits;

let deployer: Signer;
let node1: Signer;
let node2: Signer;
Expand All @@ -23,6 +31,8 @@ describe('Rewards Manager', () => {
rewardsManager = contracts.rewardsManager;
registries = contracts.registries;
ticketing = contracts.ticketing;
deposits = contracts.deposits;

deployer = accounts[0];
node1 = accounts[10];
node2 = accounts[11];
Expand Down Expand Up @@ -93,18 +103,10 @@ describe('Rewards Manager', () => {
);
});

it('cannot increment reward pool with invalid node address', async () => {
await expect(
ticketing.testerIncrementRewardPool(ethers.ZeroAddress, 0, 0),
).to.be.revertedWithCustomError(
rewardsManager,
'CannotIncrementRewardPoolWithZeroNodeAddress',
);
});

it('cannot increment reward pool with invalid amount', async () => {
// No deposit is added so no amount can be added to reward pool
await expect(
ticketing.testerIncrementRewardPool(await deployer.getAddress(), 0, 0),
incrementRewardPool(node1, 1, 0n, 0n),
).to.be.revertedWithCustomError(
rewardsManager,
'CannotIncrementRewardPoolWithZeroAmount',
Expand All @@ -116,11 +118,11 @@ describe('Rewards Manager', () => {

await checkInitialRewardPoolState(rewardsManager);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 0, 100);
await incrementRewardPool(node1, 1, 100n, 100n);

const rewardPool = await rewardsManager.getRewardPool(
await node1.getAddress(),
0,
1,
);
const unclaimedCommission = await rewardsManager.getUnclaimedNodeCommission(
await node1.getAddress(),
Expand All @@ -133,11 +135,11 @@ describe('Rewards Manager', () => {
it('can increment reward pool', async () => {
await checkInitialRewardPoolState(rewardsManager);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 0, 100);
await incrementRewardPool(node1, 1, 100n, 100n);

const rewardPool = await rewardsManager.getRewardPool(
await node1.getAddress(),
0,
1,
);

assert.equal(Number(rewardPool), 5);
Expand All @@ -146,16 +148,16 @@ describe('Rewards Manager', () => {
it('can increment reward pool multiple nodes', async () => {
await checkInitialRewardPoolState(rewardsManager);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 0, 100);
await ticketing.testerIncrementRewardPool(await node2.getAddress(), 0, 200);
await incrementRewardPool(node1, 1, 100n, 100n);
await incrementRewardPool(node2, 1, 200n, 100n);

const rewardPool = await rewardsManager.getRewardPool(
await node1.getAddress(),
0,
1,
);
const rewardPool2 = await rewardsManager.getRewardPool(
await node2.getAddress(),
0,
1,
);

assert.equal(Number(rewardPool), 5);
Expand All @@ -165,27 +167,19 @@ describe('Rewards Manager', () => {
it('can increment reward pool over multiple cycles', async () => {
await checkInitialRewardPoolState(rewardsManager);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 0, 100);
await ticketing.testerIncrementRewardPool(await node1.getAddress(), 1, 200);
await ticketing.testerIncrementRewardPool(await node2.getAddress(), 0, 300);
await ticketing.testerIncrementRewardPool(await node2.getAddress(), 1, 500);
await incrementRewardPool(node1, 1, 100n, 100n);
await incrementRewardPool(node1, 2, 200n, 100n);
await incrementRewardPool(node2, 1, 300n, 100n);
await incrementRewardPool(node2, 2, 500n, 100n);

// const rewardPoolNode1Cycle1 = await rewardsManager.getRewardPool(
// await node1.getAddress(),
// 0,
// );
const rewardPoolNode1 = await rewardsManager.getRewardPools(
await node1.getAddress(),
[0, 1],
[1, 2],
);

// const rewardPoolNode2Cycle1 = await rewardsManager.getRewardPool(
// await node2.getAddress(),
// 0,
// );
const rewardPoolNode2 = await rewardsManager.getRewardPools(
await node2.getAddress(),
[0, 1],
[1, 2],
);

assert.equal(Number(rewardPoolNode1[0]), 5);
Expand All @@ -200,22 +194,22 @@ describe('Rewards Manager', () => {

await checkInitialRewardPoolState(rewardsManager);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 0, 100);
await ticketing.testerIncrementRewardPool(await node2.getAddress(), 0, 300);
await incrementRewardPool(node1, 1, 100n, 100n);
await incrementRewardPool(node2, 1, 300n, 100n);

await registries.setDefaultPayoutPercentage(10000);

await ticketing.testerIncrementRewardPool(await node1.getAddress(), 1, 200);
await ticketing.testerIncrementRewardPool(await node2.getAddress(), 1, 500);
await incrementRewardPool(node1, 2, 200n, 100n);
await incrementRewardPool(node2, 2, 500n, 100n);

const rewardPoolNode1 = await rewardsManager.getRewardPools(
await node1.getAddress(),
[0, 1],
[1, 2],
);

const rewardPoolNode2 = await rewardsManager.getRewardPools(
await node2.getAddress(),
[0, 1],
[1, 2],
);

const unclaimedNode1Commission =
Expand All @@ -237,6 +231,7 @@ describe('Rewards Manager', () => {
const abi = [
'function incrementRewardPool(address node, uint256 cycle, uint256 amount) external',
'function getRewardPool(address node, uint256 cycle) external view returns (uint256)',
'function getRewardPools(address node, uint256[] cycles) external view returns (uint256[])',
'function getUnclaimedNodeCommission(address node) external view returns (uint256)',
'function claim(address node, uint256 cycle) external',
];
Expand All @@ -251,22 +246,6 @@ describe('Rewards Manager', () => {
'Expected rewards manager to support correct interface',
);

const abiERC165 = [
'function supportsInterface(bytes4 interfaceId) external view returns (bool)',
];

const interfaceIdERC165 = getInterfaceId(abiERC165);

const supportsERC165 = await rewardsManager.supportsInterface(
interfaceIdERC165,
);

assert.equal(
supportsERC165,
true,
'Expected rewards manager to support ERC165',
);

const invalidAbi = ['function foo(uint256 duration) external'];

const invalidAbiInterfaceId = getInterfaceId(invalidAbi);
Expand All @@ -282,6 +261,43 @@ describe('Rewards Manager', () => {
);
});

const setupUser = async (tokenBalance = 1_000_000n) => {
const user = ethers.Wallet.createRandom(ethers.provider);

await accounts[0].sendTransaction({
to: user.address,
value: ethers.parseEther('10'),
});

await contracts.syloToken.transfer(user.address, tokenBalance);
await contracts.syloToken
.connect(user)
.approve(await contracts.deposits.getAddress(), tokenBalance);

return user;
};

async function incrementRewardPool(
redeemer: Signer,
cycle: number,
escrowAmount?: bigint,
penaltyAmount?: bigint,
) {
await redeemTicket(
ticketing,
deposits,
{
sender: await setupUser(),
receiver: accounts[1],
redeemer: redeemer,
redeemerRand: 1,
cycle: cycle,
},
escrowAmount,
penaltyAmount,
);
}

async function checkInitialRewardPoolState(_rewardsManager: RewardsManager) {
const rewardPool = await _rewardsManager.getRewardPool(
await node1.getAddress(),
Expand Down
Loading

0 comments on commit e5af4a3

Please sign in to comment.