Skip to content
On this page

decodeErrorResult

Decodes reverted error from a contract function call.

Install

ts
import { decodeErrorResult } from 'viem/contract'

Usage

ts
import { decodeErrorResult } from 'viem/contract'

const value = decodeErrorResult({
  abi: wagmiAbi,
  data: '0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000'
})
// { errorName: 'InvalidTokenError', args: ['sold out'] }
ts
export const wagmiAbi = [
  ...
  {
		inputs: [
			{
				name: "reason",
				type: "string"
			}
		],
		name: "InvalidTokenError",
		type: "error"
	},
  ...
] as const;
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})

Return Value

The decoded error. Type is inferred from the ABI.

Parameters

abi

The contract's ABI.

ts
const value = decodeErrorResult({
  abi: wagmiAbi, 
  data: '0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000'
})

data

  • Type: Hex

The calldata.

ts
const value = decodeErrorResult({
  abi: wagmiAbi,
  data: '0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000' 
})

Released under the MIT License.