From 45c2c40a3e7f68543d8ec2fe86afd3399f0771b1 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 9 Sep 2020 13:31:06 +0200 Subject: [PATCH] derive.staking.stakerPrefs (#2594) --- CHANGELOG.md | 3 +- packages/api-derive/src/staking/index.ts | 1 + .../api-derive/src/staking/stakerPrefs.ts | 34 +++++++++++++++++++ packages/api-derive/src/staking/types.ts | 5 +++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/api-derive/src/staking/stakerPrefs.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 12859a6f84..2fc24e0eb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,9 @@ Changes: -- Optimize derive `receivedHeartbeats` to not re-create the full object - Allow `paymentInfo` on any extrinsic (already signed) with optional `{ blockHash: }` param for historic queries +- Optimize derive `receivedHeartbeats` to not re-create the full object +- Add `staking.stakerPrefs` derive to retrieve validatorPrefs over a range of eras ## 1.32.1 Sep 7, 2020 diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index 8b4a96e125..43dc7a0927 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -18,6 +18,7 @@ export * from './ownSlashes'; export * from './query'; export * from './stakerExposure'; export * from './stakerPoints'; +export * from './stakerPrefs'; export * from './stakerRewards'; export * from './stakerSlashes'; export * from './stashes'; diff --git a/packages/api-derive/src/staking/stakerPrefs.ts b/packages/api-derive/src/staking/stakerPrefs.ts new file mode 100644 index 0000000000..eb47797c9f --- /dev/null +++ b/packages/api-derive/src/staking/stakerPrefs.ts @@ -0,0 +1,34 @@ +// 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 { EraIndex, ValidatorPrefs } from '@polkadot/types/interfaces'; +import { DeriveStakerPrefs } from '../types'; + +import { Observable } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +export function _stakerPrefs (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => { + return api.query.staking.erasValidatorPrefs.multi(eras.map((era) => [era, accountId])).pipe( + map((all): DeriveStakerPrefs[] => + all.map((validatorPrefs, index): DeriveStakerPrefs => ({ + era: eras[index], + validatorPrefs + })) + ) + ); + }); +} + +export function stakerPrefs (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable { + return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable => + api.derive.staking.erasHistoric(withActive).pipe( + switchMap((eras) => api.derive.staking._stakerPrefs(accountId, eras, withActive)) + ) + ); +} diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 8ede11abfe..c66ba69cc7 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -69,6 +69,11 @@ export interface DeriveStakerExposure { validators: DeriveEraValidatorExposure; } +export interface DeriveStakerPrefs { + era: EraIndex; + validatorPrefs: ValidatorPrefs; +} + export interface DeriveStakerRewardValidator { total: Balance; value: Balance;