// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { EraIndex, ValidatorPrefs } from '@polkadot/types/interfaces'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveStakerPrefs } from '../types'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; export function _stakerPrefs (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 => api.query.staking.erasValidatorPrefs.multi(eras.map((era) => [era, accountId])).pipe( map((all): DeriveStakerPrefs[] => all.map((validatorPrefs, index): DeriveStakerPrefs => ({ era: eras[index], validatorPrefs })) ) ) ); } export function stakerPrefs (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._stakerPrefs(accountId, eras, withActive)) ) ); }