EIPs Explained

ERC-2309 · Final(最终) · ERC · 2019-10-08

ERC-2309: ERC-721 Consecutive Transfer Extension

一个可选的 ERC-721 事件 ConsecutiveTransfer,用一条日志报告整段 token ID 的铸造、转移或销毁,使超大规模的 NFT 批量操作不必为每个代币发出一个 Transfer 事件。

它解决什么问题?

ERC-721 规定,任何 NFT 的所有权发生变化时都必须发出 Transfer 事件。索引器、市场和区块浏览器都依赖这些事件来判断谁拥有什么。

这条规则让大批量操作变得昂贵。合约可以借助巧妙的数据结构在一笔交易中铸造一百万个代币,但在循环中发出一百万个 Transfer 事件会耗尽 gas。ERC-2309 为这类合约提供了一个标准事件,一次性描述一整段连续的 token ID。

它如何运作

ERC-2309 只定义了一个事件,别无其他。它不规定如何实现批量铸造或转移,只规定如何对外宣告:

event ConsecutiveTransfer(
    uint256 indexed fromTokenId,
    uint256 toTokenId,
    address indexed fromAddress,
    address indexed toAddress
);

规则:

  • fromTokenId 到 toTokenId 必须是一段连续的 ID。
  • 铸造使用 fromAddress == address(0);销毁使用 toAddress == address(0)。
  • fromTokenId、fromAddress 和 toAddress 是 indexed;toTokenId 不是,它位于日志数据中。
  • 调用者必须拥有该范围内的每个代币,或者是获批的操作者。
  • 为一组代币发出 ConsecutiveTransfer 时,不得再为它们发出 Transfer。合约仍可对普通的单个代币转移使用 Transfer。

该事件也适用于单个代币:ConsecutiveTransfer(1, 1, address(0), alice) 铸造代币 1。

示例代码

批量铸造时发出该事件(为便于说明,簿记逻辑做了简化):

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

contract BatchNFT {
    event ConsecutiveTransfer(
        uint256 indexed fromTokenId,
        uint256 toTokenId,
        address indexed fromAddress,
        address indexed toAddress
    );

    uint256 public nextId = 1;
    // A real contract stores ownership compactly, e.g. one entry per batch.
    mapping(uint256 => address) internal batchOwner; // keyed by first id of batch

    function mintBatch(address to, uint256 quantity) external {
        require(quantity > 0, "qty");
        uint256 first = nextId;
        uint256 last = first + quantity - 1;
        batchOwner[first] = to;
        nextId = last + 1;
        emit ConsecutiveTransfer(first, last, address(0), to); // one log for the whole range
    }
}

在索引器中监听(ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const nft = new Contract(nftAddress, [
  "event ConsecutiveTransfer(uint256 indexed fromTokenId, uint256 toTokenId, address indexed fromAddress, address indexed toAddress)",
], provider);

nft.on("ConsecutiveTransfer", (fromId, toId, from, to) => {
  console.log(`tokens ${fromId}..${toId}: ${from} -> ${to}`);
});

需要注意

  • 状态为 Final,但支持是可选的。只监听 Transfer 的索引器会漏掉通过 ConsecutiveTransfer 报告的所有权变更,因此工具应同时监听两者。
  • OpenZeppelin 的 ERC721Consecutive 扩展在构造期间使用该事件进行批量铸造。
  • 规范将它与 ERC-1155 作了对比:ERC-1155 适合批量处理半同质化物品,但仍会按 ID 记录日志数据;ERC-2309 面向的是铸造海量唯一代币。
  • 检测合约是否为 NFT 仍通过 ERC-165 的 supportsInterface 完成;该事件本身没有接口 ID。

#nft #erc721 #events #batch #minting

详细信息

作者
Sean Papanikolas (@pizzarob)
状态
Final(最终)
类型
Standards Track / ERC
创建日期
2019-10-08
依赖的提案

机器可读数据: Markdown · JSON