From f9605cd710410c1df529445622a5ba44c6f00420 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 15 Jun 2019 23:37:24 +0200 Subject: [PATCH] Update nextKeyFor to cater for SessionKeys or SessionKey (#1025) * Update nextKeyFor to cater for SessionKeys or SessionKey * no StructAny (yes, done in other PR, but we are touching this...) --- packages/api-derive/src/staking/info.ts | 53 +++++++++++++++---------- packages/api-derive/src/types.ts | 2 +- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index 23ffd77b18..a87c13a9aa 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -3,17 +3,18 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId, BlockNumber, Exposure, Option, RewardDestination, StakingLedger, StructAny, ValidatorPrefs, UnlockChunk } from '@polkadot/types'; import { DerivedStaking, DerivedUnlocking } from '../types'; import BN from 'bn.js'; import { combineLatest, Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; +import { AccountId, BlockNumber, Exposure, Keys, Option, RewardDestination, SessionKey, SessionKeys, StakingLedger, ValidatorPrefs, UnlockChunk } from '@polkadot/types'; + +import { isUndefined } from '@polkadot/util'; import { bestNumber } from '../chain/bestNumber'; import { drr } from '../util/drr'; import { eraLength } from '../session/eraLength'; -import { isUndefined } from '@polkadot/util'; function calculateUnlocking (stakingLedger: StakingLedger | undefined, eraLength: BN, bestNumber: BlockNumber): DerivedUnlocking | undefined { if (isUndefined(stakingLedger)) { @@ -69,6 +70,15 @@ function remainingBlocks (era: BN, eraLength: BN, bestNumber: BlockNumber) { return remaining.lten(0) ? new BN(0) : remaining; } +function nextSessionId (_nextKeyFor: Option): AccountId | undefined { + const nextKeyFor: SessionKeys | SessionKey | null = _nextKeyFor.unwrapOr(null); + + // For substrate 2.x, nextKeyFor is SessionKeys/Keys, for 1.x it is SessionKey + return nextKeyFor + ? (nextKeyFor as SessionKeys).sessionKey || nextKeyFor + : undefined; +} + function withStashController (api: ApiInterface$Rx, accountId: AccountId, controllerId: AccountId): Observable { const stashId = accountId; @@ -84,15 +94,15 @@ function withStashController (api: ApiInterface$Rx, accountId: AccountId, contro [api.query.staking.stakers, stashId], [api.query.staking.validators, stashId] ]) - ]) as any as Observable<[BN, BlockNumber, [Option, Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]]> + ]) as any as Observable<[BN, BlockNumber, [Option, Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]]> ).pipe( - map(([eraLength, bestNumber,[nextKeyFor, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs]]]) => { - const stakingLedger = _stakingLedger.isSome ? _stakingLedger.unwrap() : undefined; + map(([eraLength, bestNumber, [nextKeyFor, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs]]]) => { + const stakingLedger = _stakingLedger.unwrapOr(null) || undefined; - return new StructAny({ + return { accountId, controllerId, - nextSessionId: nextKeyFor.unwrapOr(null) || undefined, + nextSessionId: nextSessionId(nextKeyFor), nominators, redeemable: redeemableSum(stakingLedger, eraLength, bestNumber), rewardDestination, @@ -101,7 +111,7 @@ function withStashController (api: ApiInterface$Rx, accountId: AccountId, contro stashId, unlocking: calculateUnlocking(stakingLedger, eraLength, bestNumber), validatorPrefs - }) as DerivedStaking; + } as DerivedStaking; }), drr() ); @@ -118,20 +128,19 @@ function withControllerLedger (api: ApiInterface$Rx, accountId: AccountId, staki [api.query.staking.payee, stashId], [api.query.staking.stakers, stashId], [api.query.staking.validators, stashId] - ]) as any as Observable<[Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]> + ]) as any as Observable<[Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]> ).pipe( - map(([nextKeyFor, [nominators], rewardDestination, stakers, [validatorPrefs]]) => - new StructAny({ - accountId, - controllerId, - nextSessionId: nextKeyFor.unwrapOr(null) || undefined, - nominators, - rewardDestination, - stakers, - stakingLedger, - stashId, - validatorPrefs - }) as DerivedStaking), + map(([nextKeyFor, [nominators], rewardDestination, stakers, [validatorPrefs]]) => ({ + accountId, + controllerId, + nextSessionId: nextSessionId(nextKeyFor), + nominators, + rewardDestination, + stakers, + stakingLedger, + stashId, + validatorPrefs + } as DerivedStaking)), drr() ); } @@ -157,7 +166,7 @@ export function info (api: ApiInterface$Rx) { stakingLedger.isSome ? withControllerLedger(api, accountId, stakingLedger.unwrap()) // dangit, this is something else, ok, we are done - : of(new StructAny({ accountId }) as DerivedStaking) + : of({ accountId } as DerivedStaking) ) ), drr() diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index a851a060cc..be7e225215 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -59,7 +59,7 @@ export interface DerivedSessionInfo extends StructAny { sessionProgress: BN; } -export interface DerivedStaking extends StructAny { +export interface DerivedStaking { accountId: AccountId; controllerId?: AccountId; nextSessionId?: AccountId;