Minor parachain option renames (& opts) (#7177)

This commit is contained in:
Jaco 2022-03-17 13:05:55 +01:00 committed by GitHub
parent 8146009a24
commit 3519f22d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 62 deletions

View File

@ -16,17 +16,21 @@ interface Props {
value?: number | BN | null;
}
function calcBlocks (leasePeriod?: LeasePeriod | null, value?: number | BN | null): BN | null | undefined | 0 {
return leasePeriod && value &&
bnToBn(value)
.sub(BN_ONE)
.imul(leasePeriod.length)
.iadd(leasePeriod.remainder);
}
function LeaseBlocks ({ children, className, leasePeriod, value }: Props): React.ReactElement<Props> | null {
const blocks = useMemo(
() => leasePeriod && value &&
bnToBn(value)
.sub(BN_ONE)
.imul(leasePeriod.length)
.iadd(leasePeriod.remainder),
() => calcBlocks(leasePeriod, value),
[leasePeriod, value]
);
if (!leasePeriod || !blocks) {
if (!blocks) {
return null;
}

View File

@ -1,29 +1,22 @@
// Copyright 2017-2022 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BlockNumber, Header, ParaId, RuntimeVersion } from '@polkadot/types/interfaces';
import type { ParaId } from '@polkadot/types/interfaces';
import React from 'react';
import styled from 'styled-components';
import { useCall, useParaApi } from '@polkadot/react-hooks';
import { formatNumber } from '@polkadot/util';
import useChainDetails from './useChainDetails';
interface Props {
className?: string;
id: ParaId;
}
const transformHeader = {
transform: (header: Header) => header.number.unwrap()
};
function ParachainInfo ({ className, id }: Props): React.ReactElement<Props> {
const { api } = useParaApi(id);
// We are not using the derive here, we keep this queries to the point to not overload
const bestNumber = useCall<BlockNumber>(api?.rpc.chain.subscribeNewHeads, undefined, transformHeader);
const runtimeVersion = useCall<RuntimeVersion>(api?.rpc.state.subscribeRuntimeVersion);
const { bestNumber, runtimeVersion } = useChainDetails(id);
return (
<div className={className}>

View File

@ -17,27 +17,31 @@ interface Props {
periods: number[];
}
function getMapped (periods: number[], currentPeriod?: BN): string | undefined {
return currentPeriod && periods &&
periods
.reduce((all: [BN, BN][], period): [BN, BN][] => {
const bnp = currentPeriod.addn(period);
if (!all.length || all[all.length - 1][1].add(BN_ONE).lt(bnp)) {
all.push([bnp, bnp]);
} else {
all[all.length - 1][1] = bnp;
}
return all;
}, [])
.map(([a, b]) =>
a.eq(b)
? formatNumber(a)
: `${formatNumber(a)} - ${formatNumber(b)}`
)
.join(', ');
}
function Periods ({ className, fromFirst, leasePeriod, periods }: Props): React.ReactElement<Props> {
const mapped = useMemo(
() => leasePeriod?.currentPeriod && periods &&
periods
.reduce((all: [BN, BN][], period): [BN, BN][] => {
const bnp = leasePeriod.currentPeriod.addn(period);
if (!all.length || all[all.length - 1][1].add(BN_ONE).lt(bnp)) {
all.push([bnp, bnp]);
} else {
all[all.length - 1][1] = bnp;
}
return all;
}, [])
.map(([a, b]) =>
a.eq(b)
? formatNumber(a)
: `${formatNumber(a)} - ${formatNumber(b)}`
)
.join(', '),
() => getMapped(periods, leasePeriod?.currentPeriod),
[leasePeriod?.currentPeriod, periods]
);

View File

@ -0,0 +1,27 @@
// Copyright 2017-2022 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BlockNumber, Header, ParaId, RuntimeVersion } from '@polkadot/types/interfaces';
import { createNamedHook, useCall, useParaApi } from '@polkadot/react-hooks';
interface Result {
bestNumber?: BlockNumber;
runtimeVersion?: RuntimeVersion;
}
const HDR_OPTS = {
transform: (header: Header) => header.number.unwrap()
};
function useChainDetailsImpl (id: ParaId): Result {
const { api } = useParaApi(id);
// We are not using the derive here, we keep this queries to the point to not overload
return {
bestNumber: useCall<BlockNumber>(api?.rpc.chain.subscribeNewHeads, undefined, HDR_OPTS),
runtimeVersion: useCall<RuntimeVersion>(api?.rpc.state.subscribeRuntimeVersion)
};
}
export default createNamedHook('useChainDetails', useChainDetailsImpl);

View File

@ -26,7 +26,7 @@ interface Result {
watermark: BlockNumber | null;
}
const optionsMulti = {
const MULTI_OPTS = {
defaultValue: {
headHex: null,
leases: [],
@ -74,7 +74,7 @@ function useParaInfoImpl (id: ParaId): Result {
[(api.query.parasInclusion || api.query.paraInclusion || api.query.inclusion)?.pendingAvailability, id],
[api.query.registrar.paras, id],
[api.query.slots.leases, id]
], optionsMulti);
], MULTI_OPTS);
}
export default createNamedHook('useParaInfo', useParaInfoImpl);

View File

@ -20,7 +20,7 @@ interface Props {
ownedIds: OwnedId[];
}
const transformId = {
const OPT_NEXT = {
transform: (nextId: ParaId) =>
nextId.isZero()
? LOWEST_PUBLIC_ID
@ -32,7 +32,7 @@ function Actions ({ className, ownedIds }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [isRegisterOpen, toggleRegisterOpen] = useToggle();
const [isReserveOpen, toggleReserveOpen] = useToggle();
const nextParaId = useCall<ParaId | BN>(api.query.registrar.nextFreeParaId, [], transformId);
const nextParaId = useCall<ParaId | BN>(api.query.registrar.nextFreeParaId, [], OPT_NEXT);
return (
<Button.Group className={className}>

View File

@ -15,7 +15,7 @@ interface Result {
manager: AccountId | null;
}
const optMulti = {
const OPT_MULTI = {
defaultValue: {
headHex: null,
lifecycle: null,
@ -42,7 +42,7 @@ function useThreadInfoImpl (id: ParaId): Result {
[api.query.paras.upcomingParasGenesis, id],
[api.query.paras.paraLifecycles, id],
[api.query.registrar.paras, id]
], optMulti);
], OPT_MULTI);
}
export default createNamedHook('useThreadInfo', useThreadInfoImpl);

View File

@ -12,7 +12,7 @@ import { BN_EIGHT, BN_FIVE, BN_FOUR, BN_NINE, BN_ONE, BN_SEVEN, BN_SIX, BN_TEN,
const INC = [BN_ONE, BN_TWO, BN_THREE, BN_FOUR, BN_FIVE, BN_SIX, BN_SEVEN, BN_EIGHT, BN_NINE, BN_TEN];
const callOpts = {
const OPT_NEXT = {
withParams: true
};
@ -20,7 +20,7 @@ function useActionsQueueImpl (): QueuedAction[] {
const { api } = useApi();
const currentIndex = useCall<SessionIndex>(api.query.session.currentIndex);
const queryIndexes = useMemo(() => currentIndex && INC.map((i) => currentIndex.add(i)), [currentIndex]);
const nextActions = useCall<[[BN[]], ParaId[][]]>(queryIndexes && api.query.paras.actionsQueue.multi, [queryIndexes], callOpts);
const nextActions = useCall<[[BN[]], ParaId[][]]>(queryIndexes && api.query.paras.actionsQueue.multi, [queryIndexes], OPT_NEXT);
return useMemo(
(): QueuedAction[] =>

View File

@ -8,7 +8,7 @@ import type { AuctionInfo } from './types';
import { createNamedHook, useApi, useCallMulti } from '@polkadot/react-hooks';
const optionsMulti = {
const OPT_MULTI = {
transform: ([numAuctions, optInfo]: [AuctionIndex, Option<ITuple<[LeasePeriodOf, BlockNumber]>>]): AuctionInfo => {
const [leasePeriod, endBlock] = optInfo.unwrapOr([null, null]);
@ -26,7 +26,7 @@ function useAuctionInfoImpl (): AuctionInfo | undefined {
return useCallMulti<AuctionInfo>([
api.query.auctions?.auctionCounter,
api.query.auctions?.auctionInfo
], optionsMulti);
], OPT_MULTI);
}
export default createNamedHook('useAuctionInfo', useAuctionInfoImpl);

View File

@ -111,7 +111,7 @@ function createResult (bestNumber: BlockNumber, minContribution: BN, funds: Camp
};
}
const optFundMulti = {
const OPT_FUND = {
transform: ([[paraIds], optFunds]: [[ParaId[]], Option<PolkadotRuntimeCommonCrowdloanFundInfo>[]]): Campaign[] =>
paraIds
.map((paraId, i): [ParaId, PolkadotRuntimeCommonCrowdloanFundInfo | null] => [paraId, optFunds[i].unwrapOr(null)])
@ -135,7 +135,7 @@ const optFundMulti = {
withParamsTransform: true
};
const optLeaseMulti = {
const OPT_LEASE = {
transform: ([[paraIds], leases]: [[ParaId[]], Option<ITuple<[AccountId, BalanceOf]>>[][]]): ParaId[] =>
paraIds.filter((paraId, i) =>
leases[i]
@ -158,8 +158,8 @@ function useFundsImpl (): Campaigns {
const mountedRef = useIsMountedRef();
const trigger = useEventTrigger([api.events.crowdloan?.Created]);
const paraIds = useMapKeys(api.query.crowdloan?.funds, fundOpts, trigger.blockHash);
const campaigns = useCall<Campaign[]>(api.query.crowdloan?.funds.multi, [paraIds], optFundMulti);
const leases = useCall<ParaId[]>(api.query.slots.leases.multi, [paraIds], optLeaseMulti);
const campaigns = useCall<Campaign[]>(api.query.crowdloan?.funds.multi, [paraIds], OPT_FUND);
const leases = useCall<ParaId[]>(api.query.slots.leases.multi, [paraIds], OPT_LEASE);
const [result, setResult] = useState<Campaigns>(EMPTY);
// here we manually add the actual ending status and calculate the totals

View File

@ -20,7 +20,7 @@ interface Owned {
owned: OwnedIdPartial[];
}
const entriesOptions = {
const OPT_ENTRIES = {
transform: (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeCommonParasRegistrarParaInfo>][]): Owned => {
const owned = entries
.map(([{ args: [paraId] }, optInfo]): OwnedIdPartial | null => {
@ -45,7 +45,7 @@ const entriesOptions = {
}
};
const hashesOption = {
const OPT_HASHES = {
transform: ([[paraIds], optHashes]: [[ParaId[]], Option<Hash>[]]) =>
paraIds.map((paraId, index): CodeHash => ({
hash: optHashes[index].unwrapOr(null),
@ -61,8 +61,8 @@ function useOwnedIdsImpl (): OwnedId[] {
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);
const unfiltered = useMapEntries<Owned>(api.query.registrar.paras, OPT_ENTRIES, trigger.blockHash);
const hashes = useCall(api.query.paras.currentCodeHash.multi, [unfiltered ? unfiltered.ids : []], OPT_HASHES);
return useMemo(
() => unfiltered && hashes

View File

@ -16,16 +16,16 @@ interface Scheduled {
sessionIndex: SessionIndex;
}
const optionsMulti = {
const OPT_MULTI = {
defaultValue: [undefined, undefined] as MultiQuery
};
const proposalOpts = {
const OPT_IDS = {
transform: (keys: StorageKey<[ParaId]>[]): ParaId[] =>
keys.map(({ args: [id] }) => id)
};
const scheduledOpts = {
const OPT_SCHED = {
transform: (entries: [StorageKey<[SessionIndex]>, ParaId[]][]): Scheduled[] =>
entries.map(([{ args: [sessionIndex] }, scheduledIds]) => ({
scheduledIds,
@ -37,12 +37,12 @@ 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, scheduledOpts, trigger.blockHash);
const proposalIds = useMapKeys(api.query.proposeParachain?.proposals, OPT_IDS, trigger.blockHash);
const scheduled = useMapEntries(api.query.proposeParachain?.scheduledProposals, OPT_SCHED, trigger.blockHash);
const [sessionIndex, approvedIds] = useCallMulti<MultiQuery>([
api.query.session.currentIndex,
api.query.proposeParachain?.approvedProposals
], optionsMulti);
], OPT_MULTI);
return useMemo(
() => approvedIds && sessionIndex && proposalIds && scheduled && mountedRef.current

View File

@ -7,7 +7,7 @@ import type { PolkadotRuntimeParachainsParasParaLifecycle } from '@polkadot/type
import { createNamedHook, useApi, useEventTrigger, useMapEntries } from '@polkadot/react-hooks';
const options = {
const OPT_ENTRIES = {
transform: (entries: [StorageKey<[ParaId]>, Option<PolkadotRuntimeParachainsParasParaLifecycle>][]): ParaId[] =>
entries
.map(([{ args: [paraId] }, optValue]): ParaId | null => {
@ -33,7 +33,7 @@ function useUpomingIdsImpl (): ParaId[] | undefined {
api.events.registrar.Registered
]);
return useMapEntries(api.query.paras.paraLifecycles, options, trigger.blockHash);
return useMapEntries(api.query.paras.paraLifecycles, OPT_ENTRIES, trigger.blockHash);
}
export default createNamedHook('useUpomingIds', useUpomingIdsImpl);