54 lines
2.5 KiB
TypeScript
54 lines
2.5 KiB
TypeScript
// 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<DeriveStakerSlashes[]> {
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable<DeriveStakerSlashes[]> =>
|
|
eras.length
|
|
? api.queryMulti<(Option<ITuple<[Perbill, BalanceOf]>> | Option<BalanceOf>)[]>([
|
|
...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<DeriveStakerSlashes> {
|
|
return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable<DeriveStakerSlashes> =>
|
|
api.derive.staking._ownSlashes(accountId, [era], true).pipe(
|
|
map(([first]) => first)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerSlashes[]> {
|
|
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerSlashes[]> => {
|
|
return api.derive.staking.erasHistoric(withActive).pipe(
|
|
switchMap((eras) =>
|
|
api.derive.staking._ownSlashes(accountId, eras, withActive)
|
|
)
|
|
);
|
|
});
|
|
}
|