Skip to content

Commit

Permalink
Skip rewardpool (#421)
Browse files Browse the repository at this point in the history
* skip reward pool for state channel rewards

* fix test

* build abi
  • Loading branch information
ianhe8x authored Sep 11, 2024
1 parent c96a1f8 commit ba78249
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 154 deletions.
6 changes: 3 additions & 3 deletions contracts/RewardsDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ contract RewardsDistributor is IRewardsDistributor, Initializable, OwnableUpgrad
rewardInfo.eraReward -= rewardInfo.eraRewardRemoveTable[rewardInfo.lastClaimEra];
delete rewardInfo.eraRewardAddTable[rewardInfo.lastClaimEra];
delete rewardInfo.eraRewardRemoveTable[rewardInfo.lastClaimEra];
if (rewardInfo.eraReward != 0) {
uint256 totalStake = rewardsStaking.getTotalStakingAmount(runner);
uint256 totalStake = rewardsStaking.getTotalStakingAmount(runner);
if (rewardInfo.eraReward != 0 && totalStake > 0) {
uint256 selfStake = rewardsStaking.getDelegationAmount(runner, runner);
require(totalStake > 0, 'RD006');
// require(totalStake > 0, 'RD006'); moved to if condition

uint256 commissionRate = IIndexerRegistry(
settings.getContractAddress(SQContracts.IndexerRegistry)
Expand Down
39 changes: 33 additions & 6 deletions contracts/StateChannel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import '@openzeppelin/contracts-upgradeable/utils/introspection/ERC165CheckerUpgradeable.sol';

import './interfaces/IConsumer.sol';
import './interfaces/IEraManager.sol';
import './interfaces/IIndexerRegistry.sol';
import './interfaces/ISettings.sol';
import './interfaces/IRewardsDistributor.sol';
import './interfaces/IRewardsPool.sol';
import './interfaces/IConsumerRegistry.sol';
import './interfaces/IRewardsBooster.sol';
Expand Down Expand Up @@ -103,6 +105,13 @@ contract StateChannel is Initializable, OwnableUpgradeable, SQParameter {
event ChannelFinalize(uint256 indexed channelId, uint256 total, uint256 remain);
/// @notice Emitted when Settle the channel with new state
event ChannelLabor(bytes32 deploymentId, address indexer, uint256 amount);
/// @notice Emitted when Settle the channel with new state
event ChannelLabor2(
uint256 indexed channelId,
bytes32 deploymentId,
address indexer,
uint256 amount
);

/**
* @dev ### FUNCTIONS
Expand Down Expand Up @@ -525,14 +534,32 @@ contract StateChannel is Initializable, OwnableUpgradeable, SQParameter {
if (amount > 0) {
address indexer = channels[query.channelId].indexer;
bytes32 deploymentId = channels[query.channelId].deploymentId;
address rewardPoolAddress = settings.getContractAddress(SQContracts.RewardsPool);
IERC20(settings.getContractAddress(SQContracts.SQToken)).approve(
rewardPoolAddress,
// rewards pool is deprecated
// address rewardPoolAddress = settings.getContractAddress(SQContracts.RewardsPool);
// IERC20(settings.getContractAddress(SQContracts.SQToken)).approve(
// rewardPoolAddress,
// amount
// );
// IRewardsPool rewardsPool = IRewardsPool(rewardPoolAddress);
// rewardsPool.labor(deploymentId, indexer, amount);

IRewardsDistributor rewardsDistributor = IRewardsDistributor(
ISettings(settings).getContractAddress(SQContracts.RewardsDistributor)
);
IEraManager eraManager = IEraManager(
ISettings(settings).getContractAddress(SQContracts.EraManager)
);
IERC20(settings.getContractAddress(SQContracts.SQToken)).safeIncreaseAllowance(
address(rewardsDistributor),
amount
);
IRewardsPool rewardsPool = IRewardsPool(rewardPoolAddress);
rewardsPool.labor(deploymentId, indexer, amount);
emit ChannelLabor(deploymentId, indexer, amount);
rewardsDistributor.addInstantRewards(
indexer,
address(this),
amount,
eraManager.safeUpdateAndGetEra()
);
emit ChannelLabor2(query.channelId, deploymentId, indexer, amount);
}

// finalise channel if meet the requirements
Expand Down
31 changes: 31 additions & 0 deletions publish/ABI/StateChannel.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@
"name": "ChannelLabor",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "channelId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "deploymentId",
"type": "bytes32"
},
{
"indexed": false,
"internalType": "address",
"name": "indexer",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "ChannelLabor2",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
23 changes: 17 additions & 6 deletions test/RewardsPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import { expect } from 'chai';
import { EraManager, IndexerRegistry, RewardsDistributor, RewardsHelper, RewardsPool, ERC20, Staking } from '../src';
import { deploymentIds } from './constants';
import { etherParse, registerRunner, startNewEra, time, timeTravel } from './helper';
import { etherParse, eventsFrom, registerRunner, startNewEra, time, timeTravel } from './helper';
import { deployContracts } from './setup';
import { ethers, waffle } from 'hardhat';

describe('RewardsPool Contract', () => {
describe.skip('RewardsPool Contract', () => {
const deploymentId0 = deploymentIds[0];
const deploymentId1 = deploymentIds[1];
const deploymentId2 = deploymentIds[2];
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('RewardsPool Contract', () => {

// Moved to era 2.
await registerRunner(token, indexerRegistry, staking, root, root, etherParse('1000'), 1e5);
await registerRunner(token, indexerRegistry, staking, root, runner0, etherParse('1000'), 1e5);
await registerRunner(token, indexerRegistry, staking, root, runner0, etherParse('100000'), 1e5);
await registerRunner(token, indexerRegistry, staking, root, runner1, etherParse('1000'), 1e5);
await registerRunner(token, indexerRegistry, staking, root, runner2, etherParse('1000'), 1e5);
});
Expand Down Expand Up @@ -104,26 +104,37 @@ describe('RewardsPool Contract', () => {
});

it('Batch collect from RewardsDistributor', async () => {
await rewardsPool.setAlpha(9, 10);
await startNewEra(eraManager);
await rewardsDistributor.collectAndDistributeRewards(runner0.address);
await rewardsDistributor.collectAndDistributeRewards(runner1.address);
const era = await eraManager.eraNumber();

const indexerAmount0 = etherParse('1');
const indexerAmount1 = etherParse('2');
await rewardsPool.connect(root).labor(deploymentId0, runner0.address, indexerAmount0);
await rewardsPool.connect(root).labor(deploymentId1, runner0.address, indexerAmount0);
// await rewardsPool.connect(root).labor(deploymentId1, runner0.address, indexerAmount0);
await rewardsPool.connect(root).labor(deploymentId0, runner1.address, indexerAmount1);

// Check the status.
const rewards1 = await rewardsPool.getReward(deploymentId0, era, runner0.address);
expect(rewards1[0]).to.be.eq(etherParse('1')); // labor
expect(rewards1[1]).to.be.eq(etherParse('3')); // reward
// expect(rewards1[0]).to.be.eq(etherParse('1')); // labor
// expect(rewards1[1]).to.be.eq(etherParse('3')); // reward
const rewards1_1 = await rewardsPool.getReward(deploymentId0, era, runner1.address);

await timeTravel(time.duration.days(1).toNumber());

// Auto collect
await rewardsDistributor.collectAndDistributeRewards(runner0.address);

const tx2 = await rewardsDistributor.collectAndDistributeRewards(runner1.address);
const evts2 = await eventsFrom(
tx2,
rewardsDistributor,
'DistributeRewards(address,uint256,uint256,uint256)'
);
const tx = await rewardsDistributor.collectAndDistributeRewards(runner0.address);
const evts = await eventsFrom(tx, rewardsDistributor, 'DistributeRewards(address,uint256,uint256,uint256)');
const rewards2 = await rewardsPool.getReward(deploymentId0, era, runner0.address);
expect(rewards2[0]).to.be.eq(etherParse('0')); // already collected
const isClaimed1 = await rewardsPool.isClaimed(era, runner0.address);
Expand Down
Loading

0 comments on commit ba78249

Please sign in to comment.