Polulate paraid from owners (#5276)

This commit is contained in:
Jaco 2021-05-13 23:30:18 +03:00 committed by GitHub
parent ce39bb9875
commit 50a22c1a2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 25 deletions

View File

@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { OwnedId } from '../types';
import React from 'react';
import { Button } from '@polkadot/react-components';
@ -12,9 +14,10 @@ import RegisterThread from './RegisterThread';
interface Props {
className?: string;
ownedIds: OwnedId[];
}
function Actions ({ className }: Props): React.ReactElement<Props> {
function Actions ({ className, ownedIds }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [isRegisterOpen, toggleRegisterOpen] = useToggle();
@ -33,11 +36,15 @@ function Actions ({ className }: Props): React.ReactElement<Props> {
)}
<Button
icon='plus'
isDisabled={api.tx.registrar.reserve ? !ownedIds.length : false}
label={t<string>('ParaThread')}
onClick={toggleRegisterOpen}
/>
{isRegisterOpen && (
<RegisterThread onClose={toggleRegisterOpen} />
<RegisterThread
onClose={toggleRegisterOpen}
ownedIds={ownedIds}
/>
)}
</Button.Group>
);

View File

@ -1,24 +1,27 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type BN from 'bn.js';
import type { BalanceOf } from '@polkadot/types/interfaces';
import type { OwnedId, OwnerInfo } from '../types';
import BN from 'bn.js';
import React, { useCallback, useMemo, useState } from 'react';
import { InputAddress, InputBalance, InputFile, InputNumber, Modal, TxButton } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { compactAddLength } from '@polkadot/util';
import InputOwner from '../InputOwner';
import { useTranslation } from '../translate';
import { LOWEST_INVALID_ID } from './constants';
interface Props {
className?: string;
onClose: () => void;
ownedIds: OwnedId[];
}
function RegisterThread ({ className, onClose }: Props): React.ReactElement<Props> {
function RegisterThread ({ className, onClose, ownedIds }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [accountId, setAccountId] = useState<string | null>(null);
@ -36,6 +39,14 @@ function RegisterThread ({ className, onClose }: Props): React.ReactElement<Prop
[]
);
const _setOwner = useCallback(
({ accountId, paraId }: OwnerInfo) => {
setAccountId(accountId);
setParaId(new BN(paraId));
},
[]
);
const reservedDeposit = useMemo(
() => (api.consts.registrar.paraDeposit as BalanceOf)
.add((api.consts.registrar.dataDepositPerByte as BalanceOf).muln(wasm ? wasm.length : 0))
@ -52,24 +63,37 @@ function RegisterThread ({ className, onClose }: Props): React.ReactElement<Prop
size='large'
>
<Modal.Content>
<Modal.Columns hint={t<string>('This account will be associated with the parachain and pay the deposit.')}>
<InputAddress
label={t<string>('register from')}
onChange={setAccountId}
type='account'
value={accountId}
/>
</Modal.Columns>
<Modal.Columns hint={t<string>('The id of this parachain as known on the network')}>
<InputNumber
autoFocus
defaultValue={LOWEST_INVALID_ID}
isError={isIdError}
isZeroable={false}
label={t<string>('parachain id')}
onChange={setParaId}
/>
</Modal.Columns>
{api.tx.registrar.reserve
? (
<InputOwner
noCodeCheck
onChange={_setOwner}
ownedIds={ownedIds}
/>
)
: (
<>
<Modal.Columns hint={t<string>('This account will be associated with the parachain and pay the deposit.')}>
<InputAddress
label={t<string>('register from')}
onChange={setAccountId}
type='account'
value={accountId}
/>
</Modal.Columns>
<Modal.Columns hint={t<string>('The id of this parachain as known on the network')}>
<InputNumber
autoFocus
defaultValue={LOWEST_INVALID_ID}
isError={isIdError}
isZeroable={false}
label={t<string>('parachain id')}
onChange={setParaId}
/>
</Modal.Columns>
</>
)
}
<Modal.Columns hint={t<string>('The WASM validation function for this parachain.')}>
<InputFile
help={t<string>('The compiled runtime WASM for the parachain you wish to register.')}

View File

@ -4,7 +4,7 @@
import type { Option } from '@polkadot/types';
import type { AccountId, BalanceOf, ParaId } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import type { LeaseInfo, LeasePeriod, QueuedAction } from '../types';
import type { LeaseInfo, LeasePeriod, OwnedId, QueuedAction } from '../types';
import React, { useCallback, useRef } from 'react';
@ -20,6 +20,7 @@ interface Props {
className?: string;
ids?: ParaId[];
leasePeriod?: LeasePeriod;
ownedIds: OwnedId[];
}
type ParaMap = [ParaId, LeaseInfo[]][];
@ -66,7 +67,7 @@ function extractParaMap (hasLinksMap: Record<string, boolean>, paraIds: ParaId[]
});
}
function Parathreads ({ actionsQueue, className, ids, leasePeriod }: Props): React.ReactElement<Props> {
function Parathreads ({ actionsQueue, className, ids, leasePeriod, ownedIds }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const hasLinksMap = useIsParasLinked(ids);
@ -90,7 +91,7 @@ function Parathreads ({ actionsQueue, className, ids, leasePeriod }: Props): Rea
return (
<div className={className}>
<Actions />
<Actions ownedIds={ownedIds} />
<Table
empty={leasePeriod && ids && (ids.length === 0 || leaseMap) && t<string>('There are no available parathreads')}
header={headerRef.current}

View File

@ -109,6 +109,7 @@ function ParachainsApp ({ basePath, className }: Props): React.ReactElement<Prop
className={pathname === `${basePath}/parathreads` ? '' : 'parachains--hidden'}
ids={upcomingIds}
leasePeriod={leasePeriod}
ownedIds={ownedIds}
/>
</main>
);