Skip to content

Commit

Permalink
fix: call revert data for node clients that have nested error data.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed May 13, 2023
1 parent d130c72 commit 670d825
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-yaks-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed \`call\` revert data for node clients that have nested error data.
7 changes: 7 additions & 0 deletions src/actions/public/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,5 +812,12 @@ describe('getRevertErrorData', () => {
}),
),
).toBe('0x556f1830')
expect(
getRevertErrorData(
new BaseError('error', {
cause: new RawContractError({ data: { data: '0x556f1830' } }),
}),
),
).toBe('0x556f1830')
})
})
3 changes: 2 additions & 1 deletion src/actions/public/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,6 @@ async function scheduleMulticall<TChain extends Chain | undefined>(

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
}
7 changes: 5 additions & 2 deletions src/errors/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/errors/getContractError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getContractError(
) {
cause = new ContractFunctionRevertedError({
abi,
data,
data: typeof data === 'object' ? data.data : data,
functionName,
message: shortMessage ?? message,
})
Expand Down

1 comment on commit 670d825

@vercel
Copy link

@vercel vercel bot commented on 670d825 May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.