Skip to content
On this page

encodeEventTopics

Encodes an event (with optional arguments) into filter topics.

Install

ts
import { encodeEventTopics } from 'viem/contract'

Usage

Below is a very basic example of how to encode event topics without arguments.

ts
import { encodeEventTopics } from 'viem/contract'

const topics = encodeEventTopics({
  abi: wagmiAbi,
  eventName: 'Transfer'
})
// ["0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0"]
ts
export const wagmiAbi = [
  ...
  {
    inputs: [
      {
        indexed: true,
        name: 'from',
        type: 'address',
      },
      { indexed: true, name: 'to', type: 'address' },
      {
        indexed: false,
        name: 'value',
        type: 'uint256',
      },
    ],
    name: 'Transfer',
    type: 'event',
  },
  ...
] as const;
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})

Passing Arguments

If your event has indexed parameters, you can pass their values through with the args attribute.

TypeScript types for args will be inferred from the event name & ABI, to guard you from inserting the wrong values.

For example, the Transfer event below accepts an address argument for the from and to attributes, and it is typed as "0x${string}".

ts
import { encodeEventTopics } from 'viem/contract'

const topics = encodeEventTopics({
  abi: wagmiAbi,
  eventName: 'Transfer'
  args: {
    from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
  }
})
// ["0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0", "0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8"]
ts
export const wagmiAbi = [
  ...
  {
    inputs: [
      {
        indexed: true,
        name: 'from',
        type: 'address',
      },
      { indexed: true, name: 'to', type: 'address' },
      {
        indexed: false,
        name: 'value',
        type: 'uint256',
      },
    ],
    name: 'Transfer',
    type: 'event',
  },
  ...
] as const;
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})

Return Value

Encoded topics.

Parameters

abi

The contract's ABI.

ts
const data = encodeEventTopics({
  abi: wagmiAbi, 
  functionName: 'Transfer',
})

eventName

  • Type: string

Name of the event.

ts
const data = encodeEventTopics({
  abi: wagmiAbi,
  eventName: 'Transfer', 
})

args (optional)

  • Type: string

A list of indexed event arguments.

ts
const data = encodeEventTopics({
  abi: wagmiAbi,
  eventName: 'Transfer',
  args: { 
    from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
    to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'
  }
})

Released under the MIT License.