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

# 列出 IDL 版本

> 列出 Parsed Events 为某个 Solana 程序提供的所有 IDL 版本，以及每个版本开始生效的槽位。

此页面上的路径参数、响应模式和示例均根据 OpenAPI 定义生成。

列出 IDL 版本会从 Parsed Events 和 [Parsed Streams](https://www.helius.dev/docs/zh/parsed-streams.md) 用于解码指令的 IDL 目录中，返回程序接口的历史记录。每一项代表一个版本，并通过其开始生效的槽位进行标识。各项按从旧到新的顺序排列，因此最后一项是当前版本。

将响应中的任意 `slot` 传递给[获取 IDL](https://www.helius.dev/docs/zh/api-reference/parsed-events/idl.md)，即可获取该版本的 IDL 文档。[Orb 浏览器](https://www.helius.dev/docs/zh/orb/explore-programs.md)使用这些端点显示程序 IDL。

## 使用场景

* **跟踪接口变更。** 定期轮询你所依赖程序的“列出 IDL 版本”。出现新版本时，使用“获取 IDL”获取新版本和上一版本，并比较它们的 `instructions`。新增管理员或权限指令时，值得进行安全审查。
* **检查目录覆盖范围。** `404` 表示目录中没有该程序的 IDL。SPL Token 等程序改由内置解码器解码，因此也会返回 `404`。

## 示例：检测新的 IDL 版本

此脚本会列出程序的 IDL 版本，获取最新的两个版本，并打印新增或移除的指令：

```javascript diff-idl-versions.js theme={"system"}
const API_KEY = process.env.HELIUS_API_KEY;

if (!API_KEY) {
  console.error("Missing HELIUS_API_KEY.");
  process.exit(1);
}

const BASE = "https://mainnet.helius-rpc.com/v1/parsed-events/idl";
const PROGRAM_ID = "KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD";

async function get(path) {
  const response = await fetch(`${BASE}/${path}?api-key=${API_KEY}`);
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}: ${await response.text()}`);
  }
  return response.json();
}

const versions = await get(PROGRAM_ID);
if (versions.length < 2) {
  console.log("Fewer than two IDL versions; nothing to compare.");
  process.exit(0);
}

const [previous, latest] = versions.slice(-2);
const [before, after] = await Promise.all([
  get(`${PROGRAM_ID}/${previous.slot}`),
  get(`${PROGRAM_ID}/${latest.slot}`),
]);

const names = (idl) => new Set(idl.instructions.map((ix) => ix.name));
const beforeNames = names(before);
const afterNames = names(after);

console.log(`Comparing slot ${previous.slot} with slot ${latest.slot}`);
console.log("Added:", [...afterNames].filter((name) => !beforeNames.has(name)));
console.log("Removed:", [...beforeNames].filter((name) => !afterNames.has(name)));
```

要检测随时间发生的变更，请存储你上次看到的版本所对应的 `slot`，并在每次轮询时与其进行比较。

## 积分

每个请求消耗 **10 积分**，所有套餐均可使用。上面的示例会发出三个请求，因此消耗 30 积分。请参阅[积分](https://www.helius.dev/docs/zh/billing/credits.md#解析事件)。

## 错误

| 状态 | 发生条件 |
| - | - |
| `400` | `programId` 不是有效的 base58 地址。 |
| `401` | API 密钥缺失或无效。响应正文为纯文本：`Unauthorized`。 |
| `404` | 目录中没有该程序的 IDL。 |

其他错误会返回包含 `error` 消息的 JSON 正文，例如 `{"error": "no IDLs found for program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"}`。

## 请求参数

**programId** `string` *required*

程序地址，使用 base58 编码。


## OpenAPI

````yaml zh/openapi/parsed-events.yaml GET /v1/parsed-events/idl/{programId}
openapi: 3.0.3
info:
  title: 解析事件
  version: 0.1.0
  description: 使用解析事件解析Solana交易和解析交易历史。
servers:
  - url: https://mainnet.helius-rpc.com
security:
  - ApiKeyQuery: []
paths:
  /v1/parsed-events/idl/{programId}:
    get:
      tags:
        - 解析事件
      summary: 列出 IDL 版本
      description: 列出解析事件中某个程序的所有 IDL 版本，按从旧到新的顺序排列。每个版本由其生效时的时隙标识。
      operationId: listIdlVersionsParsedEvents
      parameters:
        - $ref: '#/components/parameters/ProgramId'
      responses:
        '200':
          description: 程序的 IDL 版本，按 `slot` 升序排列。
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/IdlVersion'
              examples:
                default:
                  summary: Kamino Lend（43 个版本中的前 3 个）
                  value:
                    - slot: 182747476
                      sources:
                        - anchor
                    - slot: 205509752
                      sources:
                        - anchor
                    - slot: 206074074
                      sources:
                        - anchor
        '400':
          description: '`programId` 不是有效地址。'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                default:
                  value:
                    error: 'invalid program id: notanaddress: String is the wrong size'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          description: 目录中没有该程序的 IDL。解析事件使用内置解码器解码的程序（例如 SPL Token）会返回 `404`。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                default:
                  value:
                    error: >-
                      no IDLs found for program
                      TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
components:
  parameters:
    ProgramId:
      name: programId
      in: path
      required: true
      description: 程序地址，使用 base58 编码。
      schema:
        type: string
        example: KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD
  schemas:
    IdlVersion:
      type: object
      description: 程序的一个 IDL 版本。
      required:
        - slot
        - sources
      properties:
        slot:
          type: integer
          format: uint64
          description: 此 IDL 版本生效时的时隙。将其传给“获取 IDL”即可获取此版本。
        sources:
          type: array
          description: >-
            解析事件找到此版本的位置。值包括 `anchor`、`program_metadata` 和
            `github`，未来可能会添加更多值。一个版本可以有多个来源。
          items:
            type: string
    ErrorResponse:
      type: object
      description: 请求级错误响应。
      properties:
        error:
          type: string
          description: 错误信息。
  responses:
    Unauthorized:
      description: API 密钥缺失或无效。响应正文为纯文本。
      content:
        text/plain:
          schema:
            type: string
          examples:
            default:
              value: Unauthorized
  securitySchemes:
    ApiKeyQuery:
      type: apiKey
      in: query
      name: api-key

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.