Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix displaying of custom solidity errors #638

Merged
merged 10 commits into from
Dec 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const ContractReadMethods = ({ deployedContractData }: { deployedContract
<>
{functionsToDisplay.map(({ fn, inheritedFrom }) => (
<ReadOnlyFunctionForm
abi={deployedContractData.abi as Abi}
contractAddress={deployedContractData.address}
abiFunction={fn}
key={fn.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const ContractVariables = ({
<>
{functionsToDisplay.map(({ fn, inheritedFrom }) => (
<DisplayVariable
abi={deployedContractData.abi as Abi}
abiFunction={fn}
contractAddress={deployedContractData.address}
key={fn.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const ContractWriteMethods = ({
<>
{functionsToDisplay.map(({ fn, inheritedFrom }, idx) => (
<WriteOnlyFunctionForm
abi={deployedContractData.abi as Abi}
key={`${fn.name}-${idx}}`}
abiFunction={fn}
onChange={onChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ type DisplayVariableProps = {
abiFunction: AbiFunction;
refreshDisplayVariables: boolean;
inheritedFrom?: string;
abi: Abi;
};

export const DisplayVariable = ({
contractAddress,
abiFunction,
refreshDisplayVariables,
abi,
inheritedFrom,
}: DisplayVariableProps) => {
const {
Expand All @@ -28,7 +30,7 @@ export const DisplayVariable = ({
} = useContractRead({
address: contractAddress,
functionName: abiFunction.name,
abi: [abiFunction] as Abi,
abi: abi,
onError: error => {
notification.error(error.message);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ import {
getFunctionInputKey,
getInitialFormState,
getParsedContractFunctionArgs,
getParsedError,
} from "~~/components/scaffold-eth";
import { notification } from "~~/utils/scaffold-eth";

type ReadOnlyFunctionFormProps = {
contractAddress: Address;
abiFunction: AbiFunction;
inheritedFrom?: string;
abi: Abi;
};

export const ReadOnlyFunctionForm = ({ contractAddress, abiFunction, inheritedFrom }: ReadOnlyFunctionFormProps) => {
export const ReadOnlyFunctionForm = ({
contractAddress,
abiFunction,
inheritedFrom,
abi,
}: ReadOnlyFunctionFormProps) => {
const [form, setForm] = useState<Record<string, any>>(() => getInitialFormState(abiFunction));
const [result, setResult] = useState<unknown>();

const { isFetching, refetch } = useContractRead({
address: contractAddress,
functionName: abiFunction.name,
abi: [abiFunction] as Abi,
abi: abi,
args: getParsedContractFunctionArgs(form),
enabled: false,
onError: (error: any) => {
notification.error(error.message);
const parsedErrror = getParsedError(error);
notification.error(parsedErrror);
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import { useTargetNetwork } from "~~/hooks/scaffold-eth/useTargetNetwork";
import { notification } from "~~/utils/scaffold-eth";

type WriteOnlyFunctionFormProps = {
abi: Abi;
abiFunction: AbiFunction;
onChange: () => void;
contractAddress: Address;
inheritedFrom?: string;
};

export const WriteOnlyFunctionForm = ({
abi,
abiFunction,
onChange,
contractAddress,
Expand All @@ -43,7 +45,7 @@ export const WriteOnlyFunctionForm = ({
} = useContractWrite({
address: contractAddress,
functionName: abiFunction.name,
abi: [abiFunction] as Abi,
abi: abi,
args: getParsedContractFunctionArgs(form),
});

Expand Down
15 changes: 11 additions & 4 deletions packages/nextjs/components/scaffold-eth/Contract/utilsContract.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbiFunction, AbiParameter } from "abitype";
import { BaseError as BaseViemError } from "viem";
import { BaseError as BaseViemError, DecodeErrorResultReturnType } from "viem";

/**
* Generates a key based on function metadata
Expand All @@ -18,14 +18,21 @@ const getFunctionInputKey = (functionName: string, input: AbiParameter, inputInd
* @param e - error object
* @returns {string} parsed error string
*/
const getParsedError = (e: any | BaseViemError): string => {
let message = e.message ?? "An unknown error occurred";

const getParsedError = (e: any): string => {
let message: string = e.message ?? "An unknown error occurred";
if (e instanceof BaseViemError) {
if (e.details) {
message = e.details;
} else if (e.shortMessage) {
message = e.shortMessage;
const cause = e.cause as any as { data?: DecodeErrorResultReturnType };
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
// if its not generic error, append custom error name and its args to message
if (cause.data && cause.data?.abiItem?.name !== "Error") {
const customErrorArgs = cause.data.args?.toString() ?? "";
message = `${message.replace(/reverted\.$/, "reverted with following reason:")}\n${
carletex marked this conversation as resolved.
Show resolved Hide resolved
cause.data.errorName
}(${customErrorArgs})`;
}
} else if (e.message) {
message = e.message;
} else if (e.name) {
Expand Down