From 34d637c0d88f86a680e7b256ed3831f666e26a84 Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Fri, 20 Mar 2020 10:44:04 +1300 Subject: [PATCH 1/4] Derived account info flags --- packages/api-derive/src/accounts/info.ts | 57 +++++++++++++++++++++--- packages/api-derive/src/types.ts | 8 ++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index 85b1da5e5adb..c350e28f919e 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -5,15 +5,23 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { AccountId, AccountIndex, Address, Balance, Registration } from '@polkadot/types/interfaces'; import { ITuple } from '@polkadot/types/types'; -import { DeriveAccountInfo, DeriveAccountRegistration } from '../types'; +import { DeriveAccountFlags, DeriveAccountInfo, DeriveAccountRegistration } from '../types'; import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; -import { Bytes, Data, Option, u32 } from '@polkadot/types'; +import { Bytes, Data, Option, u32, Vec } from '@polkadot/types'; import { u8aToString } from '@polkadot/util'; import { memo } from '../util'; +type FlagsIntermediate = [ + Vec> | undefined, + AccountId[], + AccountId[], + AccountId[], + AccountId | undefined +]; + function dataAsString (data: Data): string | undefined { return data.isRaw ? u8aToString(data.asRaw.toU8a(true)) @@ -22,6 +30,12 @@ function dataAsString (data: Data): string | undefined { : undefined; } +function isIncludedFn (accountId: AccountId): (_: AccountId) => boolean { + return function (id: AccountId): boolean { + return id.toString() === accountId.toString(); + }; +} + function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable { return (( accountId && api.query.nicks?.nameOf @@ -109,6 +123,38 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab ); } +function retrieveFlags (api: ApiInterfaceRx, accountId?: AccountId): Observable { + const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections'; + return (combineLatest([ + accountId && api.query[councilSection]?.members + ? api.query[councilSection].members() + : of(undefined), + accountId && api.query.council.members + ? api.query.council.members() + : of([]), + accountId && api.query.technicalCommittee?.members + ? api.query.technicalCommittee.members() + : of([]), + accountId && api.query.society?.members + ? api.query.society.members() + : of([]), + accountId && api.query.sudo?.key + ? api.query.sudo.key() + : of(undefined) + ]) as Observable).pipe( + map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => { + const isIncluded = accountId ? isIncludedFn(accountId) : (): boolean => false; + + return { + isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded), + isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded), + isSociety: (societyMembers || []).some(isIncluded), + isSudo: sudoKey?.toString() === accountId?.toString() + }; + }) + ); +} + /** * @name info * @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname @@ -116,15 +162,16 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable { return memo((address?: AccountIndex | AccountId | Address | string | null): Observable => api.derive.accounts.idAndIndex(address).pipe( - switchMap(([accountId, accountIndex]): Observable<[Partial, DeriveAccountRegistration, string?]> => + switchMap(([accountId, accountIndex]): Observable<[Partial, DeriveAccountFlags, DeriveAccountRegistration, string?]> => combineLatest([ of({ accountId, accountIndex }), + retrieveFlags(api, accountId), retrieveIdentity(api, accountId), retrieveNick(api, accountId) ]) ), - map(([{ accountId, accountIndex }, identity, nickname]): DeriveAccountInfo => ({ - accountId, accountIndex, identity, nickname + map(([{ accountId, accountIndex }, flags, identity, nickname]): DeriveAccountInfo => ({ + accountId, accountIndex, flags, identity, nickname })) )); } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 3e9ffa0febce..fd1bfa07b210 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -28,9 +28,17 @@ export interface DeriveAccountRegistration { judgements: RegistrationJudgement[]; } +export interface DeriveAccountFlags { + isCouncil: boolean; + isSociety: boolean; + isSudo: boolean; + isTechCommittee: boolean; +} + export interface DeriveAccountInfo { accountId?: AccountId; accountIndex?: AccountIndex; + flags: DeriveAccountFlags; identity: DeriveAccountRegistration; nickname?: string; } From 187cc2b742797ad7f336b30009edfe9ae952f0ff Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Fri, 20 Mar 2020 11:26:15 +1300 Subject: [PATCH 2/4] Update packages/api-derive/src/accounts/info.ts Co-Authored-By: Jaco Greeff --- packages/api-derive/src/accounts/info.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index c350e28f919e..dedcf686aac5 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -129,7 +129,7 @@ function retrieveFlags (api: ApiInterfaceRx, accountId?: AccountId): Observable< accountId && api.query[councilSection]?.members ? api.query[councilSection].members() : of(undefined), - accountId && api.query.council.members + accountId && api.query.council?.members ? api.query.council.members() : of([]), accountId && api.query.technicalCommittee?.members From 39a693be256e2e3f7df61d7885c0ca0611bc64e7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 20 Mar 2020 16:48:00 +0100 Subject: [PATCH 3/4] Apply suggestions from code review --- packages/api-derive/src/accounts/info.ts | 8 +++++--- packages/api-derive/src/types.ts | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index dedcf686aac5..3c2f88259518 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -30,9 +30,11 @@ function dataAsString (data: Data): string | undefined { : undefined; } -function isIncludedFn (accountId: AccountId): (_: AccountId) => boolean { +function isIncludedFn (_accountId: AccountId): (_: AccountId) => boolean { + const accountId = _accountId.toString(); + return function (id: AccountId): boolean { - return id.toString() === accountId.toString(); + return id.toString() === accountId; }; } @@ -171,7 +173,7 @@ export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId ]) ), map(([{ accountId, accountIndex }, flags, identity, nickname]): DeriveAccountInfo => ({ - accountId, accountIndex, flags, identity, nickname + accountId, accountIndex, ...flags, identity, nickname })) )); } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index fd1bfa07b210..a6a49462d0eb 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -35,10 +35,9 @@ export interface DeriveAccountFlags { isTechCommittee: boolean; } -export interface DeriveAccountInfo { +export interface DeriveAccountInfo extends DeriveAccountFlags { accountId?: AccountId; accountIndex?: AccountIndex; - flags: DeriveAccountFlags; identity: DeriveAccountRegistration; nickname?: string; } From 55b4d38075511c1236cb202ae0192b53d4f9f31a Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 22 Mar 2020 07:42:12 +0100 Subject: [PATCH 4/4] Cleanups --- packages/api-derive/src/accounts/info.ts | 41 +++++++++--------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index 3c2f88259518..121343ad2d82 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -14,14 +14,6 @@ import { u8aToString } from '@polkadot/util'; import { memo } from '../util'; -type FlagsIntermediate = [ - Vec> | undefined, - AccountId[], - AccountId[], - AccountId[], - AccountId | undefined -]; - function dataAsString (data: Data): string | undefined { return data.isRaw ? u8aToString(data.asRaw.toU8a(true)) @@ -30,11 +22,9 @@ function dataAsString (data: Data): string | undefined { : undefined; } -function isIncludedFn (_accountId: AccountId): (_: AccountId) => boolean { - const accountId = _accountId.toString(); - +function includedWrapper (accountId?: AccountId): (_: AccountId) => boolean { return function (id: AccountId): boolean { - return id.toString() === accountId; + return id.eq(accountId); }; } @@ -127,31 +117,32 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab function retrieveFlags (api: ApiInterfaceRx, accountId?: AccountId): Observable { const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections'; - return (combineLatest([ - accountId && api.query[councilSection]?.members - ? api.query[councilSection].members() + + return combineLatest([ + api.query[councilSection]?.members + ? api.query[councilSection].members>>() : of(undefined), - accountId && api.query.council?.members + api.query.council?.members ? api.query.council.members() : of([]), - accountId && api.query.technicalCommittee?.members + api.query.technicalCommittee?.members ? api.query.technicalCommittee.members() : of([]), - accountId && api.query.society?.members + api.query.society?.members ? api.query.society.members() : of([]), - accountId && api.query.sudo?.key + api.query.sudo?.key ? api.query.sudo.key() : of(undefined) - ]) as Observable).pipe( + ]).pipe( map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => { - const isIncluded = accountId ? isIncludedFn(accountId) : (): boolean => false; + const checkIncluded = includedWrapper(accountId); return { - isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded), - isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded), - isSociety: (societyMembers || []).some(isIncluded), - isSudo: sudoKey?.toString() === accountId?.toString() + isCouncil: (electionsMembers?.map(([id]) => id) || councilMembers || []).some(checkIncluded), + isTechCommittee: technicalCommitteeMembers.some(checkIncluded), + isSociety: societyMembers.some(checkIncluded), + isSudo: !!sudoKey && sudoKey.eq(accountId) }; }) );