Skip to content

AbiItem.from

Parses an arbitrary JSON ABI Item or Human Readable ABI Item into a typed AbiItem.AbiItem.

Imports

Named
import { AbiItem } from 'ox'

Examples

JSON ABIs

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

Human Readable ABIs

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

It is possible to specify structs along with your definitions:

import { AbiItem } from 'ox'
 
const abiItem = AbiItem.from([
  'struct Foo { address spender; uint256 amount; }',
  'function approve(Foo foo) returns (bool)',
])
 
const abiItem: { readonly name: "approve"; readonly type: "function"; readonly stateMutability: "nonpayable"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "address"; readonly name: "spender"; }, { ...; }]; readonly name: "foo"; }]; readonly outputs: readonly [...]; }
abiItem

Definition

function from<abiItem>(
  abiItem: (abiItem | AbiItem | string | readonly string[]) & ((abiItem extends string ? internal.Signature<abiItem> : never) | (abiItem extends readonly string[] ? internal.Signatures<abiItem> : never) | AbiItem),
  options?: from.Options,
): from.ReturnType<abiItem>

Source: src/core/AbiItem.ts

Parameters

abiItem

  • Type: (abiItem | AbiItem | string | readonly string[]) & ((abiItem extends string ? internal.Signature<abiItem> : never) | (abiItem extends readonly string[] ? internal.Signatures<abiItem> : never) | AbiItem)

The ABI Item to parse.

options

  • Type: from.Options
  • Optional

options.prepare

  • Type: boolean
  • Optional

Whether or not to prepare the extracted item (optimization for encoding performance). When true, the hash property is computed and included in the returned value.

Return Type

The typed ABI Item.

from.ReturnType<abiItem>