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

> 从解析事件用于解码指令的目录中，获取 Solana 程序在任意时隙生效的 IDL。

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

“获取 IDL”会返回程序在某个时隙的 IDL：即在 `slot` 或之前生效的最新版本。`slot` 无需与某个版本完全匹配，因此你可以传入历史上的任意时间点。要获取特定版本，请传入其在[列出 IDL 版本](https://www.helius.dev/docs/zh/api-reference/parsed-events/idl-versions.md)中的 `slot`。

该 IDL 来自解析事件和[解析流](https://www.helius.dev/docs/zh/parsed-streams.md)用于解码指令的同一目录。[Orb 浏览器](https://www.helius.dev/docs/zh/orb/explore-programs.md)使用这些端点显示程序 IDL。

## 使用场景

* **使用正确的接口解码历史记录。** 传入交易的时隙，即可获取交易执行时生效的 IDL。程序的指令和账户会随时间变化，因此当前 IDL 可能会错误解析较早的交易。
* **查找用于筛选器的准确名称。** [扫描交易历史记录](https://www.helius.dev/docs/zh/api-reference/parsed-events/scan.md) `match.programs[].instructions` 接受 IDL 中定义的指令名称。
* **审查接口变更。** 获取两个版本并比较其 `instructions`，了解升级添加或移除了哪些内容。请参阅[列出 IDL 版本](https://www.helius.dev/docs/zh/api-reference/parsed-events/idl-versions.md#示例：检测新的-idl-版本)中的示例。

## IDL 格式

响应是按存储形式返回的 IDL 文档。目录中的程序使用两种 Anchor 格式之一，同一程序的不同版本可能同时使用这两种格式。读取前，请先确认收到的格式：

| | 旧版 Anchor（0.30 之前） | Anchor 0.30+ |
| - | - | - |
| 程序名称和版本 | 顶层 `name` 和 `version` | `metadata.name` 和 `metadata.version` |
| 程序地址 | 无顶层 `address` | 顶层 `address` |
| 指令判别器 | 不包含 | `instructions[].discriminator` |
| 指令账户标志 | `isMut`、`isSigner` | `writable`、`signer` |

两种格式都包含 `instructions`。大多数 IDL 还包含 `accounts`、`types` 和 `errors`，许多 IDL 还包含 `events`。

## 积分

每个请求消耗 **10 积分**，所有套餐均可使用。请参阅[积分](https://www.helius.dev/docs/zh/billing/credits.md#解析事件)。

## 错误

| 状态 | 发生条件 |
| - | - |
| `400` | `programId` 不是有效的 base58 地址，或 `slot` 不是无符号 64 位整数。 |
| `401` | API 密钥缺失或无效。响应正文为纯文本：`Unauthorized`。 |
| `404` | 目录中没有该程序的 IDL，或在 `slot` 或之前没有生效的版本。 |

其他错误会返回包含 `error` 消息的 JSON 正文，例如 `{"error": "no IDL found for program KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD at or before slot 1"}`。

## 请求参数

**programId** `string` *required*

程序地址，使用 base58 编码。

**slot** `number` *required*

要查询的时隙。它无需与某个版本的激活时隙匹配。传入“列出 IDL 版本”中的 `slot` 可获取该确切版本；传入交易的时隙可获取该交易执行时生效的 IDL。


## OpenAPI

````yaml zh/openapi/parsed-events.yaml GET /v1/parsed-events/idl/{programId}/{slot}
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}/{slot}:
    get:
      tags:
        - 解析事件
      summary: 获取 IDL
      description: 获取程序在某个时隙生效的 IDL，即在 `slot` 时或之前激活的最新版本。
      operationId: getIdlParsedEvents
      parameters:
        - $ref: '#/components/parameters/ProgramId'
        - name: slot
          in: path
          required: true
          description: >-
            要查询的时隙。它不需要与某个版本的激活时隙一致。传入“列出 IDL 版本”返回的 `slot`
            可获取该确切版本，传入交易的时隙则可获取交易执行时生效的 IDL。
          schema:
            type: integer
            format: uint64
            minimum: 0
            example: 205509752
      responses:
        '200':
          description: >-
            按存储时的原样返回 IDL 文档。可能返回旧版 Anchor 格式或 Anchor 0.30+
            格式，因此请在读取前检查收到的是哪种格式。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Idl'
              examples:
                Legacy Anchor IDL:
                  summary: 时隙 182747476 的 Kamino Lend（精简为一条指令和一个错误）
                  value:
                    name: kamino_lending
                    version: 0.1.0
                    instructions:
                      - name: refreshObligation
                        accounts:
                          - name: lendingMarket
                            isMut: false
                            isSigner: false
                          - name: obligation
                            isMut: true
                            isSigner: false
                        args: []
                    errors:
                      - code: 6000
                        name: InvalidMarketAuthority
                        msg: Market authority is invalid
                Anchor 0.30+ IDL:
                  summary: 时隙 454326107 的 Pump AMM（精简为一条指令和一个错误）
                  value:
                    address: pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA
                    metadata:
                      name: pump_amm
                      version: 0.1.0
                      spec: 0.1.0
                      description: Created with Anchor
                    instructions:
                      - name: toggle_boost
                        discriminator:
                          - 117
                          - 161
                          - 160
                          - 74
                          - 223
                          - 137
                          - 118
                          - 99
                        accounts:
                          - name: admin
                            signer: true
                            relations:
                              - global_config
                          - name: global_config
                            writable: true
                        args:
                          - name: enabled
                            type: bool
                    errors:
                      - code: 6000
                        name: FeeBasisPointsExceedsMaximum
        '400':
          $ref: '#/components/responses/IdlBadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          description: 目录中没有该程序的 IDL，或者在 `slot` 时或之前没有生效的版本。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                default:
                  value:
                    error: >-
                      no IDL found for program
                      KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD at or before
                      slot 1
components:
  parameters:
    ProgramId:
      name: programId
      in: path
      required: true
      description: 程序地址，使用 base58 编码。
      schema:
        type: string
        example: KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD
  schemas:
    Idl:
      type: object
      additionalProperties: true
      description: >-
        按存储时的原样返回 IDL 文档。可能出现两种格式：


        - **旧版 Anchor**（0.30 之前）：顶层包含 `name` 和 `version`。指令账户使用 `isMut` 和
        `isSigner`。

        - **Anchor 0.30+**：顶层包含 `address` 和
        `metadata`（`name`、`version`、`spec`、`description`）。指令包含
        `discriminator`，指令账户使用 `writable` 和 `signer`。


        两种格式都包含 `instructions`。大多数 IDL 还包含 `accounts`、`types` 和 `errors`，许多 IDL
        还包含 `events`。同一程序的不同版本可以使用不同格式。
    ErrorResponse:
      type: object
      description: 请求级错误响应。
      properties:
        error:
          type: string
          description: 错误信息。
  responses:
    IdlBadRequest:
      description: '`programId` 不是有效地址，或者 `slot` 不是无符号 64 位整数。'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            Invalid program ID:
              value:
                error: 'invalid program id: notanaddress: String is the wrong size'
            Invalid slot:
              value:
                error: 'invalid activation slot: abc: invalid digit found in string'
    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.