From b69d8ec7894854ebeaf502a3e4bb362ce34c066f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 28 Oct 2019 11:14:21 +0100 Subject: [PATCH] Account info derive (#1523) --- packages/api-derive/src/accounts/index.ts | 1 + packages/api-derive/src/accounts/indexes.ts | 2 +- packages/api-derive/src/accounts/info.ts | 41 +++++++++++++++++++++ packages/api-derive/src/types.ts | 4 ++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 packages/api-derive/src/accounts/info.ts diff --git a/packages/api-derive/src/accounts/index.ts b/packages/api-derive/src/accounts/index.ts index 732bafdc94..f46ce78c27 100644 --- a/packages/api-derive/src/accounts/index.ts +++ b/packages/api-derive/src/accounts/index.ts @@ -6,3 +6,4 @@ export * from './idAndIndex'; export * from './idToIndex'; export * from './indexToId'; export * from './indexes'; +export * from './info'; diff --git a/packages/api-derive/src/accounts/indexes.ts b/packages/api-derive/src/accounts/indexes.ts index 4d24e0b722..adbfd87342 100644 --- a/packages/api-derive/src/accounts/indexes.ts +++ b/packages/api-derive/src/accounts/indexes.ts @@ -2,11 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { ApiInterfaceRx } from '@polkadot/api/types'; import { AccountId, AccountIndex } from '@polkadot/types/interfaces'; import { Observable } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; -import { ApiInterfaceRx } from '@polkadot/api/types'; import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; import { createType } from '@polkadot/types'; diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts new file mode 100644 index 0000000000..4492787dd8 --- /dev/null +++ b/packages/api-derive/src/accounts/info.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 { ApiInterfaceRx } from '@polkadot/api/types'; +import { AccountId, 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 { 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; +} + +/** + * @name info + * @description Returns aux. info with regards to an account, current that includes the nickname + */ +export function info (api: ApiInterfaceRx): (address?: AccountId | string | null) => Observable { + // 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); + }; +} diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 1d31886d51..d27a133f0a 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -7,6 +7,10 @@ import { AccountId, Balance, BlockNumber, EraIndex, Exposure, Index, RewardDesti import BN from 'bn.js'; import { u32 } from '@polkadot/types'; +export interface DeriveAccountInfo { + nickname?: string; +} + export interface DerivedBalances { accountId: AccountId; accountNonce: Index;