EIPs Explained

ERC-6047 · Stagnant(停滞) · ERC · 2022-11-26

ERC-6047: ERC-721 Balance indexing via Transfer event

通过要求 NFT 合约即使对在构造函数内铸造的代币也触发 Transfer 事件,堵住 ERC-721 的一个漏洞,使索引器仅凭日志就能跟踪每一个代币。

它解决什么问题?

ERC-721 规定每次铸造(来自 0x0)、转让和销毁(转到 0x0)都必须触发 Transfer 事件,合约创建期间除外。因此合约可以在构造函数中铸造代币而不记录日志。

索引器、市场和钱包通常通过重放 Transfer 日志来重建谁拥有什么。部署时悄悄铸造的代币对它们不可见,除非它们增加特殊处理逻辑。ERC-1155 没有这样的例外,因此它的供应量更容易索引。

ERC-6047 为选择采用的合约取消了这一例外。

它如何运作

规范只有两条规则:

  1. 合规的合约必须实现 ERC-721。
  2. 每当代币被转让、铸造或销毁时,必须触发 Transfer,包括合约创建期间。

没有引入新事件。复用现有的 Transfer(address indexed from, address indexed to, uint256 indexed tokenId),意味着现有索引器无需改动即可工作。

每个 ERC-6047 合约都是有效的 ERC-721 合约;反之则不然。

示例代码

在构造函数中铸造,同时仍为每个代币记录日志:

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

contract GenesisNFT {
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    mapping(uint256 => address) public ownerOf;
    mapping(address => uint256) public balanceOf;

    constructor(address[] memory recipients) {
        for (uint256 id = 0; id < recipients.length; id++) {
            ownerOf[id] = recipients[id];
            balanceOf[recipients[id]] += 1;
            emit Transfer(address(0), recipients[id], id); // required by ERC-6047
        }
    }
    // ... rest of ERC-721 omitted
}

仅凭日志重建所有权(ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const nft = new Contract(process.env.NFT!, [
  "event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",
], provider);

const owners = new Map<bigint, string>();
for (const log of await nft.queryFilter(nft.filters.Transfer(), Number(process.env.DEPLOY_BLOCK))) {
  const { to, tokenId } = (log as any).args;
  owners.set(tokenId, to);
}

需要注意

  • 状态为 Stagnant:它不是被采纳的要求,链上也没有任何标记表明某个合约合规。
  • 流行的库已经在其内部的 _mint 中触发 Transfer,因此使用它们在构造函数中铸造通常本来就会产生日志。这个缺口主要影响手写的或经过 gas 优化、直接写存储的合约。
  • 索引器仍应从部署区块(包括创建交易)开始扫描,以捕获构造函数中的铸造。
  • 在构造函数中为每个代币触发一个事件会消耗 gas;规模非常大的创世铸造可能触及区块 gas 上限。

#nft #erc721 #events #indexing

详细信息

作者
Zainan Victor Zhou (@xinbenlv)
状态
Stagnant(停滞)
类型
Standards Track / ERC
创建日期
2022-11-26
依赖的提案

机器可读数据: Markdown · JSON