Files
api/packages/api-derive/src/staking/erasPoints.ts
T
Jaco Greeff 161c121cb2 Cleanups for maintainability (#2809)
* Cleanups for maintainability

* Split referendumsInfo

* Extract didUpdate -> bool

* Era filtering

* Default to council

* Points cache

* Remove cache

* Move map arg
2020-10-30 14:28:38 +01:00

72 lines
2.5 KiB
TypeScript

// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { ApiInterfaceRx } from '@polkadot/api/types';
import { EraIndex, EraRewardPoints } from '@polkadot/types/interfaces';
import { DeriveEraPoints, DeriveEraValPoints } from '../types';
import { Observable, of } from 'rxjs';
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';
function mapValidators ({ individual }: EraRewardPoints): DeriveEraValPoints {
return [...individual.entries()]
.filter(([, points]) => points.gt(BN_ZERO))
.reduce((result: DeriveEraValPoints, [validatorId, points]): DeriveEraValPoints => {
result[validatorId.toString()] = points;
return result;
}, {});
}
function mapPoints (eras: EraIndex[], points: EraRewardPoints[]): DeriveEraPoints[] {
return eras.map((era, index): DeriveEraPoints => ({
era,
eraPoints: points[index].total,
validators: mapValidators(points[index])
}));
}
export function _erasPoints (instanceId: string, api: ApiInterfaceRx): (eras: EraIndex[], withActive: boolean) => Observable<DeriveEraPoints[]> {
return memo(instanceId, (eras: EraIndex[], withActive: boolean): Observable<DeriveEraPoints[]> => {
if (!eras.length) {
return of([]);
}
const cached: DeriveEraPoints[] = withActive
? []
: eras
.map((era) => deriveCache.get<DeriveEraPoints>(`${CACHE_KEY}-${era.toString()}`))
.filter((value): value is DeriveEraPoints => !!value);
const remaining = filterEras(eras, cached);
return !remaining.length
? of(cached)
: api.query.staking.erasRewardPoints.multi<EraRewardPoints>(remaining).pipe(
map((points): DeriveEraPoints[] => {
const query = mapPoints(remaining, points);
!withActive && query.forEach((q) => deriveCache.set(`${CACHE_KEY}-${q.era.toString()}`, q));
return eras.map((era): DeriveEraPoints =>
cached.find((cached) => era.eq(cached.era)) ||
query.find((query) => era.eq(query.era)) as DeriveEraPoints
);
})
);
});
}
export function erasPoints (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPoints[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraPoints[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasPoints(eras, withActive))
)
);
}