Cleanups for maintainability (#2809)
* Cleanups for maintainability * Split referendumsInfo * Extract didUpdate -> bool * Era filtering * Default to council * Points cache * Remove cache * Move map arg
This commit is contained in:
@@ -10,6 +10,7 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { deriveCache, memo } from '../util';
|
||||
import { filterEras } from './util';
|
||||
|
||||
const CACHE_KEY = 'eraPoints';
|
||||
|
||||
@@ -42,7 +43,7 @@ export function _erasPoints (instanceId: string, api: ApiInterfaceRx): (eras: Er
|
||||
: eras
|
||||
.map((era) => deriveCache.get<DeriveEraPoints>(`${CACHE_KEY}-${era.toString()}`))
|
||||
.filter((value): value is DeriveEraPoints => !!value);
|
||||
const remaining = eras.filter((era) => !cached.some((cached) => era.eq(cached.era)));
|
||||
const remaining = filterEras(eras, cached);
|
||||
|
||||
return !remaining.length
|
||||
? of(cached)
|
||||
|
||||
@@ -10,6 +10,7 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { Option } from '@polkadot/types';
|
||||
|
||||
import { deriveCache, memo } from '../util';
|
||||
import { filterEras } from './util';
|
||||
|
||||
const CACHE_KEY = 'eraRewards';
|
||||
|
||||
@@ -31,7 +32,7 @@ export function _erasRewards (instanceId: string, api: ApiInterfaceRx): (eras: E
|
||||
: eras
|
||||
.map((era) => deriveCache.get<DeriveEraRewards>(`${CACHE_KEY}-${era.toString()}`))
|
||||
.filter((value): value is DeriveEraRewards => !!value);
|
||||
const remaining = eras.filter((era) => !cached.some((cached) => era.eq(cached.era)));
|
||||
const remaining = filterEras(eras, cached);
|
||||
|
||||
if (!remaining.length) {
|
||||
return of(cached);
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Option } from '@polkadot/types';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { DeriveStakingQuery } from './types';
|
||||
|
||||
type ErasResult = [{ unwrapOr: (value: BN) => BN }, DeriveEraPoints[], DeriveEraPrefs[], DeriveEraRewards[]];
|
||||
|
||||
@@ -99,24 +100,37 @@ function filterEras (eras: EraIndex[], stakingLedger: StakingLedger): EraIndex[]
|
||||
return eras.filter((era) => filterEra(era, stakingLedger));
|
||||
}
|
||||
|
||||
function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], migrateEra: BN, rewards: DeriveStakerReward[], stakingLedger: StakingLedger, withActive: boolean): Observable<DeriveStakerReward[]> {
|
||||
if (withActive) {
|
||||
return of(rewards);
|
||||
}
|
||||
function removeClaimed (validators: string[], queryValidators: DeriveStakingQuery[], reward: DeriveStakerReward): void {
|
||||
const rm: string[] = [];
|
||||
|
||||
Object.keys(reward.validators).forEach((validatorId): void => {
|
||||
const index = validators.indexOf(validatorId);
|
||||
|
||||
if (index !== -1) {
|
||||
const valLedger = queryValidators[index].stakingLedger;
|
||||
|
||||
if (valLedger?.claimedRewards.some((era) => reward.era.eq(era))) {
|
||||
rm.push(validatorId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rm.forEach((validatorId): void => {
|
||||
delete reward.validators[validatorId];
|
||||
});
|
||||
}
|
||||
|
||||
function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], { migrateEra, rewards, stakingLedger }: { migrateEra: BN, rewards: DeriveStakerReward[]; stakingLedger: StakingLedger }): Observable<DeriveStakerReward[]> {
|
||||
const validators = uniqValidators(rewards);
|
||||
const filter = filterEras(eras, stakingLedger);
|
||||
|
||||
return (
|
||||
isFunction(api.tx.staking.payoutStakers)
|
||||
? api.derive.staking.queryMulti(validators)
|
||||
: of([])
|
||||
).pipe(
|
||||
map((queryValidators): DeriveStakerReward[] => {
|
||||
const filter = withActive
|
||||
? eras
|
||||
: filterEras(eras, stakingLedger);
|
||||
|
||||
return rewards
|
||||
map((queryValidators): DeriveStakerReward[] =>
|
||||
rewards
|
||||
.filter(({ isEmpty }) => !isEmpty)
|
||||
.filter((reward): boolean => {
|
||||
if (!filter.some((filter) => reward.era.eq(filter))) {
|
||||
@@ -128,32 +142,16 @@ function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], migrateEra: BN, r
|
||||
|
||||
reward.isStakerPayout = true;
|
||||
|
||||
const rm: string[] = [];
|
||||
|
||||
Object.keys(reward.validators).forEach((validatorId): void => {
|
||||
const index = validators.indexOf(validatorId);
|
||||
|
||||
if (index !== -1) {
|
||||
const valLedger = queryValidators[index].stakingLedger;
|
||||
|
||||
if (valLedger?.claimedRewards.some((era) => reward.era.eq(era))) {
|
||||
rm.push(validatorId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rm.forEach((validatorId): void => {
|
||||
delete reward.validators[validatorId];
|
||||
});
|
||||
removeClaimed(validators, queryValidators, reward);
|
||||
|
||||
return true;
|
||||
})
|
||||
.filter(({ validators }) => Object.keys(validators).length !== 0)
|
||||
.map((reward) => ({
|
||||
...reward,
|
||||
nominators: reward.nominating.filter(({ validatorId }) => !!reward.validators[validatorId])
|
||||
}));
|
||||
})
|
||||
nominators: reward.nominating.filter((n) => !!reward.validators[n.validatorId])
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -178,13 +176,15 @@ export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accou
|
||||
api.derive.staking._stakerRewardsEras(eras, withActive)
|
||||
]).pipe(
|
||||
switchMap(([{ stakingLedger, stashId }, exposures, erasResult]): Observable<DeriveStakerReward[]> => {
|
||||
const migrateEra: BN = erasResult[0].unwrapOr(ZERO);
|
||||
|
||||
if (!stashId || !stakingLedger) {
|
||||
return of([]);
|
||||
}
|
||||
|
||||
return filterRewards(api, eras, migrateEra, parseRewards(api, stashId, erasResult, exposures), stakingLedger, withActive);
|
||||
const rewards = parseRewards(api, stashId, erasResult, exposures);
|
||||
|
||||
return withActive
|
||||
? of(rewards)
|
||||
: filterRewards(api, eras, { migrateEra: erasResult[0].unwrapOr(ZERO), rewards, stakingLedger });
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { EraIndex } from '@polkadot/types/interfaces';
|
||||
|
||||
export function filterEras <T extends { era: EraIndex }> (eras: EraIndex[], list: T[]): EraIndex[] {
|
||||
return eras.filter((era) => !list.some((entry) => era.eq(entry.era)));
|
||||
}
|
||||
Reference in New Issue
Block a user