# 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`).

:::tip
This function is typically used to decode an [Event Log](https://info.etherscan.com/what-is-event-logs/) that may be returned from a Log Query (e.g. `eth_getLogs`) or Transaction Receipt.

See the [End-to-end Example](#end-to-end).
:::

## Imports

:::code-group
```ts [Named]
import { AbiEvent } from 'ox'
```

```ts [Entrypoint]
import * as AbiEvent from 'ox/AbiEvent'
```
:::

## Examples

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

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: }
```

### ABI-shorthand

You can also specify an entire ABI object and an event name as parameters to [`AbiEvent.decode`](/api/AbiEvent/decode):

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

const abi = Abi.from([...])
const log = {
  // ...
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
  ],
} as const

const decoded = AbiEvent.decode(
  abi, // [!code focus]
  'Transfer', // [!code focus]
  log
)
// @log: {
// @log:   from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
// @log:   to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
// @log:   value: 1n
// @log: }
```

### End-to-end

Below is an end-to-end example of using `AbiEvent.decode` to decode the topics of a `Transfer` event on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2).

```ts twoslash
import 'ox/window'
import { AbiEvent, Hex } from 'ox'

// 1. Instantiate the `Transfer` ABI Event.
const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)

// 2. Encode the ABI Event into Event Topics.
const { topics } = AbiEvent.encode(transfer)

// 3. Query for events matching the encoded Topics.
const logs = await window.ethereum!.request({
  method: 'eth_getLogs',
  params: [
    {
      address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
      fromBlock: Hex.fromNumber(19760235n),
      toBlock: Hex.fromNumber(19760240n),
      topics
    }
  ]
})

// 4. Decode the Log. // [!code focus]
const decoded = AbiEvent.decode(transfer, logs[0]!) // [!code focus]
// @log: {
// @log:   from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
// @log:   to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
// @log:   value: 603n
// @log: }
```

:::note
For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface.
:::

## Definition

```ts
function decode<abi, name, args, abiEvent, allNames>(
  abi: abi | Abi.Abi | readonly unknown[],
  name: Hex.Hex | (name extends allNames ? name : never),
  log: decode.Log,
  options?: decode.Options,
): decode.ReturnType<abiEvent>
```

**Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1981)

## Parameters

### abi

* **Type:** `abi | Abi.Abi | readonly unknown[]`

### name

* **Type:** `Hex.Hex | (name extends allNames ? name : never)`

### log

* **Type:** `decode.Log`

`topics` & `data` to decode.

#### log.data

* **Type:** `0x${string}`
* **Optional**

#### log.topics

* **Type:** `readonly 0x${string}[]`

### options

* **Type:** `decode.Options`
* **Optional**

Decoding options.

#### options.checksumAddress

* **Type:** `boolean`
* **Optional**

Whether decoded addresses should be checksummed.

## Return Type

The decoded event.

`decode.ReturnType<abiEvent>`
