跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://docs.orbitflare.com/llms.txt

Use this file to discover all available pages before exploring further.

getRecentBlockhash

此方法已弃用。请改用 getLatestBlockhash,它提供更准确和可靠的 blockhash 信息。
返回近期 blockhash 及其费用计划。此方法将在未来版本中移除。

参数

config
object

响应

result
object

迁移指南

要从 getRecentBlockhash 迁移到 getLatestBlockhash
  1. 替换方法调用:
- const { blockhash, feeCalculator } = await connection.getRecentBlockhash();
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
  1. 更新费用计算:
// 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);

注意事项

  1. 此方法已弃用,将在未来版本中移除
  2. 新开发请使用 getLatestBlockhash
  3. blockhash 有效期有限(通常为 150 个 slot)
  4. 可以指定不同的 commitment 级别
  5. 考虑更新现有代码以使用新方法

最佳实践

  1. 新开发迁移到 getLatestBlockhash
  2. 使用 getFeeForMessage 进行费用计算
  3. 使用 lastValidBlockHeight 监控 blockhash 过期
  4. 适当处理网络错误并重试
  5. 根据需求使用适当的 commitment 级别

常见错误

错误码消息解决方案
-32601Method not found验证是否连接到 Solana RPC 节点
-32602Invalid params检查配置参数
-32007Blockhash information unavailable节点可能正在启动或同步中

用例

  1. 交易费用计算
    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 }
      };
    }
    
  2. 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 }
        };
      }
    }
    
  3. 交易规划
    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 }
      };
    }