diff --git a/package.json b/package.json index 1532e218a3..789f4a40c0 100644 --- a/package.json +++ b/package.json @@ -33,10 +33,10 @@ "afterSign": "electron-builder-notarize" }, "resolutions": { - "@polkadot/api": "^2.1.2-13", - "@polkadot/api-contract": "^2.1.2-13", + "@polkadot/api": "^2.1.2-15", + "@polkadot/api-contract": "^2.1.2-15", "@polkadot/keyring": "^3.5.1", - "@polkadot/types": "^2.1.2-13", + "@polkadot/types": "^2.1.2-15", "@polkadot/util": "^3.5.1", "@polkadot/util-crypto": "^3.5.1", "@polkadot/wasm-crypto": "^1.4.1", diff --git a/packages/page-contracts/package.json b/packages/page-contracts/package.json index feed0d31aa..563ec9bf23 100644 --- a/packages/page-contracts/package.json +++ b/packages/page-contracts/package.json @@ -12,6 +12,6 @@ "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.11.2", - "@polkadot/api-contract": "^2.1.2-13" + "@polkadot/api-contract": "^2.1.2-15" } } diff --git a/packages/page-contracts/src/Codes/Code.tsx b/packages/page-contracts/src/Codes/Code.tsx index 1caa647d7d..046da17912 100644 --- a/packages/page-contracts/src/Codes/Code.tsx +++ b/packages/page-contracts/src/Codes/Code.tsx @@ -16,41 +16,38 @@ import { useTranslation } from '../translate'; interface Props { className?: string; code: CodeStored; - onShowDeploy: (codeHash?: string, constructorIndex?: number) => () => void; + onShowDeploy: (codeHash: string, constructorIndex: number) => void; } function Code ({ className, code, onShowDeploy }: Props): React.ReactElement { - const { json: { codeHash } } = code; const { t } = useTranslation(); const [isForgetOpen, toggleIsForgetOpen] = useToggle(); const [isRemoveABIOpen, toggleIsRemoveABIOpen] = useToggle(); - const { contractAbi, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi([code.json.abi || null, code.contractAbi || null], codeHash, true); + const { contractAbi, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi([code.json.abi, code.contractAbi], code.json.codeHash, true); const _onShowDeploy = useCallback( - () => onShowDeploy(codeHash)(), - [codeHash, onShowDeploy] + () => onShowDeploy(code.json.codeHash, 0), + [code, onShowDeploy] ); const _onDeployConstructor = useCallback( - (constructorIndex = 0) => codeHash && onShowDeploy && onShowDeploy(codeHash, constructorIndex)(), - [codeHash, onShowDeploy] + (constructorIndex = 0): void => { + onShowDeploy && onShowDeploy(code.json.codeHash, constructorIndex); + }, + [code, onShowDeploy] ); const _onForget = useCallback( (): void => { - if (!codeHash) { - return; - } - try { - store.forgetCode(codeHash); + store.forgetCode(code.json.codeHash); } catch (error) { console.error(error); } finally { toggleIsForgetOpen(); } }, - [codeHash, toggleIsForgetOpen] + [code, toggleIsForgetOpen] ); return ( diff --git a/packages/page-contracts/src/Codes/index.tsx b/packages/page-contracts/src/Codes/index.tsx index 03f9d5c3d3..029ecc6150 100644 --- a/packages/page-contracts/src/Codes/index.tsx +++ b/packages/page-contracts/src/Codes/index.tsx @@ -10,7 +10,7 @@ import { useTranslation } from '../translate'; import Code from './Code'; interface Props { - onShowDeploy: (codeHash?: string, constructorIndex?: number) => () => void; + onShowDeploy: (codeHash: string, constructorIndex: number) => void; updated: number; } @@ -27,15 +27,13 @@ function Codes ({ onShowDeploy }: Props): React.ReactElement { empty={t('No code hashes available')} header={headerRef.current} > - {contracts.getAllCode().map((code): React.ReactNode => { - return ( - - ); - })} + {contracts.getAllCode().map((code): React.ReactNode => ( + + ))} ); } diff --git a/packages/page-contracts/src/Contracts/Call.tsx b/packages/page-contracts/src/Contracts/Call.tsx index 6b224e4ec6..1b51dd9c41 100644 --- a/packages/page-contracts/src/Contracts/Call.tsx +++ b/packages/page-contracts/src/Contracts/Call.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2020 @polkadot/app-contracts authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { SubmittableExtrinsic } from '@polkadot/api/types'; import { ContractCallOutcome } from '@polkadot/api-contract/types'; import { StringOrNull } from '@polkadot/react-components/types'; @@ -35,6 +36,7 @@ function Call ({ callContract, callMessageIndex, className = '', onChangeCallCon const [endowment, isEndowmentValid, setEndowment] = useFormField(BN_ZERO); const [isBusy, , setIsBusy] = useToggle(); const [outcomes, setOutcomes] = useState([]); + const [execTx, setExecTx] = useState | null>(null); const [params, setParams] = useState(callMessage ? callMessage.args.map(({ type }) => createValue({ type })) : []); const useWeightHook = useWeight(); const { isValid: isWeightValid, weight } = useWeightHook; @@ -46,8 +48,10 @@ function Call ({ callContract, callMessageIndex, className = '', onChangeCallCon }, [callContract, callMessageIndex]); useEffect((): void => { - setOutcomes([]); - }, [callContract]); + endowment && callContract.hasRpcContractsCall && callMessage.isMutating && setExecTx( + () => callContract.exec(callMessage, endowment, weight, ...params) + ); + }, [accountId, callContract, callMessage, endowment, weight, params]); const _onChangeCallMessageIndexString = useCallback( (callMessageIndexString: string): void => { @@ -58,17 +62,6 @@ function Call ({ callContract, callMessageIndex, className = '', onChangeCallCon [onChangeCallMessageIndex] ); - const _constructTx = useCallback( - (): unknown[] => { - if (!accountId || !callMessage || !callContract.address) { - return []; - } - - return [callContract.address.toString(), endowment, weight, callMessage(...params)]; - }, - [accountId, callContract, callMessage, endowment, weight, params] - ); - const _onSubmitRpc = useCallback( (): void => { if (!accountId || !callMessage || !endowment || !weight) return; @@ -108,68 +101,64 @@ function Call ({ callContract, callMessageIndex, className = '', onChangeCallCon onClose={onClose} > - {callContract && ( -
- ('Specify the user account to use for this contract call. And fees will be deducted from this account.')} - isDisabled={isBusy} - label={t('call from account')} - onChange={setAccountId} - type='account' - value={accountId} - /> - ('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')} - isDisabled={isBusy} - label={t('contract to use')} - onChange={onChangeCallContractAddress} - type='contract' - value={callContract.address.toString()} - /> - {callMessageIndex !== null && ( - <> - ('The message to send to this contract. Parameters are adjusted based on the ABI provided.')} - isDisabled={isBusy} - isError={callMessage === null} - label={t('message to send')} - onChange={_onChangeCallMessageIndexString} - options={getCallMessageOptions(callContract)} - value={`${callMessageIndex}`} - /> - - - )} - {!isViaRpc && ( - ('The allotted value for this contract, i.e. the amount transferred to the contract as part of this call.')} - isDisabled={isBusy} - isError={!isEndowmentValid} - isZeroable - label={t('value')} - onChange={setEndowment} - value={endowment} - /> - )} - ('The maximum amount of gas to use for this contract call. If the call requires more, it will fail.')} - label={t('maximum gas allowed')} - {...useWeightHook} - /> -
- )} - {outcomes.length > 0 && ( + ('Specify the user account to use for this contract call. And fees will be deducted from this account.')} + isDisabled={isBusy} + label={t('call from account')} + onChange={setAccountId} + type='account' + value={accountId} + /> + ('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')} + isDisabled={isBusy} + label={t('contract to use')} + onChange={onChangeCallContractAddress} + type='contract' + value={callContract.address.toString()} + /> + {callMessageIndex !== null && ( <> + ('The message to send to this contract. Parameters are adjusted based on the ABI provided.')} + isDisabled={isBusy} + isError={callMessage === null} + label={t('message to send')} + onChange={_onChangeCallMessageIndexString} + options={getCallMessageOptions(callContract)} + value={`${callMessageIndex}`} + /> + + + )} + {!isViaRpc && ( + ('The allotted value for this contract, i.e. the amount transferred to the contract as part of this call.')} + isDisabled={isBusy} + isError={!isEndowmentValid} + isZeroable + label={t('value')} + onChange={setEndowment} + value={endowment} + /> + )} + ('The maximum amount of gas to use for this contract call. If the call requires more, it will fail.')} + label={t('maximum gas allowed')} + {...useWeightHook} + /> + {outcomes.length > 0 && ( +

{t('Call results')} ))}

- + )}
@@ -204,14 +193,13 @@ function Call ({ callContract, callMessageIndex, className = '', onChangeCallCon : ( setIsBusy(true)} onFailed={(): void => setIsBusy(false)} onSuccess={(): void => setIsBusy(false)} - params={_constructTx} - tx='contracts.call' withSpinner /> ) diff --git a/packages/page-contracts/src/Deploy.tsx b/packages/page-contracts/src/Deploy.tsx index 58ab59e183..b4aa2a6d0a 100644 --- a/packages/page-contracts/src/Deploy.tsx +++ b/packages/page-contracts/src/Deploy.tsx @@ -1,7 +1,7 @@ // Copyright 2017-2020 @polkadot/app-contracts authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { StringOrNull, VoidFn } from '@polkadot/react-components/types'; +import { StringOrNull } from '@polkadot/react-components/types'; import { AccountId } from '@polkadot/types/interfaces'; import { CodecArg } from '@polkadot/types/types'; import { CodeStored } from './types'; @@ -21,16 +21,13 @@ import useAbi from './useAbi'; import useWeight from './useWeight'; import { ENDOWMENT } from './constants'; -type CodeOptions = { text: string; value: string }[]; type ConstructOptions = { key: string; text: React.ReactNode; value: string }[]; interface Props { basePath: string; - allCodes: CodeStored[]; codeHash: string; constructorIndex?: number; - onClose: VoidFn; - setCodeHash: React.Dispatch; + onClose: () => void; setConstructorIndex: React.Dispatch; } @@ -38,7 +35,7 @@ function defaultContractName (name: string) { return `${name} (instance)`; } -function Deploy ({ allCodes, basePath, codeHash, constructorIndex = 0, onClose, setCodeHash, setConstructorIndex }: Props): React.ReactElement { +function Deploy ({ basePath, codeHash, constructorIndex = 0, onClose, setConstructorIndex }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const history = useHistory(); @@ -52,19 +49,12 @@ function Deploy ({ allCodes, basePath, codeHash, constructorIndex = 0, onClose, const [accountId, isAccountIdValid, setAccountId] = useFormField(null); const [endowment, isEndowmentValid, setEndowment] = useNonZeroBn(new BN(ENDOWMENT)); const [name, isNameValid, setName] = useNonEmptyString(t(defaultContractName(code.json.name))); - const { abi, contractAbi, errorText, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi([code.json.abi || null, code.contractAbi || null], codeHash, true); + const { abi, contractAbi, errorText, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi([code.json.abi, code.contractAbi], codeHash, true); const isValid = useMemo( - (): boolean => isNameValid && isEndowmentValid && isWeightValid && isAccountIdValid, + () => isNameValid && isEndowmentValid && isWeightValid && isAccountIdValid, [isAccountIdValid, isEndowmentValid, isNameValid, isWeightValid] ); - const codeOptions = useMemo( - (): CodeOptions => allCodes.map(({ json: { codeHash, name } }): { text: string; value: string } => ({ - text: `${name} (${codeHash})`, - value: codeHash - })), - [allCodes] - ); const constructOptions = useMemo( (): ConstructOptions => { @@ -147,18 +137,6 @@ function Deploy ({ allCodes, basePath, codeHash, constructorIndex = 0, onClose, type='account' value={accountId} /> - void => - (): void => { - setCodeHash(codeHash || (allCodes && allCodes[0] ? allCodes[0].json.codeHash : undefined)); - setConstructorIndex(constructorIndex); - toggleDeploy(); - }, + (codeHash: string, constructorIndex: number): void => { + setCodeHash(codeHash || (allCodes && allCodes[0] ? allCodes[0].json.codeHash : undefined)); + setConstructorIndex(constructorIndex); + toggleDeploy(); + }, [allCodes, toggleDeploy] ); @@ -93,12 +92,6 @@ function ContractsApp ({ basePath, className = '' }: Props): React.ReactElement< label={t('Add an existing code hash')} onClick={toggleHash} /> - {/*