From 453aacb669dc6aa5e78697718e4906b91c60017b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 31 Oct 2019 10:48:29 +0100 Subject: [PATCH] derive.accounts.info returns accountId/Index (#1533) * derive.accounts.info returns accountId/Index * Swap all idAndIndex to account.info --- packages/api-derive/src/accounts/info.ts | 48 ++++++++++--------- packages/api-derive/src/balances/all.ts | 8 ++-- .../balances/votingBalancesNominatorsFor.ts | 8 ++-- packages/api-derive/src/types.ts | 4 +- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index 4492787dd8..73331dbeab 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -3,39 +3,43 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Balance } from '@polkadot/types/interfaces'; +import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/interfaces'; import { Codec } from '@polkadot/types/types'; import { DeriveAccountInfo } from '../types'; import { Observable, of } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { map, switchMap } from 'rxjs/operators'; import { Bytes, Option, u32 } from '@polkadot/types'; import { u8aToString } from '@polkadot/util'; import { drr } from '../util/drr'; - -type Result = Option<[Bytes, Balance] & Codec>; - -function formatNickname (api: ApiInterfaceRx, result: Result): string | undefined { - return result.isSome - ? u8aToString(result.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber()) - : undefined; -} +import { idAndIndex } from './idAndIndex'; /** * @name info - * @description Returns aux. info with regards to an account, current that includes the nickname + * @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname */ -export function info (api: ApiInterfaceRx): (address?: AccountId | string | null) => Observable { +export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable { + const idAndIndexCall = idAndIndex(api); + // TODO We would really like to pass in an Address or AccountIndex here as well - return (address?: AccountId | string | null): Observable => { - return address && api.query.nicks - ? api.query.nicks.nameOf(address).pipe( - map((result): DeriveAccountInfo => ({ - nickname: formatNickname(api, result) - })), - drr() - ) - : of({} as DeriveAccountInfo); - }; + return (address?: AccountIndex | AccountId | Address | string | null): Observable => + idAndIndexCall(address).pipe( + switchMap(([accountId, accountIndex]): Observable => + accountId && api.query.nicks + ? api.query.nicks + .nameOf>(accountId) + .pipe( + map((result): DeriveAccountInfo => ({ + accountId, + accountIndex, + nickname: result.isSome + ? u8aToString(result.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber()) + : undefined + })) + ) + : of({ accountId, accountIndex }) + ), + drr() + ); } diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index 0651722fcc..e3cc16d55d 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -11,7 +11,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { Option, Vec, createType } from '@polkadot/types'; import { bnMax } from '@polkadot/util'; -import { idAndIndex } from '../accounts/idAndIndex'; +import { info } from '../accounts/info'; import { bestNumber } from '../chain/bestNumber'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; @@ -84,11 +84,11 @@ function queryBalances (api: ApiInterfaceRx, accountId: AccountId): Observable Observable { const bestNumberCall = bestNumber(api); - const idAndIndexCall = idAndIndex(api); + const infoCall = info(api); return (address: AccountIndex | AccountId | Address | string): Observable => { - return idAndIndexCall(address).pipe( - switchMap(([accountId]): Observable => + return infoCall(address).pipe( + switchMap(({ accountId }): Observable => (accountId ? combineLatest([ of(accountId), diff --git a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts index 85743dec4b..4363ef34ab 100644 --- a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts +++ b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts @@ -9,17 +9,17 @@ import { switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Vec } from '@polkadot/types'; -import { idAndIndex } from '../accounts/idAndIndex'; +import { info } from '../accounts/info'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; import { votingBalances } from './votingBalances'; export function votingBalancesNominatorsFor (api: ApiInterfaceRx): (address: AccountId | AccountIndex | Address | string) => Observable { - const idAndIndexCall = idAndIndex(api); + const infoCall = info(api); return (address: AccountId | AccountIndex | Address | string): Observable => { - return idAndIndexCall(address).pipe( - switchMap(([accountId]): Observable => + return infoCall(address).pipe( + switchMap(({ accountId }): Observable => accountId ? (api.query.staking.nominatorsFor>(accountId)) : of([] as AccountId[]) diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index dceddf6c07..145840d5db 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -2,12 +2,14 @@ // 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, BlockNumber, EraIndex, Exposure, Index, RewardDestination, SessionIndex, SetIndex, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types/interfaces'; +import { AccountId, AccountIndex, Balance, BlockNumber, EraIndex, Exposure, Index, RewardDestination, SessionIndex, SetIndex, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { u32 } from '@polkadot/types'; export interface DeriveAccountInfo { + accountId?: AccountId; + accountIndex?: AccountIndex; nickname?: string; }