Manage asset creation state (#4914)

* Manage asset creation state

* Updates
This commit is contained in:
Jaco Greeff 2021-03-24 12:59:10 +01:00 committed by GitHub
parent d48a7a7f4b
commit 118b3f2514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 191 additions and 124 deletions

View File

@ -18,6 +18,7 @@ function Asset ({ className, value: { details, id, metadata } }: Props): React.R
<tr className={className}>
<td className='number'><h1>{formatNumber(id)}</h1></td>
<td className='together'>{metadata?.name.toUtf8()}</td>
<td className='address'>{details && <AddressSmall value={details.owner} />}</td>
<td className='address'>{details && <AddressSmall value={details.admin} />}</td>
<td className='address'>{details && <AddressSmall value={details.issuer} />}</td>
<td className='address'>{details && <AddressSmall value={details.freezer} />}</td>

View File

@ -22,9 +22,10 @@ function Assets ({ assetIds, className }: Props): React.ReactElement<Props> {
const headerRef = useRef([
[t('assets'), 'start', 2],
[t('admin'), 'start'],
[t('issuer'), 'start'],
[t('freezer'), 'start'],
[t('owner'), 'address'],
[t('admin'), 'address'],
[t('issuer'), 'address'],
[t('freezer'), 'address'],
[t('decimals')],
[t('symbol'), 'start'],
[]

View File

@ -0,0 +1,116 @@
// Copyright 2017-2021 @polkadot/app-assets authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { SubmittableExtrinsic } from '@polkadot/api/types';
import type { AssetId } from '@polkadot/types/interfaces';
import type { InfoState, MetadataState, TeamState } from './types';
import React, { useEffect, useState } from 'react';
import { Button, Modal, TxButton } from '@polkadot/react-components';
import { useApi, useStepper } from '@polkadot/react-hooks';
import { useTranslation } from '../../translate';
import Info from './Info';
import Metadata from './Metadata';
import Team from './Team';
interface Props {
assetIds: AssetId[];
className?: string;
onClose: () => void;
}
function Create ({ assetIds, className, onClose }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [step, nextStep, prevStep] = useStepper();
const [asset, setAsset] = useState<InfoState | null>(null);
const [metadata, setMetadata] = useState<MetadataState | null>(null);
const [team, setTeam] = useState<TeamState | null>(null);
const [extrinsic, setExtrinsic] = useState<SubmittableExtrinsic<'promise'> | null>(null);
useEffect((): void => {
setExtrinsic(
() => asset && metadata && team
? team.teamTx
? api.tx.utility.batchAll([asset.createTx, metadata.metadataTx, team.teamTx])
: api.tx.utility.batchAll([asset.createTx, metadata.metadataTx])
: null
);
}, [api, asset, metadata, team]);
return (
<Modal
className={className}
header={t<string>('create asset {{step}}/{{steps}}', { replace: { step, steps: 3 } })}
size='large'
>
{step === 1 && (
<Info
assetIds={assetIds}
defaultValue={asset}
onChange={setAsset}
/>
)}
{step === 2 && (
<Metadata
assetId={asset?.assetId}
defaultValue={metadata}
onChange={setMetadata}
/>
)}
{step === 3 && (
<Team
accountId={asset?.accountId}
assetId={asset?.assetId}
defaultValue={team}
onChange={setTeam}
/>
)}
<Modal.Actions onCancel={onClose}>
{step === 1 &&
<Button
icon='step-forward'
isDisabled={!asset}
label={t<string>('Next')}
onClick={nextStep}
/>
}
{step === 2 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={prevStep}
/>
<Button
icon='step-forward'
isDisabled={!metadata}
label={t<string>('Next')}
onClick={nextStep}
/>
</>
)}
{step === 3 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={prevStep}
/>
<TxButton
accountId={asset?.accountId}
extrinsic={extrinsic}
icon='plus'
label={t<string>('Create')}
onStart={onClose}
/>
</>
)}
</Modal.Actions>
</Modal>
);
}
export default React.memo(Create);

View File

@ -9,19 +9,28 @@ import React, { useEffect, useMemo, useState } from 'react';
import { InputAddress, InputNumber, Modal } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { BN_ZERO } from '@polkadot/util';
import { BN_ONE, BN_ZERO } from '@polkadot/util';
import { useTranslation } from '../../translate';
interface Props {
assetIds: AssetId[];
className?: string;
defaultValue: InfoState | null;
onChange: (info: InfoState | null) => void;
}
function Info ({ assetIds, className = '', onChange }: Props): React.ReactElement<Props> {
function findAvailbleId (assetIds: AssetId[]): BN {
return assetIds.length
? assetIds[assetIds.length - 1].add(BN_ONE)
: BN_ONE;
}
function Info ({ assetIds, className = '', defaultValue, onChange }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [initial] = useState(() => defaultValue);
const [initialId] = useState(() => findAvailbleId(assetIds));
const [accountId, setAccountId] = useState<string | null>(null);
const [assetId, setAssetId] = useState<BN | null>(null);
const [minBalance, setMinBalance] = useState<BN | null>(null);
@ -34,11 +43,7 @@ function Info ({ assetIds, className = '', onChange }: Props): React.ReactElemen
useEffect((): void => {
onChange(
assetId && accountId && minBalance && isIdValid && !minBalance.isZero()
? {
accountId,
assetId,
createTx: api.tx.assets.create(assetId, accountId, minBalance)
}
? { accountId, assetId, createTx: api.tx.assets.create(assetId, accountId, minBalance), minBalance }
: null
);
}, [api, accountId, assetId, assetIds, isIdValid, minBalance, onChange]);
@ -47,14 +52,16 @@ function Info ({ assetIds, className = '', onChange }: Props): React.ReactElemen
<Modal.Content className={className}>
<Modal.Columns hint={t<string>('The account that is to be used to create this asset and setup the initial metadata.')}>
<InputAddress
defaultValue={initial?.accountId}
label={t<string>('creator account')}
onChange={setAccountId}
type='account'
/>
</Modal.Columns>
<Modal.Columns hint={t<string>('The selected id for the asset. Should not match an already-existing id.')}>
<Modal.Columns hint={t<string>('The selected id for the asset. This should not match an already-existing asset id.')}>
<InputNumber
autoFocus
defaultValue={initial?.assetId || initialId}
isError={!isIdValid}
isZeroable={false}
label={t<string>('asset id')}
@ -63,6 +70,7 @@ function Info ({ assetIds, className = '', onChange }: Props): React.ReactElemen
</Modal.Columns>
<Modal.Columns hint={t<string>('The minimum balance for the asset.')}>
<InputNumber
defaultValue={initial?.minBalance || BN_ONE}
isZeroable={false}
label={t<string>('minimum balance')}
onChange={setMinBalance}

View File

@ -14,15 +14,17 @@ import { useTranslation } from '../../translate';
interface Props {
assetId?: BN | null;
className?: string;
defaultValue: MetadataState | null;
onChange: (info: MetadataState | null) => void;
}
function Metadata ({ assetId, className = '', onChange }: Props): React.ReactElement<Props> {
function Metadata ({ assetId, className = '', defaultValue, onChange }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [initial] = useState(() => defaultValue);
const [assetDecimals, setAssetDecimals] = useState<BN | null>(null);
const [assetName, setAssetName] = useState<string | null>(null);
const [assetSymbol, setAssetSymbol] = useState<string | null>(null);
const [assetName, setAssetName] = useState<string | null | undefined>(() => defaultValue?.assetName);
const [assetSymbol, setAssetSymbol] = useState<string | null | undefined>(() => defaultValue?.assetSymbol);
const isValidDecimals = useMemo(
() => !!assetDecimals && assetDecimals.lten(20),
@ -42,7 +44,7 @@ function Metadata ({ assetId, className = '', onChange }: Props): React.ReactEle
useEffect((): void => {
onChange(
assetId && assetName && assetSymbol && assetDecimals && isValidName && isValidSymbol && isValidDecimals
? { metadataTx: api.tx.assets.setMetadata(assetId, assetName, assetSymbol, assetDecimals) }
? { assetDecimals, assetName, assetSymbol, metadataTx: api.tx.assets.setMetadata(assetId, assetName, assetSymbol, assetDecimals) }
: null
);
}, [api, assetDecimals, assetId, assetName, assetSymbol, isValidName, isValidSymbol, isValidDecimals, onChange]);
@ -52,6 +54,7 @@ function Metadata ({ assetId, className = '', onChange }: Props): React.ReactEle
<Modal.Columns hint={t<string>('The descriptive name for this asset.')}>
<Input
autoFocus
defaultValue={initial?.assetName}
isError={!isValidName}
label={t<string>('asset name')}
onChange={setAssetName}
@ -59,6 +62,7 @@ function Metadata ({ assetId, className = '', onChange }: Props): React.ReactEle
</Modal.Columns>
<Modal.Columns hint={t<string>('The symbol that will represent this asset.')}>
<Input
defaultValue={initial?.assetSymbol}
isError={!isValidSymbol}
label={t<string>('asset symbol')}
onChange={setAssetSymbol}
@ -66,6 +70,7 @@ function Metadata ({ assetId, className = '', onChange }: Props): React.ReactEle
</Modal.Columns>
<Modal.Columns hint={t<string>('The number of decimals for this token. Max allowed via the UI is set to 20.')}>
<InputNumber
defaultValue={initial?.assetDecimals}
isError={!isValidDecimals}
label={t<string>('asset decimals')}
onChange={setAssetDecimals}

View File

@ -15,12 +15,14 @@ interface Props {
accountId?: string;
assetId?: BN;
className?: string;
defaultValue: TeamState | null;
onChange: (info: TeamState | null) => void;
}
function Team ({ accountId, assetId, className = '', onChange }: Props): React.ReactElement<Props> {
function Team ({ accountId, assetId, className = '', defaultValue, onChange }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [initial] = useState(() => defaultValue);
const [adminId, setAdminId] = useState<string | null>(null);
const [freezerId, setFreezerId] = useState<string | null>(null);
const [issuerId, setIssuerId] = useState<string | null>(null);
@ -29,8 +31,8 @@ function Team ({ accountId, assetId, className = '', onChange }: Props): React.R
onChange(
assetId && accountId && adminId && freezerId && issuerId
? adminId !== accountId || freezerId !== accountId || issuerId !== accountId
? { teamTx: api.tx.assets.setTeam(assetId, issuerId, adminId, freezerId) }
: { teamTx: null }
? { adminId, freezerId, issuerId, teamTx: api.tx.assets.setTeam(assetId, issuerId, adminId, freezerId) }
: { adminId, freezerId, issuerId, teamTx: null }
: null
);
}, [api, assetId, accountId, adminId, freezerId, issuerId, onChange]);
@ -39,7 +41,7 @@ function Team ({ accountId, assetId, className = '', onChange }: Props): React.R
<Modal.Content className={className}>
<Modal.Columns hint={t<string>('The account that is to be used for ongoing admin on the token.')}>
<InputAddress
defaultValue={accountId}
defaultValue={initial?.adminId || accountId}
label={t<string>('admin account')}
onChange={setAdminId}
type='account'
@ -47,7 +49,7 @@ function Team ({ accountId, assetId, className = '', onChange }: Props): React.R
</Modal.Columns>
<Modal.Columns hint={t<string>('The account that is to be used for issuing for this token.')}>
<InputAddress
defaultValue={accountId}
defaultValue={initial?.issuerId || accountId}
label={t<string>('issuer account')}
onChange={setIssuerId}
type='account'
@ -55,7 +57,7 @@ function Team ({ accountId, assetId, className = '', onChange }: Props): React.R
</Modal.Columns>
<Modal.Columns hint={t<string>('The account that is to be used for performing freezing.')}>
<InputAddress
defaultValue={accountId}
defaultValue={initial?.freezerId || accountId}
label={t<string>('freezer account')}
onChange={setFreezerId}
type='account'

View File

@ -1,47 +1,27 @@
// Copyright 2017-2021 @polkadot/app-assets authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { SubmittableExtrinsic } from '@polkadot/api/types';
import type { AssetId } from '@polkadot/types/interfaces';
import type { InfoState, MetadataState, TeamState } from './types';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import React from 'react';
import { Button, Modal, TxButton } from '@polkadot/react-components';
import { useAccounts, useApi, useStepper, useToggle } from '@polkadot/react-hooks';
import { Button } from '@polkadot/react-components';
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
import { isFunction } from '@polkadot/util';
import { useTranslation } from '../../translate';
import Info from './Info';
import Metadata from './Metadata';
import Team from './Team';
import Create from './Create';
interface Props {
assetIds?: AssetId[];
className?: string;
}
function Create ({ assetIds, className }: Props): React.ReactElement<Props> {
function CreateButton ({ assetIds, className }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { hasAccounts } = useAccounts();
const [step, nextStep, prevStep] = useStepper();
const [isOpen, toggleOpen] = useToggle();
const [asset, setAsset] = useState<InfoState | null>(null);
const [metadata, setMetadata] = useState<MetadataState | null>(null);
const [team, setTeam] = useState<TeamState | null>(null);
const [extrinsic, setExtrinsic] = useState<SubmittableExtrinsic<'promise'> | null>(null);
useEffect((): void => {
setExtrinsic(
() => asset && metadata && team
? team.teamTx
? api.tx.utility.batchAll([asset.createTx, metadata.metadataTx, team.teamTx])
: api.tx.utility.batchAll([asset.createTx, metadata.metadataTx])
: null
);
}, [api, asset, metadata, team]);
return (
<>
@ -52,79 +32,14 @@ function Create ({ assetIds, className }: Props): React.ReactElement<Props> {
onClick={toggleOpen}
/>
{isOpen && assetIds && (
<Modal
<Create
assetIds={assetIds}
className={className}
header={t<string>('create asset {{step}}/{{steps}}', { replace: { step, steps: 3 } })}
size='large'
>
{step === 1 && (
<Info
assetIds={assetIds}
onChange={setAsset}
/>
)}
{step === 2 && (
<Metadata
assetId={asset?.assetId}
onChange={setMetadata}
/>
)}
{step === 3 && (
<Team
accountId={asset?.accountId}
assetId={asset?.assetId}
onChange={setTeam}
/>
)}
<Modal.Actions onCancel={toggleOpen}>
{step === 1 &&
<Button
icon='step-forward'
isDisabled={!asset}
label={t<string>('Next')}
onClick={nextStep}
/>
}
{step === 2 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={prevStep}
/>
<Button
icon='step-forward'
isDisabled={!metadata}
label={t<string>('Next')}
onClick={nextStep}
/>
</>
)}
{step === 3 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={prevStep}
/>
<TxButton
accountId={asset?.accountId}
extrinsic={extrinsic}
icon='plus'
label={t<string>('Create')}
onStart={toggleOpen}
/>
</>
)}
</Modal.Actions>
</Modal>
onClose={toggleOpen}
/>
)}
</>
);
}
export default React.memo(styled(Create)`
.stepHidden.content {
display: none !important;
}
`);
export default React.memo(CreateButton);

View File

@ -8,12 +8,19 @@ export interface InfoState {
accountId: string;
assetId: BN;
createTx: SubmittableExtrinsic<'promise'>;
minBalance: BN;
}
export interface MetadataState {
assetDecimals: BN;
assetName: string;
assetSymbol: string;
metadataTx: SubmittableExtrinsic<'promise'>;
}
export interface TeamState {
adminId: string;
issuerId: string;
freezerId: string;
teamTx: SubmittableExtrinsic<'promise'> | null;
}

View File

@ -6,10 +6,10 @@ import type { AssetDetails, AssetId, AssetMetadata } from '@polkadot/types/inter
export interface AssetInfo {
details: AssetDetails | null;
id: AssetId;
isAdmin: boolean;
isOwner: boolean;
isIssuer: boolean;
isFreezer: boolean;
isAdminMe: boolean;
isIssuerMe: boolean;
isFreezerMe: boolean;
isOwnerMe: boolean;
key: string;
metadata: AssetMetadata | null;
}

View File

@ -9,6 +9,13 @@ import { useMemo } from 'react';
import { useAccounts, useApi, useCall } from '@polkadot/react-hooks';
const EMPTY_FLAGS = {
isAdminMe: false,
isFreezerMe: false,
isIssuerMe: false,
isOwnerMe: false
};
function isAccount (allAccounts: string[], accountId: AccountId): boolean {
const address = accountId.toString();
@ -19,12 +26,17 @@ function extractInfo (allAccounts: string[], id: AssetId, optDetails: Option<Ass
const details = optDetails.unwrapOr(null);
return {
...(details
? {
isAdminMe: isAccount(allAccounts, details.admin),
isFreezerMe: isAccount(allAccounts, details.freezer),
isIssuerMe: isAccount(allAccounts, details.issuer),
isOwnerMe: isAccount(allAccounts, details.owner)
}
: EMPTY_FLAGS
),
details,
id,
isAdmin: !!details && isAccount(allAccounts, details.admin),
isFreezer: !!details && isAccount(allAccounts, details.freezer),
isIssuer: !!details && isAccount(allAccounts, details.issuer),
isOwner: !!details && isAccount(allAccounts, details.owner),
key: id.toString(),
metadata: metadata.isEmpty
? null