> ## 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

> [已弃用] 返回近期 blockhash 及其费用计划

# getRecentBlockhash

<Warning>
  此方法已弃用。请改用 [`getLatestBlockhash`](/rpc/methods/getLatestBlockhash)，它提供更准确和可靠的 blockhash 信息。
</Warning>

返回近期 blockhash 及其费用计划。此方法将在未来版本中移除。

## 参数

<ResponseField name="config" type="object" optional>
  <Expandable title="config fields">
    <ResponseField name="commitment" type="string" optional>
      Commitment 级别（processed、confirmed、finalized）
    </ResponseField>
  </Expandable>
</ResponseField>

## 响应

<ResponseField name="result" type="object">
  <Expandable title="result fields">
    <ResponseField name="context" type="object">
      <ResponseField name="slot" type="number">
        处理请求时的 slot
      </ResponseField>
    </ResponseField>

    <ResponseField name="value" type="object">
      <ResponseField name="blockhash" type="string">
        近期 blockhash（base-58 编码）
      </ResponseField>

      <ResponseField name="feeCalculator" type="object">
        <ResponseField name="lamportsPerSignature" type="number">
          每个签名的手续费（以 lamports 为单位）
        </ResponseField>
      </ResponseField>
    </ResponseField>
  </Expandable>
</ResponseField>

## 迁移指南

要从 `getRecentBlockhash` 迁移到 `getLatestBlockhash`：

1. 替换方法调用：

```diff theme={null}
- const { blockhash, feeCalculator } = await connection.getRecentBlockhash();
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
```

2. 更新费用计算：

```typescript theme={null}
// Old way
const fee = feeCalculator.lamportsPerSignature * numSignatures;

// New way
const fee = await connection.getFeeForMessage(message);
```

## 代码示例

### 基本请求（旧版）

```bash theme={null}
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 的请求（旧版）

```bash theme={null}
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（旧版）

```typescript theme={null}
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 级别

## 常见错误

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

## 用例

1. **交易费用计算**
   ```typescript theme={null}
   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 监控**
   ```typescript theme={null}
   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. **交易规划**
   ```typescript theme={null}
   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 }
     };
   }
   ```
