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
Name | Description |
---|---|
AbiItem.format | Formats an AbiItem.AbiItem into a Human Readable ABI Item. |
AbiItem.from | Parses an arbitrary JSON ABI Item or Human Readable ABI Item into a typed AbiItem.AbiItem . |
AbiItem.fromAbi | Extracts an AbiItem.AbiItem from an Abi.Abi given a name and optional arguments. |
AbiItem.getSelector | Computes the 4-byte selector for an AbiItem.AbiItem . |
AbiItem.getSignature | Computes the stringified signature for a given AbiItem.AbiItem . |
AbiItem.getSignatureHash | Computes the signature hash for an AbiItem.AbiItem . |
Errors
Name | Description |
---|---|
AbiItem.AmbiguityError | Throws when ambiguous types are found on overloaded ABI items. |
AbiItem.InvalidSelectorSizeError | Throws when the selector size is invalid. |
AbiItem.NotFoundError | Throws when an ABI item is not found in the ABI. |
Types
Name | Description |
---|---|
AbiItem.AbiItem | Root type for an item on an Abi.Abi . |
AbiItem.ExtractNames | |
AbiItem.FromAbi | Extracts an AbiItem.AbiItem item from an Abi.Abi , given a name. |
AbiItem.Name | Extracts the names of all AbiItem.AbiItem items in an Abi.Abi . |