Skip to content

watchBlockNumber

Action for watching changes to the block number on a Starknet network.

Import

import { watchBlockNumber } from "starkweb/core";

Usage

index.ts
import { watchBlockNumber } from "starkweb/core";
import { config } from "./config";
 
const unwatch = watchBlockNumber(config, {
  onBlockNumber(blockNumber) { 
    console.log('New block:', blockNumber); 
  }, 
  pollingInterval: 1_000, 
});

Parameters

import { type WatchBlockNumberParameters } from "starkweb/core";

onBlockNumber

(blockNumber: bigint) => void

Callback function that is called when a new block is detected. Receives the block number as argument.

pollingInterval (optional)

number

Polling interval in milliseconds. Defaults to 4000 (4 seconds).

syncConnectedChain (optional)

boolean

Whether to sync with the connected chain. Defaults to true.

index.ts
import { watchBlockNumber } from "starkweb/core";
import { config } from "./config";
 
const unwatch = watchBlockNumber(config, {
  onBlockNumber(blockNumber) { 
    console.log('New block:', blockNumber); 
  }, 
  pollingInterval: 1_000, 
  syncConnectedChain: true, 
});

chainId (optional)

Hex | undefined

The chain ID to watch. If not provided, uses the current chain.

index.ts
import { watchBlockNumber } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
const unwatch = watchBlockNumber(config, {
  onBlockNumber(blockNumber) {
    console.log('New block:', blockNumber);
  },
  chainId: mainnet.chain_id, 
});

Return Type

import { type WatchBlockNumberReturnType } from "starkweb/core";

() => void

Function to stop watching for block number changes.

Example

example.ts
import { watchBlockNumber } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
// Start watching block number changes
const unwatch = watchBlockNumber(config, {
  onBlockNumber(blockNumber) { 
    console.log('New block:', blockNumber);
  },
  pollingInterval: 1_000, 
  chainId: mainnet.chain_id, 
});
 
// Later: stop watching
unwatch();