EIPs Explained

ERC-7572 · Draft(草案) · ERC · 2023-12-06

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

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

它解决什么问题?

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

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

它如何运作

合约实现:

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。

示例代码

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

// 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:

{
  "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 增加更多字段。

#metadata #nft #marketplaces #json

详细信息

作者
Devin Finzer (@dfinzer), Alex Atallah (@alexanderatallah), Ryan Ghods (@ryanio)
状态
Draft(草案)
类型
Standards Track / ERC
创建日期
2023-12-06
被以下提案依赖

机器可读数据: Markdown · JSON