Add firstMemo (dedupes) (#4321)

* Add firstMemo (dedupes)

* Adjust

* flatten

* Readability

* Readability
This commit is contained in:
Jaco
2021-12-12 08:47:58 +02:00
committed by GitHub
parent 4b0c6aa515
commit 59c2327768
14 changed files with 90 additions and 70 deletions
+8 -7
View File
@@ -11,7 +11,7 @@ import { combineLatest, map, switchMap } from 'rxjs';
import { BN, BN_ZERO } from '@polkadot/util';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
const QUERY_OPTS = {
withDestination: true,
@@ -73,7 +73,9 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds:
api.derive.staking.keysMulti(accountIds),
api.derive.staking.queryMulti(accountIds, QUERY_OPTS)
]).pipe(
map(([keys, queries]) => queries.map((query, index) => parseResult(api, sessionInfo, keys[index], query)))
map(([keys, queries]) =>
queries.map((q, index) => parseResult(api, sessionInfo, keys[index], q))
)
)
)
)
@@ -83,8 +85,7 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds:
/**
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
*/
export function account (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo(instanceId, (accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
firstObservable(api.derive.staking.accounts([accountId]))
);
}
export const account = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string) =>
api.derive.staking.accounts([accountId])
);
@@ -38,8 +38,8 @@ function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposur
return { era, nominators, validators };
}
export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive: boolean) => Observable<DeriveEraExposure> {
return memo(instanceId, (era: EraIndex, withActive: boolean): Observable<DeriveEraExposure> => {
export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposure> {
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposure> => {
const [cacheKey, cached] = getEraCache<DeriveEraExposure>(CACHE_KEY, era, withActive);
return cached
@@ -13,8 +13,8 @@ import { BN_ONE, BN_ZERO } from '@polkadot/util';
import { memo } from '../util';
export function erasHistoric (instanceId: string, api: ApiInterfaceRx): (withActive: boolean) => Observable<EraIndex[]> {
return memo(instanceId, (withActive: boolean): Observable<EraIndex[]> =>
export function erasHistoric (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<EraIndex[]> {
return memo(instanceId, (withActive?: boolean): Observable<EraIndex[]> =>
api.queryMulti<[Option<ActiveEraInfo>, u32]>([
api.query.staking.activeEra,
api.query.staking.historyDepth
+5 -6
View File
@@ -11,7 +11,7 @@ import type { DeriveStakingKeys } from './types';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
function extractsIds (stashId: Uint8Array | string, queuedKeys: [AccountId, NodeRuntimeSessionKeys | AccountId[]][], nextKeys: Option<NodeRuntimeSessionKeys>): DeriveStakingKeys {
const sessionIds = (queuedKeys.find(([currentId]) => currentId.eq(stashId)) || [undefined, [] as AccountId[]])[1];
@@ -27,11 +27,10 @@ function extractsIds (stashId: Uint8Array | string, queuedKeys: [AccountId, Node
};
}
export function keys (instanceId: string, api: ApiInterfaceRx): (stashId: Uint8Array | string) => Observable<DeriveStakingKeys> {
return memo(instanceId, (stashId: Uint8Array | string): Observable<DeriveStakingKeys> =>
firstObservable(api.derive.staking.keysMulti([stashId]))
);
}
export const keys = firstMemo(
(api: ApiInterfaceRx, stashId: Uint8Array | string) =>
api.derive.staking.keysMulti([stashId])
);
export function keysMulti (instanceId: string, api: ApiInterfaceRx): (stashIds: (Uint8Array | string)[]) => Observable<DeriveStakingKeys[]> {
return memo(instanceId, (stashIds: (Uint8Array | string)[]): Observable<DeriveStakingKeys[]> =>
@@ -9,7 +9,7 @@ import type { DeriveOwnExposure } from '../types';
import { map, of } from 'rxjs';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _ownExposures (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveOwnExposure[]> {
@@ -28,10 +28,9 @@ export function _ownExposures (instanceId: string, api: ApiInterfaceRx): (accoun
);
}
export function ownExposure (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, era: EraIndex) => Observable<DeriveOwnExposure> {
return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable<DeriveOwnExposure> =>
firstObservable(api.derive.staking._ownExposures(accountId, [era], true))
);
}
export const ownExposure = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string, era: EraIndex) =>
api.derive.staking._ownExposures(accountId, [era], true)
);
export const ownExposures = erasHistoricApplyAccount('_ownExposures');
@@ -10,7 +10,7 @@ import type { DeriveStakerSlashes } from '../types';
import { map, of } from 'rxjs';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerSlashes[]> {
@@ -34,10 +34,9 @@ export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountI
);
}
export function ownSlash (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, era: EraIndex) => Observable<DeriveStakerSlashes> {
return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable<DeriveStakerSlashes> =>
firstObservable(api.derive.staking._ownSlashes(accountId, [era], true))
);
}
export const ownSlash = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string, era: EraIndex) =>
api.derive.staking._ownSlashes(accountId, [era], true)
);
export const ownSlashes = erasHistoricApplyAccount('_ownSlashes');
+5 -6
View File
@@ -10,7 +10,7 @@ import type { DeriveStakingQuery, StakingQueryFlags } from '../types';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
function parseDetails (stashId: AccountId, controllerIdOpt: Option<AccountId> | null, nominatorsOpt: Option<PalletStakingNominations>, rewardDestination: PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: PalletStakingExposure, stakingLedgerOpt: Option<PalletStakingStakingLedger>): DeriveStakingQuery {
return {
@@ -93,11 +93,10 @@ function getBatch (api: ApiInterfaceRx, activeEra: EraIndex, stashIds: AccountId
/**
* @description From a stash, retrieve the controllerId and all relevant details
*/
export function query (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, flags: StakingQueryFlags) => Observable<DeriveStakingQuery> {
return memo(instanceId, (accountId: Uint8Array | string, flags: StakingQueryFlags): Observable<DeriveStakingQuery> =>
firstObservable(api.derive.staking.queryMulti([accountId], flags))
);
}
export const query = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string, flags: StakingQueryFlags) =>
api.derive.staking.queryMulti([accountId], flags)
);
export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags) => Observable<DeriveStakingQuery[]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags): Observable<DeriveStakingQuery[]> =>
@@ -8,10 +8,10 @@ import type { DeriveEraValidatorExposure, DeriveStakerExposure } from '../types'
import { map, switchMap } from 'rxjs';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
export function _stakerExposures (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerExposure[][]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive: boolean): Observable<DeriveStakerExposure[][]> => {
export function _stakerExposures (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive?: boolean) => Observable<DeriveStakerExposure[][]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive = false): Observable<DeriveStakerExposure[][]> => {
const stakerIds = accountIds.map((a) => api.registry.createType('AccountId', a).toString());
return api.derive.staking._erasExposure(eras, withActive).pipe(
@@ -46,8 +46,7 @@ export function stakerExposures (instanceId: string, api: ApiInterfaceRx): (acco
);
}
export function stakerExposure (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerExposure[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerExposure[]> =>
firstObservable(api.derive.staking.stakerExposures([accountId], withActive))
);
}
export const stakerExposure = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string, withActive?: boolean) =>
api.derive.staking.stakerExposures([accountId], withActive)
);
@@ -13,7 +13,7 @@ import { combineLatest, map, of, switchMap } from 'rxjs';
import { BN_BILLION, BN_ZERO } from '@polkadot/util';
import { firstObservable, memo } from '../util';
import { firstMemo, memo } from '../util';
type ErasResult = [DeriveEraPoints[], DeriveEraPrefs[], DeriveEraRewards[]];
@@ -132,8 +132,8 @@ function filterRewards (eras: EraIndex[], valInfo: [string, DeriveStakingQuery][
}));
}
export function _stakerRewardsEras (instanceId: string, api: ApiInterfaceRx): (eras: EraIndex[], withActive: boolean) => Observable<ErasResult> {
return memo(instanceId, (eras: EraIndex[], withActive: boolean): Observable<ErasResult> =>
export function _stakerRewardsEras (instanceId: string, api: ApiInterfaceRx): (eras: EraIndex[], withActive?: boolean) => Observable<ErasResult> {
return memo(instanceId, (eras: EraIndex[], withActive = false): Observable<ErasResult> =>
combineLatest([
api.derive.staking._erasPoints(eras, withActive),
api.derive.staking._erasPrefs(eras, withActive),
@@ -142,8 +142,8 @@ export function _stakerRewardsEras (instanceId: string, api: ApiInterfaceRx): (e
);
}
export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerReward[][]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive: boolean): Observable<DeriveStakerReward[][]> =>
export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive?: boolean) => Observable<DeriveStakerReward[][]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive = false): Observable<DeriveStakerReward[][]> =>
combineLatest([
api.derive.staking.queryMulti(accountIds, { withLedger: true }),
api.derive.staking._stakerExposures(accountIds, eras, withActive),
@@ -184,13 +184,12 @@ export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accou
);
}
export function stakerRewards (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerReward[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerReward[]> =>
firstObservable(api.derive.staking.erasHistoric(withActive).pipe(
export const stakerRewards = firstMemo(
(api: ApiInterfaceRx, accountId: Uint8Array | string, withActive?: boolean) =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._stakerRewards([accountId], eras, withActive))
))
);
}
)
);
export function stakerRewardsMultiEras (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], eras: EraIndex[]) => Observable<DeriveStakerReward[][]> {
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[]): Observable<DeriveStakerReward[][]> =>