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

# getBlocks

> 返回两个 slot 之间的已确认区块列表

## 参数

<ParamField query="startSlot" type="number" required>
  起始 slot（包含）
</ParamField>

<ParamField query="endSlot" type="number" required>
  结束 slot（包含）
</ParamField>

<ParamField query="config" type="object" optional>
  包含以下可选字段的配置对象：

  <Expandable title="config fields">
    <ParamField query="commitment" type="string" optional>
      使用的 commitment 级别：

      * `processed`：最新区块（未确认）
      * `confirmed`：超级多数确认
      * `finalized`：超级多数最终确认
    </ParamField>
  </Expandable>
</ParamField>

## 响应

<ResponseField name="result" type="array">
  按升序排列的区块 slot 数组
</ResponseField>

## 代码示例

### 基本请求

```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": "getBlocks",
  "params": [
    100000000,
    100000100
  ]
}'
```

### 带 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": "getBlocks",
  "params": [
    100000000,
    100000100,
    {
      "commitment": "finalized"
    }
  ]
}'
```

### 使用 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 blocks in range
const blocks = await connection.getBlocks(100000000, 100000100);
console.log('Blocks:', blocks);

// Get finalized blocks
const finalizedBlocks = await connection.getBlocks(100000000, 100000100, 'finalized');
console.log('Finalized blocks:', finalizedBlocks);
```

## 注意事项

1. 返回指定 slot 之间的已确认区块
2. 结果按升序返回
3. 范围包含起始和结束 slot
4. 某些 slot 可能被跳过（未生产区块）
5. 响应是即时的，因为它从当前状态读取

## 最佳实践

1. 根据需求使用适当的 commitment 级别：
   * `processed` 获取最新区块
   * `confirmed` 获取高概率最终性
   * `finalized` 获取保证的最终性
2. 保持 slot 范围合理以避免超时
3. 考虑使用 `getBlocksWithLimit` 进行分页
4. 适当时缓存结果以减少 RPC 负载
5. 在应用逻辑中处理跳过的 slot

## 常见错误

| 错误码    | 消息                                                 | 解决方案                    |
| ------ | -------------------------------------------------- | ----------------------- |
| -32602 | Invalid param: startSlot must be less than endSlot | 确保 startSlot 小于 endSlot |
| -32602 | Invalid param: slot range too large                | 减小 slot 范围大小            |
| -32601 | Method not found                                   | 验证是否连接到 Solana RPC 节点   |
| -32007 | Block information unavailable                      | 节点可能正在启动或范围太旧           |
