From 184c896173aa1023cd67e32418fcbfc70fd05b12 Mon Sep 17 00:00:00 2001 From: Andreea Eftene Date: Mon, 28 Aug 2023 14:34:19 +0300 Subject: [PATCH 1/2] catch instantiate reversion --- src/helpers/checkInstantiateReversion.ts | 10 ++++++++++ src/ui/components/instantiate/DryRun.tsx | 11 ++++++++++- src/ui/components/instantiate/Step2.tsx | 6 +++++- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/helpers/checkInstantiateReversion.ts diff --git a/src/helpers/checkInstantiateReversion.ts b/src/helpers/checkInstantiateReversion.ts new file mode 100644 index 00000000..2b4cd5e8 --- /dev/null +++ b/src/helpers/checkInstantiateReversion.ts @@ -0,0 +1,10 @@ +// Copyright 2022 @paritytech/contracts-ui authors & contributors +// SPDX-License-Identifier: GPL-3.0-only + +import { ContractInstantiateResult } from 'types'; + +export function checkInstantiateReversion(dryRunResult: ContractInstantiateResult | undefined) { + return dryRunResult?.result.isOk + ? dryRunResult.result.asOk.result.flags.toHuman().includes('Revert') + : false; +} diff --git a/src/ui/components/instantiate/DryRun.tsx b/src/ui/components/instantiate/DryRun.tsx index 206fc775..8f25170a 100644 --- a/src/ui/components/instantiate/DryRun.tsx +++ b/src/ui/components/instantiate/DryRun.tsx @@ -6,6 +6,7 @@ import { SidePanel } from '../common/SidePanel'; import { Account } from '../account/Account'; import { OutcomeItem } from '../contract/OutcomeItem'; import { useApi, useInstantiate } from 'ui/contexts'; +import { checkInstantiateReversion } from 'helpers/checkInstantiateReversion'; export function DryRun() { const { @@ -18,6 +19,8 @@ export function DryRun() { ? api.registry.findMetaError(dryRunResult?.result.asErr.asModule) : null; + const isReversion = checkInstantiateReversion(dryRunResult); + return (
@@ -96,12 +99,18 @@ export function DryRun() {
- {dryRunResult?.result.isOk && ( + {dryRunResult?.result.isOk && !isReversion && (
The instantiation will be successful.
)} + {isReversion && ( +
+ + Contract reverted! The instantiation will not be successful. +
+ )} {dryRunError && dryRunResult && ( <>
diff --git a/src/ui/components/instantiate/Step2.tsx b/src/ui/components/instantiate/Step2.tsx index 35a92064..cbdf0c73 100644 --- a/src/ui/components/instantiate/Step2.tsx +++ b/src/ui/components/instantiate/Step2.tsx @@ -32,6 +32,7 @@ import { useBalance, } from 'ui/hooks'; import { AbiMessage, Balance, OrFalsy } from 'types'; +import { checkInstantiateReversion } from 'helpers/checkInstantiateReversion'; function validateSalt(value: OrFalsy) { if (!!value && value.length === 66) { @@ -60,6 +61,8 @@ export function Step2() { const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>(); const isCustom = refTime.mode === 'custom' || proofSize.mode === 'custom'; + const isReversion = checkInstantiateReversion(dryRunResult); + useEffect(() => { setConstructorIndex(0); metadata && setDeployConstructor(metadata.constructors[0]); @@ -205,7 +208,8 @@ export function Step2() { !storageDepositLimit.isValid || !deployConstructor?.method || !!dryRunResult?.result.isErr || - !dryRunResult + !dryRunResult || + isReversion } onClick={onSubmit} variant="primary" From 309ab541d080276e155bd46d93dadc6ca640ea71 Mon Sep 17 00:00:00 2001 From: Andreea Eftene Date: Wed, 30 Aug 2023 15:50:12 +0300 Subject: [PATCH 2/2] naming is hard --- src/helpers/checkInstantiateReversion.ts | 10 ---------- src/helpers/hasRevertFlag.ts | 8 ++++++++ src/ui/components/instantiate/DryRun.tsx | 8 ++++---- src/ui/components/instantiate/Step2.tsx | 6 +++--- 4 files changed, 15 insertions(+), 17 deletions(-) delete mode 100644 src/helpers/checkInstantiateReversion.ts create mode 100644 src/helpers/hasRevertFlag.ts diff --git a/src/helpers/checkInstantiateReversion.ts b/src/helpers/checkInstantiateReversion.ts deleted file mode 100644 index 2b4cd5e8..00000000 --- a/src/helpers/checkInstantiateReversion.ts +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2022 @paritytech/contracts-ui authors & contributors -// SPDX-License-Identifier: GPL-3.0-only - -import { ContractInstantiateResult } from 'types'; - -export function checkInstantiateReversion(dryRunResult: ContractInstantiateResult | undefined) { - return dryRunResult?.result.isOk - ? dryRunResult.result.asOk.result.flags.toHuman().includes('Revert') - : false; -} diff --git a/src/helpers/hasRevertFlag.ts b/src/helpers/hasRevertFlag.ts new file mode 100644 index 00000000..41d265cd --- /dev/null +++ b/src/helpers/hasRevertFlag.ts @@ -0,0 +1,8 @@ +// Copyright 2022 @paritytech/contracts-ui authors & contributors +// SPDX-License-Identifier: GPL-3.0-only + +import { ContractInstantiateResult } from 'types'; + +export function hasRevertFlag(dryRunResult: ContractInstantiateResult | undefined) { + return dryRunResult?.result.isOk ? dryRunResult.result.asOk.result.flags.isRevert : false; +} diff --git a/src/ui/components/instantiate/DryRun.tsx b/src/ui/components/instantiate/DryRun.tsx index 8f25170a..60eefb9a 100644 --- a/src/ui/components/instantiate/DryRun.tsx +++ b/src/ui/components/instantiate/DryRun.tsx @@ -6,7 +6,7 @@ import { SidePanel } from '../common/SidePanel'; import { Account } from '../account/Account'; import { OutcomeItem } from '../contract/OutcomeItem'; import { useApi, useInstantiate } from 'ui/contexts'; -import { checkInstantiateReversion } from 'helpers/checkInstantiateReversion'; +import { hasRevertFlag } from 'helpers/hasRevertFlag'; export function DryRun() { const { @@ -19,7 +19,7 @@ export function DryRun() { ? api.registry.findMetaError(dryRunResult?.result.asErr.asModule) : null; - const isReversion = checkInstantiateReversion(dryRunResult); + const isReverted = hasRevertFlag(dryRunResult); return ( @@ -99,13 +99,13 @@ export function DryRun() {
- {dryRunResult?.result.isOk && !isReversion && ( + {dryRunResult?.result.isOk && !isReverted && (
The instantiation will be successful.
)} - {isReversion && ( + {isReverted && (
Contract reverted! The instantiation will not be successful. diff --git a/src/ui/components/instantiate/Step2.tsx b/src/ui/components/instantiate/Step2.tsx index cbdf0c73..ddd1ccf9 100644 --- a/src/ui/components/instantiate/Step2.tsx +++ b/src/ui/components/instantiate/Step2.tsx @@ -32,7 +32,7 @@ import { useBalance, } from 'ui/hooks'; import { AbiMessage, Balance, OrFalsy } from 'types'; -import { checkInstantiateReversion } from 'helpers/checkInstantiateReversion'; +import { hasRevertFlag } from 'helpers/hasRevertFlag'; function validateSalt(value: OrFalsy) { if (!!value && value.length === 66) { @@ -61,7 +61,7 @@ export function Step2() { const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>(); const isCustom = refTime.mode === 'custom' || proofSize.mode === 'custom'; - const isReversion = checkInstantiateReversion(dryRunResult); + const isReverted = hasRevertFlag(dryRunResult); useEffect(() => { setConstructorIndex(0); @@ -209,7 +209,7 @@ export function Step2() { !deployConstructor?.method || !!dryRunResult?.result.isErr || !dryRunResult || - isReversion + isReverted } onClick={onSubmit} variant="primary"