Rework eras* calls with minimum information (#2018)
This commit is contained in:
@@ -3,49 +3,60 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Exposure } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraExposure, DeriveEraExposures, DeriveEraPointsAll, DeriveEraValPoints } from '../types';
|
||||
import { EraIndex, Exposure } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorExposure } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap, take } from 'rxjs/operators';
|
||||
import { StorageKey } from '@polkadot/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function mapValidators (stakers: [StorageKey, Exposure][], validators: DeriveEraValPoints): DeriveEraExposures {
|
||||
return stakers.reduce((result: DeriveEraExposures, [key, exposure]): DeriveEraExposures => {
|
||||
const validatorId = key.args[1].toString();
|
||||
|
||||
result[validatorId] = {
|
||||
exposure,
|
||||
points: validators[validatorId]
|
||||
};
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
interface Mapped {
|
||||
nominators: DeriveEraNominatorExposure;
|
||||
validators: DeriveEraValidatorExposure;
|
||||
}
|
||||
|
||||
export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraExposure[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraExposure[]> =>
|
||||
api.derive.staking.erasPoints(withActive).pipe(
|
||||
switchMap((allPoints): Observable<[DeriveEraPointsAll[], [StorageKey, Exposure][][]]> =>
|
||||
function mapStakers (stakers: [StorageKey, Exposure][]): Mapped {
|
||||
const nominators: DeriveEraNominatorExposure = {};
|
||||
const validators: DeriveEraValidatorExposure = {};
|
||||
|
||||
stakers.forEach(([key, exposure]): void => {
|
||||
const validatorId = key.args[1].toString();
|
||||
|
||||
validators[validatorId] = exposure;
|
||||
|
||||
exposure.others.forEach(({ who }, index): void => {
|
||||
const nominatorId = who.toString();
|
||||
|
||||
nominators[nominatorId] = nominators[nominatorId] || [];
|
||||
nominators[nominatorId].push([validatorId, index]);
|
||||
});
|
||||
});
|
||||
|
||||
return { nominators, validators };
|
||||
}
|
||||
|
||||
export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean | BN) => Observable<DeriveEraExposure[]> {
|
||||
return memo((withActive?: boolean | BN): Observable<DeriveEraExposure[]> =>
|
||||
api.derive.staking.erasHistoric(withActive).pipe(
|
||||
switchMap((allEras): Observable<[EraIndex[], [StorageKey, Exposure][][]]> =>
|
||||
combineLatest([
|
||||
of(allPoints),
|
||||
of(allEras),
|
||||
combineLatest(
|
||||
// we could just do entries over the full set, however the set can be quite large - split it into
|
||||
// batches - may need to re-visit this, or alternatively use pages keys for exceptionally large sets
|
||||
allPoints.map(({ era }): Observable<[StorageKey, Exposure][]> =>
|
||||
allEras.map((era): Observable<[StorageKey, Exposure][]> =>
|
||||
api.query.staking.erasStakers.entries(era).pipe(take(1))
|
||||
)
|
||||
)
|
||||
])
|
||||
),
|
||||
map(([allPoints, erasStakers]): DeriveEraExposure[] =>
|
||||
allPoints.map(({ era, eraPoints, eraReward, validators }, index): DeriveEraExposure => ({
|
||||
map(([allEras, erasStakers]): DeriveEraExposure[] =>
|
||||
allEras.map((era, index): DeriveEraExposure => ({
|
||||
era,
|
||||
eraPoints,
|
||||
eraReward,
|
||||
validators: mapValidators(erasStakers[index], validators)
|
||||
...mapStakers(erasStakers[index])
|
||||
}))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { ActiveEraInfo, EraIndex } from '@polkadot/types/interfaces';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { Option, u32 } from '@polkadot/types';
|
||||
import { isBoolean, isUndefined } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function erasHistoric (api: ApiInterfaceRx): (withActive?: boolean | BN) => Observable<EraIndex[]> {
|
||||
return memo((withActive?: boolean | BN): Observable<EraIndex[]> =>
|
||||
api.query.staking?.activeEra
|
||||
? api.queryMulti<[Option<ActiveEraInfo>, u32]>([
|
||||
api.query.staking.activeEra,
|
||||
api.query.staking.historyDepth
|
||||
]).pipe(
|
||||
map(([activeEraOpt, historyDepth]): EraIndex[] => {
|
||||
const result: EraIndex[] = [];
|
||||
const max = historyDepth.toNumber();
|
||||
let lastEra = activeEraOpt.unwrapOrDefault().index.subn(withActive === true ? 0 : 1);
|
||||
|
||||
while (lastEra.gten(0) && result.length < max) {
|
||||
result.push(api.registry.createType('EraIndex', lastEra));
|
||||
|
||||
lastEra = lastEra.subn(1);
|
||||
}
|
||||
|
||||
// go from oldest to newest
|
||||
return result
|
||||
.filter((era): boolean =>
|
||||
isUndefined(withActive) || isBoolean(withActive)
|
||||
? true
|
||||
: era.gte(withActive)
|
||||
)
|
||||
.reverse();
|
||||
})
|
||||
)
|
||||
: of([])
|
||||
);
|
||||
}
|
||||
@@ -3,39 +3,14 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { ActiveEraInfo, Balance, EraIndex, EraRewardPoints } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraPointsAll, DeriveEraValPoints } from '../types';
|
||||
import { EraIndex, EraRewardPoints } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraPoints, DeriveEraValPoints } from '../types';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap, take } from 'rxjs/operators';
|
||||
import { Option, u32 } from '@polkadot/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function getAvailableIndexes (api: ApiInterfaceRx, withActive?: boolean): Observable<EraIndex[]> {
|
||||
return api.query.staking?.activeEra
|
||||
? api.queryMulti<[Option<ActiveEraInfo>, u32]>([
|
||||
api.query.staking.activeEra,
|
||||
api.query.staking.historyDepth
|
||||
]).pipe(
|
||||
map(([activeEraOpt, historyDepth]): EraIndex[] => {
|
||||
const result: EraIndex[] = [];
|
||||
const max = historyDepth.toNumber();
|
||||
let lastEra = activeEraOpt.unwrapOrDefault().index.subn(withActive ? 0 : 1);
|
||||
|
||||
while (lastEra.gten(0) && result.length < max) {
|
||||
result.push(api.registry.createType('EraIndex', lastEra));
|
||||
|
||||
lastEra = lastEra.subn(1);
|
||||
}
|
||||
|
||||
// go from oldest to newest
|
||||
return result.reverse();
|
||||
})
|
||||
)
|
||||
: of([]);
|
||||
}
|
||||
|
||||
function mapValidators ({ individual }: EraRewardPoints): DeriveEraValPoints {
|
||||
return [...individual.entries()]
|
||||
.filter(([, points]): boolean => points.gtn(0))
|
||||
@@ -46,25 +21,21 @@ function mapValidators ({ individual }: EraRewardPoints): DeriveEraValPoints {
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPointsAll[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraPointsAll[]> =>
|
||||
getAvailableIndexes(api, withActive).pipe(
|
||||
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[], Option<Balance>[]]> =>
|
||||
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPoints[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraPoints[]> =>
|
||||
api.derive.staking.erasHistoric(withActive).pipe(
|
||||
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[]]> =>
|
||||
combineLatest([
|
||||
of(indexes),
|
||||
indexes.length
|
||||
? api.query.staking.erasRewardPoints.multi<EraRewardPoints>(indexes).pipe(take(1))
|
||||
: of([]),
|
||||
indexes.length
|
||||
? api.query.staking.erasValidatorReward.multi<Option<Balance>>(indexes).pipe(take(1))
|
||||
: of([])
|
||||
])
|
||||
),
|
||||
map(([eras, points, rewards]): DeriveEraPointsAll[] =>
|
||||
eras.map((era, index): DeriveEraPointsAll => ({
|
||||
map(([eras, points]): DeriveEraPoints[] =>
|
||||
eras.map((era, index): DeriveEraPoints => ({
|
||||
era,
|
||||
eraPoints: points[index].total,
|
||||
eraReward: rewards[index].unwrapOrDefault(),
|
||||
validators: mapValidators(points[index])
|
||||
}))
|
||||
)
|
||||
|
||||
+5
@@ -20,6 +20,11 @@ function includeExposure (validatorId: string, { others }: Exposure, nominators:
|
||||
});
|
||||
}
|
||||
|
||||
// ,
|
||||
// indexes.length
|
||||
// ? api.query.staking.erasValidatorReward.multi<Option<Balance>>(indexes).pipe(take(1))
|
||||
// : of([])
|
||||
|
||||
export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraRewardsAll[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
|
||||
api.derive.staking.erasExposure(withActive).pipe(
|
||||
@@ -4,11 +4,12 @@
|
||||
|
||||
export * from './account';
|
||||
export * from './erasExposure';
|
||||
export * from './erasHistoric';
|
||||
export * from './erasPoints';
|
||||
export * from './erasRewards';
|
||||
// export * from './erasRewards';
|
||||
export * from './controllers';
|
||||
export * from './electedInfo';
|
||||
export * from './overview';
|
||||
export * from './query';
|
||||
export * from './stakerRewards';
|
||||
export * from './stakerExposure';
|
||||
export * from './validators';
|
||||
|
||||
+9
-8
@@ -3,22 +3,23 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { DeriveEraExposures, DeriveStakerReward } from '../types';
|
||||
import { DeriveStakerExpoure, DeriveEraValidatorExposure } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerReward[]> {
|
||||
return memo((accountId: Uint8Array | string, withActive?: boolean): Observable<DeriveStakerReward[]> => {
|
||||
export function stakerExposure (api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean | BN) => Observable<DeriveStakerExpoure[]> {
|
||||
return memo((accountId: Uint8Array | string, withActive?: boolean | BN): Observable<DeriveStakerExpoure[]> => {
|
||||
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
||||
|
||||
return api.derive.staking.erasRewards(withActive).pipe(
|
||||
map((rewards): DeriveStakerReward[] =>
|
||||
rewards.map(({ era, eraPoints, eraReward, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
|
||||
return api.derive.staking.erasExposure(withActive).pipe(
|
||||
map((exposures): DeriveStakerExpoure[] =>
|
||||
exposures.map(({ era, nominators: allNominators, validators: allValidators }): DeriveStakerExpoure => {
|
||||
const isValidator = !!allValidators[stakerId];
|
||||
const validators: DeriveEraExposures = {};
|
||||
const validators: DeriveEraValidatorExposure = {};
|
||||
let nominating: [string, number][] = [];
|
||||
|
||||
if (isValidator) {
|
||||
@@ -31,7 +32,7 @@ export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | st
|
||||
});
|
||||
}
|
||||
|
||||
return { era, eraPoints, eraReward, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
|
||||
return { era, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
|
||||
})
|
||||
)
|
||||
);
|
||||
@@ -5,41 +5,36 @@
|
||||
import { AccountId, Balance, BlockNumber, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
|
||||
import { DeriveSessionIndexes } from '../session/types';
|
||||
|
||||
interface EraPointInfo {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
eraReward: Balance;
|
||||
}
|
||||
|
||||
export type DeriveEraValPoints = Record<string, RewardPoint>;
|
||||
|
||||
export interface DeriveEraPointsAll extends EraPointInfo {
|
||||
export interface DeriveEraPoints {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
validators: DeriveEraValPoints;
|
||||
}
|
||||
|
||||
export interface DeriveEraPoints extends EraPointInfo {
|
||||
export interface DeriveStakersPoints {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
own: RewardPoint;
|
||||
}
|
||||
|
||||
export type DeriveEraExposures = Record<string, {
|
||||
exposure: Exposure;
|
||||
points: RewardPoint;
|
||||
}>;
|
||||
export type DeriveEraNominatorExposure = Record<string, [string, number][]>;
|
||||
|
||||
export interface DeriveEraExposure extends EraPointInfo {
|
||||
validators: DeriveEraExposures;
|
||||
export type DeriveEraValidatorExposure = Record<string, Exposure>;
|
||||
|
||||
export interface DeriveEraExposure {
|
||||
era: EraIndex;
|
||||
nominators: DeriveEraNominatorExposure;
|
||||
validators: DeriveEraValidatorExposure;
|
||||
}
|
||||
|
||||
export interface DeriveEraRewardsAll extends EraPointInfo {
|
||||
nominators: Record<string, [string, number][]>;
|
||||
validators: DeriveEraExposures;
|
||||
}
|
||||
|
||||
export interface DeriveStakerReward extends EraPointInfo {
|
||||
export interface DeriveStakerExpoure {
|
||||
era: EraIndex;
|
||||
isEmpty: boolean;
|
||||
isValidator: boolean;
|
||||
nominating: [string, number][];
|
||||
validators: DeriveEraExposures;
|
||||
validators: DeriveEraValidatorExposure;
|
||||
}
|
||||
|
||||
export interface DerivedStakingElected {
|
||||
|
||||
Reference in New Issue
Block a user