Skip to content

Commit

Permalink
Update logic for Relaychains (#10)
Browse files Browse the repository at this point in the history
* fix payout algorithm

* refactoring

* fix name

* fix force batch
  • Loading branch information
stepanLav authored Apr 25, 2024
1 parent 291c05d commit 599e9b1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
82 changes: 82 additions & 0 deletions src/payoutHelper/payouters/NewRelaychainPayoutLogic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { Validator } from "../../config/conf";
import { sendTransaction } from "../../utils";
import { PayoutHelper } from "../PayoutHelper";

/***
* New logic to handle payouts if erasStakersOverview exists.
*/
export class NewRelaychainPayoutLogic extends PayoutHelper {

/**
* New method to handle payouts if erasStakersOverview exists.
* @param validators - The validators to payout rewards for.
* @param sender - The sender of the transaction.
* @param depth - Whether to check the history for unclaimed rewards.
*/
async payoutRewards(validators: Validator[], sender, depth: boolean = false): Promise<void> {
const currentEra = await this.getCurrentEraNumber();
const eraToReward = currentEra - 1
const historyDepth = this.getHistoryDepth();
const startEra = Math.max(eraToReward - historyDepth, 0);
const transactions: Array<SubmittableExtrinsic<any, any>> = [];

await this.collectTransactionsForUnclaimedRewards(startEra, currentEra, validators, transactions);
await this.executeTransactionsInBatches(transactions, sender, 4);
}

/**
* Collects transactions for unclaimed rewards for validators within a specified era range.
* @param startEra The starting era from which to collect unclaimed rewards.
* @param currentEra The current era up to which to check for unclaimed rewards.
* @param validators An array of validators for whom to check unclaimed rewards.
* @param transactions An array to which the payout transactions will be added.
*/
private async collectTransactionsForUnclaimedRewards(startEra: number, currentEra: number, validators: Validator[], transactions: Array<SubmittableExtrinsic<any, any>>): Promise<void> {
for (let era = startEra; era < currentEra; era++) {
for (const validator of validators) {
const eraStakersOverview = (await this.api.query.staking.erasStakersOverview(era, validator.address)).toJSON() || {};
if (Object.keys(eraStakersOverview).length === 0) continue;

const claimedPages = (await this.api.query.staking.claimedRewards(era, validator.address)).toJSON();
// @ts-ignore
if (!claimedPages || claimedPages.length === 0) {
const transaction = this.api.tx.staking.payoutStakers(validator.address, era);
transactions.push(transaction);
}
}
}
}

/**
* Executes transactions in batches of a specified size.
* @param transactions An array of transactions to be executed.
* @param sender The sender of the transactions.
* @param size The number of transactions to include in each batch. Defaults to 4.
*/
private async executeTransactionsInBatches(transactions: Array<SubmittableExtrinsic<any, any>>, sender, size: number = 4): Promise<void> {
for (let i = 0; i < transactions.length; i += size) {
const batchTransactions = transactions.slice(i, i + size);
const batchTransaction = this.api.tx.utility.forceBatch(batchTransactions);
await sendTransaction(batchTransaction, sender, this.api);
}
}

/**
* Retrieves the current era number from the blockchain.
* @returns {Promise<number>} The current era as a number.
*/
private async getCurrentEraNumber(): Promise<number> {
const currentEra = await this.api.query.staking.currentEra();
// @ts-ignore
return currentEra.unwrap().toNumber();
}

/**
* Fetches the history depth value from the blockchain constants.
* @returns {number} The history depth as a number.
*/
private getHistoryDepth(): number {
return Number(this.api.consts.staking.historyDepth.toString());
}
}
19 changes: 15 additions & 4 deletions src/payoutHelper/payouters/RelaychainPayouter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Validator } from "../../config/conf";
import { sendTransaction } from "../../utils";
import { PayoutHelper } from "../PayoutHelper";
import { NewRelaychainPayoutLogic } from "./NewRelaychainPayoutLogic";
import { SubstrateConnection } from "../../connection";

/**
* RelychainPayoutHelper is a class that extends PayoutHelper and provides methods to handle payouts on the Relychain network.
*/
export class RelychainPayoutHelper extends PayoutHelper {
private newPayoutLogic: NewRelaychainPayoutLogic;

constructor(api: SubstrateConnection) {
super(api);
this.newPayoutLogic = new NewRelaychainPayoutLogic(api);
}
/**
* Payout rewards for the given validators.
* @param validators - The validators to payout rewards for.
Expand All @@ -15,10 +22,14 @@ export class RelychainPayoutHelper extends PayoutHelper {
* @returns A promise that resolves when the rewards have been paid out.
*/
async payoutRewards(validators: Validator[], sender, depth: boolean = false): Promise<void> {
for (const validator of validators) {
const unclaimedPayouts = await this.checkPayouts(validator.address, depth);
for (const payout of unclaimedPayouts) {
await this.payout(validator.address, payout, sender);
if (this.api.query.staking.erasStakersOverview) {
await this.newPayoutLogic.payoutRewards(validators, sender, depth);
} else {
for (const validator of validators) {
const unclaimedPayouts = await this.checkPayouts(validator.address, depth);
for (const payout of unclaimedPayouts) {
await this.payout(validator.address, payout, sender);
}
}
}
}
Expand Down

0 comments on commit 599e9b1

Please sign in to comment.