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

# 如何使用 getEpochInfo

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

[RPC 方法](https://www.helius.dev/docs/api-reference/rpc/http/getepochinfo)返回有关当前 epoch 的信息。这对于了解网络的当前状态、网络在当前 epoch 中的进展以及下一个 epoch 可能何时开始非常有用。要更好地理解 Solana 上的[epochs、slots 和 blocks](https://www.helius.dev/blog/solana-slots-blocks-and-epochs)如何协同工作，请参考我们的详细文章。

## 常见用例

* **监控 Epoch 进度：** 跟踪 epoch 中的当前 slot 索引和 epoch 中的总 slots 以估算剩余时间。
* **网络状态分析：** 获取当前 epoch 编号、区块高度以及当前 epoch 中处理的交易数量。
* **同步检查：** 通过比较节点的 epoch 信息来验证其是否与网络合理同步。

## 请求参数

该方法可以选择性地采用带有以下参数的配置对象：

* **（string，可选）**：指定查询分类帐时使用的[承诺级别](https://www.helius.dev/blog/solana-commitment-levels)。
  * ：节点将查询集群超多数确认为已达到最大锁定的最新区块。
  * ：节点将查询由集群超多数投票的最新区块。
  * ：节点将查询其最新区块。注意区块可能不完整。
  * 如果未提供，默认承诺为。
* **（number，可选）**：请求可评估的最小 slot。可用于确保响应来自足够新的状态。

## 响应结构

JSON-RPC 响应的字段将是一个包含以下字段的对象：

* **（u64）**：当前绝对 slot 编号。
* **（u64）**：当前区块高度。
* **（u64）**：当前 epoch 编号。
* **（u64）**：相对于当前 epoch 开始的当前 slot。
* **（u64）**：当前 epoch 中的总 slot 数。
* **（u64 | null）**：当前 epoch 中处理的总交易数。如果数据不可用，可以为。

## 示例

### 1. 获取当前纪元信息（无参数）

此示例使用默认承诺获取有关当前纪元的信息。

<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": "getEpochInfo"
    }'
  ```

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

  async function getCurrentEpochInfo() {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const epochInfo = await connection.getEpochInfo();
      console.log(JSON.stringify(epochInfo, null, 2));
    } catch (error) {
      console.error('Error fetching epoch information:', error);
    }
  }

  getCurrentEpochInfo();
  ```
</CodeGroup>

### 2. 使用“confirmed”承诺获取当前纪元信息

此示例使用`confirmed` 承诺获取纪元信息。

<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": "getEpochInfo",
      "params": [
        {
          "commitment": "confirmed"
        }
      ]
    }'
  ```

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

  async function getConfirmedEpochInfo() {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const epochInfo = await connection.getEpochInfo('confirmed'); // Or connection.getEpochInfo({ commitment: 'confirmed' });
      console.log(JSON.stringify(epochInfo, null, 2));
    } catch (error) {
      console.error('Error fetching confirmed epoch information:', error);
    }
  }

  getConfirmedEpochInfo();
  ```
</CodeGroup>

## 开发者提示

* **承诺级别：** 选择`commitment`可能会影响数据的及时性和最终性。`processed`最快但最不安全，而`finalized`最安全但可能会稍有延迟。了解更多关于[承诺级别](https://www.helius.dev/blog/solana-commitment-levels)的详细信息。
* **`transactionCount` 可用性：** 如果节点没有这些信息或未跟踪特定承诺级别，`transactionCount`字段可能为`null`。
* **纪元长度：** `slotsInEpoch`的数量可能会有所不同。你可以使用`getEpochSchedule`获取有关纪元计划的更多详细信息。

本指南提供了使用`getEpochInfo` RPC方法来检索Solana网络上当前纪元详细信息的必要信息。

## 相关方法

<CardGroup cols={2}>
  <Card title="getEpochSchedule" href="/docs/zh/api-reference/rpc/http/getepochschedule">
    获取纪元计划的详细信息
  </Card>

  <Card title="getSlot" href="/docs/zh/api-reference/rpc/http/getslot">
    获取当前槽号
  </Card>
</CardGroup>
