|
| 1 | +// Copyright 2017-2020 @polkadot/api-derive authors & contributors |
| 2 | +// This software may be modified and distributed under the terms |
| 3 | +// of the Apache-2.0 license. See the LICENSE file for details. |
| 4 | + |
| 5 | +import { ApiInterfaceRx } from '@polkadot/api/types'; |
| 6 | +import { AccountId, Address, Balance } from '@polkadot/types/interfaces'; |
| 7 | +import { ITuple } from '@polkadot/types/types'; |
| 8 | +import { DeriveAccountFlags } from '../types'; |
| 9 | + |
| 10 | +import { Observable, combineLatest, of } from 'rxjs'; |
| 11 | +import { map } from 'rxjs/operators'; |
| 12 | +import { Vec } from '@polkadot/types'; |
| 13 | + |
| 14 | +import { memo } from '../util'; |
| 15 | + |
| 16 | +type FlagsIntermediate = [ |
| 17 | + Vec<ITuple<[AccountId, Balance]>> | undefined, |
| 18 | + AccountId[], |
| 19 | + AccountId[], |
| 20 | + AccountId[], |
| 21 | + AccountId | undefined |
| 22 | +]; |
| 23 | + |
| 24 | +function isIncludedFn (accountId: AccountId | Address | string): (_: AccountId | Address |string) => boolean { |
| 25 | + return function (id: AccountId | Address | string): boolean { |
| 26 | + return id.toString() === accountId.toString(); |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * @name info |
| 32 | + * @description Returns account membership flags |
| 33 | + */ |
| 34 | +export function flags (api: ApiInterfaceRx): (address?: AccountId | Address | string | null) => Observable<DeriveAccountFlags> { |
| 35 | + return memo((address?: AccountId | Address | string | null): Observable<DeriveAccountFlags> => { |
| 36 | + const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections'; |
| 37 | + |
| 38 | + return ( |
| 39 | + combineLatest([ |
| 40 | + address && api.query[councilSection]?.members |
| 41 | + ? api.query[councilSection].members() |
| 42 | + : of(undefined), |
| 43 | + address && api.query.council.members |
| 44 | + ? api.query.council.members() |
| 45 | + : of([]), |
| 46 | + address && api.query.technicalCommittee?.members |
| 47 | + ? api.query.technicalCommittee.members() |
| 48 | + : of([]), |
| 49 | + address && api.query.society?.members |
| 50 | + ? api.query.society.members() |
| 51 | + : of([]), |
| 52 | + address && api.query.sudo?.key |
| 53 | + ? api.query.sudo.key() |
| 54 | + : of(undefined) |
| 55 | + ]) as Observable<FlagsIntermediate> |
| 56 | + ).pipe( |
| 57 | + map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => { |
| 58 | + const isIncluded = address ? isIncludedFn(address) : (): boolean => false; |
| 59 | + |
| 60 | + return { |
| 61 | + isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded), |
| 62 | + isSociety: (societyMembers || []).some(isIncluded), |
| 63 | + isSudo: sudoKey?.toString() === address?.toString(), |
| 64 | + isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded) |
| 65 | + }; |
| 66 | + }) |
| 67 | + ); |
| 68 | + }); |
| 69 | +} |
0 commit comments