Skip to content

useWatchBlockNumber

Composable that watches for block number changes.

Import

ts
import { useWatchBlockNumber } from '@wagmi/vue'

Usage

ts
<script setup lang="ts">
import { useWatchBlockNumber } from '@wagmi/vue'

useWatchBlockNumber({
  onBlockNumber(blockNumber) {
    console.log('Block number changed!', blockNumber)
  },
})
</script>
ts
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'

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

Parameters

ts
import { type UseWatchBlockNumberParameters } from '@wagmi/vue'

chainId

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

ID of chain to watch blocks at.

config

Config | undefined

Config to use instead of retrieving from the from nearest WagmiProvider.

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.

emitOnBegin

boolean

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

enabled

boolean

Whether or not to watch for blocks. Defaults to true.

onBlockNumber

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

Callback for when block changes.

onError

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

Error thrown from getting the block.

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.

pollingInterval

number | undefined

syncConnectedChain

boolean | undefined

Return Type

ts
import { type UseWatchBlockNumberReturnType } from '@wagmi/vue'

Function for cleaning up watcher.

Action

Released under the MIT License.