# AbiEvent.encode

ABI-encodes the provided event input (`inputs`) into an array of [Event Topics](https://info.etherscan.com/what-is-event-logs/).

:::tip
This function is typically used to encode event arguments into [Event Topics](https://info.etherscan.com/what-is-event-logs/).

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 { topics } = AbiEvent.encode(transfer)
// @log: ['0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0']
```

### Passing Arguments

You can pass `indexed` parameter values to `AbiEvent.encode`.

TypeScript types will be inferred from the ABI Event, to guard you from inserting the wrong values.

For example, the `Transfer` event below accepts an `address` type for the `from` and `to` attributes.

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

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

### ABI-shorthand

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

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

const abi = Abi.from([...])

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

### End-to-end

Below is an end-to-end example of using `AbiEvent.encode` to encode the topics of a `Transfer` event and query for events matching the encoded topics 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
    }
  ]
})
// @log: [
// @log:   "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
// @log:   "0x0000000000000000000000000000000000000000000000000000000000000000",
// @log:   "0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1",
// @log:   "0x000000000000000000000000000000000000000000000000000000000000025b",
// @log: ]
```

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

## Definition

```ts
function encode<abi, name, args, abiEvent, allNames>(
  abi: abi | Abi.Abi | readonly unknown[],
  name: Hex.Hex | (name extends allNames ? name : never),
  [args]: encode.Args<abiEvent>,
): encode.ReturnType
```

**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)`

### \[args]

* **Type:** `encode.Args<abiEvent>`

## Return Type

The encoded event topics.

`encode.ReturnType`
