getLatestBlockhash
The getLatestBlockhash method returns the latest blockhash from the ledger along with important context about when it will expire. Blockhashes are essential components of Solana transactions, serving as timestamps and preventing transaction replay attacks.
Every Solana transaction must include a recent blockhash to be considered valid. The blockhash serves multiple purposes: it acts as a timestamp indicating when the transaction was created, prevents replay attacks by making transactions unique to a specific time window, and helps validators determine if a transaction is too old to process. Blockhashes expire after approximately 60-90 seconds (about 150 blocks), after which transactions using that blockhash will be rejected.
This method is crucial for transaction creation workflows. Applications must fetch a recent blockhash before building transactions, and should monitor the lastValidBlockHeight to ensure transactions are submitted before the blockhash expires. The method provides both the blockhash itself and metadata about its validity window, enabling robust transaction submission logic with proper timeout handling.
Parameters
parameter | type | description |
|---|---|---|
config | object | Optional configuration object |
config.commitment | string | Level of commitment: 'processed', 'confirmed', or 'finalized' (default: 'finalized') |
config.minContextSlot | number | Optional. Minimum slot that the request can be evaluated at |
Return Object
field | type | description |
|---|---|---|
context | object | RPC response context with slot information |
context.slot | number | The slot at which this blockhash was retrieved |
value | object | Blockhash information object |
value.blockhash | string | The latest blockhash as a base-58 encoded string |
value.lastValidBlockHeight | number | The block height after which this blockhash will no longer be valid. Transactions must be landed before this height |
Request Example
Response Example
Tip: Always fetch a fresh blockhash immediately before creating transactions. Monitor the current block height (via getBlockHeight) and ensure your transaction is submitted before lastValidBlockHeight. For 'processed' commitment, blockhashes are fresher but may be from forks that get rolled back.