Skip to content

AbiItem

Utilities & types for working with ABI Items

The AbiItem type is a super-type of:

Examples

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

Instantiating via JSON ABI

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

import { Abi, AbiItem } from 'ox'
 
const abi = Abi.from([
  'function foo()',
  'event Transfer(address owner, address to, uint256 tokenId)',
  'function bar(string a) returns (uint256 x)',
])
 
const 
const item: { readonly name: "Transfer"; readonly type: "event"; readonly inputs: readonly [...]; }
item
= AbiItem.fromAbi(abi, 'Transfer')

Instantiating via Human-Readable ABI Item

A Human Readable ABI can be parsed into a typed ABI object:

import { AbiItem } from 'ox'
 
const abiItem = AbiItem.from('function approve(address spender, uint256 amount) returns (bool)')
 
const abiItem: { 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 [...]; }
abiItem

Formatting ABI Items

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

import { AbiItem } from 'ox'
 
const abiItem = AbiItem.from('function approve(address spender, uint256 amount) returns (bool)')
 
const formatted = AbiItem.format(abiItem)
'function approve(address spender, uint256 amount) returns (bool)'

Functions

NameDescription
AbiItem.formatFormats an AbiItem.AbiItem into a Human Readable ABI Item.
AbiItem.fromParses an arbitrary JSON ABI Item or Human Readable ABI Item into a typed AbiItem.AbiItem.
AbiItem.fromAbiExtracts an AbiItem.AbiItem from an Abi.Abi given a name and optional arguments.
AbiItem.getSelectorComputes the 4-byte selector for an AbiItem.AbiItem.
AbiItem.getSignatureComputes the stringified signature for a given AbiItem.AbiItem.
AbiItem.getSignatureHashComputes the signature hash for an AbiItem.AbiItem.

Errors

NameDescription
AbiItem.AmbiguityErrorThrows when ambiguous types are found on overloaded ABI items.
AbiItem.InvalidSelectorSizeErrorThrows when the selector size is invalid.
AbiItem.NotFoundErrorThrows when an ABI item is not found in the ABI.

Types

NameDescription
AbiItem.AbiItemRoot type for an item on an Abi.Abi.
AbiItem.ExtractNames
AbiItem.FromAbiExtracts an AbiItem.AbiItem item from an Abi.Abi, given a name.
AbiItem.NameExtracts the names of all AbiItem.AbiItem items in an Abi.Abi.