Additional types for latest substrate master (#1016)
* Additional types for latest substrate master * Method tests updated for new structure * Adjust extrinsic test indexes * Adjust type-extrinsics tests * Adapt for new session/era calc * Small comment update * Update with patch for system * Adjust tests with system calls in metadata
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import { DerivedSessionInfo } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable, combineLatest } from 'rxjs';
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { BlockNumber, Option, StructAny } from '@polkadot/types';
|
||||
@@ -13,12 +13,13 @@ import { BlockNumber, Option, StructAny } from '@polkadot/types';
|
||||
import { drr } from '../util/drr';
|
||||
import { bestNumber } from '../chain';
|
||||
|
||||
type Result = [BN, [BlockNumber, Option<BlockNumber>, BlockNumber, BlockNumber, BlockNumber]];
|
||||
type Result0_94 = [BN, [BN, Option<BlockNumber>, BN, BN, BN]];
|
||||
type Result = [BN, [BN, BN]];
|
||||
|
||||
const ZERO = new BN(0);
|
||||
|
||||
// internal helper to just split the logic - take all inputs, do the calculations and combine
|
||||
function createDerived ([bestNumber, [currentIndex, _lastLengthChange, sessionLength, lastEraLengthChange, sessionsPerEra]]: Result): DerivedSessionInfo {
|
||||
function createDerived0_94 ([bestNumber, [currentIndex, _lastLengthChange, sessionLength, lastEraLengthChange, sessionsPerEra]]: Result0_94): DerivedSessionInfo {
|
||||
const eraLength = sessionLength.mul(sessionsPerEra);
|
||||
const lastLengthChange = _lastLengthChange
|
||||
? _lastLengthChange.unwrapOr(ZERO)
|
||||
@@ -27,13 +28,15 @@ function createDerived ([bestNumber, [currentIndex, _lastLengthChange, sessionLe
|
||||
.sub(lastLengthChange)
|
||||
.add(sessionLength)
|
||||
.mod(sessionLength);
|
||||
const eraProgress = (currentIndex)
|
||||
const currentEra = (currentIndex)
|
||||
.sub(lastEraLengthChange)
|
||||
.mod(sessionsPerEra)
|
||||
.mod(sessionsPerEra);
|
||||
const eraProgress = currentEra
|
||||
.mul(sessionLength)
|
||||
.add(sessionProgress);
|
||||
|
||||
return new StructAny({
|
||||
currentEra,
|
||||
currentIndex,
|
||||
eraLength,
|
||||
eraProgress,
|
||||
@@ -45,24 +48,53 @@ function createDerived ([bestNumber, [currentIndex, _lastLengthChange, sessionLe
|
||||
}) as DerivedSessionInfo;
|
||||
}
|
||||
|
||||
function createDerived ([sessionsPerEra, [currentIndex, currentEra]]: Result): DerivedSessionInfo {
|
||||
const eraProgress = (currentIndex).mod(sessionsPerEra);
|
||||
|
||||
return new StructAny({
|
||||
currentEra,
|
||||
currentIndex,
|
||||
eraLength: ZERO,
|
||||
eraProgress,
|
||||
lastEraLengthChange: ZERO,
|
||||
lastLengthChange: ZERO,
|
||||
sessionLength: ZERO,
|
||||
sessionsPerEra,
|
||||
sessionProgress: ZERO
|
||||
}) as DerivedSessionInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves all the session and era info and calculates specific valus on it sunh as the length of the session and eras
|
||||
*/
|
||||
export function info (api: ApiInterface$Rx) {
|
||||
return (): Observable<DerivedSessionInfo> =>
|
||||
// This is a much more optimal way to calculate since we only make a single call to the RPC backend
|
||||
// instead of making a subscription for each of the params (this means all others in session use)
|
||||
(combineLatest([
|
||||
bestNumber(api)(),
|
||||
return (): Observable<DerivedSessionInfo> => {
|
||||
// with 94, the era and session has been explicitly exposed, pre-94
|
||||
// we had more info and needed to calculate (handle old/Alex first)
|
||||
return api.query.session.lastLengthChange
|
||||
? (combineLatest([
|
||||
bestNumber(api)(),
|
||||
api.queryMulti([
|
||||
api.query.session.currentIndex,
|
||||
api.query.session.lastLengthChange,
|
||||
api.query.session.sessionLength,
|
||||
api.query.staking.lastEraLengthChange,
|
||||
api.query.staking.sessionsPerEra
|
||||
])
|
||||
]) as any as Observable<Result0_94>).pipe(
|
||||
map(createDerived0_94),
|
||||
drr()
|
||||
)
|
||||
: (combineLatest([
|
||||
// sessionsPerEra, hardcoded, due to https://github.com/paritytech/substrate/pull/2802/files#diff-5e5e1c3aec9ddfde0a9054d062ab3db9R156
|
||||
of(new BN(6)),
|
||||
api.queryMulti([
|
||||
api.query.session.currentIndex,
|
||||
api.query.session.lastLengthChange,
|
||||
api.query.session.sessionLength,
|
||||
api.query.staking.lastEraLengthChange,
|
||||
api.query.staking.sessionsPerEra
|
||||
api.query.staking.currentEra
|
||||
])
|
||||
]) as any as Observable<Result>).pipe(
|
||||
map(createDerived),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { AccountId, Balance, BlockNumber, Exposure, Index, RewardDestination, StakingLedger, StructAny, ValidatorPrefs, Vote } from '@polkadot/types';
|
||||
import { AccountId, Balance, Exposure, Index, RewardDestination, StakingLedger, StructAny, ValidatorPrefs, Vote } from '@polkadot/types';
|
||||
|
||||
export interface DerivedBalances extends StructAny {
|
||||
accountId: AccountId;
|
||||
@@ -48,13 +48,14 @@ export interface DerivedReferendumVote extends StructAny {
|
||||
}
|
||||
|
||||
export interface DerivedSessionInfo extends StructAny {
|
||||
currentIndex: BlockNumber;
|
||||
currentEra: BN;
|
||||
currentIndex: BN;
|
||||
eraLength: BN;
|
||||
eraProgress: BN;
|
||||
lastEraLengthChange: BlockNumber;
|
||||
lastEraLengthChange: BN;
|
||||
lastLengthChange: BN;
|
||||
sessionLength: BlockNumber;
|
||||
sessionsPerEra: BlockNumber;
|
||||
sessionLength: BN;
|
||||
sessionsPerEra: BN;
|
||||
sessionProgress: BN;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user