Skip to content

watchBlocks ​

Action that watches for block changes.

Import ​

ts
import { watchBlocks } from '@wagmi/core'
import { watchBlocks } from '@wagmi/core'

Usage ​

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

Parameters ​

ts
import { type WatchBlocksParameters } from '@wagmi/core'
import { type WatchBlocksParameters } from '@wagmi/core'

blockTag ​

'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'

Watch for new blocks on a given tag. Defaults to 'latest'.

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  blockTag: 'pending', 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  blockTag: 'pending', 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

chainId ​

config['chains'][number]['id'] | undefined

ID of chain to use when fetching data.

ts
import { watchBlocks } from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { config } from './config'

const unwatch = watchBlocks(config, {
  chainId: mainnet.id, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { config } from './config'

const unwatch = watchBlocks(config, {
  chainId: mainnet.id, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

emitMissed ​

boolean

Whether or not to emit missed blocks to the callback. Defaults to false.

Missed blocks may occur in instances where internet connection is lost, or the block time is lesser than the polling interval of the client.

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  emitMissed: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  emitMissed: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

emitOnBegin ​

boolean

Whether or not to emit the block to the callback when the subscription opens. Defaults to false.

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  emitOnBegin: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  emitOnBegin: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

onBlock ​

(block: Block, prevblock: Block | undefined) => void

Callback for when block changes.

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) { 
    console.log('Block changed!', block) 
  }, 
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) { 
    console.log('Block changed!', block) 
  }, 
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

onError ​

((error: Error) => void) | undefined

Error thrown from getting the block.

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) { 
    console.log('Block changed!', block) 
  }, 
  onError(error) { 
    console.error('Block error', error) 
  }, 
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) { 
    console.log('Block changed!', block) 
  }, 
  onError(error) { 
    console.error('Block error', error) 
  }, 
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

poll ​

boolean | undefined

  • Whether or not to use a polling mechanism to check for new blocks instead of a WebSocket subscription.
  • Defaults to false for WebSocket Clients, and true for non-WebSocket Clients.
ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  poll: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  poll: true, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

pollingInterval ​

number | undefined

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  pollingInterval: 1_000, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  pollingInterval: 1_000, 
  onBlock(block) {
    console.log('Block changed!', block)
  },
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

syncConnectedChain ​

boolean | undefined

ts
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) {
    console.log('Block changed!', block)
  },
  syncConnectedChain: false, 
})
unwatch()
import { watchBlocks } from '@wagmi/core'
import { config } from './config'

const unwatch = watchBlocks(config, {
  onBlock(block) {
    console.log('Block changed!', block)
  },
  syncConnectedChain: false, 
})
unwatch()
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

Return Type ​

ts
import { type WatchBlocksReturnType } from '@wagmi/core'
import { type WatchBlocksReturnType } from '@wagmi/core'

Function for cleaning up watcher.

Viem ​

Released under the MIT License.