Cater for new non-linked map validator queries (#2055)

* Cater for new non-linked map validator queries

* Update staking tests

* stakers -> stashes
This commit is contained in:
Jaco Greeff
2020-03-16 11:13:46 +01:00
committed by GitHub
parent c06f260d3c
commit 82c1e3f35a
5 changed files with 34 additions and 36 deletions
+1 -1
View File
@@ -61,7 +61,7 @@ describe('derive', (): void => {
testFunction(api)('session', 'sessionProgress', []);
testFunction(api)('staking', 'account', []);
testFunction(api)('staking', 'controllers', []);
testFunction(api)('staking', 'stashes', []);
});
describe('custom', (): void => {
@@ -1,33 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, ValidatorPrefs } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { Observable, combineLatest, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Option, Vec } from '@polkadot/types';
import { memo } from '../util';
type DeriveControllers = [AccountId[], Option<AccountId>[]];
/**
* @description From the list of stash accounts, retrieve the list of controllers
*/
export function controllers (api: ApiInterfaceRx): () => Observable<DeriveControllers> {
return memo((): Observable<[AccountId[], Option<AccountId>[]]> =>
api.query.staking.validators<ITuple<[Vec<AccountId>, Vec<ValidatorPrefs>]>>().pipe(
switchMap(([stashIds]): Observable<DeriveControllers> =>
combineLatest([
of(stashIds),
// for V2, don't return all the controllers, we call bonded at a later point
api.consts.session
? of([])
: api.query.staking.bonded.multi<Option<AccountId>>(stashIds)
])
)
));
}
+1 -1
View File
@@ -9,7 +9,6 @@ export * from './erasPoints';
export * from './erasPrefs';
export * from './erasRewards';
export * from './erasSlashes';
export * from './controllers';
export * from './electedInfo';
export * from './overview';
export * from './ownExposure';
@@ -19,4 +18,5 @@ export * from './stakerExposure';
export * from './stakerPoints';
export * from './stakerRewards';
export * from './stakerSlashes';
export * from './stashes';
export * from './validators';
@@ -0,0 +1,28 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, ValidatorPrefs } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Vec } from '@polkadot/types';
import { memo } from '../util';
/**
* @description Retrieve the list of all validator stashes
*/
export function stashes (api: ApiInterfaceRx): () => Observable<AccountId[]> {
return memo((): Observable<AccountId[]> =>
api.query.staking.validators.creator.meta.type.asMap.linked.isTrue
? api.query.staking.validators<ITuple<[Vec<AccountId>, Vec<ValidatorPrefs>]>>().pipe(
map(([stashIds]) => stashIds)
)
: api.query.staking.validators.keys().pipe(
map((keys) => keys.map((key) => key.args[0] as AccountId))
)
);
}