---
eip: 7572
label: ERC-7572
title: Contract-level metadata via `contractURI()`
status: Draft
type: Standards Track
category: ERC
created: 2023-12-06
official: https://eips.ethereum.org/EIPS/eip-7572
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7572.md
canonical: https://eips.huatuo.xyz/zh/eip-7572/
lang: zh
tldr: 把长期使用的 contractURI() 函数和 ContractURIUpdated 事件标准化，让 dApp 和索引器可以通过一份 JSON 文档展示合约的名称、描述、图片和链接。
tags: [metadata, nft, marketplaces, json]
---

# ERC-7572: Contract-level metadata via `contractURI()`

> Specifying and updating contract-level metadata

**TL;DR:** 把长期使用的 contractURI() 函数和 ContractURIUpdated 事件标准化，让 dApp 和索引器可以通过一份 JSON 文档展示合约的名称、描述、图片和链接。

## 它解决什么问题？

代币级别的元数据已经有了归处：[ERC-721](/eip-721/) 的 `tokenURI` 和 [ERC-1155](/eip-1155/) 的 `uri`。但一个合集或代币合约作为整体也需要一份简介：显示名称、描述、logo、横幅和网站。市场多年来一直在读取 `contractURI()` 函数，但没有任何 ERC 定义它，也没有可靠的方式通知索引器数据已经变化。

ERC-7572 把这个事实上的函数、它返回内容的 JSON schema，以及用于通知更新的事件写成了标准。

## 它如何运作

合约实现：

```solidity
interface IERC7572 {
    function contractURI() external view returns (string memory);
    event ContractURIUpdated();
}
```

- `contractURI()` 返回指向链下文档的链接（`ipfs://`、`https://` 等），或以 `data:application/json;utf8,{...}` 字符串形式内联的 JSON。
- JSON 必须是至少包含 `name` 的对象。可选字段有：`symbol`、`description`、`image`（头像）、`banner_image`、`featured_image`、`external_link` 以及 `collaborators`（允许在 dApp 上编辑合约信息的地址）。
- 每当元数据变化时触发 `ContractURIUpdated()`，让索引器知道需要重新获取。
- 如果合约同时有 `name()` 或 `symbol()`，建议以 `contractURI` 中的值为准，这让创作者无需重新部署就能给合集改名。

URI 如何设置由合约自行决定；通常是一个仅限所有者调用的简单 setter。

## 示例代码

一个元数据可更新的合集：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MyCollection is ERC721, Ownable {
    event ContractURIUpdated();

    string private _contractURI;

    constructor(string memory uri_) ERC721("My Collection", "MYC") Ownable(msg.sender) {
        _contractURI = uri_;
    }

    function contractURI() external view returns (string memory) {
        return _contractURI;
    }

    function setContractURI(string calldata newURI) external onlyOwner {
        _contractURI = newURI;
        emit ContractURIUpdated();
    }
}
```

它所指向的 JSON：

```json
{
  "name": "My Collection",
  "symbol": "MYC",
  "description": "Hand-drawn landscapes, one per day.",
  "image": "ipfs://<cid>/logo.png",
  "banner_image": "ipfs://<cid>/banner.png",
  "external_link": "https://example.com",
  "collaborators": []
}
```

## 需要注意

- **状态：Draft**，尽管许多市场已经在读取 `contractURI()`。schema 的细节仍可能变化。
- 列在 `collaborators` 中的地址，可能在遵循该标准的 dApp 上被授予管理员级别的编辑权限；只列出你信任的地址。
- 该函数不限于 NFT；任何合约（包括同质化代币）都可以提供它。
- 未来的 ERC 可能会为 schema 增加更多字段。


---

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