55import { ApiInterfaceRx } from '@polkadot/api/types' ;
66import { AccountId , AccountIndex , Address , Balance , Registration } from '@polkadot/types/interfaces' ;
77import { ITuple } from '@polkadot/types/types' ;
8- import { DeriveAccountInfo , DeriveAccountRegistration } from '../types' ;
8+ import { DeriveAccountFlags , DeriveAccountInfo , DeriveAccountRegistration } from '../types' ;
99
1010import { Observable , combineLatest , of } from 'rxjs' ;
1111import { map , switchMap } from 'rxjs/operators' ;
12- import { Bytes , Data , Option , u32 } from '@polkadot/types' ;
12+ import { Bytes , Data , Option , u32 , Vec } from '@polkadot/types' ;
1313import { u8aToString } from '@polkadot/util' ;
1414
1515import { 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+
2531function 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 */
116155export 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}
0 commit comments