Rework eras* calls with minimum information (#2018)

This commit is contained in:
Jaco Greeff
2020-03-10 08:20:54 +01:00
committed by GitHub
parent 201098e9f6
commit 049a1a59f5
7 changed files with 122 additions and 92 deletions
@@ -1,39 +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 { Exposure } from '@polkadot/types/interfaces';
import { DeriveEraRewardsAll } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { memo } from '../util';
function includeExposure (validatorId: string, { others }: Exposure, nominators: Record<string, [string, number][]>): void {
others.forEach(({ who }, index): void => {
const nominatorId = who.toString();
nominators[nominatorId] = nominators[nominatorId] || [];
nominators[nominatorId].push([validatorId, index]);
});
}
export function erasRewards (api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraRewardsAll[]> {
return memo((withActive?: boolean): Observable<DeriveEraRewardsAll[]> =>
api.derive.staking.erasExposure(withActive).pipe(
map((exposures): DeriveEraRewardsAll[] =>
exposures.map(({ era, eraPoints, eraReward, validators }): DeriveEraRewardsAll =>
Object
.entries(validators)
.reduce((rewards: DeriveEraRewardsAll, [validatorId, { exposure }]): DeriveEraRewardsAll => {
includeExposure(validatorId, exposure, rewards.nominators);
return rewards;
}, { era, eraPoints, eraReward, nominators: {}, validators })
)
)
)
);
}