@@ -54,41 +54,17 @@ function getConstants (api: ApiInterfaceRx, elections: string | null): Partial<D
5454 : { } ;
5555}
5656
57- function queryElections ( api : ApiInterfaceRx ) : Observable < DeriveElectionsInfo > {
57+ function getModules ( api : ApiInterfaceRx ) : [ string , string | null ] {
58+ const [ council ] = api . registry . getModuleInstances ( api . runtimeVersion . specName . toString ( ) , 'council' ) || [ 'council' ] ;
5859 const elections = api . query . phragmenElection
5960 ? 'phragmenElection'
6061 : api . query . electionsPhragmen
6162 ? 'electionsPhragmen'
6263 : api . query . elections
6364 ? 'elections'
6465 : null ;
65- const [ council ] = api . registry . getModuleInstances ( api . runtimeVersion . specName . toString ( ) , 'council' ) || [ 'council' ] ;
6666
67- return (
68- elections
69- ? api . queryMulti < [ Vec < AccountId > , Vec < Candidate > , Vec < Member > , Vec < Member > ] > ( [
70- api . query [ council ] . members ,
71- api . query [ elections ] . candidates ,
72- api . query [ elections ] . members ,
73- api . query [ elections ] . runnersUp
74- ] )
75- : combineLatest ( [
76- api . query [ council ] . members < Vec < AccountId > > ( ) ,
77- of < Candidate [ ] > ( [ ] ) ,
78- of < Member [ ] > ( [ ] ) ,
79- of < Member [ ] > ( [ ] )
80- ] )
81- ) . pipe (
82- map ( ( [ councilMembers , candidates , members , runnersUp ] ) : DeriveElectionsInfo => ( {
83- ...getConstants ( api , elections ) ,
84- candidateCount : api . registry . createType ( 'u32' , candidates . length ) ,
85- candidates : candidates . map ( getCandidate ) ,
86- members : members . length
87- ? members . map ( getAccountTuple ) . sort ( sortAccounts )
88- : councilMembers . map ( ( accountId ) : [ AccountId , Balance ] => [ accountId , api . registry . createType ( 'Balance' ) ] ) ,
89- runnersUp : runnersUp . map ( getAccountTuple ) . sort ( sortAccounts )
90- } ) )
91- ) ;
67+ return [ council , elections ] ;
9268}
9369
9470/**
@@ -105,5 +81,33 @@ function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
10581 * ```
10682 */
10783export function info ( instanceId : string , api : ApiInterfaceRx ) : ( ) => Observable < DeriveElectionsInfo > {
108- return memo ( instanceId , ( ) : Observable < DeriveElectionsInfo > => queryElections ( api ) ) ;
84+ return memo ( instanceId , ( ) : Observable < DeriveElectionsInfo > => {
85+ const [ council , elections ] = getModules ( api ) ;
86+
87+ return (
88+ elections
89+ ? api . queryMulti < [ Vec < AccountId > , Vec < Candidate > , Vec < Member > , Vec < Member > ] > ( [
90+ api . query [ council ] . members ,
91+ api . query [ elections ] . candidates ,
92+ api . query [ elections ] . members ,
93+ api . query [ elections ] . runnersUp
94+ ] )
95+ : combineLatest ( [
96+ api . query [ council ] . members < Vec < AccountId > > ( ) ,
97+ of < Candidate [ ] > ( [ ] ) ,
98+ of < Member [ ] > ( [ ] ) ,
99+ of < Member [ ] > ( [ ] )
100+ ] )
101+ ) . pipe (
102+ map ( ( [ councilMembers , candidates , members , runnersUp ] ) : DeriveElectionsInfo => ( {
103+ ...getConstants ( api , elections ) ,
104+ candidateCount : api . registry . createType ( 'u32' , candidates . length ) ,
105+ candidates : candidates . map ( getCandidate ) ,
106+ members : members . length
107+ ? members . map ( getAccountTuple ) . sort ( sortAccounts )
108+ : councilMembers . map ( ( a ) : [ AccountId , Balance ] => [ a , api . registry . createType ( 'Balance' ) ] ) ,
109+ runnersUp : runnersUp . map ( getAccountTuple ) . sort ( sortAccounts )
110+ } ) )
111+ ) ;
112+ } ) ;
109113}
0 commit comments