• Core
  • Actions
  • watchReadContract

watchReadContract

Action for subscribing to readContract changes.

import { watchReadContract } from '@wagmi/core'

Usage

By default, the callback will be invoked on chain change.

To listen for block changes, you can use the listenToBlock config.

import { watchReadContract } from '@wagmi/core'
 
const config = {
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
}
 
let data = await readContract(config)
const unwatch = watchReadContract(config, (data_) => (data = data_))

Return Value

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

Configuration

address

The address of the contract.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'getHunger',
  },
  (data) => console.log(data),
)

abi

The ABI of the contract.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'getHunger',
  },
  (data) => console.log(data),
)

functionName

The name of the function on the contract to call.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'getHunger',
  },
  (data) => console.log(data),
)

args (optional)

Arguments to pass to function call.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'love',
    args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'],
  },
  (data) => console.log(data),
)

chainId (optional)

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

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'love',
    chainId: 1,
  },
  (data) => console.log(data),
)

listenToBlock (optional)

Listen for block changes & invoke multicall.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'love',
    listenToBlock: true,
  },
  (data) => console.log(data),
)

blockNumber (optional)

The block number to perform the read against.

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'getHunger',
    blockNumber: 15121123n,
  },
  (data) => console.log(data),
)

blockTag (optional)

The block tag to perform the read against. Accepts: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'

import { watchReadContract } from '@wagmi/core'
 
watchReadContract(
  {
    address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    abi: wagmigotchiABI,
    functionName: 'getHunger',
    blockTag: 'safe',
  },
  (data) => console.log(data),
)