Add derive functions (#564)
* Add drr * Update headers * Lint * Copy other functions * Account indexes * accountIndexToId * accountIdAndIndex * Fees * votingBalance * votingBalances * votingBalancesNominatorsFor * validatingBalance * validatingBalances * bestNumberFinalised * eraLength * Add drr * eraProgress * votes * referendumInfos * referendumVotesFor * referendums * Clean code * Add tests * Fix lint * Fix bug eraProgress * Fix bug fees * combineLatest without array * Fix lint
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2019 @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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function eraLength (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
(combineLatest(
|
||||
api.query.session.sessionLength(),
|
||||
api.query.staking.sessionsPerEra()
|
||||
) as Observable<[BlockNumber?, BlockNumber?]>).pipe(
|
||||
map(
|
||||
([sessionLength, sessionsPerEra]) =>
|
||||
(sessionLength || new BN(1)).mul(sessionsPerEra || new BN(1))
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2019 @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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber } from '@polkadot/types/index';
|
||||
|
||||
import { sessionProgress } from './sessionProgress';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function eraProgress (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
(combineLatest(
|
||||
sessionProgress(api)(),
|
||||
api.query.session.currentIndex(),
|
||||
api.query.session.sessionLength(),
|
||||
api.query.staking.lastEraLengthChange(),
|
||||
api.query.staking.sessionsPerEra()
|
||||
) as Observable<[BN, BlockNumber?, BlockNumber?, BlockNumber?, BlockNumber?]>).pipe(
|
||||
map(
|
||||
([sessionProgress, currentIndex, sessionLength, lastEraLengthChange, sessionsPerEra]) =>
|
||||
(currentIndex || new BN(0))
|
||||
.sub(lastEraLengthChange || new BN(0))
|
||||
.mod(sessionsPerEra || new BN(1))
|
||||
.mul(sessionLength || new BN(1))
|
||||
.add(sessionProgress || new BN(0)
|
||||
)
|
||||
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -2,4 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './eraLength';
|
||||
export * from './eraProgress';
|
||||
export * from './sessionProgress';
|
||||
|
||||
@@ -13,14 +13,14 @@ import { drr } from '../util/drr';
|
||||
|
||||
export function sessionProgress (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
combineLatest([
|
||||
combineLatest(
|
||||
bestNumber(api)(),
|
||||
api.query.session.sessionLength(),
|
||||
api.query.session.lastLengthChange()
|
||||
]).pipe(
|
||||
).pipe(
|
||||
map(
|
||||
([bestNumber, sessionLength, lastLengthChange]) =>
|
||||
(bestNumber as BlockNumber || new BN(0))
|
||||
(bestNumber || new BN(0))
|
||||
.sub(lastLengthChange as BlockNumber || new BN(0))
|
||||
.add(sessionLength as BlockNumber || new BN(1))
|
||||
.mod(sessionLength as BlockNumber || new BN(1))
|
||||
|
||||
Reference in New Issue
Block a user