diff --git a/CHANGELOG.md b/CHANGELOG.md index 521867ab25..841e86664c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Changes: - Added `api.{connect, disconnect}()` as well as `isConnected` interfaces. The first functions returning `Promise` - Error on provider connections will now emit and error (as expected) via the event emitter - Add `api.derive.accounts.accountId` to perform AccountId lookups (from indices or actual AccountId) +- The `api.derive.staking.query/queryMulti` no longer retrieves session keys (can be done via `.keys/keysMulti`) - Lessen load of `paymentInfo` queries to only use accounId mappings as available - Adjust staking derives to cater for early Substrate 2.0 chains (optional/non-optional EraIndexes) - Cater for the handling of nested aliased types, e.g. wrapped inside Vec or Tuple diff --git a/packages/api-derive/src/staking/account.ts b/packages/api-derive/src/staking/account.ts index 638bf17f20..1591b0e228 100644 --- a/packages/api-derive/src/staking/account.ts +++ b/packages/api-derive/src/staking/account.ts @@ -4,10 +4,10 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { Balance, StakingLedger, UnlockChunk } from '@polkadot/types/interfaces'; -import { DeriveSessionInfo, DeriveStakingAccount, DeriveStakingQuery, DeriveUnlocking } from '../types'; +import { DeriveSessionInfo, DeriveStakingAccount, DeriveStakingKeys, DeriveStakingQuery, DeriveUnlocking } from '../types'; import BN from 'bn.js'; -import { Observable } from 'rxjs'; +import { combineLatest, Observable } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { BN_ZERO } from '@polkadot/util'; @@ -46,8 +46,9 @@ function redeemableSum (api: ApiInterfaceRx, stakingLedger: StakingLedger | unde }, new BN(0))); } -function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query: DeriveStakingQuery): DeriveStakingAccount { +function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, keys: DeriveStakingKeys, query: DeriveStakingQuery): DeriveStakingAccount { return { + ...keys, ...query, redeemable: redeemableSum(api, query.stakingLedger, sessionInfo), unlocking: calculateUnlocking(api, query.stakingLedger, sessionInfo) @@ -61,8 +62,11 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds: return memo(instanceId, (accountIds: (Uint8Array | string)[]): Observable => api.derive.session.info().pipe( switchMap((sessionInfo) => - api.derive.staking.queryMulti(accountIds).pipe( - map((queries) => queries.map((query) => parseResult(api, sessionInfo, query))) + combineLatest([ + api.derive.staking.keysMulti(accountIds), + api.derive.staking.queryMulti(accountIds) + ]).pipe( + map(([keys, queries]) => queries.map((query, index) => parseResult(api, sessionInfo, keys[index], query))) ) ) ) diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index a828db7657..8b4a96e125 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -11,6 +11,7 @@ export * from './erasPrefs'; export * from './erasRewards'; export * from './erasSlashes'; export * from './electedInfo'; +export * from './keys'; export * from './overview'; export * from './ownExposure'; export * from './ownSlashes'; diff --git a/packages/api-derive/src/staking/keys.ts b/packages/api-derive/src/staking/keys.ts new file mode 100644 index 0000000000..135e87da58 --- /dev/null +++ b/packages/api-derive/src/staking/keys.ts @@ -0,0 +1,52 @@ +// 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 { AccountId, Keys } from '@polkadot/types/interfaces'; +import { ITuple } from '@polkadot/types/types'; +import { DeriveStakingKeys } from './types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import { Option, Vec } from '@polkadot/types'; + +import { memo } from '../util'; + +function extractsIds (stashId: Uint8Array | string, queuedKeys: [AccountId, Keys][], nextKeys: Option): DeriveStakingKeys { + const sessionIds = (queuedKeys.find(([currentId]) => currentId.eq(stashId)) || [undefined, [] as AccountId[]])[1]; + const nextSessionIds = nextKeys.unwrapOr([] as AccountId[]); + + return { + nextSessionIds, + sessionIds + }; +} + +export function keys (instanceId: string, api: ApiInterfaceRx): (stashId: Uint8Array | string) => Observable { + return memo(instanceId, (stashId: Uint8Array | string): Observable => + api.derive.staking.keysMulti([stashId]).pipe( + map(([first]) => first) + ) + ); +} + +export function keysMulti (instanceId: string, api: ApiInterfaceRx): (stashIds: (Uint8Array | string)[]) => Observable { + return memo(instanceId, (stashIds: (Uint8Array | string)[]): Observable => + stashIds.length + ? api.query.session.queuedKeys>>().pipe( + switchMap((queuedKeys): Observable<[Vec>, Option[]]> => + combineLatest([ + of(queuedKeys), + api.consts.session?.dedupKeyPrefix + ? api.query.session.nextKeys.multi>(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId])) + : api.query.session.nextKeys.multi>(stashIds) + ]) + ), + map(([queuedKeys, nextKeys]) => + stashIds.map((stashId, index) => extractsIds(stashId, queuedKeys, nextKeys[index])) + ) + ) + : of([]) + ); +} diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index 76ff24abeb..d7fbb76c7a 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -3,32 +3,20 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, EraIndex, Exposure, Keys, Nominations, RewardDestination, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; +import { AccountId, EraIndex, Exposure, Nominations, RewardDestination, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; import { ITuple } from '@polkadot/types/types'; import { DeriveStakingQuery } from '../types'; import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; -import { Option, Vec } from '@polkadot/types'; +import { Option } from '@polkadot/types'; import { isFunction } from '@polkadot/util'; import { memo } from '../util'; -type MultiResult = [Option, Option | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Option, Exposure]; +type MultiResult = [Option, Option | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Exposure]; -function parseSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][], nextKeys: Option): { nextSessionIds: AccountId[]; sessionIds: AccountId[] } { - const sessionIds = (queuedKeys.find(([currentId]): boolean => - currentId.eq(stashId) - ) || [undefined, [] as AccountId[]])[1]; - const nextSessionIds = nextKeys.unwrapOr([] as AccountId[]); - - return { - nextSessionIds, - sessionIds - }; -} - -function parseController (stashId: AccountId, queuedKeys: Vec>, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]: MultiResult, stakingLedgerOpt: Option): DeriveStakingQuery { +function parseController (stashId: AccountId, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure]: MultiResult, stakingLedgerOpt: Option): DeriveStakingQuery { const controllerId = controllerIdOpt.unwrapOr(null); const nominators = nominatorsOpt.unwrapOr(null); @@ -47,10 +35,9 @@ function parseController (stashId: AccountId, queuedKeys: Vec { @@ -59,7 +46,6 @@ function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable api.registry.createType('Option'))), api.query.staking.payee.multi(stashIds), api.query.staking.validators.multi(stashIds), - api.consts.session?.dedupKeyPrefix - ? api.query.session.nextKeys.multi>(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId])) - : api.query.session.nextKeys.multi>(stashIds), api.query.staking.erasStakers.multi(stashIds.map((stashId) => [activeEra, stashId])) ]).pipe( - map(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]): MultiResult[] => + map(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure]): MultiResult[] => controllerIdOpt.map((controllerIdOpt, index): MultiResult => - [controllerIdOpt, nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], nextKeys[index], exposure[index]] + [controllerIdOpt, nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], exposure[index]] ) ) ); @@ -119,11 +102,8 @@ export function query (instanceId: string, api: ApiInterfaceRx): (accountId: Uin export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable { return memo(instanceId, (accountIds: (Uint8Array | string)[]): Observable => accountIds.length - ? combineLatest([ - api.query.session.queuedKeys>>(), - api.derive.session.indexes() - ]).pipe( - switchMap(([queuedKeys, { activeEra }]): Observable => { + ? api.derive.session.indexes().pipe( + switchMap(({ activeEra }): Observable => { const stashIds = accountIds.map((accountId) => api.registry.createType('AccountId', accountId)); return ( @@ -134,7 +114,7 @@ export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountId switchMap((results): Observable => retrieveControllers(api, results.map(([optController]) => optController)).pipe( map((stakingLedgerOpts) => - stashIds.map((stashId, index) => parseController(stashId, queuedKeys, results[index], stakingLedgerOpts[index])) + stashIds.map((stashId, index) => parseController(stashId, results[index], stakingLedgerOpts[index])) ) ) ) diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index c779414b76..50e8bc2cb5 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -91,6 +91,11 @@ export interface DeriveStakerSlashes { export type DeriveOwnSlashes = DeriveStakerSlashes; +export interface DeriveStakingKeys { + nextSessionIds: AccountId[]; + sessionIds: AccountId[]; +} + export interface DeriveStakingValidators { nextElected: AccountId[]; validators: AccountId[]; @@ -109,8 +114,6 @@ export interface DeriveStakingStash { export interface DeriveStakingQuery extends DeriveStakingStash { accountId: AccountId; - nextSessionIds: AccountId[]; - sessionIds: AccountId[]; stakingLedger?: StakingLedger; } @@ -129,7 +132,7 @@ export interface DeriveUnlocking { value: Balance; } -export interface DeriveStakingAccount extends DeriveStakingQuery { +export interface DeriveStakingAccount extends DeriveStakingQuery, DeriveStakingKeys { redeemable?: Balance; unlocking?: DeriveUnlocking[]; }