Detect session.validators for derive extraction (#1562)

* Detect session.validators for derive extraction

* Add comment

* Indexed extraction without session
This commit is contained in:
Jaco Greeff
2019-11-10 12:25:34 +01:00
committed by GitHub
parent 06418913e5
commit 3c47c3fdc3
3 changed files with 24 additions and 13 deletions
+11 -5
View File
@@ -6,7 +6,7 @@ import { AccountId } from '@polkadot/types/interfaces';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveStakingValidators } from '../types';
import { Observable } from 'rxjs';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Vec } from '@polkadot/types';
@@ -17,10 +17,16 @@ import { drr } from '../util';
*/
export function validators (api: ApiInterfaceRx): () => Observable<DeriveStakingValidators> {
return (): Observable<DeriveStakingValidators> =>
api.queryMulti<[Vec<AccountId>, Vec<AccountId>]>([
api.query.session.validators,
api.query.staking.currentElected
]).pipe(
(
// Sadly the node-template is (for some obscure reason) not comprehensive, so while the derive works
// in all actual real-world deployed chains, it does create some confusion for limited template chains
api.query.session && api.query.staking
? api.queryMulti<[Vec<AccountId>, Vec<AccountId>]>([
api.query.session.validators,
api.query.staking.currentElected
])
: of([[], []] as [AccountId[], AccountId[]])
).pipe(
map(([validators, currentElected]): DeriveStakingValidators => ({
currentElected, validators
})),