> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://viem-k66tqerhl-wevm.vercel.app/api/mcp` to find what you need.

# AbiEvent

:::info
`AbiEvent` is a re-export of ox's [`AbiEvent`](https://oxlib.sh/api/AbiEvent). Refer to the ox documentation for the full reference.
:::

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

`AbiEvent` is a sub-type of [`AbiItem`](/docs/utilities/abiitem).

## Examples

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

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

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

* [Encoding to Event Topics](#encoding-to-event-topics)

* [Decoding Event Topics and Data](#decoding-event-topics-and-data)

### Instantiating via JSON ABI

An `AbiEvent` can be instantiated from a JSON ABI by using [`AbiEvent.fromAbi`](/docs/utilities/abievent/fromAbi):

```ts twoslash
// @noErrors
import { Abi, AbiEvent } from 'viem/utils'

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

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

### Instantiating via Human-Readable ABI Item

An `AbiEvent` can be instantiated from a human-readable ABI by using [`AbiEvent.from`](/docs/utilities/abievent/from):

```ts twoslash
// @noErrors
import { AbiEvent } from 'viem/utils'

const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)' // [!code hl]
)

transfer
//^?
```

### Encoding to Event Topics

Encode an `AbiEvent` into topics using [`AbiEvent.encode`](/docs/utilities/abievent/encode):

```ts twoslash
// @noErrors
import { AbiEvent } from 'viem/utils'

const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)

const { topics } = AbiEvent.encode(transfer, {
  from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code hl]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8' // [!code hl]
})
// @log: [
// @log:   '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0',
// @log:   '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
// @log:   '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
// @log: ]
```

### Decoding Event Topics and Data

Event topics and data can be decoded using [`AbiEvent.decode`](/docs/utilities/abievent/decode):

```ts twoslash
// @noErrors
import { AbiEvent } from 'viem/utils'

const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)

const log = {
  // ...
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac'
  ]
} as const

const decoded = AbiEvent.decode(transfer, log)
// @log: {
// @log:   from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
// @log:   to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
// @log:   value: 1n
// @log: }
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiEvent.assertArgs`](/docs/utilities/abievent/assertArgs) | Asserts that the provided arguments match the decoded log arguments. |
| [`AbiEvent.decode`](/docs/utilities/abievent/decode) | ABI-Decodes the provided [Log Topics and Data](https://info.etherscan.com/what-is-event-logs/) according to the ABI Event's parameter types (`input`). |
| [`AbiEvent.decodeLog`](/docs/utilities/abievent/decodeLog) | Extracts an [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent) from an [`Abi.Abi`](/docs/utilities/abi/types#abi) and decodes its arguments from a Log. |
| [`AbiEvent.encode`](/docs/utilities/abievent/encode) | ABI-encodes the provided event input (`inputs`) into an array of [Event Topics](https://info.etherscan.com/what-is-event-logs/). |
| [`AbiEvent.extractLogs`](/docs/utilities/abievent/extractLogs) | Extracts and decodes Logs that match an ABI Event in an ABI. |
| [`AbiEvent.format`](/docs/utilities/abievent/format) | Formats an [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent) into a **Human Readable ABI Error**. |
| [`AbiEvent.from`](/docs/utilities/abievent/from) | Parses an arbitrary **JSON ABI Event** or **Human Readable ABI Event** into a typed [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent). |
| [`AbiEvent.fromAbi`](/docs/utilities/abievent/fromAbi) | Extracts an [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent) from an [`Abi.Abi`](/docs/utilities/abi/types#abi) given a name and optional arguments. |
| [`AbiEvent.getSelector`](/docs/utilities/abievent/getSelector) | Computes the event selector (hash of event signature) for an [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent). |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiEvent.AnonymousEventAmbiguityError`](/docs/utilities/abievent/errors#abieventanonymouseventambiguityerror) | Thrown when a log fits more than one anonymous event in an ABI. An anonymous log has no selector topic, so anonymous events with the same number of indexed inputs cannot be told apart. |
| [`AbiEvent.ArgsMismatchError`](/docs/utilities/abievent/errors#abieventargsmismatcherror) | Thrown when the provided arguments do not match the expected arguments. |
| [`AbiEvent.DataMismatchError`](/docs/utilities/abievent/errors#abieventdatamismatcherror) | Thrown when the provided data size does not match the expected size from the non-indexed parameters. |
| [`AbiEvent.FilterTypeNotSupportedError`](/docs/utilities/abievent/errors#abieventfiltertypenotsupportederror) | Thrown when the provided filter type is not supported. |
| [`AbiEvent.InputNotFoundError`](/docs/utilities/abievent/errors#abieventinputnotfounderror) | Thrown when no argument was found on the event signature. |
| [`AbiEvent.SelectorTopicMismatchError`](/docs/utilities/abievent/errors#abieventselectortopicmismatcherror) | Thrown when the provided selector does not match the expected selector. |
| [`AbiEvent.SelectorTopicNotFoundError`](/docs/utilities/abievent/errors#abieventselectortopicnotfounderror) | Thrown when the selector topic is not found. |
| [`AbiEvent.TopicsMismatchError`](/docs/utilities/abievent/errors#abieventtopicsmismatcherror) | Thrown when the provided topics do not match the expected number of topics. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abieventabievent) | Root type for an [`AbiItem.AbiItem`](/docs/utilities/abiitem/types#abiitem) with an `event` type. |
| [`AbiEvent.ExtractNames`](/docs/utilities/abievent/types#abieventextractnames) |  |
| [`AbiEvent.FromAbi`](/docs/utilities/abievent/types#abieventfromabi) | Extracts an [`AbiEvent.AbiEvent`](/docs/utilities/abievent/types#abievent) item from an [`Abi.Abi`](/docs/utilities/abi/types#abi), given a name. |
| [`AbiEvent.Name`](/docs/utilities/abievent/types#abieventname) | Extracts the names of all [`AbiError.AbiError`](/docs/utilities/abierror/types#abierror) items in an [`Abi.Abi`](/docs/utilities/abi/types#abi). |
