Expose eraReward on derives (#2015)

This commit is contained in:
Jaco Greeff
2020-03-09 10:14:45 +01:00
committed by GitHub
parent ff8383c5ed
commit 81a2a51610
5 changed files with 31 additions and 29 deletions
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, EraIndex, Exposure, RewardPoint } from '@polkadot/types/interfaces';
import { AccountId, Balance, EraIndex, Exposure, RewardPoint } from '@polkadot/types/interfaces';
import { DeriveEraExposure, DeriveEraExposures, DeriveEraPointsAll } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
@@ -12,7 +12,7 @@ import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
function queryClipped (api: ApiInterfaceRx, withActive?: boolean): Observable<[EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]][]> {
function queryClipped (api: ApiInterfaceRx, withActive?: boolean): Observable<[EraIndex, RewardPoint, Balance, [AccountId, Exposure, RewardPoint][]][]> {
return api.derive.staking.erasPoints(withActive).pipe(
switchMap((allPoints): Observable<[DeriveEraPointsAll[], [StorageKey, Exposure][][]]> =>
combineLatest([
@@ -26,10 +26,11 @@ function queryClipped (api: ApiInterfaceRx, withActive?: boolean): Observable<[E
)
])
),
map(([allPoints, erasStakers]): [EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]][] =>
allPoints.map(({ all, era, eraPoints }, index): [EraIndex, RewardPoint, [AccountId, Exposure, RewardPoint][]] => [
map(([allPoints, erasStakers]): [EraIndex, RewardPoint, Balance, [AccountId, Exposure, RewardPoint][]][] =>
allPoints.map(({ all, era, eraPoints, eraReward }, index): [EraIndex, RewardPoint, Balance, [AccountId, Exposure, RewardPoint][]] => [
era,
eraPoints,
eraReward,
erasStakers[index].map(([key, exposure]): [AccountId, Exposure, RewardPoint] => [
key.args[1] as AccountId,
exposure,
@@ -44,7 +45,7 @@ export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Ob
return memo((withActive?: boolean): Observable<DeriveEraExposure[]> =>
queryClipped(api, withActive).pipe(
map((clipped): DeriveEraExposure[] =>
clipped.map(([era, eraPoints, validators]): DeriveEraExposure => ({
clipped.map(([era, eraPoints, eraReward, validators]): DeriveEraExposure => ({
all: validators.reduce((all: DeriveEraExposures, [validtorId, exposure, points]): DeriveEraExposures => {
all[validtorId.toString()] = {
exposure,
@@ -54,7 +55,8 @@ export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Ob
return all;
}, {}),
era,
eraPoints
eraPoints,
eraReward
}))
)
)
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ActiveEraInfo, EraIndex, EraRewardPoints, RewardPoint } from '@polkadot/types/interfaces';
import { ActiveEraInfo, Balance, EraIndex, EraRewardPoints, RewardPoint } from '@polkadot/types/interfaces';
import { DeriveEraPointsAll } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
@@ -39,17 +39,20 @@ function getAvailableIndexes (api: ApiInterfaceRx, withActive?: boolean): Observ
export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPointsAll[]> {
return memo((withActive?: boolean): Observable<DeriveEraPointsAll[]> =>
getAvailableIndexes(api, withActive).pipe(
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[]]> =>
switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[], Option<Balance>[]]> =>
combineLatest([
of(indexes),
indexes.length
? api.query.staking.erasRewardPoints.multi<EraRewardPoints>(indexes)
: of([]),
indexes.length
? api.query.staking.erasValidatorReward.multi<Option<Balance>>(indexes)
: of([])
])
),
map(([eras, rewards]): DeriveEraPointsAll[] =>
map(([eras, points, rewards]): DeriveEraPointsAll[] =>
eras.map((era, index): DeriveEraPointsAll => ({
all: [...rewards[index].individual.entries()]
all: [...points[index].individual.entries()]
.filter(([, points]): boolean => points.gtn(0))
.reduce((all: Record<string, RewardPoint>, [validatorId, points]): Record<string, RewardPoint> => {
all[validatorId.toString()] = points;
@@ -57,7 +60,8 @@ export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Obse
return all;
}, {}),
era,
eraPoints: rewards[index].total
eraPoints: points[index].total,
eraReward: rewards[index].unwrapOrDefault()
}))
)
)
@@ -14,7 +14,7 @@ export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Obs
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
api.derive.staking.erasExposure(withActive).pipe(
map((exposures): DeriveEraRewardsAll[] =>
exposures.map(({ all, era, eraPoints }): DeriveEraRewardsAll =>
exposures.map(({ all, era, eraPoints, eraReward }): DeriveEraRewardsAll =>
Object
.entries(all)
.reduce((rewards: DeriveEraRewardsAll, [validatorId, data]): DeriveEraRewardsAll => {
@@ -28,7 +28,7 @@ export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Obs
});
return rewards;
}, { era, eraPoints, nominators: {}, validators: {} })
}, { era, eraPoints, eraReward, nominators: {}, validators: {} })
)
)
)
@@ -16,7 +16,7 @@ export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | st
return api.derive.staking.erasRewards(withActive).pipe(
map((rewards): DeriveStakerReward[] =>
rewards.map(({ era, eraPoints, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
rewards.map(({ era, eraPoints, eraReward, nominators: allNominators, validators: allValidators }): DeriveStakerReward => {
const isValidator = !!allValidators[stakerId];
const validators: DeriveEraExposures = {};
let nominating: [string, number][] = [];
@@ -31,7 +31,7 @@ export function stakerRewards (api: ApiInterfaceRx): (accountId: Uint8Array | st
});
}
return { era, eraPoints, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
return { era, eraPoints, eraReward, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
})
)
);
+10 -14
View File
@@ -5,15 +5,17 @@
import { AccountId, Balance, BlockNumber, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
import { DeriveSessionIndexes } from '../session/types';
export interface DeriveEraPointsAll {
all: Record<string, RewardPoint>;
interface EraPointInfo {
era: EraIndex;
eraPoints: RewardPoint;
eraReward: Balance;
}
export interface DeriveEraPoints {
era: EraIndex;
eraPoints: RewardPoint;
export interface DeriveEraPointsAll extends EraPointInfo {
all: Record<string, RewardPoint>;
}
export interface DeriveEraPoints extends EraPointInfo {
own: RewardPoint;
}
@@ -22,22 +24,16 @@ export type DeriveEraExposures = Record<string, {
points: RewardPoint;
}>;
export interface DeriveEraExposure {
export interface DeriveEraExposure extends EraPointInfo {
all: DeriveEraExposures;
era: EraIndex;
eraPoints: RewardPoint;
}
export interface DeriveEraRewardsAll {
era: EraIndex;
eraPoints: RewardPoint;
export interface DeriveEraRewardsAll extends EraPointInfo {
nominators: Record<string, [string, number][]>;
validators: DeriveEraExposures;
}
export interface DeriveStakerReward {
era: EraIndex;
eraPoints: RewardPoint;
export interface DeriveStakerReward extends EraPointInfo {
isEmpty: boolean;
isValidator: boolean;
nominating: [string, number][];