ERC-4400 · Final(最终) · ERC · 2021-10-30
ERC-4400: EIP-721 Consumable Extension
为 ERC-721 NFT 增加一个标准的"consumer"角色:该地址可以使用代币的功能,但不能转移或授权代币,并且每次转移时自动重置。
它解决什么问题?
NFT 往往有除所有权之外的用途:在元宇宙土地上部署场景、穿戴游戏内物品、进入活动现场。所有者常常希望让别人享受这些功能,而不必交出代币本身。许多项目为此各自发明了角色(operator、contributor、user……),名称和语义各不相同,因此市场和工具无法通用地支持它们。
ERC-4400 定义了一个最小化的标准角色,即 consumer,让租赁市场、质押合约和 dApp 都能以同样的方式读取和设置它。例如,质押合约可以持有你的 NFT,而你仍然是它的 consumer,可以继续在游戏中使用。
它如何运作
该扩展增加一个读取函数、一个修改函数和一个事件:
| 成员 | 作用 |
|---|---|
consumerOf(tokenId) |
当前 consumer,没有则为 address(0) |
changeConsumer(consumer, tokenId) |
设置或清除 consumer;所有者、被授权地址或 operator 均可调用 |
ConsumerChanged(owner, consumer, tokenId) |
每次变更时触发 |
规则:
- consumer 不得被视为所有者、operator 或被授权地址。它永远不能转移或授权代币。
- 每次转移时,consumer 都会重置为默认值,建议为
address(0)。因此Transfer事件也意味着 consumer 已被清除。 - consumer 实际上能做什么,由各个应用自行决定。
supportsInterface(0x953c8dfa)必须返回true(ERC-165)。
示例代码
基于 OpenZeppelin v5 ERC721 的最小实现:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
interface IERC721Consumable {
event ConsumerChanged(address indexed owner, address indexed consumer, uint256 indexed tokenId);
function consumerOf(uint256 tokenId) external view returns (address);
function changeConsumer(address consumer, uint256 tokenId) external;
}
contract ConsumableNFT is ERC721, IERC721Consumable {
mapping(uint256 => address) private _consumers;
constructor() ERC721("Land", "LAND") {}
function consumerOf(uint256 tokenId) external view returns (address) {
_requireOwned(tokenId);
return _consumers[tokenId];
}
function changeConsumer(address consumer, uint256 tokenId) external {
address owner = _requireOwned(tokenId);
require(_isAuthorized(owner, msg.sender, tokenId), "not authorised");
_consumers[tokenId] = consumer;
emit ConsumerChanged(owner, consumer, tokenId);
}
function _update(address to, uint256 tokenId, address auth) internal override returns (address from) {
from = super._update(to, tokenId, auth);
_consumers[tokenId] = address(0); // reset on every transfer
}
function supportsInterface(bytes4 id) public view override returns (bool) {
return id == 0x953c8dfa || super.supportsInterface(id);
}
}