diff --git a/packages/page-contracts/src/Contracts/Call.tsx b/packages/page-contracts/src/Contracts/Call.tsx index b57d0c1b7e..b98d424821 100644 --- a/packages/page-contracts/src/Contracts/Call.tsx +++ b/packages/page-contracts/src/Contracts/Call.tsx @@ -2,14 +2,15 @@ // SPDX-License-Identifier: Apache-2.0 import { SubmittableExtrinsic } from '@polkadot/api/types'; +import { ContractCallOutcome } from '@polkadot/api-contract/types'; import { CallResult } from './types'; import BN from 'bn.js'; import React, { useCallback, useState, useEffect } from 'react'; import styled from 'styled-components'; -import { Button, Dropdown, Expander, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; +import { Button, Dropdown, Expander, InputAddress, InputBalance, Modal, Toggle, TxButton } from '@polkadot/react-components'; import { ContractPromise } from '@polkadot/api-contract'; -import { useAccountId, useFormField } from '@polkadot/react-hooks'; +import { useAccountId, useFormField, useToggle } from '@polkadot/react-hooks'; import { BN_ZERO } from '@polkadot/util'; import { InputMegaGas, Params } from '../shared'; @@ -22,12 +23,12 @@ interface Props { className?: string; contract: ContractPromise; messageIndex: number; - onChangeCallContractAddress: (address: string | null) => void; - onChangeCallMessageIndex: (messageIndex: number) => void; + onCallResult?: (messageIndex: number, result?: ContractCallOutcome | void) => void; + onChangeMessage: (messageIndex: number) => void; onClose: () => void; } -function Call ({ className = '', contract, messageIndex, onChangeCallContractAddress, onChangeCallMessageIndex, onClose }: Props): React.ReactElement | null { +function Call ({ className = '', contract, messageIndex, onCallResult, onChangeMessage, onClose }: Props): React.ReactElement | null { const { t } = useTranslation(); const message = contract.abi.messages[messageIndex]; const [accountId, setAccountId] = useAccountId(); @@ -35,6 +36,7 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd const [outcomes, setOutcomes] = useState([]); const [execTx, setExecTx] = useState | null>(null); const [params, setParams] = useState([]); + const [isViaCall, toggleViaCall] = useToggle(); const weight = useWeight(); useEffect((): void => { @@ -58,19 +60,22 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd contract .read(message, message.isPayable ? value : 0, weight.weight, ...params) .send(accountId) - .then((outcome) => setOutcomes([ - { - ...outcome, + .then((result): void => { + setOutcomes([{ + ...result, from: accountId, message, params, when: new Date() - }, - ...outcomes - ])) - .catch(console.error); + }, ...outcomes]); + onCallResult && onCallResult(messageIndex, result); + }) + .catch((error): void => { + console.error(error); + onCallResult && onCallResult(messageIndex); + }); }, - [accountId, contract, message, value, weight, outcomes, params] + [accountId, contract, message, messageIndex, onCallResult, outcomes, params, value, weight] ); const _onClearOutcome = useCallback( @@ -80,7 +85,7 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd ); const isValid = !!(accountId && weight.isValid && isValueValid); - const isViaRpc = contract.hasRpcContractsCall && !message.isMutating; + const isViaRpc = contract.hasRpcContractsCall && (isViaCall || !message.isMutating); return ( + ('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')} + isDisabled + label={t('contract to use')} + type='contract' + value={contract.address} + /> ('Specify the user account to use for this contract call. And fees will be deducted from this account.')} @@ -97,13 +109,6 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd type='account' value={accountId} /> - ('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')} - label={t('contract to use')} - onChange={onChangeCallContractAddress} - type='contract' - value={contract.address} - /> {messageIndex !== null && ( <> ('The message to send to this contract. Parameters are adjusted based on the ABI provided.')} isError={message === null} label={t('message to send')} - onChange={onChangeCallMessageIndex} + onChange={onChangeMessage} options={getCallMessageOptions(contract)} value={messageIndex} /> @@ -140,6 +145,14 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd label={t('maximum gas allowed')} {...weight} /> + {message.isMutating && ( + ('read contract only, no execution')} + onChange={toggleViaCall} + value={isViaCall} + /> + )} {outcomes.length > 0 && ( ) } diff --git a/packages/page-contracts/src/Contracts/Contract.tsx b/packages/page-contracts/src/Contracts/Contract.tsx index de21ab888e..53cf8d57cb 100644 --- a/packages/page-contracts/src/Contracts/Contract.tsx +++ b/packages/page-contracts/src/Contracts/Contract.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2020 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { ContractCallOutcome } from '@polkadot/api-contract/types'; import { ActionStatus } from '@polkadot/react-components/Status/types'; import { ContractInfo } from '@polkadot/types/interfaces'; @@ -18,19 +19,25 @@ import { useTranslation } from '../translate'; interface Props { className?: string; contract: ContractPromise; - onCall: (index?: number) => void; + index: number; + onCall: (contractIndex: number, messaeIndex: number, resultCb: (messageIndex: number, result?: ContractCallOutcome) => void) => void; } function transformInfo (optInfo: Option): ContractInfo | null { return optInfo.unwrapOr(null); } -function Contract ({ className, contract, onCall }: Props): React.ReactElement | null { +function Contract ({ className, contract, index, onCall }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const info = useCall(api.query.contracts.contractInfoOf, [contract.address], { transform: transformInfo }); const [isForgetOpen, toggleIsForgetOpen] = useToggle(); + const _onCall = useCallback( + (messageIndex: number, resultCb: (messageIndex: number, result?: ContractCallOutcome) => void) => onCall(index, messageIndex, resultCb), + [index, onCall] + ); + const _onForget = useCallback( (): void => { const status: Partial = { @@ -71,7 +78,7 @@ function Contract ({ className, contract, onCall }: Props): React.ReactElement

@@ -95,13 +102,6 @@ function Contract ({ className, contract, onCall }: Props): React.ReactElement

- {!contract.abi && ( -