36 lines
1.6 KiB
TypeScript
36 lines
1.6 KiB
TypeScript
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
|
import type { EraIndex } from '@polkadot/types/interfaces';
|
|
import type { Observable } from '@polkadot/x-rxjs';
|
|
import type { DeriveStakerPoints } from '../types';
|
|
|
|
import { map, switchMap } from '@polkadot/x-rxjs/operators';
|
|
|
|
import { memo } from '../util';
|
|
|
|
export function _stakerPoints (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerPoints[]> {
|
|
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable<DeriveStakerPoints[]> => {
|
|
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
|
|
|
return api.derive.staking._erasPoints(eras, withActive).pipe(
|
|
map((points): DeriveStakerPoints[] =>
|
|
points.map(({ era, eraPoints, validators }): DeriveStakerPoints => ({
|
|
era,
|
|
eraPoints,
|
|
points: validators[stakerId] || api.registry.createType('RewardPoint')
|
|
}))
|
|
)
|
|
);
|
|
});
|
|
}
|
|
|
|
export function stakerPoints (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerPoints[]> {
|
|
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerPoints[]> =>
|
|
api.derive.staking.erasHistoric(withActive).pipe(
|
|
switchMap((eras) => api.derive.staking._stakerPoints(accountId, eras, withActive))
|
|
)
|
|
);
|
|
}
|