Combine points into stakerRewards (#2008)
* Combine points into stakerRewards * Bump dev
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { AccountId, EraIndex, Exposure } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraExposure, DeriveEraPointsAll } from '../types';
|
||||
import { AccountId, EraIndex, Exposure, RewardPoint } from '@polkadot/types/interfaces';
|
||||
import { DeriveEraExposure, DeriveEraExposures, DeriveEraPointsAll } from '../types';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
@@ -12,13 +12,13 @@ import { StorageKey } from '@polkadot/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function queryClipped (api: ApiInterfaceRx): Observable<[EraIndex, [AccountId, Exposure][]][]> {
|
||||
return api.derive.staking.erasPoints().pipe(
|
||||
function queryClipped (api: ApiInterfaceRx, withActive?: boolean): Observable<[EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]][]> {
|
||||
return api.derive.staking.erasPoints(withActive).pipe(
|
||||
switchMap((allPoints): Observable<[DeriveEraPointsAll[], [StorageKey, Exposure][][]]> =>
|
||||
combineLatest([
|
||||
of(allPoints),
|
||||
combineLatest(
|
||||
// we could just do entries over the full set, however the resultset 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
|
||||
allPoints.map(({ era }): Observable<[StorageKey, Exposure][]> =>
|
||||
api.query.staking.erasStakersClipped.entries(era)
|
||||
@@ -26,29 +26,35 @@ function queryClipped (api: ApiInterfaceRx): Observable<[EraIndex, [AccountId, E
|
||||
)
|
||||
])
|
||||
),
|
||||
map(([allPoints, erasStakers]): [EraIndex, [AccountId, Exposure][]][] =>
|
||||
allPoints.map(({ era }, index): [EraIndex, [AccountId, Exposure][]] => [
|
||||
map(([allPoints, erasStakers]): [EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]][] =>
|
||||
allPoints.map(({ all, era, eraPoints }, index): [EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]] => [
|
||||
era,
|
||||
erasStakers[index].map(([key, exposure]): [AccountId, Exposure] => [
|
||||
eraPoints,
|
||||
erasStakers[index].map(([key, exposure]): [AccountId, Exposure, RewardPoint] => [
|
||||
key.args[1] as AccountId,
|
||||
exposure
|
||||
exposure,
|
||||
all[key.args[1].toString()]
|
||||
])
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function erasExposure (api: ApiInterfaceRx): () => Observable<DeriveEraExposure[]> {
|
||||
return memo((): Observable<DeriveEraExposure[]> =>
|
||||
queryClipped(api).pipe(
|
||||
export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraExposure[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraExposure[]> =>
|
||||
queryClipped(api, withActive).pipe(
|
||||
map((clipped): DeriveEraExposure[] =>
|
||||
clipped.map(([era, validators]): DeriveEraExposure => ({
|
||||
all: validators.reduce((all: Record<string, Exposure>, [validtorId, exposure]): Record<string, Exposure> => {
|
||||
all[validtorId.toString()] = exposure;
|
||||
clipped.map(([era, eraPoints, validators]): DeriveEraExposure => ({
|
||||
all: validators.reduce((all: DeriveEraExposures, [validtorId, exposure, points]): DeriveEraExposures => {
|
||||
all[validtorId.toString()] = {
|
||||
exposure,
|
||||
points
|
||||
};
|
||||
|
||||
return all;
|
||||
}, {}),
|
||||
era
|
||||
era,
|
||||
eraPoints
|
||||
}))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ import { Option, u32 } from '@polkadot/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function getAvailableIndexes (api: ApiInterfaceRx): Observable<EraIndex[]> {
|
||||
function getAvailableIndexes (api: ApiInterfaceRx, withActive?: boolean): Observable<EraIndex[]> {
|
||||
return api.query.staking?.activeEra
|
||||
? api.queryMulti<[Option<ActiveEraInfo>, u32]>([
|
||||
api.query.staking.activeEra,
|
||||
@@ -21,7 +21,7 @@ function getAvailableIndexes (api: ApiInterfaceRx): Observable<EraIndex[]> {
|
||||
map(([activeEraOpt, historyDepth]): EraIndex[] => {
|
||||
const result: EraIndex[] = [];
|
||||
const max = historyDepth.toNumber();
|
||||
let lastEra = activeEraOpt.unwrapOrDefault().index.subn(1);
|
||||
let lastEra = activeEraOpt.unwrapOrDefault().index.subn(withActive ? 0 : 1);
|
||||
|
||||
while (lastEra.gten(0) && result.length < max) {
|
||||
result.push(api.registry.createType('EraIndex', lastEra));
|
||||
@@ -36,9 +36,9 @@ function getAvailableIndexes (api: ApiInterfaceRx): Observable<EraIndex[]> {
|
||||
: of([]);
|
||||
}
|
||||
|
||||
export function erasPoints (api: ApiInterfaceRx): () => Observable<DeriveEraPointsAll[]> {
|
||||
return memo((): Observable<DeriveEraPointsAll[]> =>
|
||||
getAvailableIndexes(api).pipe(
|
||||
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPointsAll[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraPointsAll[]> =>
|
||||
getAvailableIndexes(api, withActive).pipe(
|
||||
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[]]> =>
|
||||
combineLatest([
|
||||
of(indexes),
|
||||
@@ -57,7 +57,7 @@ export function erasPoints (api: ApiInterfaceRx): () => Observable<DeriveEraPoin
|
||||
return all;
|
||||
}, {}),
|
||||
era,
|
||||
total: rewards[index].total
|
||||
eraPoints: rewards[index].total
|
||||
}))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -10,17 +10,17 @@ import { map } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function erasRewards (api: ApiInterfaceRx): () => Observable<DeriveEraRewardsAll[]> {
|
||||
return memo((): Observable<DeriveEraRewardsAll[]> =>
|
||||
api.derive.staking.erasExposure().pipe(
|
||||
export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraRewardsAll[]> {
|
||||
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
|
||||
api.derive.staking.erasExposure(withActive).pipe(
|
||||
map((exposures): DeriveEraRewardsAll[] =>
|
||||
exposures.map(({ era, all }): DeriveEraRewardsAll =>
|
||||
exposures.map(({ all, era, eraPoints }): DeriveEraRewardsAll =>
|
||||
Object
|
||||
.entries(all)
|
||||
.reduce((rewards: DeriveEraRewardsAll, [validatorId, exposure]): DeriveEraRewardsAll => {
|
||||
rewards.validators[validatorId] = exposure;
|
||||
.reduce((rewards: DeriveEraRewardsAll, [validatorId, data]): DeriveEraRewardsAll => {
|
||||
rewards.validators[validatorId] = data;
|
||||
|
||||
exposure.others.forEach(({ who }, index): void => {
|
||||
data.exposure.others.forEach(({ who }, index): void => {
|
||||
const nominatorId = who.toString();
|
||||
|
||||
rewards.nominators[nominatorId] = rewards.nominators[nominatorId] || [];
|
||||
@@ -28,7 +28,7 @@ export function erasRewards (api: ApiInterfaceRx): () => Observable<DeriveEraRew
|
||||
});
|
||||
|
||||
return rewards;
|
||||
}, { era, nominators: {}, validators: {} })
|
||||
}, { era, eraPoints, nominators: {}, validators: {} })
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -12,4 +12,3 @@ export * from './overview';
|
||||
export * from './query';
|
||||
export * from './stakerRewards';
|
||||
export * from './validators';
|
||||
export * from './validatorPoints';
|
||||
|
||||
@@ -3,39 +3,36 @@
|
||||
// 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 { DeriveStakerReward } from '../types';
|
||||
import { DeriveEraExposures, DeriveStakerReward } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakerReward[]> {
|
||||
return memo((accountId: Uint8Array | string): Observable<DeriveStakerReward[]> => {
|
||||
export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerReward[]> {
|
||||
return memo((accountId: Uint8Array | string, withActive?: boolean): Observable<DeriveStakerReward[]> => {
|
||||
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
||||
|
||||
return api.derive.staking.erasRewards().pipe(
|
||||
return api.derive.staking.erasRewards(withActive).pipe(
|
||||
map((rewards): DeriveStakerReward[] =>
|
||||
rewards
|
||||
.map(({ era, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
|
||||
const isValidator = !!allValidators[stakerId];
|
||||
const validators: Record<string, Exposure> = {};
|
||||
let nominating: [string, number][] = [];
|
||||
rewards.map(({ era, eraPoints, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
|
||||
const isValidator = !!allValidators[stakerId];
|
||||
const validators: DeriveEraExposures = {};
|
||||
let nominating: [string, number][] = [];
|
||||
|
||||
if (isValidator) {
|
||||
validators[stakerId] = allValidators[stakerId];
|
||||
} else if (allNominators[stakerId]) {
|
||||
nominating = allNominators[stakerId];
|
||||
if (isValidator) {
|
||||
validators[stakerId] = allValidators[stakerId];
|
||||
} else if (allNominators[stakerId]) {
|
||||
nominating = allNominators[stakerId];
|
||||
|
||||
nominating.forEach(([validatorId]): void => {
|
||||
validators[validatorId] = allValidators[validatorId];
|
||||
});
|
||||
}
|
||||
nominating.forEach(([validatorId]): void => {
|
||||
validators[validatorId] = allValidators[validatorId];
|
||||
});
|
||||
}
|
||||
|
||||
return { era, isValidator, nominating, validators };
|
||||
})
|
||||
.filter(({ validators }): boolean => Object.keys(validators).length !== 0)
|
||||
return { era, eraPoints, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -8,31 +8,40 @@ import { DeriveSessionIndexes } from '../session/types';
|
||||
export interface DeriveEraPointsAll {
|
||||
all: Record<string, RewardPoint>;
|
||||
era: EraIndex;
|
||||
total: RewardPoint;
|
||||
eraPoints: RewardPoint;
|
||||
}
|
||||
|
||||
export interface DeriveEraPoints {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
own: RewardPoint;
|
||||
total: RewardPoint;
|
||||
}
|
||||
|
||||
export type DeriveEraExposures = Record<string, {
|
||||
exposure: Exposure;
|
||||
points: RewardPoint;
|
||||
}>;
|
||||
|
||||
export interface DeriveEraExposure {
|
||||
all: Record<string, Exposure>;
|
||||
all: DeriveEraExposures;
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
}
|
||||
|
||||
export interface DeriveEraRewardsAll {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
nominators: Record<string, [string, number][]>;
|
||||
validators: Record<string, Exposure>;
|
||||
validators: DeriveEraExposures;
|
||||
}
|
||||
|
||||
export interface DeriveStakerReward {
|
||||
era: EraIndex;
|
||||
eraPoints: RewardPoint;
|
||||
isEmpty: boolean;
|
||||
isValidator: boolean;
|
||||
nominating: [string, number][];
|
||||
validators: Record<string, Exposure>;
|
||||
validators: DeriveEraExposures;
|
||||
}
|
||||
|
||||
export interface DerivedStakingElected {
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// 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 { DeriveEraPoints } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function validatorPoints (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveEraPoints[]> {
|
||||
return memo((_accountId: Uint8Array | string): Observable<DeriveEraPoints[]> => {
|
||||
const accountId = api.registry.createType('AccountId', _accountId).toString();
|
||||
|
||||
return api.derive.staking.erasPoints().pipe(
|
||||
map((all): DeriveEraPoints[] =>
|
||||
all.map(({ all, era, total }): DeriveEraPoints => ({
|
||||
era,
|
||||
own: all[accountId] || api.registry.createType('RewardPoint'),
|
||||
total
|
||||
}))
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user