The getLatestBlockhash RPC method is essential for preparing and sending transactions on the Solana network. It retrieves the most recent blockhash processed by the node, along with the last block height at which this blockhash will remain valid. For more details on how to effectively use blockhashes and ensure your transactions land, refer to our comprehensive guide.

Every transaction on Solana must reference a recent blockhash. This mechanism helps prevent certain types of attacks, such as transaction replay on a forked chain.

Version Note: This method is available in solana-core v1.9 or newer. For nodes running solana-core v1.8 or older, use the getRecentBlockhash method instead.

Common Use Cases

  • Transaction Building: Obtain a recent blockhash to include in a new transaction before signing and sending it.
  • Transaction Lifetime Management: Use lastValidBlockHeight to understand how long a transaction referencing the fetched blockhash can be expected to remain valid.
  • Preflight Checks: While simulateTransaction can do this implicitly, applications might fetch a blockhash to manually prepare a transaction for simulation.

Request Parameters

This method can optionally take a configuration object with the following parameters:

  • commitment (string, optional): Specifies the commitment level for the query. If omitted, the node’s default commitment is used. For transactions, it’s common to use confirmed or finalized to ensure the blockhash is from a reasonably stable part of the chain.
  • minContextSlot (integer, optional): The minimum slot that the request can be evaluated at. This ensures the query is made against a ledger state that has processed up to at least this slot.

Response Structure

The result field of the JSON-RPC response will be an RpcResponse object. The value field within this object contains:

  • blockhash (string): A base-58 encoded string representing the latest blockhash.
  • lastValidBlockHeight (u64): The block height at which the blockhash will expire. A transaction citing this blockhash is valid until the network reaches this lastValidBlockHeight.

The response also includes a context object with the slot at which the information was retrieved.

Examples

1. Get the Latest Blockhash with Default Commitment

This example fetches the latest blockhash using the node’s default commitment.

# Replace <api-key> with your Helius API key
curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
  '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getLatestBlockhash"
  }'

2. Get the Latest Blockhash with ‘confirmed’ Commitment

This example explicitly requests the latest blockhash with confirmed commitment.

# Replace <api-key> with your Helius API key
curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
  '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getLatestBlockhash",
    "params": [{ "commitment": "confirmed" }]
  }'

Developer Tips

  • Blockhash Validity: A blockhash is valid for a limited time, approximately 2 minutes (though this can vary). Transactions must be confirmed by the network before their lastValidBlockHeight is passed. Always fetch a fresh blockhash if considerable time has passed since the last one was obtained.
  • Choosing Commitment: For critical transactions, using finalized commitment provides the strongest guarantee that the blockhash is from the main chain fork, but it may be slightly older. confirmed offers a good balance. Learn more about commitment levels for detailed information.
  • Transaction Fees: Remember to also calculate and include appropriate transaction fees. The blockhash itself does not determine the fee.
  • Retry Logic: If a transaction expires because its lastValidBlockHeight is passed, you must re-sign it with a new, more recent blockhash and resubmit.

This guide provides the steps to use getLatestBlockhash effectively, a cornerstone for interacting with the Solana network through transactions.