# AbiConstructor

Utilities & types for working with [Constructors](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs.

`AbiConstructor` is a sub-type of [`AbiItem`](/api/AbiItem).

## Examples

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

* [Instantiating via JSON ABI](#instantiating-via-json-abi)

* [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item)

* [Encoding to Deploy Data](#encoding-to-deploy-data)

### Instantiating via JSON ABI

An `AbiConstructor` can be instantiated from a JSON ABI by using [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi):

```ts twoslash
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 item = AbiConstructor.fromAbi(abi) // [!code focus]
//    ^?
```

### Instantiating via Human-Readable ABI Item

An `AbiConstructor` can be instantiated from a human-readable ABI by using [`AbiConstructor.from`](/api/AbiConstructor/from):

```ts twoslash
import { AbiConstructor } from 'ox'

const constructor = AbiConstructor.from(
  'constructor(address owner)'
)

constructor
//^?
```

### Encoding to Deploy Data

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

```ts twoslash
import { AbiConstructor } from 'ox'

const constructor = AbiConstructor.from(
  'constructor(address, uint256)'
)

const data = AbiConstructor.encode(constructor, {
  // [!code focus]
  bytecode: '0x...', // [!code focus]
  args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n] // [!code focus]
}) // [!code focus]
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiConstructor.decode`](/api/AbiConstructor/decode) | ABI-decodes the provided constructor input (`inputs`). |
| [`AbiConstructor.encode`](/api/AbiConstructor/encode) | ABI-encodes the provided constructor input (`inputs`). |
| [`AbiConstructor.format`](/api/AbiConstructor/format) | Formats an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) into a **Human Readable ABI Function**. |
| [`AbiConstructor.from`](/api/AbiConstructor/from) | Parses an arbitrary **JSON ABI Constructor** or **Human Readable ABI Constructor** into a typed [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor). |
| [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi) | Extracts an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiConstructor.BytecodeMismatchError`](/api/AbiConstructor/errors#abiconstructorbytecodemismatcherror) | Throws when the provided `data` does not begin with the provided `bytecode`. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructorabiconstructor) | Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with a `constructor` type. |
