diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index ebf4fc5859..0895d13997 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -12,6 +12,8 @@ export * from './erasSlashes'; export * from './controllers'; export * from './electedInfo'; export * from './overview'; +export * from './ownExposure'; +export * from './ownSlashes'; export * from './query'; export * from './stakerExposure'; export * from './stakerPoints'; diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts new file mode 100644 index 0000000000..2dab3f5626 --- /dev/null +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -0,0 +1,37 @@ +// 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 { DeriveOwnExposure } from '../types'; + +import BN from 'bn.js'; +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +export function ownExposure (api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean | BN | number) => Observable { + return memo((accountId: Uint8Array | string, withActive?: boolean | BN | number): Observable => { + return api.derive.staking.erasHistoric(withActive).pipe( + switchMap((eras) => + eras.length + ? combineLatest( + eras.map((era) => combineLatest([ + of(era), + api.query.staking.erasStakersClipped(era, accountId), + api.query.staking.erasStakers(era, accountId) + ])) + ) + : of([]) + ), + map((result): DeriveOwnExposure[] => + result.map(([era, clipped, exposure]): DeriveOwnExposure => ({ + clipped, + era, + exposure + })) + ) + ); + }); +} diff --git a/packages/api-derive/src/staking/ownSlashes.ts b/packages/api-derive/src/staking/ownSlashes.ts new file mode 100644 index 0000000000..0949f9140d --- /dev/null +++ b/packages/api-derive/src/staking/ownSlashes.ts @@ -0,0 +1,40 @@ +// 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 { DeriveStakerSlashes } from '../types'; + +import BN from 'bn.js'; +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +export function ownSlashes (api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean | BN | number) => Observable { + return memo((accountId: Uint8Array | string, withActive?: boolean | BN | number): Observable => { + return api.derive.staking.erasHistoric(withActive).pipe( + switchMap((eras) => + eras.length + ? combineLatest( + eras.map((era) => combineLatest([ + of(era), + api.query.staking.nominatorSlashInEra(era, accountId), + api.query.staking.validatorSlashInEra(era, accountId) + ])) + ) + : of([]) + ), + map((result): DeriveStakerSlashes[] => + result.map(([era, optNom, optVal]): DeriveStakerSlashes => ({ + era, + total: optVal.isSome + ? optVal.unwrap()[1] + : optNom.isSome + ? optNom.unwrap() + : api.registry.createType('Balance') + })) + ) + ); + }); +} diff --git a/packages/api-derive/src/staking/stakerSlashes.ts b/packages/api-derive/src/staking/stakerSlashes.ts index d0d24415b4..07df88eb75 100644 --- a/packages/api-derive/src/staking/stakerSlashes.ts +++ b/packages/api-derive/src/staking/stakerSlashes.ts @@ -19,7 +19,6 @@ export function stakerSlashes (api: ApiInterfaceRx): (accountId: Uint8Array | st map((slashes): DeriveStakerSlashes[] => slashes.map(({ era, nominators, validators }): DeriveStakerSlashes => ({ era, - isValidator: !!validators[stakerId], total: nominators[stakerId] || validators[stakerId] || api.registry.createType('Balance') })) ) diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 7492230834..a896f1259d 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -49,6 +49,14 @@ export interface DeriveEraExposure { validators: DeriveEraValidatorExposure; } +export interface DeriveOwnExposure { + clipped: Exposure; + era: EraIndex; + exposure: Exposure; +} + +export type DeriveOwnSlashes = DeriveStakerSlashes; + export interface DeriveStakerExposure { era: EraIndex; isEmpty: boolean; @@ -68,7 +76,6 @@ export interface DeriveStakerReward { export interface DeriveStakerSlashes { era: EraIndex; - isValidator: boolean; total: Balance; }