From 724c4b8263df1a71d7ef5b607c4fd8467392cc34 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 6 Aug 2019 16:34:11 +0200 Subject: [PATCH] Return sessionIds & nextSessionIds in staking.info (#1272) * Return sessionIds & nextSessionIds in staking.info * Simplify --- packages/api-derive/src/staking/info.ts | 29 +++++++++++-------- packages/api-derive/src/types.ts | 4 +++ .../e2e/api/promise-queries-doubleMap.spec.ts | 10 +++---- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index f16587f0d8..c53f780fd2 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -84,27 +84,32 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN, ); } -function unwrapSessionIds (stashId: AccountId, queuedKeys: Option | Vec<[AccountId, Keys] & Codec>, nextKeys: Option): { nextSessionId?: AccountId; sessionId?: AccountId } { +function unwrapSessionIds (stashId: AccountId, queuedKeys: Option | Vec<[AccountId, Keys] & Codec>, nextKeys: Option): { nextSessionIds: AccountId[]; nextSessionId?: AccountId; sessionIds: AccountId[]; sessionId?: AccountId } { // for 2.x we have a Vec<(ValidatorId,Keys)> of the keys if (Array.isArray(queuedKeys)) { - const [, { ed25519: sessionId }] = queuedKeys.find(([currentId]): boolean => currentId.eq(stashId)) || [undefined, { ed25519: undefined }]; + const sessionIds = (queuedKeys.find(([currentId]): boolean => currentId.eq(stashId)) || [undefined, { toArray: (): AccountId[] => [] }])[1].toArray() as AccountId[]; + const nextSessionIds = nextKeys.isSome + ? nextKeys.unwrap().toArray() as AccountId[] + : []; return { - nextSessionId: nextKeys.isSome - ? nextKeys.unwrap().ed25519 - : undefined, - sessionId + nextSessionId: nextSessionIds[0], + nextSessionIds, + sessionId: sessionIds[0], + sessionIds }; } // substrate 1.x - const nextSessionId = queuedKeys.isSome - ? queuedKeys.unwrap() - : undefined; + const nextSessionIds = queuedKeys.isSome + ? [queuedKeys.unwrap()] + : []; return { - nextSessionId, - sessionId: nextSessionId + nextSessionId: nextSessionIds[0], + nextSessionIds, + sessionId: nextSessionIds[0], + sessionIds: nextSessionIds }; } @@ -177,7 +182,7 @@ export function info (api: ApiInterfaceRx): (_accountId: Uint8Array | string) => ? retrieveInfo(api, accountId, controllerId.unwrap()) : stakingLedger.isSome ? retrieveInfo(api, stakingLedger.unwrap().stash, accountId) - : of({ accountId }) + : of({ accountId, nextSessionIds: [], sessionIds: [] }) ), drr() ); diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 2787a6c6c0..43af9cb09b 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -95,11 +95,15 @@ export interface DerivedStakingOnlineStatus { export interface DerivedStaking extends DerivedStakingOnlineStatus { accountId: AccountId; controllerId?: AccountId; + // @deprecated Use nextSessionIds instead nextSessionId?: AccountId; + nextSessionIds: AccountId[]; nominators?: AccountId[]; redeemable?: BN; rewardDestination?: RewardDestination; + // @deprecated Use sessionIds instead sessionId?: AccountId; + sessionIds: AccountId[]; stakers?: Exposure; stakingLedger?: StakingLedger; stashId?: AccountId; diff --git a/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts b/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts index 131ee979eb..ba93557e5b 100644 --- a/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts +++ b/packages/api/test/e2e/api/promise-queries-doubleMap.spec.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 { Header } from '@polkadot/types/interfaces'; +import { Header, Keys } from '@polkadot/types/interfaces'; import testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; @@ -106,15 +106,15 @@ describeE2E({ console.error('*** query.session.nextKeys'); const result = JSON.stringify( - await api.query.session.nextKeys( + (await api.query.session.nextKeys( api.consts.session.dedupKeyPrefix, keyring.bob.address - ) + )).toArray() ); - console.error(result, { ed25519, sr25519 }); + console.error(result, [ed25519, sr25519]); - expect(result).toEqual(JSON.stringify({ ed25519, sr25519 })); + expect(result).toEqual(JSON.stringify([ed25519, sr25519])); done(); }