watchContractEvent
Action for subscribing to ethers Contract events.
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',
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
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',
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
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',
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
eventName
Name of the event to listen to.
import { watchContractEvent } from '@wagmi/core'
const unwatch = watchContractEvent(
{
address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
abi: ensRegistryABI,
eventName: 'NewResolver',
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
listener
Callback that receives event.
import { watchContractEvent } from '@wagmi/core'
const unwatch = watchContractEvent(
{
address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
abi: ensRegistryABI,
eventName: 'NewOwner',
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
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 { watchContractEvent } from '@wagmi/core'
const unwatch = watchContractEvent(
{
address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
abi: ensRegistryABI,
eventName: 'NewOwner',
chainId: 1,
},
(node, label, owner) => {
console.log(node, label, owner)
},
)
once (optional)
Receive only a single event, then stop listener. Defaults to false
.
import { watchContractEvent } from '@wagmi/core'
const unwatch = watchContractEvent(
{
address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
abi: ensRegistryABI,
eventName: 'NewOwner',
once: true,
},
(node, label, owner) => {
console.log(node, label, owner)
},
)