diff --git a/packages/page-parachains/src/Auctions/Auction.tsx b/packages/page-parachains/src/Auctions/Auction.tsx index 4ef2803db1..b5f235fbcf 100644 --- a/packages/page-parachains/src/Auctions/Auction.tsx +++ b/packages/page-parachains/src/Auctions/Auction.tsx @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AuctionIndex, BlockNumber, LeasePeriodOf } from '@polkadot/types/interfaces'; +import type { AuctionInfo } from '../types'; import type { Winning } from './types'; import React, { useRef } from 'react'; @@ -12,13 +12,12 @@ import { useTranslation } from '../translate'; import WinRange from './WinRange'; interface Props { - auctionInfo: [LeasePeriodOf, BlockNumber] | null; + auctionInfo: AuctionInfo; className?: string; - numAuctions: AuctionIndex | null; winningData: Winning[] | null; } -function Auction ({ auctionInfo, className, numAuctions, winningData }: Props): React.ReactElement { +function Auction ({ auctionInfo, className, winningData }: Props): React.ReactElement { const { t } = useTranslation(); const headerRef = useRef([ @@ -31,8 +30,8 @@ function Auction ({ auctionInfo, className, numAuctions, winningData }: Props): ('No winners in this auction') : t('No ongoing auction') ) diff --git a/packages/page-parachains/src/Auctions/Bid.tsx b/packages/page-parachains/src/Auctions/Bid.tsx index 3423aa70b2..fe655dc03d 100644 --- a/packages/page-parachains/src/Auctions/Bid.tsx +++ b/packages/page-parachains/src/Auctions/Bid.tsx @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 import type BN from 'bn.js'; -import type { AuctionIndex, BlockNumber, LeasePeriodOf } from '@polkadot/types/interfaces'; -import type { OwnedId, OwnerInfo } from '../types'; +import type { BlockNumber } from '@polkadot/types/interfaces'; +import type { AuctionInfo, OwnedId, OwnerInfo } from '../types'; import React, { useMemo, useState } from 'react'; @@ -16,9 +16,8 @@ import { useTranslation } from '../translate'; import { RANGES } from './constants'; interface Props { - auctionInfo: [LeasePeriodOf, BlockNumber] | null; + auctionInfo: AuctionInfo; className?: string; - id: AuctionIndex | null; ownedIds: OwnedId[]; } @@ -27,7 +26,7 @@ interface Option { value: number; } -function Bid ({ auctionInfo, className, id, ownedIds }: Props): React.ReactElement { +function Bid ({ auctionInfo, className, ownedIds }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { hasAccounts } = useAccounts(); @@ -38,26 +37,21 @@ function Bid ({ auctionInfo, className, id, ownedIds }: Props): React.ReactEleme const [isOpen, toggleOpen] = useToggle(); const rangeOpts = useMemo( - (): Option[] => { - const [leasePeriod] = auctionInfo || [null, null]; - - return leasePeriod + (): Option[] => + auctionInfo.leasePeriod ? RANGES.map(([first, last], value): Option => ({ - text: `${formatNumber(leasePeriod.addn(first))} - ${formatNumber(leasePeriod.addn(last))}`, + text: `${formatNumber(auctionInfo.leasePeriod?.addn(first))} - ${formatNumber(auctionInfo.leasePeriod?.addn(last))}`, value })) - : []; - }, + : [], [auctionInfo] ); - const [leasePeriod, endBlock] = auctionInfo || [null, null]; - return ( <> @@ -32,14 +31,16 @@ function WinRanges ({ auctionInfo, blockNumber, className = '', isFirst, isLates {isFirst && (

{isLatest ? t('latest') - : <>#{formatNumber(blockNumber.isZero() ? endBlock : blockNumber)} + : <>#{formatNumber(blockNumber.isZero() ? auctionInfo.endBlock : blockNumber)} }

)} - + ); diff --git a/packages/page-parachains/src/Auctions/index.tsx b/packages/page-parachains/src/Auctions/index.tsx index 3bff19babc..e765bbf456 100644 --- a/packages/page-parachains/src/Auctions/index.tsx +++ b/packages/page-parachains/src/Auctions/index.tsx @@ -1,15 +1,11 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Option } from '@polkadot/types'; -import type { AuctionIndex, BlockNumber, LeasePeriodOf } from '@polkadot/types/interfaces'; -import type { ITuple } from '@polkadot/types/types'; -import type { OwnedId } from '../types'; +import type { AuctionInfo, OwnedId } from '../types'; import React from 'react'; import { Button } from '@polkadot/react-components'; -import { useApi, useCallMulti } from '@polkadot/react-hooks'; import Auction from './Auction'; import Bid from './Bid'; @@ -17,32 +13,12 @@ import Summary from './Summary'; import useWinningData from './useWinningData'; interface Props { + auctionInfo: AuctionInfo; className?: string; ownedIds: OwnedId[]; } -interface QueryState { - auctionInfo: [LeasePeriodOf, BlockNumber] | null; - numAuctions: AuctionIndex | null; -} - -const optionsMulti = { - defaultValue: { - auctionInfo: null, - numAuctions: null - }, - transform: ([numAuctions, optInfo]: [AuctionIndex, Option>]) => ({ - auctionInfo: optInfo.unwrapOr(null), - numAuctions - }) -}; - -function Auctions ({ className, ownedIds }: Props): React.ReactElement { - const { api } = useApi(); - const { auctionInfo, numAuctions } = useCallMulti([ - api.query.auctions.auctionCounter, - api.query.auctions.auctionInfo - ], optionsMulti); +function Auctions ({ auctionInfo, className, ownedIds }: Props): React.ReactElement { const winningData = useWinningData(auctionInfo); return ( @@ -50,18 +26,15 @@ function Auctions ({ className, ownedIds }: Props): React.ReactElement { diff --git a/packages/page-parachains/src/Auctions/useWinningData.ts b/packages/page-parachains/src/Auctions/useWinningData.ts index f76b1ce128..6a8a6c6c08 100644 --- a/packages/page-parachains/src/Auctions/useWinningData.ts +++ b/packages/page-parachains/src/Auctions/useWinningData.ts @@ -2,7 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 import type { Option, StorageKey } from '@polkadot/types'; -import type { BlockNumber, LeasePeriodOf, WinningData } from '@polkadot/types/interfaces'; +import type { BlockNumber, WinningData } from '@polkadot/types/interfaces'; +import type { AuctionInfo } from '../types'; import type { WinnerData, Winning } from './types'; import BN from 'bn.js'; @@ -89,7 +90,7 @@ function mergeFirst (prev: Winning[] | null, optFirstData: Option): return prev; } -export default function useWinningData (auctionInfo: [LeasePeriodOf, BlockNumber] | null): Winning[] | null { +export default function useWinningData (auctionInfo: AuctionInfo): Winning[] | null { const { api } = useApi(); const [result, setResult] = useState(null); const bestNumber = useBestNumber(); @@ -101,7 +102,7 @@ export default function useWinningData (auctionInfo: [LeasePeriodOf, BlockNumber // should be fired once, all entries as an initial round useEffect((): void => { allEntries && setResult( - extractData(auctionInfo && auctionInfo[1], allEntries) + extractData(auctionInfo.endBlock, allEntries) ); }, [allEntries, auctionInfo]); @@ -116,7 +117,7 @@ export default function useWinningData (auctionInfo: [LeasePeriodOf, BlockNumber // and add it to the list when not duplicated. Additionally we cleanup after ourselves when endBlock // gets cleared useEffect((): void => { - const [, endBlock] = auctionInfo || [null, null]; + const { endBlock } = auctionInfo; if (!endBlock) { setResult((prev) => prev && prev.length ? [] : prev); diff --git a/packages/page-parachains/src/Crowdloan/FundAdd.tsx b/packages/page-parachains/src/Crowdloan/FundAdd.tsx index 48f41fc534..12b324642d 100644 --- a/packages/page-parachains/src/Crowdloan/FundAdd.tsx +++ b/packages/page-parachains/src/Crowdloan/FundAdd.tsx @@ -2,24 +2,26 @@ // SPDX-License-Identifier: Apache-2.0 import type BN from 'bn.js'; -import type { OwnedId, OwnerInfo } from '../types'; +import type { AuctionInfo, LeasePeriod, OwnedId, OwnerInfo } from '../types'; import React, { useState } from 'react'; import { Button, InputBalance, InputNumber, Modal, TxButton } from '@polkadot/react-components'; import { useApi, useToggle } from '@polkadot/react-hooks'; -import { BN_ZERO } from '@polkadot/util'; +import { BN_ONE, BN_THREE, BN_ZERO } from '@polkadot/util'; import InputOwner from '../InputOwner'; import { useTranslation } from '../translate'; interface Props { + auctionInfo: AuctionInfo; bestNumber?: BN; className?: string; + leasePeriod: LeasePeriod | null; ownedIds: OwnedId[]; } -function FundAdd ({ bestNumber, className, ownedIds }: Props): React.ReactElement { +function FundAdd ({ auctionInfo, bestNumber, className, leasePeriod, ownedIds }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [{ accountId, paraId }, setOwnerInfo] = useState({ accountId: null, paraId: 0 }); @@ -30,7 +32,9 @@ function FundAdd ({ bestNumber, className, ownedIds }: Props): React.ReactElemen const [isOpen, toggleOpen] = useToggle(); const isEndError = !bestNumber || !endBlock || endBlock.lt(bestNumber); - const isLastError = !lastSlot || !firstSlot || lastSlot.lt(firstSlot) || lastSlot.gt(firstSlot.addn(3)); + const isFirstError = !firstSlot || (!!leasePeriod && firstSlot.lte(leasePeriod.currentPeriod)); + const isLastError = !lastSlot || !firstSlot || lastSlot.lt(firstSlot) || lastSlot.gt(firstSlot.add(BN_THREE)); + const defaultSlot = (auctionInfo.leasePeriod || leasePeriod?.currentPeriod.add(BN_ONE) || 1).toString(); // TODO Add verifier @@ -53,26 +57,34 @@ function FundAdd ({ bestNumber, className, ownedIds }: Props): React.ReactElemen onChange={setOwnerInfo} ownedIds={ownedIds} /> - ('The amount to be raised in this funding campaign')}> + ('The amount to be raised in this funding campaign.')}> ('crowdfund cap')} onChange={setCap} /> - ('The block until which this fund is active for')}> + ('The end block for contributions to this fund.')}> ('ending block')} onChange={setEndBlock} /> - ('The first and last slots for this funding campaign. The last slot should be after the first and a maximum of 3 slots more than the first')}> + +

{t('The first and last slots for this funding campaign.')}

+

{t('The last slot should be after the first and a maximum of 3 slots more than the first')}

+ + }> ('first slot')} onChange={setFirstSlot} /> ('last slot')} onChange={setLastSlot} @@ -83,7 +95,7 @@ function FundAdd ({ bestNumber, className, ownedIds }: Props): React.ReactElemen ('Add')} onStart={toggleOpen} params={[paraId, cap, firstSlot, lastSlot, endBlock, null]} diff --git a/packages/page-parachains/src/Crowdloan/index.tsx b/packages/page-parachains/src/Crowdloan/index.tsx index ed0be7f916..3dbd8ce08f 100644 --- a/packages/page-parachains/src/Crowdloan/index.tsx +++ b/packages/page-parachains/src/Crowdloan/index.tsx @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { OwnedId } from '../types'; +import type { AuctionInfo, LeasePeriod, OwnedId } from '../types'; import React from 'react'; @@ -14,11 +14,13 @@ import Summary from './Summary'; import useFunds from './useFunds'; interface Props { + auctionInfo: AuctionInfo; className?: string; + leasePeriod: LeasePeriod | null; ownedIds: OwnedId[]; } -function Crowdloan ({ className, ownedIds }: Props): React.ReactElement { +function Crowdloan ({ auctionInfo, className, leasePeriod, ownedIds }: Props): React.ReactElement { const bestNumber = useBestNumber(); const { activeCap, activeRaised, funds, totalCap, totalRaised } = useFunds(); @@ -33,7 +35,9 @@ function Crowdloan ({ className, ownedIds }: Props): React.ReactElement { /> diff --git a/packages/page-parachains/src/InputOwner.tsx b/packages/page-parachains/src/InputOwner.tsx index 7891efc23f..714ac98346 100644 --- a/packages/page-parachains/src/InputOwner.tsx +++ b/packages/page-parachains/src/InputOwner.tsx @@ -36,28 +36,29 @@ function InputOwner ({ onChange, ownedIds }: Props): React.ReactElement { ); return ( - <> - ('This account that has been used to register the parachain. This will pay all associated fees.')}> - ('parachain owner')} - onChange={setAccountId} - type='account' - value={accountId} - /> - + +

{t('This account that has been used to register the parachain. This will pay all associated fees.')}

+

{t('The parachain id is associated with the selected account via parathread registration.')}

+ + }> + ('parachain owner')} + onChange={setAccountId} + type='account' + value={accountId} + /> {accountId && ( - ('The parachain id, as associated with the account on the registrar')}> - ('parachain id')} - onChange={setParaId} - options={optIds} - /> - + ('parachain id')} + onChange={setParaId} + options={optIds} + /> )} - +
); } diff --git a/packages/page-parachains/src/Overview/Summary.tsx b/packages/page-parachains/src/Overview/Summary.tsx index b7872ed925..1f93478c90 100644 --- a/packages/page-parachains/src/Overview/Summary.tsx +++ b/packages/page-parachains/src/Overview/Summary.tsx @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { LeasePeriod } from './types'; +import type { LeasePeriod } from '../types'; import React from 'react'; diff --git a/packages/page-parachains/src/Overview/index.tsx b/packages/page-parachains/src/Overview/index.tsx index 133444aa06..87c38f4617 100644 --- a/packages/page-parachains/src/Overview/index.tsx +++ b/packages/page-parachains/src/Overview/index.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { ParaId } from '@polkadot/types/interfaces'; -import type { Proposals } from '../types'; +import type { LeasePeriod, Proposals } from '../types'; import React from 'react'; @@ -13,19 +13,18 @@ import Parachains from './Parachains'; import Summary from './Summary'; import Upcomings from './Upcomings'; import useActionsQueue from './useActionsQueue'; -import useLeasePeriod from './useLeasePeriod'; import useUpcomingIds from './useUpcomingIds'; interface Props { className?: string; + leasePeriod: LeasePeriod | null; proposals?: Proposals; } -function Overview ({ className, proposals }: Props): React.ReactElement { +function Overview ({ className, leasePeriod, proposals }: Props): React.ReactElement { const { api } = useApi(); const paraIds = useCall(api.query.paras?.parachains); const actionsQueue = useActionsQueue(); - const leasePeriod = useLeasePeriod(); const upcomingIds = useUpcomingIds(); return ( diff --git a/packages/page-parachains/src/Overview/types.ts b/packages/page-parachains/src/Overview/types.ts index d3c414f272..d2f2791f60 100644 --- a/packages/page-parachains/src/Overview/types.ts +++ b/packages/page-parachains/src/Overview/types.ts @@ -10,12 +10,6 @@ export interface LeaseInfo { period: number; } -export interface LeasePeriod { - currentPeriod: BN; - length: BN; - remainder: BN; -} - export interface QueuedAction { paraIds: ParaId[]; sessionIndex: BN; diff --git a/packages/page-parachains/src/index.tsx b/packages/page-parachains/src/index.tsx index db4cdb6461..289ad6996f 100644 --- a/packages/page-parachains/src/index.tsx +++ b/packages/page-parachains/src/index.tsx @@ -14,6 +14,8 @@ import Crowdloan from './Crowdloan'; import Overview from './Overview'; import Proposals from './Proposals'; import { useTranslation } from './translate'; +import useAuctionInfo from './useAuctionInfo'; +import useLeasePeriod from './useLeasePeriod'; import useOwnedIds from './useOwnedIds'; import useProposals from './useProposals'; @@ -26,6 +28,8 @@ function ParachainsApp ({ basePath, className }: Props): React.ReactElement - + - + @@ -68,6 +79,7 @@ function ParachainsApp ({ basePath, className }: Props): React.ReactElement diff --git a/packages/page-parachains/src/types.ts b/packages/page-parachains/src/types.ts index 2ecc3d6995..83ee68f5a5 100644 --- a/packages/page-parachains/src/types.ts +++ b/packages/page-parachains/src/types.ts @@ -1,7 +1,14 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { ParachainProposal, ParaId, ParaInfo, SessionIndex } from '@polkadot/types/interfaces'; +import type BN from 'bn.js'; +import type { AuctionIndex, BlockNumber, LeasePeriodOf, ParachainProposal, ParaId, ParaInfo, SessionIndex } from '@polkadot/types/interfaces'; + +export interface AuctionInfo { + endBlock: BlockNumber | null; + leasePeriod: LeasePeriodOf | null; + numAuctions: AuctionIndex | null; +} export interface ProposalExt { id: ParaId; @@ -15,6 +22,12 @@ export interface ScheduledProposals { sessionIndex: SessionIndex; } +export interface LeasePeriod { + currentPeriod: BN; + length: BN; + remainder: BN; +} + export interface Proposals { approvedIds: ParaId[]; proposalIds: ParaId[]; diff --git a/packages/page-parachains/src/useAuctionInfo.tsx b/packages/page-parachains/src/useAuctionInfo.tsx new file mode 100644 index 0000000000..2a088501b0 --- /dev/null +++ b/packages/page-parachains/src/useAuctionInfo.tsx @@ -0,0 +1,37 @@ +// Copyright 2017-2021 @polkadot/app-parachains authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Option } from '@polkadot/types'; +import type { AuctionIndex, BlockNumber, LeasePeriodOf } from '@polkadot/types/interfaces'; +import type { ITuple } from '@polkadot/types/types'; +import type { AuctionInfo } from './types'; + +import { useApi, useCallMulti } from '@polkadot/react-hooks'; + +const defaultValue: AuctionInfo = { + endBlock: null, + leasePeriod: null, + numAuctions: null +}; + +const optionsMulti = { + defaultValue, + transform: ([numAuctions, optInfo]: [AuctionIndex, Option>]): AuctionInfo => { + const [leasePeriod, endBlock] = optInfo.unwrapOr([null, null]); + + return { + endBlock, + leasePeriod, + numAuctions + }; + } +}; + +export default function useAuctionInfo (): AuctionInfo { + const { api } = useApi(); + + return useCallMulti([ + api.query.auctions?.auctionCounter, + api.query.auctions?.auctionInfo + ], optionsMulti); +} diff --git a/packages/page-parachains/src/Overview/useLeasePeriod.ts b/packages/page-parachains/src/useLeasePeriod.ts similarity index 100% rename from packages/page-parachains/src/Overview/useLeasePeriod.ts rename to packages/page-parachains/src/useLeasePeriod.ts

{formatNumber(paraId)}

{auctionInfo.leasePeriod && ( + + )}