From 2a2df9a1c3316609bf844d92c6ee6b7b192692de Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 10 Jan 2019 17:39:25 +0100 Subject: [PATCH] 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 --- .../src/balances/accountIdAndIndex.ts | 43 +++++++++++++++++++ .../src/balances/accountIdToIndex.ts | 20 +++++++++ .../src/balances/accountIndexToId.ts | 27 ++++++++++++ .../api-derive/src/balances/accountIndexes.ts | 40 +++++++++++++++++ packages/api-derive/src/balances/fees.ts | 30 +++++++++++++ packages/api-derive/src/balances/index.ts | 14 ++++++ .../src/balances/validatingBalance.ts | 38 ++++++++++++++++ .../src/balances/validatingBalances.ts | 32 ++++++++++++++ .../api-derive/src/balances/votingBalance.ts | 41 ++++++++++++++++++ .../api-derive/src/balances/votingBalances.ts | 19 ++++++++ .../balances/votingBalancesNominatorsFor.ts | 21 +++++++++ packages/api-derive/src/chain/bestNumber.ts | 5 ++- .../src/chain/bestNumberFinalised.ts | 23 ++++++++++ packages/api-derive/src/chain/index.ts | 1 + packages/api-derive/src/democracy/index.ts | 8 ++++ .../src/democracy/referendumInfos.ts | 28 ++++++++++++ .../src/democracy/referendumVotesFor.ts | 37 ++++++++++++++++ .../api-derive/src/democracy/referendums.ts | 29 +++++++++++++ packages/api-derive/src/democracy/votes.ts | 21 +++++++++ packages/api-derive/src/index.spec.ts | 20 +++++++++ packages/api-derive/src/index.ts | 6 ++- packages/api-derive/src/session/eraLength.ts | 25 +++++++++++ .../api-derive/src/session/eraProgress.ts | 35 +++++++++++++++ packages/api-derive/src/session/index.ts | 2 + .../api-derive/src/session/sessionProgress.ts | 6 +-- packages/api-derive/src/types.ts | 25 +++++++++++ 26 files changed, 591 insertions(+), 5 deletions(-) create mode 100644 packages/api-derive/src/balances/accountIdAndIndex.ts create mode 100644 packages/api-derive/src/balances/accountIdToIndex.ts create mode 100644 packages/api-derive/src/balances/accountIndexToId.ts create mode 100644 packages/api-derive/src/balances/accountIndexes.ts create mode 100644 packages/api-derive/src/balances/fees.ts create mode 100644 packages/api-derive/src/balances/index.ts create mode 100644 packages/api-derive/src/balances/validatingBalance.ts create mode 100644 packages/api-derive/src/balances/validatingBalances.ts create mode 100644 packages/api-derive/src/balances/votingBalance.ts create mode 100644 packages/api-derive/src/balances/votingBalances.ts create mode 100644 packages/api-derive/src/balances/votingBalancesNominatorsFor.ts create mode 100644 packages/api-derive/src/chain/bestNumberFinalised.ts create mode 100644 packages/api-derive/src/democracy/index.ts create mode 100644 packages/api-derive/src/democracy/referendumInfos.ts create mode 100644 packages/api-derive/src/democracy/referendumVotesFor.ts create mode 100644 packages/api-derive/src/democracy/referendums.ts create mode 100644 packages/api-derive/src/democracy/votes.ts create mode 100644 packages/api-derive/src/session/eraLength.ts create mode 100644 packages/api-derive/src/session/eraProgress.ts create mode 100644 packages/api-derive/src/types.ts diff --git a/packages/api-derive/src/balances/accountIdAndIndex.ts b/packages/api-derive/src/balances/accountIdAndIndex.ts new file mode 100644 index 0000000000..a462fa2731 --- /dev/null +++ b/packages/api-derive/src/balances/accountIdAndIndex.ts @@ -0,0 +1,43 @@ +// 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 { Observable, of } from 'rxjs'; +import { map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { decodeAddress } from '@polkadot/keyring'; +import { AccountId, AccountIndex } from '@polkadot/types/index'; + +import { accountIdToIndex } from './accountIdToIndex'; +import { accountIndexToId } from './accountIndexToId'; +import { drr } from '../util/drr'; + +export type IdAndIndex = [AccountId | undefined, AccountIndex | undefined]; + +export function accountIdAndIndex (api: ApiRx) { + return (address: AccountId | AccountIndex | string | null | undefined) + : Observable<(AccountIndex | AccountId | undefined)[]> => { + try { + // yes, this can fail, don't care too much, catch will catch it + const { length } = decodeAddress((address as any).toString()); + + if (length === 32) { + const accountId = new AccountId(address as string); + + return accountIdToIndex(api)(accountId).pipe( + map((accountIndex) => [accountId, accountIndex]), + drr() + ); + } + + const accountIndex = new AccountIndex(address as string); + + return accountIndexToId(api)(accountIndex).pipe( + map((accountId) => [accountId, accountIndex]), + drr() + ); + } catch (error) { + return of([undefined, undefined]).pipe(drr()); + } + }; +} diff --git a/packages/api-derive/src/balances/accountIdToIndex.ts b/packages/api-derive/src/balances/accountIdToIndex.ts new file mode 100644 index 0000000000..7c0ff2a103 --- /dev/null +++ b/packages/api-derive/src/balances/accountIdToIndex.ts @@ -0,0 +1,20 @@ +// 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 { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { AccountId, AccountIndex } from '@polkadot/types/index'; + +import { accountIndexes, AccountIndexes } from './accountIndexes'; +import { drr } from '../util/drr'; + +export function accountIdToIndex (api: ApiRx) { + return (accountId: AccountId | string): Observable => + accountIndexes(api)() + .pipe( + map((indexes: AccountIndexes) => (indexes || {})[accountId.toString()]), + drr() + ); +} diff --git a/packages/api-derive/src/balances/accountIndexToId.ts b/packages/api-derive/src/balances/accountIndexToId.ts new file mode 100644 index 0000000000..0b6b8d1983 --- /dev/null +++ b/packages/api-derive/src/balances/accountIndexToId.ts @@ -0,0 +1,27 @@ +// 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 { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex'; +import { Vector } from '@polkadot/types/codec'; +import { AccountId, AccountIndex } from '@polkadot/types/index'; + +import { drr } from '../util/drr'; + +export function accountIndexToId (api: ApiRx) { + return (accountIndex: AccountIndex | string): Observable => { + const _accountIndex = accountIndex instanceof AccountIndex + ? accountIndex + : new AccountIndex(accountIndex); + + return (api.query.balances.enumSet(accountIndex) as Observable>) + .pipe( + map((accounts) => (accounts || [])[_accountIndex.mod(ENUMSET_SIZE).toNumber()]), + drr() + ); + }; + +} diff --git a/packages/api-derive/src/balances/accountIndexes.ts b/packages/api-derive/src/balances/accountIndexes.ts new file mode 100644 index 0000000000..9ce0d80803 --- /dev/null +++ b/packages/api-derive/src/balances/accountIndexes.ts @@ -0,0 +1,40 @@ +// 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, switchMap } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex'; +import { AccountId, AccountIndex } from '@polkadot/types/index'; + +import { drr } from '../util/drr'; + +export type AccountIndexes = { [index: string]: AccountIndex }; + +export function accountIndexes (api: ApiRx) { + return (): Observable => + (api.query.balances.nextEnumSet() as Observable) + .pipe( + map((next: AccountIndex) => { + const enumRange = [...Array((next || new BN(0)).div(ENUMSET_SIZE).toNumber() + 1).keys()]; + + return enumRange; + }), + switchMap((enumRange) => combineLatest( + enumRange.map((index) => (api.query.balances.enumSet(index) as Observable)) + )), + map((all: Array | undefined>) => + (all || []).reduce((result, list, outerIndex) => { + (list || []).forEach((accountId, innerIndex) => { + const index = (outerIndex * ENUMSET_SIZE.toNumber()) + innerIndex; + + result[accountId.toString()] = new AccountIndex(index); + }); + + return result; + }, {} as AccountIndexes)), + drr() + ); +} diff --git a/packages/api-derive/src/balances/fees.ts b/packages/api-derive/src/balances/fees.ts new file mode 100644 index 0000000000..35c795f99d --- /dev/null +++ b/packages/api-derive/src/balances/fees.ts @@ -0,0 +1,30 @@ +// 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 } from 'rxjs'; +import { map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; + +import { drr } from '../util/drr'; + +export function fees (api: ApiRx) { + return () => + combineLatest( + api.query.balances.creationFee(), + api.query.balances.existentialDeposit(), + api.query.balances.transactionBaseFee(), + api.query.balances.transactionByteFee(), + api.query.balances.transferFee() + ).pipe( + map(([creationFee, existentialDeposit, transactionBaseFee, transactionByteFee, transferFee]) => ({ + creationFee: creationFee || new BN(0), + existentialDeposit: existentialDeposit || new BN(0), + transactionBaseFee: transactionBaseFee || new BN(0), + transactionByteFee: transactionByteFee || new BN(0), + transferFee: transferFee || new BN(0) + })), + drr() + ); +} diff --git a/packages/api-derive/src/balances/index.ts b/packages/api-derive/src/balances/index.ts new file mode 100644 index 0000000000..41201a89be --- /dev/null +++ b/packages/api-derive/src/balances/index.ts @@ -0,0 +1,14 @@ +// 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. + +export * from './accountIdAndIndex'; +export * from './accountIdToIndex'; +export * from './accountIndexToId'; +export * from './accountIndexes'; +export * from './fees'; +export * from './validatingBalance'; +export * from './validatingBalances'; +export * from './votingBalance'; +export * from './votingBalances'; +export * from './votingBalancesNominatorsFor'; diff --git a/packages/api-derive/src/balances/validatingBalance.ts b/packages/api-derive/src/balances/validatingBalance.ts new file mode 100644 index 0000000000..5e138d3b1b --- /dev/null +++ b/packages/api-derive/src/balances/validatingBalance.ts @@ -0,0 +1,38 @@ +// 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 { AccountId, Balance } from '@polkadot/types/index'; + +import { DerivedBalances } from '../types'; +import { drr } from '../util/drr'; +import { votingBalance } from './votingBalance'; +import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor'; + +export function validatingBalance (api: ApiRx) { + return (address: AccountId | string): Observable => + combineLatest( + votingBalance(api)(address), + votingBalancesNominatorsFor(api)(address) + ).pipe( + map(([balance, nominators]) => { + const nominatedBalance = nominators.reduce( + (total: BN, nominatorBalance: DerivedBalances) => total.add(nominatorBalance.votingBalance), + new BN(0)); + + return { + ...balance, + nominators, + nominatedBalance: new Balance(nominatedBalance), + stakingBalance: new Balance( + nominatedBalance.add(balance.votingBalance) + ) + }; + }), + drr() + ); +} diff --git a/packages/api-derive/src/balances/validatingBalances.ts b/packages/api-derive/src/balances/validatingBalances.ts new file mode 100644 index 0000000000..67fffd3c56 --- /dev/null +++ b/packages/api-derive/src/balances/validatingBalances.ts @@ -0,0 +1,32 @@ +// 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 { combineLatest, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { AccountId } from '@polkadot/types/index'; + +import { DerivedBalancesMap } from '../types'; +import { drr } from '../util/drr'; +import { validatingBalance } from './validatingBalance'; + +export function validatingBalances (api: ApiRx) { + return (...params: Array): Observable => { + const accountIds: Array = params.slice(0, params.length - 1); + + return combineLatest( + accountIds.map(validatingBalance(api)) + ).pipe( + map((result) => + result.reduce((balances, balance) => { + balances[balance.accountId.toString()] = balance; + + return balances; + }, {} as DerivedBalancesMap) + ), + drr() + ); + }; + +} diff --git a/packages/api-derive/src/balances/votingBalance.ts b/packages/api-derive/src/balances/votingBalance.ts new file mode 100644 index 0000000000..0db27a2ffe --- /dev/null +++ b/packages/api-derive/src/balances/votingBalance.ts @@ -0,0 +1,41 @@ +// 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 { combineLatest, of, Observable } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { AccountId, AccountIndex, Balance } from '@polkadot/types/index'; + +import { accountIdAndIndex } from './accountIdAndIndex'; +import { DerivedBalances } from '../types'; +import { drr } from '../util/drr'; + +const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32)); + +export function votingBalance (api: ApiRx) { + return (address: AccountIndex | AccountId | string): Observable => + accountIdAndIndex(api)(address).pipe( + switchMap(([accountId]) => + (accountId + ? combineLatest( + of(accountId), + api.query.balances.freeBalance(accountId), + api.query.balances.reservedBalance(accountId) + ) + : of([undefined, undefined, undefined]) + ) as Observable<[AccountId?, Balance?, Balance?]> + ), + map(([accountId, freeBalance, reservedBalance]) => ({ + accountId: accountId || EMPTY_ACCOUNT, + freeBalance: freeBalance || new Balance(0), + nominatedBalance: new Balance(0), + reservedBalance: reservedBalance || new Balance(0), + stakingBalance: new Balance(0), + votingBalance: new Balance( + (freeBalance || new Balance(0)).add(reservedBalance || new Balance(0)) + ) + })), + drr() + ); +} diff --git a/packages/api-derive/src/balances/votingBalances.ts b/packages/api-derive/src/balances/votingBalances.ts new file mode 100644 index 0000000000..1449241425 --- /dev/null +++ b/packages/api-derive/src/balances/votingBalances.ts @@ -0,0 +1,19 @@ +// 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 { combineLatest, Observable } from 'rxjs'; +import ApiRx from '@polkadot/api/rx'; +import { AccountId, AccountIndex } from '@polkadot/types/index'; + +import { DerivedBalances } from '../types'; +import { drr } from '../util/drr'; +import { votingBalance } from './votingBalance'; + +export function votingBalances (api: ApiRx) { + return (...params: Array): Observable> => { + const addresses: Array = params.slice(0, params.length - 1); + + return combineLatest(...addresses.map(votingBalance(api))).pipe(drr()); + }; +} diff --git a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts new file mode 100644 index 0000000000..62c9d71e09 --- /dev/null +++ b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts @@ -0,0 +1,21 @@ +// 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 { Observable } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { AccountId } from '@polkadot/types/index'; + +import { DerivedBalances } from '../types'; +import { drr } from '../util/drr'; +import { votingBalances } from './votingBalances'; + +export function votingBalancesNominatorsFor (api: ApiRx) { + return (accountId: AccountId | string): Observable> => + (api.query.staking.nominatorsFor(accountId) as Observable | undefined>).pipe( + map((nominators) => nominators || []), + switchMap(votingBalances(api)), + drr() + ); +} diff --git a/packages/api-derive/src/chain/bestNumber.ts b/packages/api-derive/src/chain/bestNumber.ts index 6b35e6408d..7622203432 100644 --- a/packages/api-derive/src/chain/bestNumber.ts +++ b/packages/api-derive/src/chain/bestNumber.ts @@ -7,6 +7,8 @@ import { filter, map } from 'rxjs/operators'; import ApiRx from '@polkadot/api/rx'; import { BlockNumber, Header } from '@polkadot/types/index'; +import { drr } from '../util/drr'; + /** * Get the latest block number. */ @@ -15,6 +17,7 @@ export function bestNumber (api: ApiRx) { api.rpc.chain.subscribeNewHead() .pipe( filter((header: Header) => header && !!header.blockNumber), - map(({ blockNumber }: Header) => blockNumber) + map(({ blockNumber }: Header) => blockNumber), + drr() ); } diff --git a/packages/api-derive/src/chain/bestNumberFinalised.ts b/packages/api-derive/src/chain/bestNumberFinalised.ts new file mode 100644 index 0000000000..4298a72ad6 --- /dev/null +++ b/packages/api-derive/src/chain/bestNumberFinalised.ts @@ -0,0 +1,23 @@ +// 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 { Observable } from 'rxjs'; +import { filter, map } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { BlockNumber, Header } from '@polkadot/types/index'; + +import { drr } from '../util/drr'; + +/** + * Get the latest finalised block number. + */ +export function bestNumberFinalised (api: ApiRx) { + return (): Observable => + api.rpc.chain.subscribeFinalisedHeads() + .pipe( + filter((header: Header) => header && !!header.blockNumber), + map(({ blockNumber }: Header) => blockNumber), + drr() + ); +} diff --git a/packages/api-derive/src/chain/index.ts b/packages/api-derive/src/chain/index.ts index f6c675022a..eafe91c4a7 100644 --- a/packages/api-derive/src/chain/index.ts +++ b/packages/api-derive/src/chain/index.ts @@ -3,3 +3,4 @@ // of the Apache-2.0 license. See the LICENSE file for details. export * from './bestNumber'; +export * from './bestNumberFinalised'; diff --git a/packages/api-derive/src/democracy/index.ts b/packages/api-derive/src/democracy/index.ts new file mode 100644 index 0000000000..0728f9be6a --- /dev/null +++ b/packages/api-derive/src/democracy/index.ts @@ -0,0 +1,8 @@ +// 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. + +export * from './referendumInfos'; +export * from './referendums'; +export * from './referendumVotesFor'; +export * from './votes'; diff --git a/packages/api-derive/src/democracy/referendumInfos.ts b/packages/api-derive/src/democracy/referendumInfos.ts new file mode 100644 index 0000000000..fae0ffc05d --- /dev/null +++ b/packages/api-derive/src/democracy/referendumInfos.ts @@ -0,0 +1,28 @@ +// 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 { filter } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { ReferendumInfo } from '@polkadot/types/index'; + +import { drr } from '../util/drr'; + +export function referendumInfos (api: ApiRx) { + return (...params: Array): Observable> => { + const ids: Array = params.slice(0, params.length - 1); + + return combineLatest( + ids.map( + (id) => (api.query.democracy.referendumInfoOf(id) as Observable).pipe( + filter((info) => !!info) + ) + ) + ).pipe( + drr() + ); + }; + +} diff --git a/packages/api-derive/src/democracy/referendumVotesFor.ts b/packages/api-derive/src/democracy/referendumVotesFor.ts new file mode 100644 index 0000000000..cbe8eed031 --- /dev/null +++ b/packages/api-derive/src/democracy/referendumVotesFor.ts @@ -0,0 +1,37 @@ +// 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, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { Vector } from '@polkadot/types/codec'; +import { AccountId, Balance, Vote } from '@polkadot/types/index'; + +import { DerivedReferendumVote } from '../types'; +import { drr } from '../util/drr'; +import { votes } from './votes'; +import { votingBalances } from '../balances/votingBalances'; + +export function referendumVotesFor (api: ApiRx) { + return (referendumId: BN | number): Observable> => + (api.query.democracy.votersFor(referendumId) as Observable>).pipe( + switchMap((votersFor) => + combineLatest( + of(votersFor), + votes(api)(referendumId as BN, ...votersFor), + votingBalances(api)(...votersFor) + ) + ), + map(([votersFor, votes, balances]) => + votersFor.map((accountId, index): DerivedReferendumVote => ({ + accountId, + balance: balances[index].votingBalance || new Balance(0), + vote: votes[index] || new Vote(0) + })) + ), + drr() + ); + +} diff --git a/packages/api-derive/src/democracy/referendums.ts b/packages/api-derive/src/democracy/referendums.ts new file mode 100644 index 0000000000..602a59057c --- /dev/null +++ b/packages/api-derive/src/democracy/referendums.ts @@ -0,0 +1,29 @@ +// 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 { combineLatest, Observable, of } from 'rxjs'; +import { switchMap } from 'rxjs/operators'; +import ApiRx from '@polkadot/api/rx'; +import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/index'; + +import { referendumInfos } from './referendumInfos'; +import { drr } from '../util/drr'; + +export function referendums (api: ApiRx) { + return (): Observable> => + (combineLatest( + api.query.democracy.nextTally(), + api.query.democracy.referendumCount() + ) as Observable<[ReferendumIndex?, ReferendumIndex?]>).pipe( + switchMap(([nextTally, referendumCount]) => + referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0) + ? referendumInfos(api)( + ...[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i) => + nextTally.addn(i) + )) + : of([]) + ), + drr() + ); +} diff --git a/packages/api-derive/src/democracy/votes.ts b/packages/api-derive/src/democracy/votes.ts new file mode 100644 index 0000000000..225347d30f --- /dev/null +++ b/packages/api-derive/src/democracy/votes.ts @@ -0,0 +1,21 @@ +// 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 ApiRx from '@polkadot/api/rx'; +import { AccountId, Vote } from '@polkadot/types/index'; + +import { drr } from '../util/drr'; + +export function votes (api: ApiRx) { + return (referendumId: BN, ...accountIds: Array): Observable> => + combineLatest( + accountIds.map( + (accountId) => api.query.democracy.voteOf([referendumId, accountId]) as Observable + ) + ).pipe( + drr() + ); +} diff --git a/packages/api-derive/src/index.spec.ts b/packages/api-derive/src/index.spec.ts index 7fb01a4475..f4ab5a9a6f 100644 --- a/packages/api-derive/src/index.spec.ts +++ b/packages/api-derive/src/index.spec.ts @@ -37,6 +37,26 @@ describe('derive', () => { beforeAll((done) => api.isReady.subscribe(() => done())); + testFunction(api)('balances', 'accountIdAndIndex', []); + testFunction(api)('balances', 'accountIdToIndex', []); + testFunction(api)('balances', 'accountIndexes', []); + testFunction(api)('balances', 'accountIndexToId', []); + testFunction(api)('balances', 'fees', []); + testFunction(api)('balances', 'validatingBalance', []); + testFunction(api)('balances', 'validatingBalances', []); + testFunction(api)('balances', 'votingBalance', []); + testFunction(api)('balances', 'votingBalances', []); + testFunction(api)('balances', 'votingBalancesNominatorsFor', []); + testFunction(api)('chain', 'bestNumber', []); + testFunction(api)('chain', 'bestNumberFinalised', []); + + testFunction(api)('democracy', 'referendumInfos', []); + testFunction(api)('democracy', 'referendums', []); + testFunction(api)('democracy', 'referendumVotesFor', []); + testFunction(api)('democracy', 'votes', []); + + testFunction(api)('session', 'eraLength', []); + testFunction(api)('session', 'eraProgress', []); testFunction(api)('session', 'sessionProgress', []); }); diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index c126f87360..064d5d733c 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -4,12 +4,14 @@ import ApiRx from '@polkadot/api/rx'; +import * as balances from './balances'; import * as chain from './chain'; +import * as democracy from './democracy'; import * as session from './session'; import { cache } from './util/cache'; // Put all derived functions in an object, for easier Object.keys()-ing. -const functions = { chain, session }; +const functions = { balances, chain, democracy, session }; /** * T represents the section here (chain, balances...), and P represents @@ -20,7 +22,9 @@ type ReturnTypes any>> = { }; export interface Derive { + balances: ReturnTypes; chain: ReturnTypes; + democracy: ReturnTypes; session: ReturnTypes; } diff --git a/packages/api-derive/src/session/eraLength.ts b/packages/api-derive/src/session/eraLength.ts new file mode 100644 index 0000000000..7b23a5f4a1 --- /dev/null +++ b/packages/api-derive/src/session/eraLength.ts @@ -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 => + (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() + ); +} diff --git a/packages/api-derive/src/session/eraProgress.ts b/packages/api-derive/src/session/eraProgress.ts new file mode 100644 index 0000000000..7c39fefd29 --- /dev/null +++ b/packages/api-derive/src/session/eraProgress.ts @@ -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 => + (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() + ); +} diff --git a/packages/api-derive/src/session/index.ts b/packages/api-derive/src/session/index.ts index 322a23c189..47474007b8 100644 --- a/packages/api-derive/src/session/index.ts +++ b/packages/api-derive/src/session/index.ts @@ -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'; diff --git a/packages/api-derive/src/session/sessionProgress.ts b/packages/api-derive/src/session/sessionProgress.ts index 0d050f46e0..5ce9cf7db0 100644 --- a/packages/api-derive/src/session/sessionProgress.ts +++ b/packages/api-derive/src/session/sessionProgress.ts @@ -13,14 +13,14 @@ import { drr } from '../util/drr'; export function sessionProgress (api: ApiRx) { return (): Observable => - 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)) diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts new file mode 100644 index 0000000000..4e5e73f9fc --- /dev/null +++ b/packages/api-derive/src/types.ts @@ -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 { AccountId, Balance, Vote } from '@polkadot/types/index'; + +export type DerivedBalances = { + accountId: AccountId, + freeBalance: Balance, + nominatedBalance: Balance, + reservedBalance: Balance, + votingBalance: Balance, + stakingBalance: Balance, + nominators?: Array +}; + +export type DerivedBalancesMap = { + [index: string]: DerivedBalances +}; + +export type DerivedReferendumVote = { + accountId: AccountId, + balance: Balance, + vote: Vote +};