AbiParameters
Utilities & types for encoding, decoding, and working with ABI Parameters
Examples
Below are some examples demonstrating common usages of the AbiParameters
module:
Encoding ABI Parameters
ABI Parameters can be ABI-encoded as per the Application Binary Interface (ABI) Specification using AbiParameters.encode
:
import { AbiParameters } from 'ox'
const data = AbiParameters.encode(
AbiParameters.from('string, uint, bool'),
['wagmi', 420n, true],
)
Decoding ABI Parameters
ABI-encoded data can be decoded using AbiParameters.decode
:
import { AbiParameters } from 'ox'
const data = AbiParameters.decode(
AbiParameters.from('string, uint, bool'),
'0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000057761676d69000000000000000000000000000000000000000000000000000000',
)
['wagmi', 420n, true]
JSON-ABI Parameters
JSON-ABI Parameters can be instantiated using AbiParameters.from
:
import { AbiParameters } from 'ox'
const parameters = AbiParameters.from([
{
name: 'spender',
type: 'address',
},
{
name: 'amount',
type: 'uint256',
},
])
const parameters: readonly [{
readonly name: "spender";
readonly type: "address";
}, {
readonly name: "amount";
readonly type: "uint256";
}]parameters
Human Readable ABI Parameters
Human Readable ABI Parameters can be instantiated using AbiParameters.from
:
import { AbiParameters } from 'ox'
const parameters = AbiParameters.from('address spender, uint256 amount')
const parameters: readonly [{
readonly type: "address";
readonly name: "spender";
}, {
readonly type: "uint256";
readonly name: "amount";
}]parameters
Functions
Name | Description |
---|---|
AbiParameters.decode | Decodes ABI-encoded data into its respective primitive values based on ABI Parameters. |
AbiParameters.encode | Encodes primitive values into ABI encoded data as per the Application Binary Interface (ABI) Specification. |
AbiParameters.encodePacked | Encodes an array of primitive values to a packed ABI encoding. |
AbiParameters.format | Formats AbiParameters.AbiParameters into Human Readable ABI Parameters. |
AbiParameters.from | Parses arbitrary JSON ABI Parameters or Human Readable ABI Parameters into typed AbiParameters.AbiParameters . |
Errors
Name | Description |
---|---|
AbiParameters.ArrayLengthMismatchError | The length of the array value does not match the length specified in the corresponding ABI parameter. |
AbiParameters.BytesSizeMismatchError | The size of the bytes value does not match the size specified in the corresponding ABI parameter. |
AbiParameters.DataSizeTooSmallError | Throws when the data size is too small for the given parameters. |
AbiParameters.InvalidArrayError | The value provided is not a valid array as specified in the corresponding ABI parameter. |
AbiParameters.InvalidTypeError | Throws when the ABI parameter type is invalid. |
AbiParameters.LengthMismatchError | The length of the values to encode does not match the length of the ABI parameters. |
AbiParameters.ZeroDataError | Throws when zero data is provided, but data is expected. |
Types
Name | Description |
---|---|
AbiParameters.AbiParameters | Root type for ABI parameters. |
AbiParameters.PackedAbiType | A packed ABI type. |
AbiParameters.Parameter | A parameter on an AbiParameters.AbiParameters . |