getRecentBlockhash
This method is deprecated. Please use getLatestBlockhash
instead, which provides more accurate and reliable blockhash information.
Returns a recent blockhash and its fee schedule. This method will be removed in a future release.
Parameters
Commitment level (processed, confirmed, finalized)
Response
Slot at which the request was processed
Recent blockhash (base-58 encoded)
Fee in lamports per signature
Migration Guide
To migrate from getRecentBlockhash
to getLatestBlockhash
:
- Replace method calls:
- const { blockhash, feeCalculator } = await connection.getRecentBlockhash();
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
- Update fee calculations:
// Old way
const fee = feeCalculator.lamportsPerSignature * numSignatures;
// New way
const fee = await connection.getFeeForMessage(message);
Code Examples
Basic Request (Legacy)
curl https://rpc.orbitflare.com -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentBlockhash",
"params": []
}'
Request with Commitment (Legacy)
curl https://rpc.orbitflare.com -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentBlockhash",
"params": [
{
"commitment": "confirmed"
}
]
}'
Using web3.js (Legacy)
import { Connection } from '@solana/web3.js';
const connection = new Connection('https://rpc.orbitflare.com');
// Get recent blockhash (deprecated)
const { value: { blockhash, feeCalculator } } = await connection.getRecentBlockhash();
console.log('Blockhash:', blockhash);
console.log('Fee per signature:', feeCalculator.lamportsPerSignature);
Notes
- This method is deprecated and will be removed in a future release
- Use
getLatestBlockhash
for new development
- The blockhash is valid for a limited time (typically 150 slots)
- Different commitment levels can be specified
- Consider updating existing code to use the new methods
Best Practices
- Migrate to
getLatestBlockhash
for new development
- Use
getFeeForMessage
for fee calculations
- Monitor for blockhash expiration using
lastValidBlockHeight
- Handle network errors and retry when appropriate
- Use appropriate commitment level based on your needs
Common Errors
Code | Message | Solution |
---|
-32601 | Method not found | Verify you’re connected to a Solana RPC node |
-32602 | Invalid params | Check config parameters |
-32007 | Blockhash information unavailable | Node may be bootstrapping or syncing |
Use Cases
-
Transaction Fee Calculation
interface TransactionFee {
blockhash: string;
feePerSignature: number;
totalFee: number;
metadata: {
timestamp: number;
slot: number;
};
}
async function calculateTransactionFees(
numSignatures: number
): Promise<TransactionFee> {
const { context, value } = await connection.getRecentBlockhash();
return {
blockhash: value.blockhash,
feePerSignature: value.feeCalculator.lamportsPerSignature,
totalFee: numSignatures * value.feeCalculator.lamportsPerSignature,
metadata: {
timestamp: Date.now(),
slot: context.slot
}
};
}
-
Blockhash Monitoring
interface BlockhashInfo {
blockhash: string;
slot: number;
feePerSignature: number;
metadata: {
timestamp: number;
age: number;
};
}
class BlockhashMonitor {
private currentBlockhash: string | null = null;
private currentSlot: number | null = null;
async monitorBlockhash(
interval: number = 5000
): Promise<BlockhashInfo | null> {
const { context, value } = await connection.getRecentBlockhash();
if (this.currentBlockhash === value.blockhash) {
return null;
}
this.currentBlockhash = value.blockhash;
this.currentSlot = context.slot;
return {
blockhash: value.blockhash,
slot: context.slot,
feePerSignature: value.feeCalculator.lamportsPerSignature,
metadata: {
timestamp: Date.now(),
age: 0
}
};
}
}
-
Transaction Planning
interface TransactionPlan {
blockhash: string;
feePerSignature: number;
estimatedFees: {
single: number;
batch: number;
};
metadata: {
timestamp: number;
slot: number;
};
}
async function planTransactions(
numTransactions: number,
signaturesPerTransaction: number
): Promise<TransactionPlan> {
const { context, value } = await connection.getRecentBlockhash();
const feePerSignature = value.feeCalculator.lamportsPerSignature;
return {
blockhash: value.blockhash,
feePerSignature,
estimatedFees: {
single: signaturesPerTransaction * feePerSignature,
batch: numTransactions * signaturesPerTransaction * feePerSignature
},
metadata: {
timestamp: Date.now(),
slot: context.slot
}
};
}