From 670d825aa8105a6fc5e4fedc540d055d28ec91e1 Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Sat, 13 May 2023 12:11:25 +1000 Subject: [PATCH] fix: `call` revert data for node clients that have nested error data. --- .changeset/lovely-yaks-worry.md | 5 +++++ src/actions/public/call.test.ts | 7 +++++++ src/actions/public/call.ts | 3 ++- src/errors/contract.ts | 7 +++++-- src/utils/errors/getContractError.ts | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 .changeset/lovely-yaks-worry.md diff --git a/.changeset/lovely-yaks-worry.md b/.changeset/lovely-yaks-worry.md new file mode 100644 index 0000000000..44c5578c5c --- /dev/null +++ b/.changeset/lovely-yaks-worry.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Fixed \`call\` revert data for node clients that have nested error data. diff --git a/src/actions/public/call.test.ts b/src/actions/public/call.test.ts index 10fbd6cd0f..810a958c30 100644 --- a/src/actions/public/call.test.ts +++ b/src/actions/public/call.test.ts @@ -812,5 +812,12 @@ describe('getRevertErrorData', () => { }), ), ).toBe('0x556f1830') + expect( + getRevertErrorData( + new BaseError('error', { + cause: new RawContractError({ data: { data: '0x556f1830' } }), + }), + ), + ).toBe('0x556f1830') }) }) diff --git a/src/actions/public/call.ts b/src/actions/public/call.ts index 64031ad24a..9aa339f1ba 100644 --- a/src/actions/public/call.ts +++ b/src/actions/public/call.ts @@ -285,5 +285,6 @@ async function scheduleMulticall( export function getRevertErrorData(err: unknown) { if (!(err instanceof BaseError)) return undefined - return (err.walk() as { data?: Hex })?.data + const error = err.walk() as RawContractError + return typeof error.data === 'object' ? error.data.data : error.data } diff --git a/src/errors/contract.ts b/src/errors/contract.ts index 970b4731fb..9a589ab4d0 100644 --- a/src/errors/contract.ts +++ b/src/errors/contract.ts @@ -231,9 +231,12 @@ export class RawContractError extends BaseError { code = 3 override name = 'RawContractError' - data?: Hex + data?: Hex | { data?: Hex } - constructor({ data, message }: { data?: Hex; message?: string }) { + constructor({ + data, + message, + }: { data?: Hex | { data?: Hex }; message?: string }) { super(message || '') this.data = data } diff --git a/src/utils/errors/getContractError.ts b/src/utils/errors/getContractError.ts index 04d634a651..df3e8f36f5 100644 --- a/src/utils/errors/getContractError.ts +++ b/src/utils/errors/getContractError.ts @@ -46,7 +46,7 @@ export function getContractError( ) { cause = new ContractFunctionRevertedError({ abi, - data, + data: typeof data === 'object' ? data.data : data, functionName, message: shortMessage ?? message, })