Skip to content

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

NameDescription
AbiParameters.decodeDecodes ABI-encoded data into its respective primitive values based on ABI Parameters.
AbiParameters.encodeEncodes primitive values into ABI encoded data as per the Application Binary Interface (ABI) Specification.
AbiParameters.encodePackedEncodes an array of primitive values to a packed ABI encoding.
AbiParameters.formatFormats AbiParameters.AbiParameters into Human Readable ABI Parameters.
AbiParameters.fromParses arbitrary JSON ABI Parameters or Human Readable ABI Parameters into typed AbiParameters.AbiParameters.

Errors

NameDescription
AbiParameters.ArrayLengthMismatchErrorThe length of the array value does not match the length specified in the corresponding ABI parameter.
AbiParameters.BytesSizeMismatchErrorThe size of the bytes value does not match the size specified in the corresponding ABI parameter.
AbiParameters.DataSizeTooSmallErrorThrows when the data size is too small for the given parameters.
AbiParameters.InvalidArrayErrorThe value provided is not a valid array as specified in the corresponding ABI parameter.
AbiParameters.InvalidTypeErrorThrows when the ABI parameter type is invalid.
AbiParameters.LengthMismatchErrorThe length of the values to encode does not match the length of the ABI parameters.
AbiParameters.ZeroDataErrorThrows when zero data is provided, but data is expected.

Types

NameDescription
AbiParameters.AbiParametersRoot type for ABI parameters.
AbiParameters.PackedAbiTypeA packed ABI type.
AbiParameters.ParameterA parameter on an AbiParameters.AbiParameters.