From b18bdc37f9fbfabb5a15b17e5f70b532f8ac9dc2 Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Tue, 21 Apr 2020 17:31:00 +1200 Subject: [PATCH] Add api.derive.accounts.flags (#2195) * account flags in new call * Fix --- packages/api-derive/src/accounts/flags.ts | 69 +++++++++++++++++++++++ packages/api-derive/src/accounts/index.ts | 1 + packages/api-derive/src/accounts/types.ts | 7 +++ 3 files changed, 77 insertions(+) create mode 100644 packages/api-derive/src/accounts/flags.ts diff --git a/packages/api-derive/src/accounts/flags.ts b/packages/api-derive/src/accounts/flags.ts new file mode 100644 index 0000000000..1c6d7ffb62 --- /dev/null +++ b/packages/api-derive/src/accounts/flags.ts @@ -0,0 +1,69 @@ +// Copyright 2017-2020 @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, Address, Balance } from '@polkadot/types/interfaces'; +import { ITuple } from '@polkadot/types/types'; +import { DeriveAccountFlags } from '../types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { Vec } from '@polkadot/types'; + +import { memo } from '../util'; + +type FlagsIntermediate = [ + Vec> | undefined, + AccountId[], + AccountId[], + AccountId[], + AccountId | undefined +]; + +function isIncludedFn (accountId: AccountId | Address | string): (_: AccountId | Address |string) => boolean { + return function (id: AccountId | Address | string): boolean { + return id.toString() === accountId.toString(); + }; +} + +/** + * @name info + * @description Returns account membership flags + */ +export function flags (api: ApiInterfaceRx): (address?: AccountId | Address | string | null) => Observable { + return memo((address?: AccountId | Address | string | null): Observable => { + const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections'; + + return ( + combineLatest([ + address && api.query[councilSection]?.members + ? api.query[councilSection].members() + : of(undefined), + address && api.query.council.members + ? api.query.council.members() + : of([]), + address && api.query.technicalCommittee?.members + ? api.query.technicalCommittee.members() + : of([]), + address && api.query.society?.members + ? api.query.society.members() + : of([]), + address && api.query.sudo?.key + ? api.query.sudo.key() + : of(undefined) + ]) as Observable + ).pipe( + map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => { + const isIncluded = address ? isIncludedFn(address) : (): boolean => false; + + return { + isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded), + isSociety: (societyMembers || []).some(isIncluded), + isSudo: sudoKey?.toString() === address?.toString(), + isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded) + }; + }) + ); + }); +} diff --git a/packages/api-derive/src/accounts/index.ts b/packages/api-derive/src/accounts/index.ts index f84b5e0f03..363a4e72f2 100644 --- a/packages/api-derive/src/accounts/index.ts +++ b/packages/api-derive/src/accounts/index.ts @@ -2,6 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +export * from './flags'; export * from './idAndIndex'; export * from './idToIndex'; export * from './indexToId'; diff --git a/packages/api-derive/src/accounts/types.ts b/packages/api-derive/src/accounts/types.ts index 9c85e355fb..eb9c68e42e 100644 --- a/packages/api-derive/src/accounts/types.ts +++ b/packages/api-derive/src/accounts/types.ts @@ -23,6 +23,13 @@ export interface DeriveAccountRegistration { judgements: RegistrationJudgement[]; } +export interface DeriveAccountFlags { + isCouncil: boolean; + isSociety: boolean; + isSudo: boolean; + isTechCommittee: boolean; +} + export interface DeriveAccountInfo { accountId?: AccountId; accountIndex?: AccountIndex;