> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何使用 getBlocksWithLimit

> 了解 getBlocksWithLimit 的使用案例、代码示例、请求参数、响应结构和提示。

这个[`getBlocksWithLimit`](https://www.helius.dev/docs/api-reference/rpc/http/getblockswithlimit) RPC方法允许您检索已确认的区块槽号列表，从指定的槽开始并返回至给定的限制。这在您需要从分类账某一点开始的一定数量的后续已确认[区块](https://www.helius.dev/blog/solana-slots-blocks-and-epochs)时很有用。

<Warning>
  **避免批处理以提高性能**

  批处理归档方法会显著增加延迟。不允许超过10个请求的批处理。
</Warning>

## 常见用例

* **获取固定数量的后续区块：** 获取在特定起始槽位之后确认的定义数量的区块槽位。
* **分页区块探索：** 在分析区块链的某个片段时，分块检索区块。
* **最近区块监控：** 从已知的最近槽位获取最后的N个区块。

## 请求参数

`getBlocksWithLimit`方法需要以下参数：

1. **`start_slot`** (u64, 必需): 首个考虑的槽（含）。
2. **`limit`** (u64, 必需): 要返回的最大区块槽数。查询的总槽数（从`start_slot`到其后`limit`个区块）**不得超过500,000个槽**。
3. **`commitment`** (string, 可选): 指定查询的承诺级别。如果省略，将使用节点的默认承诺。这作为配置对象中的唯一字段传递，作为最后一个参数。

## 响应结构

JSON-RPC响应的`result`字段将是u64整数的数组。数组中的每个整数表示一个已确认的区块槽号，从`start_slot`到指定的`limit`。

* 示例: 如果`start_slot`是5，`limit`是3，则可能的结果是`[5, 6, 7]`。

## 示例

### 1. 从起始槽位获取有限数量的区块

此示例从槽`280000000`开始获取最多5个已确认的区块槽。

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocksWithLimit",
      "params": [
        280000000, 
        5 
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksWithLimitExample(startSlot, limit) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocksWithLimit(startSlot, limit);
      console.log(`Up to ${limit} confirmed blocks starting from slot ${startSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks with limit:', error);
    }
  }

  // Example usage:
  const startSlot = 280000000;
  const limit = 5;
  getBlocksWithLimitExample(startSlot, limit);
  ```
</CodeGroup>

### 2. 获取具有限制和特定承诺级别的区块

此示例使用`confirmed`承诺级别从槽`290000000`开始获取最多3个区块。

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocksWithLimit",
      "params": [
        290000000,
        3,
        { "commitment": "confirmed" }
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksWithLimitAndCommitment(startSlot, limit) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocksWithLimit(startSlot, limit, { commitment: 'confirmed' });
      console.log(`Up to ${limit} confirmed blocks (with 'confirmed' commitment) starting from slot ${startSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks with limit and commitment:', error);
    }
  }

  // Example usage:
  const commitStartSlot = 290000000;
  const commitLimit = 3;
  getBlocksWithLimitAndCommitment(commitStartSlot, commitLimit);
  ```
</CodeGroup>

## 开发者提示

* **范围说明：** `limit` 参数决定要返回的最大区块*插槽*数量。500,000 插槽的限制应用于扫描的概念范围（即，从 `start_slot` 到 `start_slot + limit - 1`）。请确保此概念范围不超过 500,000 插槽，即使在该范围内找到并返回的实际区块较少。
* **节点数据可用性：** 节点可能不会保留所有历史插槽的信息。一个非常旧的 `start_slot` 可能会返回比指定的 `limit` 少的区块，或者返回一个空数组，这取决于节点的账本保留。
* **区块确认：** 此方法返回*已确认的*区块。确切的区块集可能会根据选择的 `commitment` 级别和查询的节点有所不同，特别是对于非常近期的插槽。
* **用例特性：** 当您知道起点并需要一个固定数量的后续区块时，`getBlocksWithLimit` 是理想的选择。如果您需要已知插槽范围内的所有区块，`getBlocks` 可能更合适。

本指南提供了如何使用 `getBlocksWithLimit` RPC 方法在 Solana 网络上列出特定数量已确认区块插槽的清晰概述。

## 相关方法

<CardGroup cols={2}>
  <Card title="getBlocks" href="/docs/zh/api-reference/rpc/http/getblocks">
    获取插槽范围内的所有区块
  </Card>

  <Card title="getBlock" href="/docs/zh/api-reference/rpc/http/getblock">
    获取特定区块的详细信息
  </Card>
</CardGroup>
