# AbiError.decode

ABI-decodes the provided error input (`inputs`).

:::tip
This function is typically used to decode contract function reverts (e.g. a JSON-RPC error response).

See the [End-to-end Example](#end-to-end).
:::

## Imports

:::code-group
```ts [Named]
import { AbiError } from 'ox'
```

```ts [Entrypoint]
import * as AbiError from 'ox/AbiError'
```
:::

## Examples

```ts twoslash
import { AbiError } from 'ox'

const error = AbiError.from(
  'error InvalidSignature(uint r, uint s, uint8 yParity)'
)

const value = AbiError.decode(
  error,
  '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'
)
// @log: [420n, 69n, 1]
```

You can extract an ABI Error from a JSON ABI with [`AbiError.fromAbi`](/api/AbiError/fromAbi):

```ts twoslash
// @noErrors
import { Abi, AbiError } from 'ox'

const abi = Abi.from([...]) // [!code hl]
const error = AbiError.fromAbi(abi, 'InvalidSignature') // [!code hl]

const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001')
// @log: [420n, 69n, 1]
```

You can pass the error `data` to the `name` property of [`AbiError.fromAbi`](/api/AbiError/fromAbi) to extract and infer the error by its 4-byte selector:

```ts twoslash
// @noErrors
import { Abi, AbiError } from 'ox'

const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'

const abi = Abi.from([...])
const error = AbiError.fromAbi(abi, data) // [!code hl]

const value = AbiError.decode(error, data)
// @log: [420n, 69n, 1]
```

### ABI-shorthand

You can also specify an entire ABI object as a parameter to [`AbiError.decode`](/api/AbiError/decode):

```ts twoslash
// @noErrors
import { Abi, AbiError } from 'ox'

const abi = Abi.from([...])

const value = AbiError.decode(
  abi, // [!code hl]
  'InvalidSignature', // [!code hl]
  '0x...'
)
// @log: [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](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2).

```ts twoslash
// @noErrors
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) {
  // [!code focus]
  // 4. Extract and decode the Error. // [!code focus]
  const error = AbiError.fromAbi(abi, e.data) // [!code focus]
  const value = AbiError.decode(error, e.data) // [!code focus]
  console.error(`${error.name}(${value})`) // [!code focus]
  // @error:   Error(ERC721: approve caller is not owner nor approved for all)
} // [!code focus]
```

:::note
For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface.
:::

## Definition

```ts
function decode<abi, name, args, as, abiError, allNames>(
  abi: abi | Abi.Abi | readonly unknown[],
  name: Hex.Hex | (name extends allNames ? name : never),
  data: Hex.Hex,
  options?: decode.Options<as>,
): decode.ReturnType<abiError, as>
```

**Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L837)

## Parameters

### abi

* **Type:** `abi | Abi.Abi | readonly unknown[]`

### name

* **Type:** `Hex.Hex | (name extends allNames ? name : never)`

### data

* **Type:** `Hex.Hex`

The error data.

### options

* **Type:** `decode.Options<as>`
* **Optional**

Decoding options.

#### options.as

* **Type:** `as | "Array" | "Object"`
* **Optional**

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

#### options.checksumAddress

* **Type:** `boolean`
* **Optional**

Whether decoded addresses should be checksummed.

## Return Type

The decoded error.

`decode.ReturnType<abiError, as>`
