Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new EraStakers interface support #41

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/mappings/era/ValidatorEraInfoDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {StakeTarget} from "./EraInfoDataSource";
import {CachingEraInfoDataSource} from "./CachingEraInfoDataSource";
import {BigFromINumber} from "../utils";
import {BigFromINumber, SpStakingPagedExposureMetadata, SpStakingExposurePage} from "../utils";
import {PalletStakingExposure} from "@polkadot/types/lookup";
import {Option} from "@polkadot/types-codec";
import {INumber} from "@polkadot/types-codec/types/interfaces";


export class ValidatorEraInfoDataSource extends CachingEraInfoDataSource {
Expand All @@ -20,6 +22,14 @@ export class ValidatorEraInfoDataSource extends CachingEraInfoDataSource {

protected async fetchEraStakers(): Promise<StakeTarget[]> {
const era = await this.era()
if (api.query.staking.erasStakersOverview) {
return await this.fetchEraStakersPaged(era);
} else {
return await this.fetchEraStakersClipped(era);
}
}

private async fetchEraStakersClipped(era: number): Promise<StakeTarget[]> {
const exposures = await api.query.staking.erasStakersClipped.entries(era)

return exposures.map(([key, exp]) => {
Expand All @@ -43,4 +53,43 @@ export class ValidatorEraInfoDataSource extends CachingEraInfoDataSource {
})
}

private async fetchEraStakersPaged(era: number): Promise<StakeTarget[]> {
const overview = await api.query.staking.erasStakersOverview.entries(era)
const pages = await api.query.staking.erasStakersPaged.entries(era)

const othersCounted = pages.reduce((accumulator, [key, exp]) => {
const exposure = (exp as unknown as Option<SpStakingExposurePage>).unwrap()
const [, validatorId, pageId] = key.args
const pageNumber = (pageId as INumber).toNumber()
const validatorAddress = validatorId.toString()

const others = exposure.others.map(({who, value}) => {
return {
address: who.toString(),
amount: value.toBigInt()
}
});

(accumulator[validatorAddress] = accumulator[validatorAddress] || {})[pageNumber] = others;
return accumulator;
}, {})

return overview.map(([key, exp]) => {
const exposure = (exp as unknown as Option<SpStakingPagedExposureMetadata>).unwrap()
const [, validatorId] = key.args
let validatorAddress = validatorId.toString()

let others = []
for (let i = 0; i < exposure.pageCount.toNumber(); ++i) {
others.push(...othersCounted[validatorAddress][i])
};

return {
address: validatorAddress,
selfStake: exposure.own.toBigInt(),
totalStake: BigFromINumber(exposure.total),
others: others
}
});
}
}
21 changes: 19 additions & 2 deletions src/mappings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {INumber} from "@polkadot/types-codec/types/interfaces";
import {Big} from "big.js"
import {Perbill, Percent} from "@polkadot/types/interfaces/runtime/types";
import {Compact} from '@polkadot/types-codec'
import {Perbill, Percent, AccountId32} from "@polkadot/types/interfaces/runtime/types";
import {Compact, Struct, Vec} from '@polkadot/types-codec'

export function BigFromINumber(number: INumber): Big {
return Big(number.toString())
Expand Down Expand Up @@ -65,4 +65,21 @@ export function toPlanks(amount: Big): Big {

export function aprToApy(apr: number): number {
return Math.exp(apr) - 1.0
}

export interface SpStakingPagedExposureMetadata extends Struct {
readonly total: INumber;
readonly own: INumber;
readonly nominatorCount: INumber;
readonly pageCount: INumber;
}

interface SpStakingIndividualExposure extends Struct {
readonly who: AccountId32;
readonly value: INumber;
}

export interface SpStakingExposurePage extends Struct {
readonly pageTotal: INumber;
readonly others: Vec<SpStakingIndividualExposure>;
}
Loading