Skip to content

Commit 58deb2e

Browse files
authored
Session index without staking (#3352)
* Session index without staking * Fix ordering
1 parent 83caad7 commit 58deb2e

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

packages/api-derive/src/session/indexes.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { map } from '@polkadot/x-rxjs/operators';
1313
import { memo } from '../util';
1414

1515
// parse into Indexes
16-
function parse ([activeEra, activeEraStart, currentEra, currentIndex, validatorCount]: [EraIndex, Option<Moment>, EraIndex, SessionIndex, u32]): DeriveSessionIndexes {
16+
function parse ([currentIndex, activeEra, activeEraStart, currentEra, validatorCount]: [SessionIndex, EraIndex, Option<Moment>, EraIndex, u32]): DeriveSessionIndexes {
1717
return {
1818
activeEra,
1919
activeEraStart,
@@ -24,42 +24,57 @@ function parse ([activeEra, activeEraStart, currentEra, currentIndex, validatorC
2424
}
2525

2626
// query based on latest
27-
function query (api: ApiInterfaceRx): Observable<DeriveSessionIndexes> {
28-
return api.queryMulti<[Option<ActiveEraInfo>, Option<EraIndex>, SessionIndex, u32]>([
27+
function queryStaking (api: ApiInterfaceRx): Observable<DeriveSessionIndexes> {
28+
return api.queryMulti<[SessionIndex, Option<ActiveEraInfo>, Option<EraIndex>, u32]>([
29+
api.query.session.currentIndex,
2930
api.query.staking.activeEra,
3031
api.query.staking.currentEra,
31-
api.query.session.currentIndex,
3232
api.query.staking.validatorCount
3333
]).pipe(
34-
map(([activeOpt, currentEra, currentIndex, validatorCount]): DeriveSessionIndexes => {
34+
map(([currentIndex, activeOpt, currentEra, validatorCount]): DeriveSessionIndexes => {
3535
const { index, start } = activeOpt.unwrapOrDefault();
3636

3737
return parse([
38+
currentIndex,
3839
index,
3940
start,
4041
currentEra.unwrapOrDefault(),
41-
currentIndex,
4242
validatorCount
4343
]);
4444
})
4545
);
4646
}
4747

48+
// query based on latest
49+
function querySession (api: ApiInterfaceRx): Observable<DeriveSessionIndexes> {
50+
return api.query.session.currentIndex().pipe(
51+
map((currentIndex): DeriveSessionIndexes => parse([
52+
currentIndex,
53+
api.registry.createType('EraIndex'),
54+
api.registry.createType('Option<Moment>'),
55+
api.registry.createType('EraIndex'),
56+
api.registry.createType('u32')
57+
]))
58+
);
59+
}
60+
4861
// empty set when none is available
4962
function empty (api: ApiInterfaceRx): Observable<DeriveSessionIndexes> {
5063
return of(parse([
64+
api.registry.createType('SessionIndex', 1),
5165
api.registry.createType('EraIndex'),
5266
api.registry.createType('Option<Moment>'),
5367
api.registry.createType('EraIndex'),
54-
api.registry.createType('SessionIndex', 1),
5568
api.registry.createType('u32')
5669
]));
5770
}
5871

5972
export function indexes (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveSessionIndexes> {
6073
return memo(instanceId, (): Observable<DeriveSessionIndexes> =>
61-
api.query.session && api.query.staking
62-
? query(api)
74+
api.query.session
75+
? api.query.staking
76+
? queryStaking(api)
77+
: querySession(api)
6378
: empty(api)
6479
);
6580
}

0 commit comments

Comments
 (0)