Skip to content

AbiError.decode

ABI-decodes the provided error input (inputs).

Imports

Named
import { AbiError } from 'ox'

Examples

import { AbiError } from 'ox'
 
const error = AbiError.from('error InvalidSignature(uint r, uint s, uint8 yParity)')
 
const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001')
[420n, 69n, 1]

You can extract an ABI Error from a JSON ABI with AbiError.fromAbi:

import { Abi, AbiError } from 'ox'
 
const abi = Abi.from([...])
const error = AbiError.fromAbi(abi, 'InvalidSignature')
 
const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001')
[420n, 69n, 1]

You can pass the error data to the name property of AbiError.fromAbi to extract and infer the error by its 4-byte selector:

import { Abi, AbiError } from 'ox'
 
const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'
 
const abi = Abi.from([...])
const error = AbiError.fromAbi(abi, data)
 
const value = AbiError.decode(error, data)
[420n, 69n, 1]

End-to-end

Below is an end-to-end example of using AbiError.decode to decode the revert error of an approve contract call on the Wagmi Mint Example contract.

import 'ox/window'
import { Abi, AbiError, AbiFunction } from 'ox'
 
// 1. Extract the Function from the Contract's ABI.
const abi = Abi.from([
  // ...
  {
    inputs: [
      { name: 'to', type: 'address' },
      { name: 'tokenId', type: 'uint256' },
    ],
    name: 'approve',
    outputs: [],
    stateMutability: 'nonpayable',
    type: 'function',
  },
  // ...
])
const approve = AbiFunction.fromAbi(abi, 'approve')
 
// 2. Encode the Function Input.
const data = AbiFunction.encodeData(
  approve,
  ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
)
 
try {
  // 3. Attempt to perform the the Contract Call.
  await window.ethereum!.request({
    method: 'eth_call',
    params: [
      {
        data,
        to: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
      },
    ],
  })
} catch (e) {
  // 4. Extract and decode the Error.
  const error = AbiError.fromAbi(abi, e.data)
  const value = AbiError.decode(error, e.data)
  console.error(`${error.name}(${value})`)
Error(ERC721: approve caller is not owner nor approved for all)

Definition

function decode(
  abiError: AbiError.AbiError,
  data: Hex.Hex,
  options?: decode.Options | undefined,
): unknown | readonly unknown[] | undefined

Source: src/core/AbiError.ts

Parameters

abiError

The ABI Error to decode.

data

  • Type: Hex.Hex

The error data.

options

  • Type: decode.Options | undefined
  • Optional

Decoding options.

options.as

  • Type: as | "Array" | "Object"
  • Optional

Whether the decoded values should be returned as an Object or Array.

Return Type

The decoded error.

unknown | readonly unknown[] | undefined