# AbiItem

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

The `AbiItem` type is a super-type of:

* [`AbiConstructor`](/api/AbiConstructor)
* [`AbiFunction`](/api/AbiFunction)
* [`AbiEvent`](/api/AbiEvent)
* [`AbiError`](/api/AbiError)

## Examples

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

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

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

* [Formatting ABI Items](#formatting-abi-items)

### Instantiating via JSON ABI

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

```ts twoslash
import { Abi, AbiItem } from 'ox'

const abi = Abi.from([
  'function foo()',
  'event Transfer(address owner, address to, uint256 tokenId)',
  'function bar(string a) returns (uint256 x)'
])

const item = AbiItem.fromAbi(abi, 'Transfer') // [!code focus]
//    ^?
```

### Instantiating via Human-Readable ABI Item

A Human Readable ABI can be parsed into a typed ABI object:

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

const abiItem = AbiItem.from(
  'function approve(address spender, uint256 amount) returns (bool)'
)

abiItem
//^?
```

### Formatting ABI Items

An `AbiItem` can be formatted into a human-readable ABI Item by using [`AbiItem.format`](/api/AbiItem/format):

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

const abiItem = AbiItem.from(
  'function approve(address spender, uint256 amount) returns (bool)'
)

const formatted = AbiItem.format(abiItem)
// @log: 'function approve(address spender, uint256 amount) returns (bool)'
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiItem.format`](/api/AbiItem/format) | Formats an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) into a **Human Readable ABI Item**. |
| [`AbiItem.from`](/api/AbiItem/from) | Parses an arbitrary **JSON ABI Item** or **Human Readable ABI Item** into a typed [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). |
| [`AbiItem.fromAbi`](/api/AbiItem/fromAbi) | Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. |
| [`AbiItem.getSelector`](/api/AbiItem/getSelector) | Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). |
| [`AbiItem.getSignature`](/api/AbiItem/getSignature) | Computes the stringified signature for a given [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). |
| [`AbiItem.getSignatureHash`](/api/AbiItem/getSignatureHash) | Computes the signature hash for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiItem.AmbiguityError`](/api/AbiItem/errors#abiitemambiguityerror) | Throws when ambiguous types are found on overloaded ABI items. |
| [`AbiItem.InvalidAbiItemError`](/api/AbiItem/errors#abiiteminvalidabiitemerror) |  |
| [`AbiItem.InvalidSelectorSizeError`](/api/AbiItem/errors#abiiteminvalidselectorsizeerror) | Throws when the selector size is invalid. |
| [`AbiItem.NotFoundError`](/api/AbiItem/errors#abiitemnotfounderror) | Throws when an ABI item is not found in the ABI. |
| [`AbiItem.UnknownSolidityTypeError`](/api/AbiItem/errors#abiitemunknownsoliditytypeerror) |  |
| [`AbiItem.UnknownTypeError`](/api/AbiItem/errors#abiitemunknowntypeerror) |  |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiItem.AbiItem`](/api/AbiItem/types#abiitemabiitem) | Root type for an item on an [`Abi.Abi`](/api/Abi/types#abi). |
| [`AbiItem.ExtractNames`](/api/AbiItem/types#abiitemextractnames) |  |
| [`AbiItem.FromAbi`](/api/AbiItem/types#abiitemfromabi) | Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. |
| [`AbiItem.Name`](/api/AbiItem/types#abiitemname) | Extracts the names of all [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) items in an [`Abi.Abi`](/api/Abi/types#abi). |
