# Abi

Utilities & types for working with [Application Binary Interfaces (ABIs)](https://docs.soliditylang.org/en/latest/abi-spec.html)

:::note
If you are looking for ABI parameter **encoding** & **decoding** functions, see [`AbiParameters.encode`](/api/AbiParameters/encode) & [`AbiParameters.decode`](/api/AbiParameters/decode).
:::

## Examples

Below are some examples demonstrating common usages of the `Abi` module:

* [Instantiating JSON ABIs](#instantiating-json-abis)

* [Instantiating Human Readable ABIs](#instantiating-human-readable-abis)

* [Formatting ABIs](#formatting-abis)

### Instantiating JSON ABIs

An [`Abi.Abi`](/api/Abi/types#abi) can be instantiated from a JSON ABI by using [`Abi.from`](/api/Abi/from):

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

const abi = Abi.from([
  {
    type: 'function',
    name: 'approve',
    stateMutability: 'nonpayable',
    inputs: [
      {
        name: 'spender',
        type: 'address'
      },
      {
        name: 'amount',
        type: 'uint256'
      }
    ],
    outputs: [{ type: 'bool' }]
  }
])

abi
//^?
```

### Instantiating Human Readable ABIs

An [`Abi.Abi`](/api/Abi/types#abi) can be instantiated from a human-readable ABI by using [`Abi.from`](/api/Abi/from):

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

const abi = Abi.from([
  'function approve(address spender, uint256 amount) returns (bool)'
])

abi
//^?
```

### Formatting ABIs

An [`Abi.Abi`](/api/Abi/types#abi) can be formatted into a human-readable ABI by using [`Abi.format`](/api/Abi/format):

```ts twoslash
import { Abi } from 'ox'
const abi = Abi.from([
  {
    type: 'function',
    name: 'approve',
    stateMutability: 'nonpayable',
    inputs: [
      {
        name: 'spender',
        type: 'address'
      },
      {
        name: 'amount',
        type: 'uint256'
      }
    ],
    outputs: [{ type: 'bool' }]
  }
])
//---cut---
const formatted = Abi.format(abi)

formatted
//    ^?
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Abi.format`](/api/Abi/format) | Formats an [`Abi.Abi`](/api/Abi/types#abi) into a **Human Readable ABI**. |
| [`Abi.from`](/api/Abi/from) | Parses an arbitrary **JSON ABI** or **Human Readable ABI** into a typed [`Abi.Abi`](/api/Abi/types#abi). |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Abi.CircularReferenceError`](/api/Abi/errors#abicircularreferenceerror) |  |
| [`Abi.InvalidSignatureError`](/api/Abi/errors#abiinvalidsignatureerror) |  |
| [`Abi.InvalidStructSignatureError`](/api/Abi/errors#abiinvalidstructsignatureerror) |  |
| [`Abi.UnknownSignatureError`](/api/Abi/errors#abiunknownsignatureerror) |  |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Abi.Abi`](/api/Abi/types#abiabi) | Root type for an ABI. |
