Skip to Content
DocumentationRpcGet Latest Blockhash

Get Latest Blockhash

The getLatestBlockhash function retrieves the latest blockhash from the network. Blockhashes are used to ensure transactions are recent and prevent replay attacks.

Usage

const result = await connection.getLatestBlockhash();

Parameters

  • commitment: Commitment (optional) - Confirmation level to use (default: “finalized”)
    • "processed" - Processed by current node
    • "confirmed" - Confirmed by supermajority of the cluster
    • "finalized" - Confirmed by supermajority and unlikely to revert

Returns

Returns a Promise<Object> containing:

  • blockhash: string - The latest blockhash
  • lastValidBlockHeight: bigint - The block height after which this blockhash will no longer be valid
  • context: Object - Context information including the slot

Example

// Get the latest blockhash with default commitment const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(); console.log(`Latest blockhash: ${blockhash}`); console.log(`Valid until block height: ${lastValidBlockHeight}`); // Get with specific commitment level const result = await connection.getLatestBlockhash("confirmed"); console.log(`Blockhash (confirmed): ${result.blockhash}`);

Use Cases

  • Building transactions that require recent blockhashes
  • Checking if a blockhash is still valid
  • Monitoring network progression

Notes

  • Blockhashes expire after approximately 60 seconds or 150 blocks
  • Using “finalized” commitment provides the most reliable blockhash but may be slightly older
  • Using “processed” commitment provides the newest blockhash but may not be confirmed yet
  • Transactions must be submitted before the lastValidBlockHeight is reached
Last updated on