Rework eras* calls with minimum information (#2018)

This commit is contained in:
Jaco Greeff
2020-03-10 08:20:54 +01:00
committed by GitHub
parent 201098e9f6
commit 049a1a59f5
7 changed files with 122 additions and 92 deletions
@@ -0,0 +1,46 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ActiveEraInfo, EraIndex } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Option, u32 } from '@polkadot/types';
import { isBoolean, isUndefined } from '@polkadot/util';
import { memo } from '../util';
export function erasHistoric (api: ApiInterfaceRx): (withActive?: boolean | BN) => Observable<EraIndex[]> {
return memo((withActive?: boolean | BN): Observable<EraIndex[]> =>
api.query.staking?.activeEra
? api.queryMulti<[Option<ActiveEraInfo>, u32]>([
api.query.staking.activeEra,
api.query.staking.historyDepth
]).pipe(
map(([activeEraOpt, historyDepth]): EraIndex[] => {
const result: EraIndex[] = [];
const max = historyDepth.toNumber();
let lastEra = activeEraOpt.unwrapOrDefault().index.subn(withActive === true ? 0 : 1);
while (lastEra.gten(0) && result.length < max) {
result.push(api.registry.createType('EraIndex', lastEra));
lastEra = lastEra.subn(1);
}
// go from oldest to newest
return result
.filter((era): boolean =>
isUndefined(withActive) || isBoolean(withActive)
? true
: era.gte(withActive)
)
.reverse();
})
)
: of([])
);
}