---
eip: 2069
label: EIP-2069
title: Recommendation for using YAML ABI in ERCs/EIPs
status: Stagnant
type: Informational
created: 2017-02-11
official: https://eips.ethereum.org/EIPS/eip-2069
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2069.md
canonical: https://eips.huatuo.xyz/zh/eip-2069/
lang: zh
tldr: 一项信息性（Informational）建议：EIP 和 ERC 应使用 YAML 格式的 ABI 而不是 Solidity 代码来描述合约接口，YAML 与 JSON 一样不依赖具体语言，但允许写注释。
tags: [abi, yaml, specification, process, informational]
---

# EIP-2069: Recommendation for using YAML ABI in ERCs/EIPs

> Recommendation for including contract ABI descriptions in EIPs and ERCs as YAML.

**TL;DR:** 一项信息性（Informational）建议：EIP 和 ERC 应使用 YAML 格式的 ABI 而不是 Solidity 代码来描述合约接口，YAML 与 JSON 一样不依赖具体语言，但允许写注释。

## 它解决什么问题？

大多数接口标准（如 [ERC-20](/eip-20/)）都用 Solidity 的 `interface` 来呈现 API。作者指出了三个缺点：

- 偏向某一种语言，可能让其他语言（Vyper、Fe、手写字节码、链下工具）处于劣势。
- 把规范绑定到某个 Solidity 版本和语法上，而它们会随时间变化。
- Solidity 能表达一些无法干净地映射到 ABI 的东西，让其他语言进一步处于劣势。

合约接口的中立描述是 **Contract ABI**，通常写成 JSON。但 JSON 不能包含注释，而规范需要在每个函数旁边写说明。

## 它如何运作

建议很简单：用 **YAML** 编写 ABI。YAML 是 JSON 的超集，因此内容就是编译器和库已经能理解的同一份 ABI，两者之间的转换是机械的。与 JSON 不同，YAML 允许使用 `#` 注释，作者可以在行内为每个函数写文档。

鼓励作者为 YAML ABI 添加注释。允许的字段和取值与 Solidity Contract ABI 规范完全一致；本 EIP 并没有发明新的描述语言。规范提到了 `yamabi`，这是一个在 YAML 和 JSON ABI 之间转换的 JavaScript 工具。

## 示例代码

规范中的示例，用 YAML 写的 `transfer` 函数：

```yaml
# The transfer function. Takes the recipient address
# as an input and returns a boolean signaling the result.
- name: transfer
  type: function
  stateMutability: nonpayable
  inputs:
  - name: recipient
    type: address
  - name: amount
    type: uint256
  outputs:
  - name: ''
    type: bool
```

加载到 ethers v6（任何 YAML 解析器都可以；结果是普通的 JSON ABI）：

```ts
import { readFileSync } from "node:fs";
import { parse } from "yaml";
import { Interface } from "ethers";

const abi = parse(readFileSync("transfer.abi.yaml", "utf8"));
const iface = new Interface(abi);
console.log(iface.getFunction("transfer")!.selector); // "0xa9059cbb"
```

## 需要注意

- 状态为 **Stagnant**。这是信息性建议，不是强制要求，实际上大多数 ERC 仍然用 Solidity 呈现接口。
- 规范中的 YAML 示例还包含旧版的 `payable` 和 `constant` 字段；当前的 ABI 输出只使用 `stateMutability`，所以上面省略了它们。
- 把 YAML 转成 JSON 时注释会丢失，因此请把 YAML 文件作为带文档的权威来源保留。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2069
