diff --git a/packages/api-derive/src/staking/currentPoints.ts b/packages/api-derive/src/staking/currentPoints.ts new file mode 100644 index 0000000000..7c99685405 --- /dev/null +++ b/packages/api-derive/src/staking/currentPoints.ts @@ -0,0 +1,42 @@ +// 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 { AccountId, EraPoints, EraRewardPoints, RewardPoint } from '@polkadot/types/interfaces'; +import { ApiInterfaceRx } from '@polkadot/api/types'; + +import { Observable, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +function retrievePointsPrev (api: ApiInterfaceRx, currentElected: AccountId[]): Observable { + return api.query.staking.currentEraPointsEarned().pipe( + map(({ individual, total }): EraRewardPoints => + api.registry.createType('EraRewardPoints', { + individual: new Map( + individual + .map((points): RewardPoint => api.registry.createType('RewardPoint', points)) + .map((points, index): [AccountId, RewardPoint] => [currentElected[index], points]) + ), + total + }) + ) + ); +} + +/** + * @description Retrieve the staking overview, including elected and points earned + */ +export function currentPoints (api: ApiInterfaceRx): () => Observable { + return memo((): Observable => + api.derive.staking.overview().pipe( + switchMap(({ activeEra, nextElected }) => + api.query.staking.erasRewardPoints + ? api.query.staking.erasRewardPoints(activeEra) + : api.query.staking.currentEraPointsEarned + ? retrievePointsPrev(api, nextElected) + : of(api.registry.createType('EraRewardPoints')) + ) + )); +} diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index 3509a96d8e..0b6eff83d3 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -3,6 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. export * from './account'; +export * from './currentPoints'; export * from './erasExposure'; export * from './erasHistoric'; export * from './erasPoints'; diff --git a/packages/api-derive/src/staking/overview.ts b/packages/api-derive/src/staking/overview.ts index a5715efaab..7dccfd640e 100644 --- a/packages/api-derive/src/staking/overview.ts +++ b/packages/api-derive/src/staking/overview.ts @@ -2,30 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, EraPoints, EraRewardPoints, RewardPoint } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { DeriveStakingOverview } from '../types'; -import { Observable, combineLatest, of } from 'rxjs'; -import { map, switchMap } from 'rxjs/operators'; +import { Observable, combineLatest } from 'rxjs'; +import { map } from 'rxjs/operators'; import { memo } from '../util'; -function retrievePointsPrev (api: ApiInterfaceRx, currentElected: AccountId[]): Observable { - return api.query.staking.currentEraPointsEarned().pipe( - map(({ individual, total }): EraRewardPoints => - api.registry.createType('EraRewardPoints', { - individual: new Map( - individual - .map((points): RewardPoint => api.registry.createType('RewardPoint', points)) - .map((points, index): [AccountId, RewardPoint] => [currentElected[index], points]) - ), - total - }) - ) - ); -} - /** * @description Retrieve the staking overview, including elected and points earned */ @@ -35,18 +19,10 @@ export function overview (api: ApiInterfaceRx): () => Observable - combineLatest([ - of({ ...indexes, nextElected, validators }), - api.query.staking.erasRewardPoints - ? api.query.staking.erasRewardPoints(indexes.activeEra) - : api.query.staking.currentEraPointsEarned - ? retrievePointsPrev(api, nextElected) - : of(api.registry.createType('EraRewardPoints')) - ]) - ), - map(([info, eraPoints]): DeriveStakingOverview => ({ - ...info, eraPoints + map(([indexes, { nextElected, validators }]): DeriveStakingOverview => ({ + ...indexes, + nextElected, + validators })) )); } diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 597cbc64d0..df1bbb24da 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, Balance, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; +import { AccountId, Balance, EraIndex, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; import { DeriveSessionIndexes } from '../session/types'; import BN from 'bn.js'; @@ -127,7 +127,6 @@ export interface DeriveStakingAccount extends DeriveStakingQuery { } export interface DeriveStakingOverview extends DeriveSessionIndexes { - eraPoints: EraRewardPoints; nextElected: AccountId[]; validators: AccountId[]; }