Skip to content

AbiConstructor

Utilities & types for working with Constructors on ABIs.

AbiConstructor is a sub-type of AbiItem.

Examples

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

Instantiating via JSON ABI

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

import { Abi, AbiConstructor } from 'ox'
 
const abi = Abi.from([
  'constructor(address owner)',
  'function foo()',
  'event Transfer(address owner, address to, uint256 tokenId)',
  'function bar(string a) returns (uint256 x)',
])
 
const 
const item: { readonly type: "constructor"; readonly stateMutability: "nonpayable"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; }
item
= AbiConstructor.fromAbi(abi)

Instantiating via Human-Readable ABI Item

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

import { AbiConstructor } from 'ox'
 
const constructor = AbiConstructor.from('constructor(address owner)')
 
const constructor: { readonly type: "constructor"; readonly stateMutability: "nonpayable"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; }
constructor

Encoding to Deploy Data

Constructor arguments can be ABI-encoded using AbiConstructor.encode (with bytecode) into deploy data. This data can then be passed to a transaction to deploy a contract.

import { AbiConstructor } from 'ox'
 
const constructor = AbiConstructor.from('constructor(address, uint256)')
 
const data = AbiConstructor.encode(constructor, { 
  bytecode: '0x...', 
  args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n], 
})

Functions

NameDescription
AbiConstructor.decodeABI-decodes the provided constructor input (inputs).
AbiConstructor.encodeABI-encodes the provided constructor input (inputs).
AbiConstructor.formatFormats an AbiConstructor.AbiConstructor into a Human Readable ABI Function.
AbiConstructor.fromParses an arbitrary JSON ABI Constructor or Human Readable ABI Constructor into a typed AbiConstructor.AbiConstructor.
AbiConstructor.fromAbiExtracts an AbiConstructor.AbiConstructor from an Abi.Abi given a name and optional arguments.

Types

NameDescription
AbiConstructor.AbiConstructorRoot type for an AbiItem.AbiItem with a constructor type.