Skip to content

Commit b18bdc3

Browse files
authored
Add api.derive.accounts.flags (#2195)
* account flags in new call * Fix
1 parent 79847aa commit b18bdc3

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

packages/api-derive/src/accounts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// This software may be modified and distributed under the terms
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

5+
export * from './flags';
56
export * from './idAndIndex';
67
export * from './idToIndex';
78
export * from './indexToId';

packages/api-derive/src/accounts/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export interface DeriveAccountRegistration {
2323
judgements: RegistrationJudgement[];
2424
}
2525

26+
export interface DeriveAccountFlags {
27+
isCouncil: boolean;
28+
isSociety: boolean;
29+
isSudo: boolean;
30+
isTechCommittee: boolean;
31+
}
32+
2633
export interface DeriveAccountInfo {
2734
accountId?: AccountId;
2835
accountIndex?: AccountIndex;

0 commit comments

Comments
 (0)