Skip to content

AbiFunction

Utilities & types for working with Functions on ABIs.

AbiFunction is a sub-type of AbiItem.

Examples

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

Instantiating via JSON ABI

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

import { Abi, AbiFunction } 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: { ...; }
item
= AbiFunction.fromAbi(abi, 'bar')

Instantiating via Human-Readable ABI Item

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

import { AbiFunction } from 'ox'
 
const bar = AbiFunction.from('function bar(string a) returns (uint256 x)')
 
const bar: { readonly name: "bar"; readonly type: "function"; readonly stateMutability: "nonpayable"; readonly inputs: readonly [{ readonly type: "string"; readonly name: "a"; }]; readonly outputs: readonly [{ readonly type: "uint256"; readonly name: "x"; }]; }
bar

Encoding to Function Data

A Function and its arguments can be ABI-encoded into data using the AbiFunction.encodeData function. The output of this function can then be passed to eth_sendTransaction or eth_call as the data parameter.

import { AbiFunction } from 'ox'
 
const approve = AbiFunction.from('function approve(address, uint256)')
 
const data = AbiFunction.encodeData(
  approve,
  ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
)
'0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c'

Decoding a Function's Result

A Function's result can be ABI-decoded using the AbiFunction.decodeResult function.

import { AbiFunction } from 'ox'
 
const data = '0x000000000000000000000000000000000000000000000000000000000000002a'
//    ↑ Example data that could be returned from a contract call via `eth_call`.
 
const totalSupply = AbiFunction.from('function totalSupply() returns (uint256)')
 
const output = AbiFunction.decodeResult(totalSupply, data)
42n

Functions

NameDescription
AbiFunction.decodeDataABI-decodes function arguments according to the ABI Item's input types (inputs).
AbiFunction.decodeResultABI-decodes a function's result according to the ABI Item's output types (outputs).
AbiFunction.encodeDataABI-encodes function arguments (inputs), prefixed with the 4 byte function selector.
AbiFunction.encodeResultABI-encodes a function's result (outputs).
AbiFunction.formatFormats an AbiFunction.AbiFunction into a Human Readable ABI Function.
AbiFunction.fromParses an arbitrary JSON ABI Function or Human Readable ABI Function into a typed AbiFunction.AbiFunction.
AbiFunction.fromAbiExtracts an AbiFunction.AbiFunction from an Abi.Abi given a name and optional arguments.
AbiFunction.getSelectorComputes the 4-byte selector for an AbiFunction.AbiFunction.

Errors

NameDescription
AbiFunction.SelectorMismatchErrorThrown when the selector of the provided data does not match the selector of the function, or of one of its overloads.

Types

NameDescription
AbiFunction.AbiFunctionRoot type for an AbiItem.AbiItem with a function type.
AbiFunction.ExtractNames
AbiFunction.FromAbiExtracts an AbiFunction.AbiFunction item from an Abi.Abi, given a name.
AbiFunction.NameExtracts the names of all AbiFunction.AbiFunction items in an Abi.Abi.