Minor readability enhancements (#1313)
* Minor readability enhancements * Split unwieldly ApiBase * Fix import case * Some movements, init in root * Simplify overloaded functions * Some more reworks * Hoist non-class functions * Flatten * Split decorate and init * Split meta from chain * getNext cleanup
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber, Header } from '@polkadot/types/interfaces';
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
@@ -24,10 +24,8 @@ import { drr } from '../util/drr';
|
||||
*/
|
||||
export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeNewHead()
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
map((header: Header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeNewHead().pipe(
|
||||
map((header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber, Header } from '@polkadot/types/interfaces';
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
@@ -25,10 +25,8 @@ import { drr } from '../util/drr';
|
||||
*/
|
||||
export function bestNumberFinalized (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeFinalizedHeads()
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
map((header: Header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeFinalizedHeads().pipe(
|
||||
map((header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { AccountId, Header } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { filter, map, switchMap } from 'rxjs/operators';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { HeaderExtended } from '../type';
|
||||
import { drr } from '../util/drr';
|
||||
@@ -28,23 +28,21 @@ export type HeaderAndValidators = [Header, AccountId[]];
|
||||
*/
|
||||
export function subscribeNewHead (api: ApiInterfaceRx): () => Observable<HeaderExtended> {
|
||||
return (): Observable<HeaderExtended> =>
|
||||
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
switchMap((header: Header): Observable<HeaderAndValidators> =>
|
||||
(combineLatest([
|
||||
of(header),
|
||||
// theoretically we could combine at the first call with session.validators(), however
|
||||
// we make 100% sure we actually get the validators at a specific block so when these
|
||||
// change at an era boundary, we have the previous values to ensure our indexes are correct
|
||||
api.query.session
|
||||
? api.query.session.validators.at(header.hash)
|
||||
: of([])
|
||||
]) as Observable<HeaderAndValidators>)
|
||||
),
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeNewHead().pipe(
|
||||
switchMap((header: Header): Observable<HeaderAndValidators> =>
|
||||
(combineLatest([
|
||||
of(header),
|
||||
// theoretically we could combine at the first call with session.validators(), however
|
||||
// we make 100% sure we actually get the validators at a specific block so when these
|
||||
// change at an era boundary, we have the previous values to ensure our indexes are correct
|
||||
api.query.session
|
||||
? api.query.session.validators.at(header.hash)
|
||||
: of([])
|
||||
]) as Observable<HeaderAndValidators>)
|
||||
),
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ type MultiResult = [Option<Keys>, [
|
||||
Option<AccountId> | Vec<[AccountId, Keys] & Codec>, Option<StakingLedger>, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]
|
||||
]];
|
||||
|
||||
// groups the supplied chunks by era, i.e. { [era]: BN(total of values) }
|
||||
function groupByEra (list: UnlockChunk[]): Record<string, BN> {
|
||||
return list.reduce((map: Record<string, BN>, { era, value }): Record<string, BN> => {
|
||||
const key = era.toString();
|
||||
@@ -38,6 +39,7 @@ function groupByEra (list: UnlockChunk[]): Record<string, BN> {
|
||||
}, {});
|
||||
}
|
||||
|
||||
// calculate the remining blocks in a specific unlock era
|
||||
function remainingBlocks (era: BN, eraLength: BN, bestNumber: BlockNumber): BN {
|
||||
const remaining = eraLength.mul(era).sub(bestNumber);
|
||||
|
||||
@@ -46,15 +48,19 @@ function remainingBlocks (era: BN, eraLength: BN, bestNumber: BlockNumber): BN {
|
||||
: remaining;
|
||||
}
|
||||
|
||||
// select the Unlockchunks that can't be redeemed yet
|
||||
function calcChunks (stakingLedger: StakingLedger, eraLength: BN, bestNumber: BlockNumber): UnlockChunk[] {
|
||||
return stakingLedger.unlocking.filter((chunk): boolean =>
|
||||
remainingBlocks(chunk.era.unwrap(), eraLength, bestNumber).gtn(0)
|
||||
);
|
||||
}
|
||||
|
||||
function calculateUnlocking (stakingLedger: StakingLedger | undefined, eraLength: BN, bestNumber: BlockNumber): DerivedUnlocking | undefined {
|
||||
if (isUndefined(stakingLedger)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// select the Unlockchunks that can't be redeemed yet.
|
||||
const unlockingChunks = stakingLedger.unlocking.filter((chunk): boolean =>
|
||||
remainingBlocks(chunk.era.unwrap(), eraLength, bestNumber).gtn(0)
|
||||
);
|
||||
const unlockingChunks = calcChunks(stakingLedger, eraLength, bestNumber);
|
||||
|
||||
if (!unlockingChunks.length) {
|
||||
return undefined;
|
||||
@@ -75,13 +81,9 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN,
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
return stakingLedger.unlocking
|
||||
.filter((chunk): boolean =>
|
||||
remainingBlocks(chunk.era.unwrap(), eraLength, bestNumber).eqn(0)
|
||||
)
|
||||
.reduce((curr, prev): BN =>
|
||||
curr.add(prev.value.unwrap()), new BN(0)
|
||||
);
|
||||
return calcChunks(stakingLedger, eraLength, bestNumber).reduce((curr, prev): BN =>
|
||||
curr.add(prev.value.unwrap()), new BN(0)
|
||||
);
|
||||
}
|
||||
|
||||
function unwrapSessionIds (stashId: AccountId, queuedKeys: Option<AccountId> | Vec<[AccountId, Keys] & Codec>, nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; nextSessionId?: AccountId; sessionIds: AccountId[]; sessionId?: AccountId } {
|
||||
|
||||
Reference in New Issue
Block a user