> For the complete documentation index, see [llms.txt](https://docs.termina.technology/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.termina.technology/documentation/developer-docs-zh/ji-shu-she-zhi-zhi-nan/api-can-kao.md).

# API 参考

架构

* **历史状态存档**
  * 账户、区块和增量数据以专有格式存储，支持快速的确定性查询。
* **可用范围**
  * 创建会话之前，请确认目标 slot 范围可用。
  * `curl https://<host>/available-ranges | jq`&#x20;
* **会话控制通道**
  * 连接此通道以创建和驱动回测会话。该通道上的所有消息均使用自定义 JSON 协议。
  * 端点： `ws(s)://<host>/backtest`
  * 方法： `createBacktestSession`, `attachBacktestSession`, `continue`, `continueTo`, `closeBacktestSession`
  * 响应： `sessionCreated`, `sessionAttached`, `readyForContinue`, `slotNotification`, `paused`, `discoveryBatch`, `status`（变体见下文）, `success`, `completed`, `error`&#x20;
* **带序号的响应**

  * 当 `disconnectTimeoutSecs > 0` 时，响应会附带序列 ID，以便客户端在重新连接后恢复会话。
  * `seqId` 是单调递增的序列号。客户端在调用 `attachBacktestSession` 时可传入最后收到的序号，从而恢复事件流，无需重放之前的消息。

  ```json
  {
    "seqId": 123,
    "method": "status",
    "params": {
      "status": "decodedTransactions"
    }
  }
  ```
* **发现过滤器**
  * 在创建会话时注册过滤器，以监控特定事件（例如某个程序被执行）。当即将执行的批次匹配时，服务器会发送 `discoveryBatch`，其中包含目标 slot 和批次索引。
  * 将这些值传给 `continueTo`，即可在该批次执行前立即暂停。
* **单会话 RPC 通道**
  * 会话创建后，即可与模拟的 Solana 环境交互。大多数方法遵循标准的 Solana JSON-RPC 接口。
  * 端点： `http(s)://` 或 `ws(s)://`，路径为 `/backtest/{session_id}`
  * 账户状态： `getAccountInfo`, `getBalance`, `getMultipleAccounts`, `getProgramAccounts`
  * 链状态： `getLatestBlockhash`, `getFeeForMessage`, `getSlot`&#x20;
  * 交易： `simulateTransaction`, `sendTransaction`, `getTransaction`
  * 订阅： `accountSubscribe`, `slotSubscribe`, `transactionSubscribe`

> 注意事项：
>
> * &#x20;`/backtest` 在生产环境中通过 API 密钥认证。`/backtest/{session_id}` 无需认证，用于单个会话的访问。
> * `ReadyToExecuteUserTransactions` 是发送或执行用户交易前通常需要等待的主要状态。
> * 如果客户端脚本采用严格解析，请确保其能够处理上述状态变体。

#### API 表

| 方法                      | 用途                                                                                                                         | 接口            |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `createBacktestSession` | <p>启动回测会话，并获取 <code>sessionId</code> + <code>rpcEndpoint</code>。<br>使用 <code>parallel: true</code> 可在所有可用 bundle 上创建会话</p> | 会话控制（WS）      |
| `continue`              | 执行一个批次并推进 slot；支持 `transactions` + `modifyAccountStates`。                                                                  | 会话控制（WS）      |
| `closeBacktestSession`  | 关闭并清理会话。                                                                                                                   | 会话控制（WS）      |
| `attachBacktestSession` | 重新连接到现有会话，并获取 rpcEndpoint。                                                                                                 | 会话控制（WS）      |
| `continueTo`            | 根据已注册的过滤器，在特定 slot 和批次索引之前或之后立即暂停。过滤器注册于 `discoveries`                                                                     | 会话控制（WS）      |
| `getAccountInfo`        | 获取当前 slot 下的单个账户。                                                                                                          | 单会话 RPC（HTTP） |
| `getBalance`            | 获取账户的 lamport 余额。                                                                                                          | 单会话 RPC（HTTP） |
| `getMultipleAccounts`   | 一次调用获取多个账户。                                                                                                                | 单会话 RPC（HTTP） |
| `getProgramAccounts`    | 获取某个程序拥有的账户。                                                                                                               | 单会话 RPC（HTTP） |
| `getLatestBlockhash`    | 获取当前 slot 的 blockhash。                                                                                                     | 单会话 RPC（HTTP） |
| `getFeeForMessage`      | 获取消息的手续费估算。                                                                                                                | 单会话 RPC（HTTP） |
| `getSignatureStatuses`  | 获取签名的状态。                                                                                                                   | 单会话 RPC（HTTP） |
| `getTransaction`        | 获取交易详情（包括日志）。                                                                                                              | 单会话 RPC（HTTP） |
| `simulateTransaction`   | 模拟交易，不提交状态变更。                                                                                                              | 单会话 RPC（HTTP） |
| `sendTransaction`       | 在当前 slot 中立即执行交易。                                                                                                          | 单会话 RPC（HTTP） |
| `getSlot`               | 获取当前 slot。                                                                                                                 | 单会话 RPC（HTTP） |
| `getAddressLookupTable` | 获取 ALT（地址查找表）账户。                                                                                                           | 单会话 RPC（HTTP） |
| `modifyAccounts`        | 通过 RPC 应用账户覆盖。                                                                                                             | 单会话 RPC（HTTP） |
| `accountSubscribe`      | 实时推送账户变化。                                                                                                                  | 单会话订阅（WS）     |
| `programSubscribe`      | 实时推送程序拥有的账户变化。                                                                                                             | 单会话订阅（WS）     |
| `signatureSubscribe`    | 实时推送签名状态更新。                                                                                                                | 单会话订阅（WS）     |
| `slotSubscribe`         | 实时推送 slot 更新。                                                                                                              | 单会话订阅（WS）     |
| `logsSubscribe`         | 实时推送日志更新。                                                                                                                  | 单会话订阅（WS）     |
