Skip to content

Commit aa37e37

Browse files
kwingram25jacogr
andauthored
Derived account info flags (#2068)
* Derived account info flags * Update packages/api-derive/src/accounts/info.ts Co-Authored-By: Jaco Greeff <[email protected]> * Apply suggestions from code review * Cleanups Co-authored-by: Jaco Greeff <[email protected]>
1 parent 8cdac77 commit aa37e37

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import { ApiInterfaceRx } from '@polkadot/api/types';
66
import { AccountId, AccountIndex, Address, Balance, Registration } from '@polkadot/types/interfaces';
77
import { ITuple } from '@polkadot/types/types';
8-
import { DeriveAccountInfo, DeriveAccountRegistration } from '../types';
8+
import { DeriveAccountFlags, DeriveAccountInfo, DeriveAccountRegistration } from '../types';
99

1010
import { Observable, combineLatest, of } from 'rxjs';
1111
import { map, switchMap } from 'rxjs/operators';
12-
import { Bytes, Data, Option, u32 } from '@polkadot/types';
12+
import { Bytes, Data, Option, u32, Vec } from '@polkadot/types';
1313
import { u8aToString } from '@polkadot/util';
1414

1515
import { memo } from '../util';
@@ -22,6 +22,12 @@ function dataAsString (data: Data): string | undefined {
2222
: undefined;
2323
}
2424

25+
function includedWrapper (accountId?: AccountId): (_: AccountId) => boolean {
26+
return function (id: AccountId): boolean {
27+
return id.eq(accountId);
28+
};
29+
}
30+
2531
function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<string | undefined> {
2632
return ((
2733
accountId && api.query.nicks?.nameOf
@@ -109,22 +115,56 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab
109115
);
110116
}
111117

118+
function retrieveFlags (api: ApiInterfaceRx, accountId?: AccountId): Observable<DeriveAccountFlags> {
119+
const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections';
120+
121+
return combineLatest([
122+
api.query[councilSection]?.members
123+
? api.query[councilSection].members<Vec<ITuple<[AccountId, Balance]>>>()
124+
: of(undefined),
125+
api.query.council?.members
126+
? api.query.council.members()
127+
: of([]),
128+
api.query.technicalCommittee?.members
129+
? api.query.technicalCommittee.members()
130+
: of([]),
131+
api.query.society?.members
132+
? api.query.society.members()
133+
: of([]),
134+
api.query.sudo?.key
135+
? api.query.sudo.key()
136+
: of(undefined)
137+
]).pipe(
138+
map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => {
139+
const checkIncluded = includedWrapper(accountId);
140+
141+
return {
142+
isCouncil: (electionsMembers?.map(([id]) => id) || councilMembers || []).some(checkIncluded),
143+
isTechCommittee: technicalCommitteeMembers.some(checkIncluded),
144+
isSociety: societyMembers.some(checkIncluded),
145+
isSudo: !!sudoKey && sudoKey.eq(accountId)
146+
};
147+
})
148+
);
149+
}
150+
112151
/**
113152
* @name info
114153
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
115154
*/
116155
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
117156
return memo((address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
118157
api.derive.accounts.idAndIndex(address).pipe(
119-
switchMap(([accountId, accountIndex]): Observable<[Partial<DeriveAccountInfo>, DeriveAccountRegistration, string?]> =>
158+
switchMap(([accountId, accountIndex]): Observable<[Partial<DeriveAccountInfo>, DeriveAccountFlags, DeriveAccountRegistration, string?]> =>
120159
combineLatest([
121160
of({ accountId, accountIndex }),
161+
retrieveFlags(api, accountId),
122162
retrieveIdentity(api, accountId),
123163
retrieveNick(api, accountId)
124164
])
125165
),
126-
map(([{ accountId, accountIndex }, identity, nickname]): DeriveAccountInfo => ({
127-
accountId, accountIndex, identity, nickname
166+
map(([{ accountId, accountIndex }, flags, identity, nickname]): DeriveAccountInfo => ({
167+
accountId, accountIndex, ...flags, identity, nickname
128168
}))
129169
));
130170
}

packages/api-derive/src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ export interface DeriveAccountRegistration {
2828
judgements: RegistrationJudgement[];
2929
}
3030

31-
export interface DeriveAccountInfo {
31+
export interface DeriveAccountFlags {
32+
isCouncil: boolean;
33+
isSociety: boolean;
34+
isSudo: boolean;
35+
isTechCommittee: boolean;
36+
}
37+
38+
export interface DeriveAccountInfo extends DeriveAccountFlags {
3239
accountId?: AccountId;
3340
accountIndex?: AccountIndex;
3441
identity: DeriveAccountRegistration;

0 commit comments

Comments
 (0)