Align entry retrieval with key retrieval (#7096)
This commit is contained in:
parent
5e9139be0a
commit
47767ea424
@ -15,7 +15,10 @@ const options = {
|
||||
|
||||
function useAssetIdsImpl (): AssetId[] | undefined {
|
||||
const { api } = useApi();
|
||||
const trigger = useEventTrigger([api.events.assets.Created, api.events.assets.Destroyed]);
|
||||
const trigger = useEventTrigger([
|
||||
api.events.assets.Created,
|
||||
api.events.assets.Destroyed
|
||||
]);
|
||||
|
||||
return useMapKeys(api.query.assets.asset, options, trigger.blockHash);
|
||||
}
|
||||
|
||||
@ -20,28 +20,30 @@ interface Owned {
|
||||
owned: OwnedIdPartial[];
|
||||
}
|
||||
|
||||
function extractIds (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeCommonParasRegistrarParaInfo>][]): Owned {
|
||||
const owned = entries
|
||||
.map(([{ args: [paraId] }, optInfo]): OwnedIdPartial | null => {
|
||||
if (optInfo.isNone) {
|
||||
return null;
|
||||
}
|
||||
const entriesOptions = {
|
||||
transform: (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeCommonParasRegistrarParaInfo>][]): Owned => {
|
||||
const owned = entries
|
||||
.map(([{ args: [paraId] }, optInfo]): OwnedIdPartial | null => {
|
||||
if (optInfo.isNone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const paraInfo = optInfo.unwrap();
|
||||
const paraInfo = optInfo.unwrap();
|
||||
|
||||
return {
|
||||
manager: paraInfo.manager.toString(),
|
||||
paraId,
|
||||
paraInfo
|
||||
};
|
||||
})
|
||||
.filter((id): id is OwnedIdPartial => !!id);
|
||||
return {
|
||||
manager: paraInfo.manager.toString(),
|
||||
paraId,
|
||||
paraInfo
|
||||
};
|
||||
})
|
||||
.filter((id): id is OwnedIdPartial => !!id);
|
||||
|
||||
return {
|
||||
ids: owned.map(({ paraId }) => paraId),
|
||||
owned
|
||||
};
|
||||
}
|
||||
return {
|
||||
ids: owned.map(({ paraId }) => paraId),
|
||||
owned
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const hashesOption = {
|
||||
transform: ([[paraIds], optHashes]: [[ParaId[]], Option<Hash>[]]) =>
|
||||
@ -55,8 +57,11 @@ const hashesOption = {
|
||||
function useOwnedIdsImpl (): OwnedId[] {
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const trigger = useEventTrigger([api.events.registrar.Registered, api.events.registrar.Reserved]);
|
||||
const unfiltered = useMapEntries<Owned>(api.query.registrar.paras, { at: trigger.blockHash, transform: extractIds });
|
||||
const trigger = useEventTrigger([
|
||||
api.events.registrar.Registered,
|
||||
api.events.registrar.Reserved
|
||||
]);
|
||||
const unfiltered = useMapEntries<Owned>(api.query.registrar.paras, entriesOptions, trigger.blockHash);
|
||||
const hashes = useCall(api.query.paras.currentCodeHash.multi, [unfiltered ? unfiltered.ids : []], hashesOption);
|
||||
|
||||
return useMemo(
|
||||
|
||||
@ -25,19 +25,20 @@ const proposalOpts = {
|
||||
keys.map(({ args: [id] }) => id)
|
||||
};
|
||||
|
||||
function extractScheduled (entries: [StorageKey<[SessionIndex]>, ParaId[]][]): Scheduled[] {
|
||||
return entries.map(([{ args: [sessionIndex] }, scheduledIds]) => ({
|
||||
scheduledIds,
|
||||
sessionIndex
|
||||
}));
|
||||
}
|
||||
const scheduledOpts = {
|
||||
transform: (entries: [StorageKey<[SessionIndex]>, ParaId[]][]): Scheduled[] =>
|
||||
entries.map(([{ args: [sessionIndex] }, scheduledIds]) => ({
|
||||
scheduledIds,
|
||||
sessionIndex
|
||||
}))
|
||||
};
|
||||
|
||||
function useProposalsImpl (): Proposals | undefined {
|
||||
const { api } = useApi();
|
||||
const mountedRef = useIsMountedRef();
|
||||
const trigger = useEventTrigger([api.events.proposeParachain?.ProposeParachain]);
|
||||
const proposalIds = useMapKeys(api.query.proposeParachain?.proposals, proposalOpts, trigger.blockHash);
|
||||
const scheduled = useMapEntries(api.query.proposeParachain?.scheduledProposals, { at: trigger.blockHash, transform: extractScheduled });
|
||||
const scheduled = useMapEntries(api.query.proposeParachain?.scheduledProposals, scheduledOpts, trigger.blockHash);
|
||||
const [sessionIndex, approvedIds] = useCallMulti<MultiQuery>([
|
||||
api.query.session.currentIndex,
|
||||
api.query.proposeParachain?.approvedProposals
|
||||
|
||||
@ -7,23 +7,24 @@ import type { PolkadotRuntimeParachainsParasParaLifecycle } from '@polkadot/type
|
||||
|
||||
import { createNamedHook, useApi, useEventTrigger, useMapEntries } from '@polkadot/react-hooks';
|
||||
|
||||
function extractIds (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeParachainsParasParaLifecycle>][]): ParaId[] {
|
||||
return entries
|
||||
.map(([{ args: [paraId] }, optValue]): ParaId | null => {
|
||||
const value = optValue.unwrapOr(null);
|
||||
const options = {
|
||||
transform: (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeParachainsParasParaLifecycle>][]): ParaId[] =>
|
||||
entries
|
||||
.map(([{ args: [paraId] }, optValue]): ParaId | null => {
|
||||
const value = optValue.unwrapOr(null);
|
||||
|
||||
return value && (
|
||||
value.isParathread ||
|
||||
value.isUpgradingParathread ||
|
||||
value.isOffboardingParathread ||
|
||||
value.isOnboarding
|
||||
)
|
||||
? paraId
|
||||
: null;
|
||||
})
|
||||
.filter((paraId): paraId is ParaId => !!paraId)
|
||||
.sort((a, b) => a.cmp(b));
|
||||
}
|
||||
return value && (
|
||||
value.isParathread ||
|
||||
value.isUpgradingParathread ||
|
||||
value.isOffboardingParathread ||
|
||||
value.isOnboarding
|
||||
)
|
||||
? paraId
|
||||
: null;
|
||||
})
|
||||
.filter((paraId): paraId is ParaId => !!paraId)
|
||||
.sort((a, b) => a.cmp(b))
|
||||
};
|
||||
|
||||
function useUpomingIdsImpl (): ParaId[] | undefined {
|
||||
const { api } = useApi();
|
||||
@ -32,10 +33,7 @@ function useUpomingIdsImpl (): ParaId[] | undefined {
|
||||
api.events.registrar.Registered
|
||||
]);
|
||||
|
||||
return useMapEntries(api.query.paras.paraLifecycles, {
|
||||
at: trigger.blockHash,
|
||||
transform: extractIds
|
||||
});
|
||||
return useMapEntries(api.query.paras.paraLifecycles, options, trigger.blockHash);
|
||||
}
|
||||
|
||||
export default createNamedHook('useUpomingIds', useUpomingIdsImpl);
|
||||
|
||||
@ -13,7 +13,11 @@ const options = {
|
||||
|
||||
function useTipHashesImpl (): string[] | undefined {
|
||||
const { api } = useApi();
|
||||
const trigger = useEventTrigger([api.events.tips?.NewTip, api.events.tips?.TipClosed, api.events.tips?.TipRetracted]);
|
||||
const trigger = useEventTrigger([
|
||||
api.events.tips?.NewTip,
|
||||
api.events.tips?.TipClosed,
|
||||
api.events.tips?.TipRetracted
|
||||
]);
|
||||
|
||||
return useMapKeys((api.query.tips || api.query.treasury)?.tips, options, trigger.blockHash);
|
||||
}
|
||||
|
||||
@ -6,12 +6,11 @@ import type { QueryableStorageEntry } from '@polkadot/api/types';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
interface Options <T> {
|
||||
at?: string | null | false;
|
||||
transform?: (value: any[]) => T;
|
||||
}
|
||||
|
||||
// FIXME This is generic, we cannot really use createNamedHook
|
||||
export function useMapEntries <T = any> (entry?: QueryableStorageEntry<'promise'> | null | false, { at, transform }: Options<T> = {}): T | undefined {
|
||||
export function useMapEntries <T = any> (entry?: QueryableStorageEntry<'promise'> | null | false, { transform }: Options<T> = {}, at?: string | null | false): T | undefined {
|
||||
const [state, setState] = useState<T | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user