---
eip: 1046
label: ERC-1046
title: tokenURI Interoperability
status: Final
type: Standards Track
category: ERC
created: 2018-04-13
requires: 20, 721, 1155
official: https://eips.ethereum.org/EIPS/eip-1046
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1046.md
canonical: https://eips.huatuo.xyz/zh/eip-1046/
lang: zh
tldr: 为 ERC-20 代币提供 ERC-721 风格的 tokenURI，指向 JSON 元数据（名称、描述、图片、图标），并新增 interop 字段，使钱包能够区分 ERC-20、ERC-721 和 ERC-1155 的元数据。
tags: [token, metadata, erc20, nft, wallet]
---

# ERC-1046: tokenURI Interoperability

> Extends ERC-20 with an ERC-721-like tokenURI, and extends ERC-721 and ERC-1155 with interoperability

**TL;DR:** 为 ERC-20 代币提供 ERC-721 风格的 tokenURI，指向 JSON 元数据（名称、描述、图片、图标），并新增 interop 字段，使钱包能够区分 ERC-20、ERC-721 和 ERC-1155 的元数据。

## 它解决什么问题？

[ERC-721](/eip-721/) 的 NFT 有 `tokenURI(id)`，指向一个包含名称、描述和图片的 JSON 文件。[ERC-20](/eip-20/) 代币则没有类似的东西。钱包和浏览器最终只能依赖手工维护的代币列表来显示标志或描述，也无法仅凭元数据轻松判断一个合约是哪种代币。

ERC-1046 把同样的元数据模式带给 ERC-20，并在 ERC-20、ERC-721 和 [ERC-1155](/eip-1155/) 的元数据中加入一个小型的共用 `interop` 对象，使一个解析器就能处理这三者。

## 它如何运作

**ERC-20 扩展。** 符合规范的代币增加一个无参数函数：

```solidity
function tokenURI() external view returns (string memory);
```

该 URI 必须解析为带有以下字段的 JSON：

- `interop`（必填）：`{ "erc1046": true }` 表示这是 ERC-20 元数据。
- `name`、`symbol`、`decimals`：可选。如果对应的链上函数存在并返回值，两者必须一致。`decimals` 默认为 18。
- `description`：一段纯文本。
- `image` / `images`：代币的图片。位图宽度应为 320–1080 px。
- `icons`：背景透明的方形图标。

**ERC-721 和 ERC-1155 扩展。** 它们现有的元数据 JSON 应增加 `interop` 字段，值为 `{ "erc721": true }` 或 `{ "erc1155": true }`。ERC-1155 合约也可以公开 `tokenURI(uint256)`，它必须返回与 `uri(id)` 相同的 URI。

为节省 gas，规范建议新代币省略链上的 `name()`、`symbol()` 和 `decimals()`，只在元数据中保留这些值。规范还建议当 `decimals` 为 18 时，在 JSON 中省略它。

## 示例代码

一个 ERC-20 代币的元数据文件：

```json
{
  "interop": { "erc1046": true },
  "name": "Example Token",
  "symbol": "EXT",
  "description": "A sample token used to illustrate ERC-1046 metadata.",
  "image": "ipfs://<cid>/banner.png",
  "icons": ["ipfs://<cid>/icon-512.png"]
}
```

用 ethers v6 读取它：

```ts
import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const token = new Contract(tokenAddress, ["function tokenURI() view returns (string)"], provider);

const uri: string = await token.tokenURI();
const url = uri.startsWith("ipfs://") ? `https://ipfs.io/ipfs/${uri.slice(7)}` : uri;
const meta = await (await fetch(url)).json();
if (meta.interop?.erc1046) console.log(meta.name, meta.icons?.[0]);
```

## 需要注意

- 该扩展是可选的，并且完全向后兼容。现有的 ERC-20 代币不受影响，许多工具目前也还不会查找 `tokenURI()`。
- 省略 `name()`/`symbol()`/`decimals()` 可以节省 gas，但只读取这些函数的工具就什么也显示不出来。遵循这一建议之前请权衡。
- **安全：** 从合约获取任意 URI 存在服务端请求伪造风险。钱包应只允许特定的 URI 协议和端口。
- 规范选择了 JSON 的 `interop` 字段，而不是 [ERC-165](/eip-165/) 检查，这样客户端仅凭元数据就能得知代币类型，无需额外调用。


---

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