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.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import { Exposure } from '@polkadot/types/interfaces';
|
import { EraIndex, Exposure } from '@polkadot/types/interfaces';
|
||||||
import { DeriveEraExposure, DeriveEraExposures, DeriveEraPointsAll, DeriveEraValPoints } from '../types';
|
import { DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorExposure } from '../types';
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
import { Observable, combineLatest, of } from 'rxjs';
|
import { Observable, combineLatest, of } from 'rxjs';
|
||||||
import { map, switchMap, take } from 'rxjs/operators';
|
import { map, switchMap, take } from 'rxjs/operators';
|
||||||
import { StorageKey } from '@polkadot/types';
|
import { StorageKey } from '@polkadot/types';
|
||||||
|
|
||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
function mapValidators (stakers: [StorageKey, Exposure][], validators: DeriveEraValPoints): DeriveEraExposures {
|
interface Mapped {
|
||||||
return stakers.reduce((result: DeriveEraExposures, [key, exposure]): DeriveEraExposures => {
|
nominators: DeriveEraNominatorExposure;
|
||||||
const validatorId = key.args[1].toString();
|
validators: DeriveEraValidatorExposure;
|
||||||
|
|
||||||
result[validatorId] = {
|
|
||||||
exposure,
|
|
||||||
points: validators[validatorId]
|
|
||||||
};
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}, {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraExposure[]> {
|
function mapStakers (stakers: [StorageKey, Exposure][]): Mapped {
|
||||||
return memo((withActive?: boolean): Observable<DeriveEraExposure[]> =>
|
const nominators: DeriveEraNominatorExposure = {};
|
||||||
api.derive.staking.erasPoints(withActive).pipe(
|
const validators: DeriveEraValidatorExposure = {};
|
||||||
switchMap((allPoints): Observable<[DeriveEraPointsAll[], [StorageKey, Exposure][][]]> =>
|
|
||||||
|
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([
|
combineLatest([
|
||||||
of(allPoints),
|
of(allEras),
|
||||||
combineLatest(
|
combineLatest(
|
||||||
// we could just do entries over the full set, however the set can be quite large - split it into
|
// 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
|
// 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))
|
api.query.staking.erasStakers.entries(era).pipe(take(1))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
map(([allPoints, erasStakers]): DeriveEraExposure[] =>
|
map(([allEras, erasStakers]): DeriveEraExposure[] =>
|
||||||
allPoints.map(({ era, eraPoints, eraReward, validators }, index): DeriveEraExposure => ({
|
allEras.map((era, index): DeriveEraExposure => ({
|
||||||
era,
|
era,
|
||||||
eraPoints,
|
...mapStakers(erasStakers[index])
|
||||||
eraReward,
|
|
||||||
validators: mapValidators(erasStakers[index], validators)
|
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import { ActiveEraInfo, Balance, EraIndex, EraRewardPoints } from '@polkadot/types/interfaces';
|
import { EraIndex, EraRewardPoints } from '@polkadot/types/interfaces';
|
||||||
import { DeriveEraPointsAll, DeriveEraValPoints } from '../types';
|
import { DeriveEraPoints, DeriveEraValPoints } from '../types';
|
||||||
|
|
||||||
import { Observable, combineLatest, of } from 'rxjs';
|
import { Observable, combineLatest, of } from 'rxjs';
|
||||||
import { map, switchMap, take } from 'rxjs/operators';
|
import { map, switchMap, take } from 'rxjs/operators';
|
||||||
import { Option, u32 } from '@polkadot/types';
|
|
||||||
|
|
||||||
import { memo } from '../util';
|
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 {
|
function mapValidators ({ individual }: EraRewardPoints): DeriveEraValPoints {
|
||||||
return [...individual.entries()]
|
return [...individual.entries()]
|
||||||
.filter(([, points]): boolean => points.gtn(0))
|
.filter(([, points]): boolean => points.gtn(0))
|
||||||
@@ -46,25 +21,21 @@ function mapValidators ({ individual }: EraRewardPoints): DeriveEraValPoints {
|
|||||||
}, {});
|
}, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPointsAll[]> {
|
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPoints[]> {
|
||||||
return memo((withActive?: boolean): Observable<DeriveEraPointsAll[]> =>
|
return memo((withActive?: boolean): Observable<DeriveEraPoints[]> =>
|
||||||
getAvailableIndexes(api, withActive).pipe(
|
api.derive.staking.erasHistoric(withActive).pipe(
|
||||||
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[], Option<Balance>[]]> =>
|
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[]]> =>
|
||||||
combineLatest([
|
combineLatest([
|
||||||
of(indexes),
|
of(indexes),
|
||||||
indexes.length
|
indexes.length
|
||||||
? api.query.staking.erasRewardPoints.multi<EraRewardPoints>(indexes).pipe(take(1))
|
? 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([])
|
: of([])
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
map(([eras, points, rewards]): DeriveEraPointsAll[] =>
|
map(([eras, points]): DeriveEraPoints[] =>
|
||||||
eras.map((era, index): DeriveEraPointsAll => ({
|
eras.map((era, index): DeriveEraPoints => ({
|
||||||
era,
|
era,
|
||||||
eraPoints: points[index].total,
|
eraPoints: points[index].total,
|
||||||
eraReward: rewards[index].unwrapOrDefault(),
|
|
||||||
validators: mapValidators(points[index])
|
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[]> {
|
export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraRewardsAll[]> {
|
||||||
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
|
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
|
||||||
api.derive.staking.erasExposure(withActive).pipe(
|
api.derive.staking.erasExposure(withActive).pipe(
|
||||||
@@ -4,11 +4,12 @@
|
|||||||
|
|
||||||
export * from './account';
|
export * from './account';
|
||||||
export * from './erasExposure';
|
export * from './erasExposure';
|
||||||
|
export * from './erasHistoric';
|
||||||
export * from './erasPoints';
|
export * from './erasPoints';
|
||||||
export * from './erasRewards';
|
// export * from './erasRewards';
|
||||||
export * from './controllers';
|
export * from './controllers';
|
||||||
export * from './electedInfo';
|
export * from './electedInfo';
|
||||||
export * from './overview';
|
export * from './overview';
|
||||||
export * from './query';
|
export * from './query';
|
||||||
export * from './stakerRewards';
|
export * from './stakerExposure';
|
||||||
export * from './validators';
|
export * from './validators';
|
||||||
|
|||||||
+9
-8
@@ -3,22 +3,23 @@
|
|||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
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 { Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
|
|
||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
export function stakerRewards (api: ApiInterfaceRx): (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): Observable<DeriveStakerReward[]> => {
|
return memo((accountId: Uint8Array | string, withActive?: boolean | BN): Observable<DeriveStakerExpoure[]> => {
|
||||||
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
||||||
|
|
||||||
return api.derive.staking.erasRewards(withActive).pipe(
|
return api.derive.staking.erasExposure(withActive).pipe(
|
||||||
map((rewards): DeriveStakerReward[] =>
|
map((exposures): DeriveStakerExpoure[] =>
|
||||||
rewards.map(({ era, eraPoints, eraReward, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
|
exposures.map(({ era, nominators: allNominators, validators: allValidators }): DeriveStakerExpoure => {
|
||||||
const isValidator = !!allValidators[stakerId];
|
const isValidator = !!allValidators[stakerId];
|
||||||
const validators: DeriveEraExposures = {};
|
const validators: DeriveEraValidatorExposure = {};
|
||||||
let nominating: [string, number][] = [];
|
let nominating: [string, number][] = [];
|
||||||
|
|
||||||
if (isValidator) {
|
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 { AccountId, Balance, BlockNumber, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
|
||||||
import { DeriveSessionIndexes } from '../session/types';
|
import { DeriveSessionIndexes } from '../session/types';
|
||||||
|
|
||||||
interface EraPointInfo {
|
|
||||||
era: EraIndex;
|
|
||||||
eraPoints: RewardPoint;
|
|
||||||
eraReward: Balance;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type DeriveEraValPoints = Record<string, RewardPoint>;
|
export type DeriveEraValPoints = Record<string, RewardPoint>;
|
||||||
|
|
||||||
export interface DeriveEraPointsAll extends EraPointInfo {
|
export interface DeriveEraPoints {
|
||||||
|
era: EraIndex;
|
||||||
|
eraPoints: RewardPoint;
|
||||||
validators: DeriveEraValPoints;
|
validators: DeriveEraValPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DeriveEraPoints extends EraPointInfo {
|
export interface DeriveStakersPoints {
|
||||||
|
era: EraIndex;
|
||||||
|
eraPoints: RewardPoint;
|
||||||
own: RewardPoint;
|
own: RewardPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DeriveEraExposures = Record<string, {
|
export type DeriveEraNominatorExposure = Record<string, [string, number][]>;
|
||||||
exposure: Exposure;
|
|
||||||
points: RewardPoint;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
export interface DeriveEraExposure extends EraPointInfo {
|
export type DeriveEraValidatorExposure = Record<string, Exposure>;
|
||||||
validators: DeriveEraExposures;
|
|
||||||
|
export interface DeriveEraExposure {
|
||||||
|
era: EraIndex;
|
||||||
|
nominators: DeriveEraNominatorExposure;
|
||||||
|
validators: DeriveEraValidatorExposure;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DeriveEraRewardsAll extends EraPointInfo {
|
export interface DeriveStakerExpoure {
|
||||||
nominators: Record<string, [string, number][]>;
|
era: EraIndex;
|
||||||
validators: DeriveEraExposures;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DeriveStakerReward extends EraPointInfo {
|
|
||||||
isEmpty: boolean;
|
isEmpty: boolean;
|
||||||
isValidator: boolean;
|
isValidator: boolean;
|
||||||
nominating: [string, number][];
|
nominating: [string, number][];
|
||||||
validators: DeriveEraExposures;
|
validators: DeriveEraValidatorExposure;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DerivedStakingElected {
|
export interface DerivedStakingElected {
|
||||||
|
|||||||
Reference in New Issue
Block a user