getLatestBlockhash
Explore the use cases, examples, parameters, response format, and best practices for the getLatestBlockhash RPC method.
The getLatestBlockhash
RPC method is fundamental for transaction preparation and submission on the Solana network. It retrieves the most recently processed blockhash and the last block height at which that blockhash remains valid. Since every Solana transaction must reference a recent blockhash, this mechanism plays a crucial role in preventing replay attacks, especially on forked chains.
π‘ Note: Available in
solana-core
v1.9 and newer. For older nodes (v1.8 or below), usegetRecentBlockhash
.
π Common Use Cases
Transaction Construction: Retrieve a fresh blockhash to include in new transactions before signing and submitting them.
Managing Transaction Expiry: Use
lastValidBlockHeight
to determine how long a transaction will remain valid before expiring.Manual Preflight Checks: While
simulateTransaction
handles this automatically, developers may opt to manually fetch a blockhash for custom transaction preparation and simulation.
π§Ύ Request Parameters
This method optionally accepts a configuration object:
commitment
(string, optional): The commitment level for the query. Common values includeconfirmed
orfinalized
to ensure a stable blockhash.minContextSlot
(integer, optional): Ensures that the blockhash is retrieved from a ledger state that has processed at least this slot.
π¦ Response Structure
The result
object includes:
blockhash
(string): A base-58 encoded string representing the latest blockhash.lastValidBlockHeight
(u64): The final block height where this blockhash remains valid.context.slot
(u64): The slot at which this data was retrieved.
π‘ Examples
Get the Latest Blockhash (Default Commitment) Fetches the most recent blockhash using the nodeβs default commitment level.
{
"jsonrpc": "2.0",
"id": 1,
"method": "getLatestBlockhash"
}
Get the Latest Blockhash with
confirmed
Commitment Explicitly requests a blockhash withconfirmed
commitment for more stability.
{
"jsonrpc": "2.0",
"id": 1,
"method": "getLatestBlockhash",
"params": [{ "commitment": "confirmed" }]
}
Code Examples
const fetch = require('node-fetch');
async function getLatestBlockhash(rpcUrl) {
try {
const response = await fetch(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getLatestBlockhash',
"params": [{ "commitment": "confirmed" }]
}),
});
const data = await response.json();
// Print the exact full response
console.log('Full RPC Response:');
console.log(JSON.stringify(data, null, 2));
return data;
} catch (error) {
console.error('Error getting health:', error.message);
return null;
}
}
// Example usage
const RPC_URL = 'https://rpc.coinvera.io/?x-api-key=your-coinvera-x-api-key';
getLatestBlockhash(RPC_URL);
Example Response
{
"jsonrpc": "2.0",
"result": {
"context": {
"apiVersion": "2.2.16",
"slot": 355365707
},
"value": {
"blockhash": "GUyGdZk1ghy44vBXaqYkomjUqGnTZEgrPCvCkEETD3xB",
"lastValidBlockHeight": 333557572
}
},
"id": 1
}
π Developer Tips
Blockhash Expiration: A blockhash typically remains valid for ~2 minutes, though this can vary. Always re-fetch a blockhash if your transaction hasn't been submitted promptly.
Commitment Strategy: Use
finalized
for critical, irreversible transactions. For responsiveness,confirmed
provides a balance of speed and reliability.Transaction Fees: This method only returns the blockhash, not fees. Make sure you calculate and attach appropriate fees separately.
Retries: If your transaction expires due to an outdated blockhash, re-sign it with a new one and submit again.
This guide walks you through effectively using the getLatestBlockhash
method β an essential step in ensuring your Solana transactions are fresh, valid, and ready to land.
Last updated