Skip to content

Abi

Utilities & types for working with Application Binary Interfaces (ABIs)

Examples

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

Instantiating JSON ABIs

An Abi.Abi can be instantiated from a JSON ABI by using Abi.from:

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' }],
}])
 
const abi: readonly [{ readonly type: "function"; readonly name: "approve"; readonly stateMutability: "nonpayable"; readonly inputs: readonly [{ readonly name: "spender"; readonly type: "address"; }, { readonly name: "amount"; readonly type: "uint256"; }]; readonly outputs: readonly [...]; }]
abi

Instantiating Human Readable ABIs

An Abi.Abi can be instantiated from a human-readable ABI by using Abi.from:

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

Formatting ABIs

An Abi.Abi can be formatted into a human-readable ABI by using Abi.format:

const formatted = Abi.format(abi)
 
const formatted: readonly ["function approve(address spender, uint256 amount) returns (bool)"]
formatted

Functions

NameDescription
Abi.formatFormats an Abi.Abi into a Human Readable ABI.
Abi.fromParses an arbitrary JSON ABI or Human Readable ABI into a typed Abi.Abi.

Types

NameDescription
Abi.AbiRoot type for an ABI.