• Core
  • Actions
  • watchContractEvent

watchContractEvent

Action for subscribing to Contract events.

This is a wrapper around viem's watchContractEvent.

import { watchContractEvent } from '@wagmi/core'

Usage

The following examples use the ENS Registry contract.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewOwner',
  },
  (log) => console.log(log),
)

Return Value

unwatch is a function that can be called to unsubscribe from the event.

Configuration

address

Contract address.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewOwner',
  },
  (log) => console.log(log),
)

abi

Contract ABI.

By defining inline or adding a const assertion to abi, TypeScript will infer the correct types for eventName and listener. See the wagmi TypeScript docs for more information.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewOwner',
  },
  (log) => console.log(log),
)

eventName

Name of the event to listen to.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewResolver',
  },
  (log) => console.log(log),
)

listener

Callback that receives event.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewOwner',
  },
  (log) => console.log(log),
)

chainId (optional)

Force a specific chain id. The wagmi Client's publicClient must be set up as a chain-aware function for this to work correctly.

import { watchContractEvent } from '@wagmi/core'
 
const unwatch = watchContractEvent(
  {
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    eventName: 'NewOwner',
    chainId: 1,
  },
  (log) => console.log(log),
)