From f461c846426f5bcb8e81652992620ae2a7b90f2f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 7 May 2019 10:00:53 +0200 Subject: [PATCH] Small contracts fixes & adjustments (#1092) * Adjust code deploy redirection (w & wo contracts) * Allow contract call with 0 value * TxButton to calculate all on click * Stray logging --- packages/app-contracts/src/Call.tsx | 4 +- packages/app-contracts/src/Code.tsx | 4 +- packages/ui-app/src/TxButton.tsx | 71 +++++++++++++++-------------- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/packages/app-contracts/src/Call.tsx b/packages/app-contracts/src/Call.tsx index 2dd3acf9bd..4f003de48e 100644 --- a/packages/app-contracts/src/Call.tsx +++ b/packages/app-contracts/src/Call.tsx @@ -40,7 +40,7 @@ class Call extends React.PureComponent { render () { const { t } = this.props; - const { accountId, address, contractAbi, endowment, gasLimit, isAddressValid, method } = this.state; + const { accountId, address, contractAbi, gasLimit, isAddressValid, method } = this.state; const contractOptions = store.getAllContracts().map(({ json: { address, name } }) => ({ text: `${name} (${address})`, value: address @@ -62,7 +62,7 @@ class Call extends React.PureComponent { const defaultContract = contractOptions.length ? contractOptions[contractOptions.length - 1].value : undefined; - const isEndowValid = !endowment.isZero(); + const isEndowValid = true; // !endowment.isZero(); const isGasValid = !gasLimit.isZero(); const isValid = !!accountId && isEndowValid && isGasValid && isAddressValid; diff --git a/packages/app-contracts/src/Code.tsx b/packages/app-contracts/src/Code.tsx index 92fe6f7fe6..6d5c34c0ee 100644 --- a/packages/app-contracts/src/Code.tsx +++ b/packages/app-contracts/src/Code.tsx @@ -271,7 +271,9 @@ class Deploy extends React.PureComponent { } private redirect () { - window.location.hash = this.props.basePath; + window.location.hash = store.hasContracts + ? `${this.props.basePath}/instantiate` + : this.props.basePath; } } diff --git a/packages/ui-app/src/TxButton.tsx b/packages/ui-app/src/TxButton.tsx index 64fd1ee6a6..c7b834c7fc 100644 --- a/packages/ui-app/src/TxButton.tsx +++ b/packages/ui-app/src/TxButton.tsx @@ -9,6 +9,7 @@ import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; import { QueueTx, QueueTx$ExtrinsicAdd, TxCallback } from './Status/types'; import React from 'react'; +import { SubmittableResult } from '@polkadot/api'; import { withApi } from '@polkadot/ui-api'; import { assert, isFunction, isUndefined } from '@polkadot/util'; @@ -50,36 +51,6 @@ class TxButtonInner extends React.PureComponent { isSending: false } as State; - static getDerivedStateFromProps ({ api, isDisabled, params = [], txqueue = [], tx = '', extrinsic: propsExtrinsic }: InnerProps): State | null { - if (isDisabled || !propsExtrinsic && (!tx || tx.length === 0)) { - return null; - } - - let extrinsic: any; - if (propsExtrinsic) { - extrinsic = propsExtrinsic; - } else { - const [section, method] = tx.split('.'); - - assert(api.tx[section] && api.tx[section][method], `Unable to find api.tx.${section}.${method}`); - - extrinsic = api.tx[section][method]( - ...(isFunction(params) ? params() : params) - ); - } - - let isSending = false; - const queuedTx = txqueue.find(({ extrinsic: ex }) => ex === extrinsic); - if (queuedTx) { - isSending = ['broadcast', 'sending'].includes(queuedTx.status); - } - - return { - extrinsic, - isSending - }; - } - render () { const { accountId, isDisabled, isNegative, isPrimary, label } = this.props; const { isSending } = this.state; @@ -97,21 +68,53 @@ class TxButtonInner extends React.PureComponent { } private send = (): void => { - const { accountId, onClick, onFailed, onSuccess, onUpdate, queueExtrinsic } = this.props; - const { extrinsic } = this.state; + const { accountId, api, onClick, onUpdate, params = [], queueExtrinsic, tx = '', extrinsic: propsExtrinsic } = this.props; + let extrinsic: any; + + if (propsExtrinsic) { + extrinsic = propsExtrinsic; + } else { + const [section, method] = tx.split('.'); + + assert(api.tx[section] && api.tx[section][method], `Unable to find api.tx.${section}.${method}`); + + extrinsic = api.tx[section][method](...( + isFunction(params) + ? params() + : params + )); + } assert(extrinsic, 'Expected generated extrinsic passed to TxButton'); + this.setState({ isSending: true }); + queueExtrinsic({ accountId, extrinsic, - txFailedCb: onFailed, - txSuccessCb: onSuccess, + txFailedCb: this.onFailed, + txSuccessCb: this.onSuccess, txUpdateCb: onUpdate }); onClick && onClick(); } + + private onFailed = (result: SubmittableResult): void => { + const { onFailed } = this.props; + + this.setState({ isSending: false }); + + onFailed && onFailed(result); + } + + private onSuccess = (result: SubmittableResult): void => { + const { onSuccess } = this.props; + + this.setState({ isSending: false }); + + onSuccess && onSuccess(result); + } } class TxButton extends React.PureComponent {