// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx, QueryableStorageMultiArg } from '@polkadot/api/types'; import type { Option } from '@polkadot/types'; import type { BalanceOf, EraIndex, Perbill } from '@polkadot/types/interfaces'; import type { ITuple } from '@polkadot/types/types'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveStakerSlashes } from '../types'; import { of } from '@polkadot/x-rxjs'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable { // eslint-disable-next-line @typescript-eslint/no-unused-vars return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => eras.length ? api.queryMulti<(Option> | Option)[]>([ ...eras.map((era): QueryableStorageMultiArg<'rxjs'> => [api.query.staking.validatorSlashInEra, [era, accountId]]), ...eras.map((era): QueryableStorageMultiArg<'rxjs'> => [api.query.staking.nominatorSlashInEra, [era, accountId]]) ]).pipe( map((values): DeriveStakerSlashes[] => eras.map((era, index) => ({ era, total: values[index].isSome ? (values[index].unwrap() as ITuple<[Perbill, BalanceOf]>)[1] : (values[index + eras.length].unwrapOrDefault() as BalanceOf) })) ) ) : of([]) ); } export function ownSlash (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, era: EraIndex) => Observable { return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable => api.derive.staking._ownSlashes(accountId, [era], true).pipe( map(([first]) => first) ) ); } export function ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable { return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable => { return api.derive.staking.erasHistoric(withActive).pipe( switchMap((eras) => api.derive.staking._ownSlashes(accountId, eras, withActive) ) ); }); }