// 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 } from '@polkadot/types/interfaces'; import type { BN } from '@polkadot/util'; import type { Observable } from '@polkadot/x-rxjs'; import { BN_ONE, BN_ZERO } from '@polkadot/util'; import { map } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; export function erasHistoric (instanceId: string, api: ApiInterfaceRx): (withActive: boolean) => Observable { return memo(instanceId, (withActive: boolean): Observable => api.queryMulti<[Option, u32]>([ api.query.staking.activeEra, api.query.staking.historyDepth ]).pipe( map(([activeEraOpt, historyDepth]): EraIndex[] => { const result: EraIndex[] = []; const max = historyDepth.toNumber(); const activeEra: BN = activeEraOpt.unwrapOrDefault().index; let lastEra = activeEra; while (lastEra.gte(BN_ZERO) && (result.length < max)) { if ((lastEra !== activeEra) || (withActive === true)) { result.push(api.registry.createType('EraIndex', lastEra)); } lastEra = lastEra.sub(BN_ONE); } // go from oldest to newest return result.reverse(); }) ) ); }