From 969151c4a7d62b2cf131ffc402a05cd2bf739c53 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 9 Dec 2021 11:54:51 +0100 Subject: [PATCH] Additional derive cleanups (#4308) * Additional derive cleanups * unwrapNumber * Adjust --- packages/api-derive/src/chain/bestNumber.ts | 9 ++------ .../src/chain/bestNumberFinalized.ts | 9 ++------ packages/api-derive/src/chain/util.ts | 14 +++++++++++++ packages/api-derive/src/staking/cache.ts | 6 ++++++ .../api-derive/src/staking/erasExposure.ts | 21 +++++-------------- packages/api-derive/src/staking/erasPrefs.ts | 21 +++++-------------- .../api-derive/src/staking/erasSlashes.ts | 21 +++++-------------- packages/api-derive/src/staking/util.ts | 9 ++++++++ 8 files changed, 48 insertions(+), 62 deletions(-) create mode 100644 packages/api-derive/src/chain/util.ts diff --git a/packages/api-derive/src/chain/bestNumber.ts b/packages/api-derive/src/chain/bestNumber.ts index 20201fed5b..100372f1b0 100644 --- a/packages/api-derive/src/chain/bestNumber.ts +++ b/packages/api-derive/src/chain/bestNumber.ts @@ -5,9 +5,7 @@ import type { Observable } from 'rxjs'; import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { BlockNumber } from '@polkadot/types/interfaces'; -import { map } from 'rxjs'; - -import { memo } from '../util'; +import { unwrapNumber } from './util'; /** * @name bestNumber @@ -22,8 +20,5 @@ import { memo } from '../util'; * ``` */ export function bestNumber (instanceId: string, api: ApiInterfaceRx): () => Observable { - return memo(instanceId, (): Observable => - api.derive.chain.subscribeNewHeads().pipe( - map((header) => header.number.unwrap()) - )); + return unwrapNumber(instanceId, api.derive.chain.subscribeNewHeads); } diff --git a/packages/api-derive/src/chain/bestNumberFinalized.ts b/packages/api-derive/src/chain/bestNumberFinalized.ts index 76aee7b617..1b69cb0374 100644 --- a/packages/api-derive/src/chain/bestNumberFinalized.ts +++ b/packages/api-derive/src/chain/bestNumberFinalized.ts @@ -5,9 +5,7 @@ import type { Observable } from 'rxjs'; import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { BlockNumber } from '@polkadot/types/interfaces'; -import { map } from 'rxjs'; - -import { memo } from '../util'; +import { unwrapNumber } from './util'; /** * @name bestNumberFinalized @@ -23,8 +21,5 @@ import { memo } from '../util'; * ``` */ export function bestNumberFinalized (instanceId: string, api: ApiInterfaceRx): () => Observable { - return memo(instanceId, (): Observable => - api.rpc.chain.subscribeFinalizedHeads().pipe( - map((header) => header.number.unwrap()) - )); + return unwrapNumber(instanceId, api.rpc.chain.subscribeFinalizedHeads); } diff --git a/packages/api-derive/src/chain/util.ts b/packages/api-derive/src/chain/util.ts new file mode 100644 index 0000000000..576420fb18 --- /dev/null +++ b/packages/api-derive/src/chain/util.ts @@ -0,0 +1,14 @@ +// Copyright 2017-2021 @polkadot/api-derive authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Observable } from 'rxjs'; +import type { Option } from '@polkadot/types'; +import type { BlockNumber } from '@polkadot/types/interfaces'; + +import { map } from 'rxjs'; + +import { memo } from '../util'; + +export function unwrapNumber }> (instanceId: string, fn: () => Observable): () => Observable { + return memo(instanceId, () => fn().pipe(map((r) => r.number.unwrap()))); +} diff --git a/packages/api-derive/src/staking/cache.ts b/packages/api-derive/src/staking/cache.ts index b4340a3b1d..a09d94c559 100644 --- a/packages/api-derive/src/staking/cache.ts +++ b/packages/api-derive/src/staking/cache.ts @@ -26,6 +26,12 @@ export function getEraMultiCache (CACHE_KEY: string, eras: EraIndex[], withA return cached; } +export function setEraCache (cacheKey: string, withActive: boolean, value: T): T { + !withActive && deriveCache.set(cacheKey, value); + + return value; +} + export function setEraMultiCache (CACHE_KEY: string, withActive: boolean, values: T[]): T[] { !withActive && values.forEach((v) => deriveCache.set(`${CACHE_KEY}-${v.era.toString()}`, v)); diff --git a/packages/api-derive/src/staking/erasExposure.ts b/packages/api-derive/src/staking/erasExposure.ts index 62dddcc556..994c7a36dd 100644 --- a/packages/api-derive/src/staking/erasExposure.ts +++ b/packages/api-derive/src/staking/erasExposure.ts @@ -10,9 +10,9 @@ import type { DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorE import { map, of } from 'rxjs'; -import { deriveCache, memo } from '../util'; -import { getEraCache } from './cache'; -import { combineEras, erasHistoricApply } from './util'; +import { memo } from '../util'; +import { getEraCache, setEraCache } from './cache'; +import { combineEras, erasHistoricApply, singleEra } from './util'; type KeysAndExposures = [StorageKey<[EraIndex, AccountId]>, PalletStakingExposure][]; @@ -45,22 +45,11 @@ export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: Er return cached ? of(cached) : api.query.staking.erasStakersClipped.entries(era).pipe( - map((stakers): DeriveEraExposure => { - const value = mapStakers(era, stakers); - - !withActive && deriveCache.set(cacheKey, value); - - return value; - }) + map((r) => setEraCache(cacheKey, withActive, mapStakers(era, r))) ); }); } -export function eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable { - return memo(instanceId, (era: EraIndex): Observable => - api.derive.staking._eraExposure(era, true) - ); -} - +export const eraExposure = singleEra('_eraExposure'); export const _erasExposure = combineEras('_eraExposure'); export const erasExposure = erasHistoricApply('_erasExposure'); diff --git a/packages/api-derive/src/staking/erasPrefs.ts b/packages/api-derive/src/staking/erasPrefs.ts index 383dc10aa3..310be3eecc 100644 --- a/packages/api-derive/src/staking/erasPrefs.ts +++ b/packages/api-derive/src/staking/erasPrefs.ts @@ -10,9 +10,9 @@ import type { DeriveEraPrefs, DeriveEraValPrefs } from '../types'; import { map, of } from 'rxjs'; -import { deriveCache, memo } from '../util'; -import { getEraCache } from './cache'; -import { combineEras, erasHistoricApply } from './util'; +import { memo } from '../util'; +import { getEraCache, setEraCache } from './cache'; +import { combineEras, erasHistoricApply, singleEra } from './util'; const CACHE_KEY = 'eraPrefs'; @@ -33,22 +33,11 @@ export function _eraPrefs (instanceId: string, api: ApiInterfaceRx): (era: EraIn return cached ? of(cached) : api.query.staking.erasValidatorPrefs.entries(era).pipe( - map((prefs): DeriveEraPrefs => { - const value = mapPrefs(era, prefs); - - !withActive && deriveCache.set(cacheKey, value); - - return value; - }) + map((r) => setEraCache(cacheKey, withActive, mapPrefs(era, r))) ); }); } -export function eraPrefs (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable { - return memo(instanceId, (era: EraIndex): Observable => - api.derive.staking._eraPrefs(era, true) - ); -} - +export const eraPrefs = singleEra('_eraPrefs'); export const _erasPrefs = combineEras('_eraPrefs'); export const erasPrefs = erasHistoricApply('_erasPrefs'); diff --git a/packages/api-derive/src/staking/erasSlashes.ts b/packages/api-derive/src/staking/erasSlashes.ts index 646542fc91..848ea1749b 100644 --- a/packages/api-derive/src/staking/erasSlashes.ts +++ b/packages/api-derive/src/staking/erasSlashes.ts @@ -10,9 +10,9 @@ import type { DeriveEraSlashes, DeriveEraValSlash } from '../types'; import { combineLatest, map, of } from 'rxjs'; -import { deriveCache, memo } from '../util'; -import { getEraCache } from './cache'; -import { combineEras, erasHistoricApply } from './util'; +import { memo } from '../util'; +import { getEraCache, setEraCache } from './cache'; +import { combineEras, erasHistoricApply, singleEra } from './util'; const CACHE_KEY = 'eraSlashes'; @@ -41,22 +41,11 @@ export function _eraSlashes (instanceId: string, api: ApiInterfaceRx): (era: Era api.query.staking.nominatorSlashInEra.entries(era), api.query.staking.validatorSlashInEra.entries(era) ]).pipe( - map(([noms, vals]): DeriveEraSlashes => { - const value = mapSlashes(era, noms, vals); - - !withActive && deriveCache.set(cacheKey, value); - - return value; - }) + map(([n, v]) => setEraCache(cacheKey, withActive, mapSlashes(era, n, v))) ); }); } -export function eraSlashes (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable { - return memo(instanceId, (era: EraIndex): Observable => - api.derive.staking._eraSlashes(era, true) - ); -} - +export const eraSlashes = singleEra('_eraSlashes'); export const _erasSlashes = combineEras('_eraSlashes'); export const erasSlashes = erasHistoricApply('_erasSlashes'); diff --git a/packages/api-derive/src/staking/util.ts b/packages/api-derive/src/staking/util.ts index 1b18119d60..02c39978c8 100644 --- a/packages/api-derive/src/staking/util.ts +++ b/packages/api-derive/src/staking/util.ts @@ -38,6 +38,15 @@ export function erasHistoricApplyAccount (fn: F): (instanceId: string, api: ApiInterfaceRx) => (era: EraIndex) => ApplyReturn { + return (instanceId: string, api: ApiInterfaceRx) => + // Cannot quite get the typing right, but it is right in the code + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + memo(instanceId, (era: EraIndex) => + api.derive.staking[fn](era, true) + ) as any; +} + export function combineEras (fn: F): (instanceId: string, api: ApiInterfaceRx) => (eras: EraIndex[], withActive: boolean) => Observable>[]> { return (instanceId: string, api: ApiInterfaceRx) => // Cannot quite get the typing right, but it is right in the code