Skip to content

getBlockNumber

Action for getting the latest Starknet block number.

Import

import { getBlockNumber } from "starkweb/core";

Usage

index.ts
import { getBlockNumber } from "starkweb/core";
import { config } from "./config";
 
const blockNumber = await getBlockNumber(config); 
console.log("Current block:", blockNumber);

Parameters

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

chainId (optional)

Hex | undefined

The chain ID to get the block number for. If not provided, will use the current chain.

index.ts
import { getBlockNumber } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
const blockNumber = await getBlockNumber(config, {
  chainId: mainnet.chain_id, 
});

Return Type

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

bigint

The current block number.

Example

example.ts
import { getBlockNumber } from "starkweb/core";
import { mainnet, testnet } from "starkweb/chains"; 
import { config } from "./config";
 
// Get block number for current chain
const currentBlock = await getBlockNumber(config); 
console.log("Current block:", currentBlock);
 
// Get block number for specific chain
const mainnetBlock = await getBlockNumber(config, {
  chainId: mainnet.chain_id, 
});
console.log("Mainnet block:", mainnetBlock);
 
// Get block number for testnet
const testnetBlock = await getBlockNumber(config, {
  chainId: testnet.chain_id, 
});
console.log("Testnet block:", testnetBlock);

Error

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

TanStack Query

import {
  type GetBlockNumberData,
  type GetBlockNumberQueryFnData,
  type GetBlockNumberQueryKey,
  getBlockNumberQueryKey,
  getBlockNumberQueryOptions,
} from "starkweb/query";