diff --git a/packages/api-derive/src/staking/erasExposure.ts b/packages/api-derive/src/staking/erasExposure.ts index ceb6b5908d..ba7b0ea665 100644 --- a/packages/api-derive/src/staking/erasExposure.ts +++ b/packages/api-derive/src/staking/erasExposure.ts @@ -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 => 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 })) ) ) diff --git a/packages/api-derive/src/staking/erasPoints.ts b/packages/api-derive/src/staking/erasPoints.ts index c2721dc9a8..4f2436890c 100644 --- a/packages/api-derive/src/staking/erasPoints.ts +++ b/packages/api-derive/src/staking/erasPoints.ts @@ -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 { return memo((withActive?: boolean): Observable => getAvailableIndexes(api, withActive).pipe( - switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[]]> => + switchMap((indexes): Observable<[EraIndex[], EraRewardPoints[], Option[]]> => combineLatest([ of(indexes), indexes.length ? api.query.staking.erasRewardPoints.multi(indexes) + : of([]), + indexes.length + ? api.query.staking.erasValidatorReward.multi>(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, [validatorId, points]): Record => { 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() })) ) ) diff --git a/packages/api-derive/src/staking/erasRewards.ts b/packages/api-derive/src/staking/erasRewards.ts index fd10296f9c..91e69e64c0 100644 --- a/packages/api-derive/src/staking/erasRewards.ts +++ b/packages/api-derive/src/staking/erasRewards.ts @@ -14,7 +14,7 @@ export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Obs return memo((withActive?: boolean): Observable => 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: {} }) ) ) ) diff --git a/packages/api-derive/src/staking/stakerRewards.ts b/packages/api-derive/src/staking/stakerRewards.ts index a5b5ae515f..c7f0a776cb 100644 --- a/packages/api-derive/src/staking/stakerRewards.ts +++ b/packages/api-derive/src/staking/stakerRewards.ts @@ -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 }; }) ) ); diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index caa9b39b40..d27f12fa9b 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -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; +interface EraPointInfo { era: EraIndex; eraPoints: RewardPoint; + eraReward: Balance; } -export interface DeriveEraPoints { - era: EraIndex; - eraPoints: RewardPoint; +export interface DeriveEraPointsAll extends EraPointInfo { + all: Record; +} + +export interface DeriveEraPoints extends EraPointInfo { own: RewardPoint; } @@ -22,22 +24,16 @@ export type DeriveEraExposures = Record; -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; validators: DeriveEraExposures; } -export interface DeriveStakerReward { - era: EraIndex; - eraPoints: RewardPoint; +export interface DeriveStakerReward extends EraPointInfo { isEmpty: boolean; isValidator: boolean; nominating: [string, number][];