// Copyright 2017-2021 @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 { DeriveStakerSlashes } from '../types'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; export function _stakerSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable { return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable => { const stakerId = api.registry.createType('AccountId', accountId).toString(); return api.derive.staking._erasSlashes(eras, withActive).pipe( map((slashes): DeriveStakerSlashes[] => slashes.map(({ era, nominators, validators }): DeriveStakerSlashes => ({ era, total: nominators[stakerId] || validators[stakerId] || api.registry.createType('Balance') })) ) ); }); } export function stakerSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable { return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable => api.derive.staking.erasHistoric(withActive).pipe( switchMap((eras) => api.derive.staking._stakerSlashes(accountId, eras, withActive)) ) ); }