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
Name | Description |
---|---|
AbiFunction.decodeData | ABI-decodes function arguments according to the ABI Item's input types (inputs ). |
AbiFunction.decodeResult | ABI-decodes a function's result according to the ABI Item's output types (outputs ). |
AbiFunction.encodeData | ABI-encodes function arguments (inputs ), prefixed with the 4 byte function selector. |
AbiFunction.encodeResult | ABI-encodes a function's result (outputs ). |
AbiFunction.format | Formats an AbiFunction.AbiFunction into a Human Readable ABI Function. |
AbiFunction.from | Parses an arbitrary JSON ABI Function or Human Readable ABI Function into a typed AbiFunction.AbiFunction . |
AbiFunction.fromAbi | Extracts an AbiFunction.AbiFunction from an Abi.Abi given a name and optional arguments. |
AbiFunction.getSelector | Computes the 4-byte selector for an AbiFunction.AbiFunction . |
Types
Name | Description |
---|---|
AbiFunction.AbiFunction | Root type for an AbiItem.AbiItem with a function type. |
AbiFunction.ExtractNames | |
AbiFunction.FromAbi | Extracts an AbiFunction.AbiFunction item from an Abi.Abi , given a name. |
AbiFunction.Name | Extracts the names of all AbiFunction.AbiFunction items in an Abi.Abi . |