Commit 43370774 authored by tom's avatar tom

custom error message

parent f0302aed
...@@ -81,7 +81,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit, ...@@ -81,7 +81,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit,
setLoading(false); setLoading(false);
}) })
.catch((error) => { .catch((error) => {
setResult(error?.error || error?.data || error); setResult(error?.error || error?.data || (error?.reason && { message: error.reason }) || error);
setLoading(false); setLoading(false);
}); });
}, [ onSubmit, data, inputs ]); }, [ onSubmit, data, inputs ]);
......
import { Alert } from '@chakra-ui/react'; import { Alert } from '@chakra-ui/react';
import _capitalize from 'lodash/capitalize';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import React from 'react'; import React from 'react';
import { useSigner } from 'wagmi'; import { useSigner } from 'wagmi';
...@@ -6,6 +7,7 @@ import { useSigner } from 'wagmi'; ...@@ -6,6 +7,7 @@ import { useSigner } from 'wagmi';
import type { ContractMethodWriteResult } from './types'; import type { ContractMethodWriteResult } from './types';
import type { SmartContractWriteMethod } from 'types/api/contract'; import type { SmartContractWriteMethod } from 'types/api/contract';
import config from 'configs/app/config';
import useApiQuery from 'lib/api/useApiQuery'; import useApiQuery from 'lib/api/useApiQuery';
import ContractMethodsAccordion from 'ui/address/contract/ContractMethodsAccordion'; import ContractMethodsAccordion from 'ui/address/contract/ContractMethodsAccordion';
import ContentLoader from 'ui/shared/ContentLoader'; import ContentLoader from 'ui/shared/ContentLoader';
...@@ -37,6 +39,7 @@ const ContractWrite = ({ isProxy }: Props) => { ...@@ -37,6 +39,7 @@ const ContractWrite = ({ isProxy }: Props) => {
const contract = useContractContext(); const contract = useContractContext();
const handleMethodFormSubmit = React.useCallback(async(item: SmartContractWriteMethod, args: Array<string | Array<string>>) => { const handleMethodFormSubmit = React.useCallback(async(item: SmartContractWriteMethod, args: Array<string | Array<string>>) => {
try {
if (!contract) { if (!contract) {
return; return;
} }
...@@ -60,6 +63,25 @@ const ContractWrite = ({ isProxy }: Props) => { ...@@ -60,6 +63,25 @@ const ContractWrite = ({ isProxy }: Props) => {
}); });
return { hash: result.hash as string }; return { hash: result.hash as string };
} catch (error) {
if (error instanceof Error) {
if ('reason' in error && error.reason === 'underlying network changed') {
if ('detectedNetwork' in error) {
const networkName = (error.detectedNetwork as { name: string }).name;
if (networkName) {
throw new Error(
`You connected to ${ _capitalize(networkName) } chain in the wallet,
but the current instance of Blockscout is for ${ config.network.name } chain`,
);
}
}
throw new Error('Wrong network detected, please make sure you are switched to the correct network, and try again');
}
}
throw error;
}
}, [ addressHash, contract, signer ]); }, [ addressHash, contract, signer ]);
const renderResult = React.useCallback((item: SmartContractWriteMethod, result: ContractMethodWriteResult) => { const renderResult = React.useCallback((item: SmartContractWriteMethod, result: ContractMethodWriteResult) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment