// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Option, u32 } from '@polkadot/types'; import type { ActiveEraInfo, EraIndex, Moment, SessionIndex } from '@polkadot/types/interfaces'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveSessionIndexes } from '../types'; import { of } from '@polkadot/x-rxjs'; import { map } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; // parse into Indexes function parse ([currentIndex, activeEra, activeEraStart, currentEra, validatorCount]: [SessionIndex, EraIndex, Option, EraIndex, u32]): DeriveSessionIndexes { return { activeEra, activeEraStart, currentEra, currentIndex, validatorCount }; } // query based on latest function queryStaking (api: ApiInterfaceRx): Observable { return api.queryMulti<[SessionIndex, Option, Option, u32]>([ api.query.session.currentIndex, api.query.staking.activeEra, api.query.staking.currentEra, api.query.staking.validatorCount ]).pipe( map(([currentIndex, activeOpt, currentEra, validatorCount]): DeriveSessionIndexes => { const { index, start } = activeOpt.unwrapOrDefault(); return parse([ currentIndex, index, start, currentEra.unwrapOrDefault(), validatorCount ]); }) ); } // query based on latest function querySession (api: ApiInterfaceRx): Observable { return api.query.session.currentIndex().pipe( map((currentIndex): DeriveSessionIndexes => parse([ currentIndex, api.registry.createType('EraIndex'), api.registry.createType('Option'), api.registry.createType('EraIndex'), api.registry.createType('u32') ])) ); } // empty set when none is available function empty (api: ApiInterfaceRx): Observable { return of(parse([ api.registry.createType('SessionIndex', 1), api.registry.createType('EraIndex'), api.registry.createType('Option'), api.registry.createType('EraIndex'), api.registry.createType('u32') ])); } export function indexes (instanceId: string, api: ApiInterfaceRx): () => Observable { return memo(instanceId, (): Observable => api.query.session ? api.query.staking ? queryStaking(api) : querySession(api) : empty(api) ); }