From bfe5661d8f76a0f08d9d147b4d970605606629fa Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 28 Jan 2019 11:22:11 +0100 Subject: [PATCH] derive.balances.* handle Address & AccountIndex everywhere (#639) --- .../api-derive/src/accounts/idAndIndex.ts | 9 +++++--- .../src/balances/validatingBalance.ts | 4 ++-- .../src/balances/validatingBalances.ts | 4 ++-- .../api-derive/src/balances/votingBalance.ts | 4 ++-- .../api-derive/src/balances/votingBalances.ts | 4 ++-- .../balances/votingBalancesNominatorsFor.ts | 22 +++++++++++-------- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/packages/api-derive/src/accounts/idAndIndex.ts b/packages/api-derive/src/accounts/idAndIndex.ts index 5546a7a5df..c7e7d93ac0 100644 --- a/packages/api-derive/src/accounts/idAndIndex.ts +++ b/packages/api-derive/src/accounts/idAndIndex.ts @@ -6,7 +6,8 @@ import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterface$Rx } from '@polkadot/api/types'; import { decodeAddress } from '@polkadot/keyring'; -import { AccountId, AccountIndex } from '@polkadot/types/index'; +import { AccountId, AccountIndex, Address } from '@polkadot/types/index'; +import { isU8a } from '@polkadot/util'; import { idToIndex } from './idToIndex'; import { indexToId } from './indexToId'; @@ -15,10 +16,12 @@ import { drr } from '../util/drr'; export type AccountIdAndIndex = [AccountId?, AccountIndex?]; export function idAndIndex (api: ApiInterface$Rx) { - return (address?: AccountId | AccountIndex | string | null): Observable => { + return (address?: Address | AccountId | AccountIndex | string | null): Observable => { try { // yes, this can fail, don't care too much, catch will catch it - const decoded = decodeAddress(address as any); + const decoded = isU8a(address) + ? address + : decodeAddress((address || '').toString()); if (decoded.length === 32) { const accountId = new AccountId(decoded); diff --git a/packages/api-derive/src/balances/validatingBalance.ts b/packages/api-derive/src/balances/validatingBalance.ts index 76b23ed680..cdaaf1b3f1 100644 --- a/packages/api-derive/src/balances/validatingBalance.ts +++ b/packages/api-derive/src/balances/validatingBalance.ts @@ -6,7 +6,7 @@ import BN from 'bn.js'; import { combineLatest, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId, Balance } from '@polkadot/types/index'; +import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/index'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; @@ -14,7 +14,7 @@ import { votingBalance } from './votingBalance'; import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor'; export function validatingBalance (api: ApiInterface$Rx) { - return (address: AccountId | string): Observable => { + return (address: AccountId | AccountIndex | Address | string): Observable => { return combineLatest( votingBalance(api)(address), votingBalancesNominatorsFor(api)(address) diff --git a/packages/api-derive/src/balances/validatingBalances.ts b/packages/api-derive/src/balances/validatingBalances.ts index 418b787438..e2055cce5c 100644 --- a/packages/api-derive/src/balances/validatingBalances.ts +++ b/packages/api-derive/src/balances/validatingBalances.ts @@ -5,14 +5,14 @@ import { combineLatest, Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId } from '@polkadot/types/index'; +import { AccountId, Address } from '@polkadot/types/index'; import { DerivedBalancesMap } from '../types'; import { drr } from '../util/drr'; import { validatingBalance } from './validatingBalance'; export function validatingBalances (api: ApiInterface$Rx) { - return (accountIds: Array): Observable => { + return (accountIds: Array): Observable => { return !accountIds || !accountIds.length ? of({}).pipe(drr()) : combineLatest( diff --git a/packages/api-derive/src/balances/votingBalance.ts b/packages/api-derive/src/balances/votingBalance.ts index 8cef40add8..f77b93e6c4 100644 --- a/packages/api-derive/src/balances/votingBalance.ts +++ b/packages/api-derive/src/balances/votingBalance.ts @@ -5,7 +5,7 @@ import { combineLatest, of, Observable } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId, AccountIndex, Balance } from '@polkadot/types/index'; +import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/index'; import { idAndIndex } from '../accounts/idAndIndex'; import { DerivedBalances } from '../types'; @@ -14,7 +14,7 @@ import { drr } from '../util/drr'; const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32)); export function votingBalance (api: ApiInterface$Rx) { - return (address: AccountIndex | AccountId | string): Observable => { + return (address: AccountIndex | AccountId | Address | string): Observable => { return idAndIndex(api)(address).pipe( switchMap(([accountId]) => (accountId diff --git a/packages/api-derive/src/balances/votingBalances.ts b/packages/api-derive/src/balances/votingBalances.ts index 8629a5cff0..0c0919b67c 100644 --- a/packages/api-derive/src/balances/votingBalances.ts +++ b/packages/api-derive/src/balances/votingBalances.ts @@ -4,14 +4,14 @@ import { combineLatest, Observable, of } from 'rxjs'; import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId, AccountIndex } from '@polkadot/types/index'; +import { AccountId, AccountIndex, Address } from '@polkadot/types/index'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; import { votingBalance } from './votingBalance'; export function votingBalances (api: ApiInterface$Rx) { - return (addresses?: Array): Observable> => { + return (addresses?: Array): Observable> => { return !addresses || !addresses.length ? of([]).pipe(drr()) : 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 index 78fc14b632..e3119d3569 100644 --- a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts +++ b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts @@ -2,23 +2,27 @@ // 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 { Observable, of } from 'rxjs'; import { switchMap } from 'rxjs/operators'; import { ApiInterface$Rx } from '@polkadot/api/types'; import { Vector } from '@polkadot/types/codec'; -import { AccountId } from '@polkadot/types/index'; +import { AccountId, AccountIndex, Address } from '@polkadot/types/index'; +import { idAndIndex } from '../accounts/idAndIndex'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; import { votingBalances } from './votingBalances'; export function votingBalancesNominatorsFor (api: ApiInterface$Rx) { - return (accountId: AccountId | string): Observable> => { - // tslint:disable-next-line - return (api.query.staking.nominatorsFor(accountId) as Observable>) - .pipe( - switchMap(votingBalances(api)), - drr() - ) as Observable>; + return (address: AccountId | AccountIndex | Address | string): Observable> => { + return idAndIndex(api)(address).pipe( + switchMap(([accountId]) => + accountId + ? (api.query.staking.nominatorsFor(accountId) as Observable>) + : of([] as Array) + ), + switchMap(votingBalances(api)), + drr() + ); }; }