Add new staking types, support derives (#1828)

* Add new staking types

* StakingLedgerTo213 - StakingLedgerTo223

* StakingLedgerTo223 in polkadot (dev)

* Add derive activeEra

* Map Ponts -> RewardPoints

* StakingLedger override CHANGELOG

* Cleanups

* Apparent CC dupes (don't see it, but cleanup useul)

* Cleanup result formatting for indexes

* Map nextElected -> validators

* Fix StakingLedger to current

* Merge CHANGELOG

* Mention derive staking updates

* Mark derive changes as breaking
This commit is contained in:
Jaco Greeff
2020-02-24 16:52:48 +01:00
committed by GitHub
parent 64aa4dcc1c
commit cbc14cc291
14 changed files with 394 additions and 206 deletions
+68 -14
View File
@@ -3,29 +3,83 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { EraIndex, SessionIndex } from '@polkadot/types/interfaces';
import { EraIndex, MomentOf, SessionIndex } from '@polkadot/types/interfaces';
import { DeriveSessionIndexes } from '../types';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { createType, u32 as U32 } from '@polkadot/types';
import { createType, Option, u32 } from '@polkadot/types';
import { memo } from '../util';
type Result = [EraIndex, Option<MomentOf>, EraIndex, SessionIndex, u32];
// parse into Indexes
function parse ([activeEra, activeEraStart, currentEra, currentIndex, validatorCount]: Result): DeriveSessionIndexes {
return {
activeEra,
activeEraStart,
currentEra,
currentIndex,
validatorCount
};
}
// query for previous V2
function queryNoActive (api: ApiInterfaceRx): Observable<Result> {
return api.queryMulti<[EraIndex, SessionIndex, u32]>([
api.query.staking.currentEra,
api.query.session.currentIndex,
api.query.staking.validatorCount
]).pipe(
map(([currentEra, currentIndex, validatorCount]): Result => [
currentEra,
createType(api.registry, 'Option<MomentOf>'),
currentEra,
currentIndex,
validatorCount
])
);
}
// query based on latest
function query (api: ApiInterfaceRx): Observable<Result> {
return api.queryMulti<[Option<EraIndex>, Option<MomentOf>, Option<EraIndex>, SessionIndex, u32]>([
api.query.staking.activeEra,
api.query.staking.activeEraStart,
api.query.staking.currentEra,
api.query.session.currentIndex,
api.query.staking.validatorCount
]).pipe(
map(([activeEra, activeEraStart, currentEra, currentIndex, validatorCount]): Result => [
activeEra.unwrapOr(createType(api.registry, 'EraIndex', 1)),
activeEraStart,
currentEra.unwrapOr(createType(api.registry, 'EraIndex', 1)),
currentIndex,
validatorCount
])
);
}
// empty set when none is available
function empty (api: ApiInterfaceRx): Observable<Result> {
return of([
createType(api.registry, 'EraIndex', 1),
createType(api.registry, 'Option<MomentOf>'),
createType(api.registry, 'EraIndex', 1),
createType(api.registry, 'SessionIndex', 1),
createType(api.registry, 'u32')
]);
}
export function indexes (api: ApiInterfaceRx): () => Observable<DeriveSessionIndexes> {
return memo((): Observable<DeriveSessionIndexes> =>
(
// 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(api.registry, 'SessionIndex', 1), createType(api.registry, 'EraIndex', 1), createType(api.registry, 'u32')])
).pipe(
map(([currentIndex, currentEra, validatorCount]): DeriveSessionIndexes => ({
currentIndex, currentEra, validatorCount
}))
));
? api.query.staking.activeEra
? query(api)
: queryNoActive(api)
: empty(api)
).pipe(map(parse))
);
}