Adjust contracts read results (available display) (#3861)

This commit is contained in:
Jaco Greeff 2020-10-13 17:05:14 +02:00 committed by GitHub
parent 1a72593cd4
commit b1469b2282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 64 deletions

View File

@ -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<Props> | null {
function Call ({ className = '', contract, messageIndex, onCallResult, onChangeMessage, onClose }: Props): React.ReactElement<Props> | 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<CallResult[]>([]);
const [execTx, setExecTx] = useState<SubmittableExtrinsic<'promise'> | null>(null);
const [params, setParams] = useState<any[]>([]);
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 (
<Modal
@ -89,6 +94,13 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd
onClose={onClose}
>
<Modal.Content>
<InputAddress
help={t<string>('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')}
isDisabled
label={t<string>('contract to use')}
type='contract'
value={contract.address}
/>
<InputAddress
defaultValue={accountId}
help={t<string>('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}
/>
<InputAddress
help={t<string>('A deployed contract that has either been deployed or attached. The address and ABI are used to construct the parameters.')}
label={t<string>('contract to use')}
onChange={onChangeCallContractAddress}
type='contract'
value={contract.address}
/>
{messageIndex !== null && (
<>
<Dropdown
@ -111,7 +116,7 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd
help={t<string>('The message to send to this contract. Parameters are adjusted based on the ABI provided.')}
isError={message === null}
label={t<string>('message to send')}
onChange={onChangeCallMessageIndex}
onChange={onChangeMessage}
options={getCallMessageOptions(contract)}
value={messageIndex}
/>
@ -140,6 +145,14 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd
label={t<string>('maximum gas allowed')}
{...weight}
/>
{message.isMutating && (
<Toggle
className='rpc-toggle'
label={t<string>('read contract only, no execution')}
onChange={toggleViaCall}
value={isViaCall}
/>
)}
{outcomes.length > 0 && (
<Expander
className='outcomes'
@ -173,7 +186,7 @@ function Call ({ className = '', contract, messageIndex, onChangeCallContractAdd
icon='sign-in-alt'
isDisabled={!isValid || !execTx}
label={t('Execute')}
withSpinner
onStart={onClose}
/>
)
}

View File

@ -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>): ContractInfo | null {
return optInfo.unwrapOr(null);
}
function Contract ({ className, contract, onCall }: Props): React.ReactElement<Props> | null {
function Contract ({ className, contract, index, onCall }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const info = useCall<ContractInfo | null>(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<ActionStatus> = {
@ -71,7 +78,7 @@ function Contract ({ className, contract, onCall }: Props): React.ReactElement<P
contract={contract}
contractAbi={contract.abi}
isWatching
onSelect={onCall}
onSelect={_onCall}
withMessages
/>
</td>
@ -95,13 +102,6 @@ function Contract ({ className, contract, onCall }: Props): React.ReactElement<P
icon='trash'
onClick={toggleIsForgetOpen}
/>
{!contract.abi && (
<Button
icon='play'
label={t<string>('exec')}
onClick={onCall}
/>
)}
</td>
</tr>
);

View File

@ -6,6 +6,7 @@ import { CallResult } from './types';
import React from 'react';
import styled from 'styled-components';
import { Button, IdentityIcon, Output } from '@polkadot/react-components';
import valueToText from '@polkadot/react-params/valueToText';
import MessageSignature from '../shared/MessageSignature';
@ -36,7 +37,7 @@ function Outcome ({ className = '', onClear, outcome: { from, message, output, p
{when.toLocaleTimeString()}
</span>
}
value={(output || '()').toString()}
value={valueToText('Text', output)}
/>
<Button
icon='times'

View File

@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { StringOrNull } from '@polkadot/react-components/types';
import { ContractCallOutcome } from '@polkadot/api-contract/types';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { ApiPromise } from '@polkadot/api';
@ -19,17 +19,22 @@ export interface Props {
updated: number;
}
interface Indexes {
contractIndex: number;
messageIndex: number;
onCallResult?: (messageIndex: number, result?: ContractCallOutcome) => void;
}
function filterContracts (api: ApiPromise, keyringContracts: string[] = []): ContractPromise[] {
return keyringContracts
.map((address): ContractPromise | null => getContractForAddress(api, address.toString()))
.filter((contract: ContractPromise | null): contract is ContractPromise => !!contract);
.map((address) => getContractForAddress(api, address.toString()))
.filter((contract): contract is ContractPromise => !!contract);
}
function Contracts ({ contracts: keyringContracts }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [contractIndex, setContractIndex] = useState<number>(0);
const [messageIndex, setMessageIndex] = useState<number>(0);
const [{ contractIndex, messageIndex, onCallResult }, setIndexes] = useState<Indexes>({ contractIndex: 0, messageIndex: 0 });
const [isCallOpen, setIsCallOpen] = useState(false);
const headerRef = useRef<[string?, string?, number?][]>([
@ -49,27 +54,16 @@ function Contracts ({ contracts: keyringContracts }: Props): React.ReactElement<
[]
);
const _onChangeCallContractAddress = (newCallContractAddress: StringOrNull): void => {
const index = contracts.findIndex(({ address }) => newCallContractAddress === address.toString());
if (index > -1) {
index !== contractIndex && setMessageIndex(0);
setContractIndex(index);
}
};
const _onChangeCallMessageIndex = useCallback(
(messageIndex: number) => contracts[contractIndex] && setMessageIndex(messageIndex || 0),
[contractIndex, contracts]
const _onCall = useCallback(
(contractIndex: number, messageIndex: number, onCallResult: (messageIndex: number, result?: ContractCallOutcome) => void): void => {
setIndexes({ contractIndex, messageIndex, onCallResult });
setIsCallOpen(true);
},
[]
);
const _onCall = useCallback(
(contractIndex: number) =>
(messageIndex?: number): void => {
setContractIndex(contractIndex);
setMessageIndex(messageIndex || 0);
setIsCallOpen(true);
},
const _setMessageIndex = useCallback(
(messageIndex: number) => setIndexes((state) => ({ ...state, messageIndex })),
[]
);
@ -84,8 +78,9 @@ function Contracts ({ contracts: keyringContracts }: Props): React.ReactElement<
{contracts.map((contract, index): React.ReactNode => (
<Contract
contract={contract}
index={index}
key={contract.address.toString()}
onCall={_onCall(index)}
onCall={_onCall}
/>
))}
</Table>
@ -94,8 +89,8 @@ function Contracts ({ contracts: keyringContracts }: Props): React.ReactElement<
contract={contract}
isOpen={isCallOpen}
messageIndex={messageIndex}
onChangeCallContractAddress={_onChangeCallContractAddress}
onChangeCallMessageIndex={_onChangeCallMessageIndex}
onCallResult={onCallResult}
onChangeMessage={_setMessageIndex}
onClose={_toggleCall}
/>
)}

View File

@ -65,7 +65,7 @@ function Message ({ className = '', index, lastResult, message, onSelect }: Prop
<Button
className='accessory'
icon='play'
isDisabled={lastResult?.result.isSuccess}
isDisabled={message.isMutating ? false : (!message.args.length && lastResult?.result.isSuccess)}
label={message.isMutating ? t<string>('exec') : t<string>('read')}
onClick={_onSelect}
/>

View File

@ -23,7 +23,7 @@ export interface Props {
isRemovable?: boolean;
isWatching?: boolean;
onRemove?: () => void;
onSelect?: (messageIndex: number) => void;
onSelect?: (messageIndex: number, resultCb: (messageIndex: number, result?: ContractCallOutcome) => void) => void;
onSelectConstructor?: (constructorIndex: number) => void;
withConstructors?: boolean;
withMessages?: boolean;
@ -71,6 +71,20 @@ function Messages ({ className = '', contract, contractAbi: { constructors, mess
.catch(console.error);
}, [api, contract, isUpdating, isWatching, messages, optInfo]);
const _setMessageResult = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(messageIndex: number, result?: ContractCallOutcome): void => {
// ignore... for now
// setLastResults((all) => all.map((r, index) => index === messageIndex ? result : r));
},
[]
);
const _onSelect = useCallback(
(index: number) => onSelect && onSelect(index, _setMessageResult),
[_setMessageResult, onSelect]
);
return (
<div className={`ui--Messages ${className} ${isLabelled ? 'labelled' : ''}`}>
{withConstructors && (
@ -96,7 +110,7 @@ function Messages ({ className = '', contract, contractAbi: { constructors, mess
key={index}
lastResult={lastResults[index]}
message={message}
onSelect={onSelect}
onSelect={_onSelect}
/>
))}
</Expander>