// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Option, u64 } from '@polkadot/types'; import type { SessionIndex } from '@polkadot/types/interfaces'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveSessionInfo, DeriveSessionProgress } from '../types'; import { combineLatest, of } from '@polkadot/x-rxjs'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; type ResultSlotsNoSession = [u64, u64, u64]; type ResultSlots = [u64, u64, u64, Option]; type ResultSlotsFlat = [u64, u64, u64, SessionIndex]; function createDerive (api: ApiInterfaceRx, info: DeriveSessionInfo, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]: ResultSlotsFlat): DeriveSessionProgress { const epochStartSlot = epochIndex.mul(info.sessionLength).iadd(epochOrGenesisStartSlot); const sessionProgress = currentSlot.sub(epochStartSlot); const eraProgress = info.currentIndex.sub(activeEraStartSessionIndex).imul(info.sessionLength).iadd(sessionProgress); return { ...info, eraProgress: api.registry.createType('BlockNumber', eraProgress), sessionProgress: api.registry.createType('BlockNumber', sessionProgress) }; } function queryAura (api: ApiInterfaceRx): Observable { return api.derive.session.info().pipe( map((info): DeriveSessionProgress => ({ ...info, eraProgress: api.registry.createType('BlockNumber'), sessionProgress: api.registry.createType('BlockNumber') })) ); } function queryBabe (api: ApiInterfaceRx): Observable<[DeriveSessionInfo, ResultSlotsFlat]> { return api.derive.session.info().pipe( switchMap((info): Observable<[DeriveSessionInfo, ResultSlots | ResultSlotsNoSession]> => combineLatest([ of(info), // we may have no staking, but have babe (permissioned) api.query.staking?.erasStartSessionIndex ? api.queryMulti([ api.query.babe.currentSlot, api.query.babe.epochIndex, api.query.babe.genesisSlot, [api.query.staking.erasStartSessionIndex, info.activeEra] ]) : api.queryMulti([ api.query.babe.currentSlot, api.query.babe.epochIndex, api.query.babe.genesisSlot ]) ]) ), map(([info, [currentSlot, epochIndex, genesisSlot, optStartIndex]]): [DeriveSessionInfo, ResultSlotsFlat] => [ info, [currentSlot, epochIndex, genesisSlot, optStartIndex && optStartIndex.isSome ? optStartIndex.unwrap() : api.registry.createType('SessionIndex', 1)] ]) ); } /** * @description Retrieves all the session and era query and calculates specific values on it as the length of the session and eras */ export function progress (instanceId: string, api: ApiInterfaceRx): () => Observable { return memo(instanceId, (): Observable => api.query.babe ? queryBabe(api).pipe( map(([info, slots]: [DeriveSessionInfo, ResultSlotsFlat]): DeriveSessionProgress => createDerive(api, info, slots) ) ) : queryAura(api) ); }