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...)
This commit is contained in:
@@ -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<Keys | SessionKey>): 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<DerivedStaking> {
|
||||
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<AccountId>, Option<StakingLedger>, [Array<AccountId>], RewardDestination, Exposure, [ValidatorPrefs]]]>
|
||||
]) as any as Observable<[BN, BlockNumber, [Option<Keys | SessionKey>, Option<StakingLedger>, [Array<AccountId>], 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<AccountId>, [Array<AccountId>], RewardDestination, Exposure, [ValidatorPrefs]]>
|
||||
]) as any as Observable<[Option<Keys | SessionKey>, [Array<AccountId>], 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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user