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
+12 -7
View File
@@ -6,19 +6,24 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
import { EraIndex, SessionIndex } from '@polkadot/types/interfaces';
import { DeriveSessionIndexes } from '../types';
import { Observable } from 'rxjs';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { u32 } from '@polkadot/types';
import { createType, u32 as U32 } from '@polkadot/types';
import { drr } from '../util';
export function indexes (api: ApiInterfaceRx): () => Observable<DeriveSessionIndexes> {
return (): Observable<DeriveSessionIndexes> =>
api.queryMulti<[SessionIndex, EraIndex, u32]>([
api.query.session.currentIndex,
api.query.staking.currentEra,
api.query.staking.validatorCount
]).pipe(
(
// Some chains (eg. very limited node-template), does not have session
api.query.session && api.query.staking
? api.queryMulti<[SessionIndex, EraIndex, U32]>([
api.query.session.currentIndex,
api.query.staking.currentEra,
api.query.staking.validatorCount
])
: of([createType('SessionIndex', 1), createType('EraIndex', 1), new U32()])
).pipe(
map(([currentIndex, currentEra, validatorCount]): DeriveSessionIndexes => ({
currentIndex, currentEra, validatorCount
})),
+1 -1
View File
@@ -25,7 +25,7 @@ export function overview (api: ApiInterfaceRx): () => Observable<DerivedStakingO
combineLatest([
of({ currentElected, currentEra, currentIndex, validators, validatorCount }),
// this will change on a per block basis, keep it innermost (and it needs eraIndex)
api.query.staking.currentEraPointsEarned
api.query.staking?.currentEraPointsEarned
? api.query.staking.currentEraPointsEarned<EraPoints>(currentEra)
: of(createType('EraPoints'))
])
+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
})),