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:
Amaury Martiny
2019-01-10 17:39:25 +01:00
committed by GitHub
parent a93b2f70c7
commit 2a2df9a1c3
26 changed files with 591 additions and 5 deletions
@@ -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
View File
@@ -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))