Skip to content

AbiConstructor.encode

ABI-encodes the provided constructor input (inputs).

Imports

Named
import { AbiConstructor } from 'ox'

Examples

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

End-to-end

Below is an end-to-end example of using AbiConstructor.encode to encode the constructor of a contract and deploy it.

import 'ox/window'
import { AbiConstructor, Hex } from 'ox'
 
// 1. Instantiate the ABI Constructor.
const constructor = AbiConstructor.from(
  'constructor(address owner, uint256 amount)',
)
 
// 2. Encode the ABI Constructor.
const data = AbiConstructor.encode(constructor, {
  bytecode: '0x...',
  args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n],
})
 
// 3. Deploy the contract.
const hash = await window.ethereum!.request({
  method: 'eth_sendTransaction',
  params: [{ data }],
})

Definition

function encode<abiConstructor>(
  abiConstructor: abiConstructor,
  options: encode.Options<abiConstructor>,
): encode.ReturnType

Source: src/core/AbiConstructor.ts

Parameters

abiConstructor

  • Type: abiConstructor

The ABI Constructor to encode.

options

  • Type: encode.Options<abiConstructor>

Encoding options.

options.args

  • Type: args

The constructor arguments to encode.

options.bytecode

  • Type: 0x${string}

The bytecode of the contract.

Return Type

The encoded constructor.

encode.ReturnType