Skip to content
Docs
Hooks
useContractEvent

useContractEvent

Hook for subscribing to ethers Contract events.

import { useContractEvent } from 'wagmi'

Usage

The following examples use the ENS Registry contract.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewOwner',
    listener: (event) => console.log(event),
  })
}

Arguments

addressOrName

Contract address or ENS name.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewResolver',
    listener: (event) => console.log(event),
  })
}

contractInterface

Contract ABI in JSON or JS object format. An ethers Interface is also allowed.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewResolver',
    listener: (event) => console.log(event),
  })
}

eventName

Name of the event to listen to.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewResolver',
    listener: (event) => console.log(event),
  })
}

listener

Callback that receives event.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewOwner',
    listener: (event) => console.log(event),
  })
}

chainId (optional)

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

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewOwner',
    listener: (event) => console.log(event),
    chainId: 1,
  })
}

once (optional)

Receive only a single event, then stop listener. Defaults to false.

import { useContractEvent } from 'wagmi'

function App() {
  useContractEvent({
    addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    contractInterface: ensRegistryABI,
    eventName: 'NewOwner',
    listener: (event) => console.log(event),
    once: true,
  })
}