Skip to content

getBlockHashAndNumber

Action for getting the current Starknet block hash and number.

Import

import { getBlockHashAndNumber } from "starkweb/core";

Usage

index.ts
import { getBlockHashAndNumber } from "starkweb/core";
import { config } from "./config";
 
const block = await getBlockHashAndNumber(config); 
console.log("Block hash:", block.blockHash);
console.log("Block number:", block.blockNumber);

Parameters

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

chainId (optional)

Hex | undefined

The chain ID to get the block information for. If not provided, uses the current chain.

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

Return Type

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

blockHash

Hex

The hash of the current block.

blockNumber

bigint

The number of the current block.

chainId

Hex

The chain ID of the network.

Example

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

Error

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