Fix api.derive.staking.eraExposure for compatibility (#5815)
* Fix api.derive.staking.eraExposure * fix nit
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { StorageKey } from '@polkadot/types';
|
||||
import type { AccountId, EraIndex } from '@polkadot/types/interfaces';
|
||||
import type { PalletStakingExposure } from '@polkadot/types/lookup';
|
||||
import type { DeriveApi, DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorExposure } from '../types.js';
|
||||
import type { Option, StorageKey, u32 } from '@polkadot/types';
|
||||
import type { AccountId, AccountId32, EraIndex } from '@polkadot/types/interfaces';
|
||||
import type { PalletStakingExposure, SpStakingExposurePage } from '@polkadot/types/lookup';
|
||||
import type { DeriveApi, DeriveEraExposurePaged, DeriveEraNominatorExposure, DeriveEraValidatorExposure, DeriveEraValidatorExposurePaged } from '../types.js';
|
||||
|
||||
import { map, of } from 'rxjs';
|
||||
|
||||
@@ -14,10 +14,11 @@ import { getEraCache, setEraCache } from './cache.js';
|
||||
import { combineEras, erasHistoricApply, singleEra } from './util.js';
|
||||
|
||||
type KeysAndExposures = [StorageKey<[EraIndex, AccountId]>, PalletStakingExposure][];
|
||||
type KeysAndExposuresPaged = [StorageKey<[u32, AccountId32, u32]>, Option<SpStakingExposurePage>][];
|
||||
|
||||
const CACHE_KEY = 'eraExposure';
|
||||
|
||||
function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposure {
|
||||
function mapStakersClipped (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposurePaged {
|
||||
const nominators: DeriveEraNominatorExposure = {};
|
||||
const validators: DeriveEraValidatorExposure = {};
|
||||
|
||||
@@ -37,15 +38,46 @@ function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposur
|
||||
return { era, nominators, validators };
|
||||
}
|
||||
|
||||
export function _eraExposure (instanceId: string, api: DeriveApi): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposure> {
|
||||
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposure> => {
|
||||
const [cacheKey, cached] = getEraCache<DeriveEraExposure>(CACHE_KEY, era, withActive);
|
||||
function mapStakersPaged (era: EraIndex, stakers: KeysAndExposuresPaged): DeriveEraExposurePaged {
|
||||
const nominators: DeriveEraNominatorExposure = {};
|
||||
const validators: DeriveEraValidatorExposurePaged = {};
|
||||
|
||||
stakers.forEach(([key, exposureOpt]): void => {
|
||||
if (exposureOpt.isSome) {
|
||||
const validatorId = key.args[1].toString();
|
||||
const exposure = exposureOpt.unwrap();
|
||||
|
||||
validators[validatorId] = exposure;
|
||||
|
||||
exposure.others.forEach(({ who }, validatorIndex): void => {
|
||||
const nominatorId = who.toString();
|
||||
|
||||
nominators[nominatorId] = nominators[nominatorId] || [];
|
||||
nominators[nominatorId].push({ validatorId, validatorIndex });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return { era, nominators, validators };
|
||||
}
|
||||
|
||||
/**
|
||||
* erasStakersClipped will be deprecated and replaced with erasStakersPaged. Therefore support is given for both
|
||||
* storage queries until erasStakersClipped has been completely out of use.
|
||||
*/
|
||||
export function _eraExposure (instanceId: string, api: DeriveApi): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposurePaged> {
|
||||
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposurePaged> => {
|
||||
const [cacheKey, cached] = getEraCache<DeriveEraExposurePaged>(CACHE_KEY, era, withActive);
|
||||
|
||||
return cached
|
||||
? of(cached)
|
||||
: api.query.staking.erasStakersClipped.entries(era).pipe(
|
||||
map((r) => setEraCache(cacheKey, withActive, mapStakers(era, r)))
|
||||
);
|
||||
: api.query.staking.erasStakersPaged
|
||||
? api.query.staking.erasStakersPaged.entries<Option<SpStakingExposurePage>>(era).pipe(
|
||||
map((r) => setEraCache(cacheKey, withActive, mapStakersPaged(era, r)))
|
||||
)
|
||||
: api.query.staking.erasStakersClipped.entries(era).pipe(
|
||||
map((r) => setEraCache(cacheKey, withActive, mapStakersClipped(era, r)))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user