getRecentBlockhash
此方法已弃用。请改用
getLatestBlockhash,它提供更准确和可靠的 blockhash 信息。参数
响应
迁移指南
要从getRecentBlockhash 迁移到 getLatestBlockhash:
- 替换方法调用:
- const { blockhash, feeCalculator } = await connection.getRecentBlockhash();
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
- 更新费用计算:
// Old way
const fee = feeCalculator.lamportsPerSignature * numSignatures;
// New way
const fee = await connection.getFeeForMessage(message);
代码示例
基本请求(旧版)
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentBlockhash",
"params": []
}'
带 Commitment 的请求(旧版)
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentBlockhash",
"params": [
{
"commitment": "confirmed"
}
]
}'
使用 web3.js(旧版)
import { Connection } from '@solana/web3.js';
const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
// Get recent blockhash (deprecated)
const { value: { blockhash, feeCalculator } } = await connection.getRecentBlockhash();
console.log('Blockhash:', blockhash);
console.log('Fee per signature:', feeCalculator.lamportsPerSignature);
注意事项
- 此方法已弃用,将在未来版本中移除
- 新开发请使用
getLatestBlockhash - blockhash 有效期有限(通常为 150 个 slot)
- 可以指定不同的 commitment 级别
- 考虑更新现有代码以使用新方法
最佳实践
- 新开发迁移到
getLatestBlockhash - 使用
getFeeForMessage进行费用计算 - 使用
lastValidBlockHeight监控 blockhash 过期 - 适当处理网络错误并重试
- 根据需求使用适当的 commitment 级别
常见错误
| 错误码 | 消息 | 解决方案 |
|---|---|---|
| -32601 | Method not found | 验证是否连接到 Solana RPC 节点 |
| -32602 | Invalid params | 检查配置参数 |
| -32007 | Blockhash information unavailable | 节点可能正在启动或同步中 |
用例
-
交易费用计算
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 监控
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 } }; } } -
交易规划
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 } }; }