EIPs Explained

ERC-5007 · Final(最终) · ERC · 2022-04-13

ERC-5007: Time NFT, ERC-721 Time Extension

为 ERC-721 代币增加 startTime 和 endTime,使 NFT 的有效期窗口(门票、通行证或租赁时段)可以在链上读取并自动生效,另有可选的拆分与合并功能。

它解决什么问题?

许多 NFT 只在一段时间内有意义:季票、演唱会门票、一周的虚拟地产使用权。如果代币里没有时间数据,就得有人发送一笔交易,在合适的时刻把代币打开或关闭,既麻烦又耗 gas。有些项目确实添加了时间字段,但各自使用自己的接口,因此市场和工具无法通用地支持它们。

ERC-5007 标准化了两个读取函数,让任何合约或应用都能判断代币何时有效,无需交易来"激活"或"使其过期"。

它如何运作

核心接口(ERC-165 ID 0xf140be0d):

函数 返回值
startTime(tokenId) 有效期开始时间,UNIX 时间戳(uint64)
endTime(tokenId) 有效期结束时间,UNIX 时间戳(uint64)

对于不存在的代币,两者都必须回滚。时间使用 uint64,是因为它在主流语言中都得到原生支持,并且可以持续数千亿年。

一个可选的可组合扩展(ID 0x75cf3842)把时间段视为可分割的:

  • assetId(tokenId):底层资产;只有资产相同的代币才能合并。
  • split(oldTokenId, id1, owner1, id2, owner2, splitTime):在 splitTime(必须位于旧代币的时间窗口内)把一个时间段切成两个。两个新代币保持相同的 assetId。
  • merge(firstId, secondId, newOwner, newId):合并两个相邻的时间段,要求 first.endTime + 1 == second.startTime。

这样就可以做到,例如,出售一年期通行证的后半段。

示例代码

一个最小的时间型 NFT,以及一个 consumer 侧的检查(OpenZeppelin v5):

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

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

interface IERC5007 {
    function startTime(uint256 tokenId) external view returns (uint64);
    function endTime(uint256 tokenId) external view returns (uint64);
}

contract SeasonPass is ERC721, IERC5007 {
    struct Window { uint64 start; uint64 end; }
    mapping(uint256 => Window) private _windows;

    constructor() ERC721("Season Pass", "PASS") {}

    function _mintPass(address to, uint256 id, uint64 start, uint64 end) internal {
        require(start < end, "bad window");
        _windows[id] = Window(start, end);
        _safeMint(to, id);
    }

    function startTime(uint256 id) external view returns (uint64) {
        _requireOwned(id);
        return _windows[id].start;
    }

    function endTime(uint256 id) external view returns (uint64) {
        _requireOwned(id);
        return _windows[id].end;
    }

    function isActive(uint256 id) external view returns (bool) {
        Window memory w = _windows[id];
        return block.timestamp >= w.start && block.timestamp <= w.end;
    }

    function supportsInterface(bytes4 iid) public view override returns (bool) {
        return iid == 0xf140be0d || super.supportsInterface(iid);
    }
}

需要注意

  • 状态:Final。
  • 代币在其时间窗口之外依然存在,也可以正常转移。过期是供应用遵守的信号,而不是销毁。
  • merge 的相邻规则(end + 1 == start)意味着时间窗口包含两个端点,正如上面 isActive 的检查所示。
  • 对于所有者在一段时间内出借使用权的租赁,ERC-4907(ERC-721)和 ERC-5006(ERC-1155)定义了一个带有效期的独立 user 角色;ERC-5007 则把时间放在代币本身上。

#nft #erc721 #time #tickets #rental

详细信息

作者
Anders (@0xanders), Lance (@LanceSnow), Shrug <[email protected]>
状态
Final(最终)
类型
Standards Track / ERC
创建日期
2022-04-13
依赖的提案

机器可读数据: Markdown · JSON