ERC-5633 · Stagnant(停滞) · ERC · 2022-09-09
ERC-5633: Composable Soulbound NFT, EIP-1155 Extension
一种 ERC-1155 扩展,可将单个代币 ID 标记为灵魂绑定(不可转让),使同一个合约可以同时包含可交易物品和绑定物品。
它解决什么问题?
游戏和社区项目常常希望混合多种物品:有些可以自由交易,有些则“绑定”到获得它们的账户,就像魔兽世界(World of Warcraft)里的灵魂绑定物品。像 ERC-5192 这样的单代币灵魂绑定标准面向 ERC-721,其中每个代币都是独立的 NFT。而在 ERC-1155 中,一个合约管理多种代币类型,因此需要按类型设置的开关。
ERC-5633 增加了这个开关:每个代币 ID 可以是灵魂绑定的,也可以不是,钱包在尝试转移之前可以先检查。
它如何运作
该扩展为 ERC-1155 合约添加一个 view 函数和一个事件:
- 如果代币类型
id是灵魂绑定的,isSoulbound(uint256 id)返回true。 - 当某代币类型被设为(或解除)灵魂绑定时,触发
Soulbound(uint256 indexed id, bool bounded)。
规则很简单:如果 isSoulbound(id) 为 true,那么所有把该代币从一个账户转到另一个账户的 ERC-1155 函数都必须 revert。铸造(来自零地址)和销毁(转到零地址)仍然允许,因此发行方可以授予和移除绑定物品。
合约必须实现 ERC-165,并对接口 ID 0x911ec470 返回 true,这样钱包或市场就能检测支持情况,并把绑定 ID 的“转账”按钮置灰。
如果一个集合中的所有代币都要绑定,规范建议 isSoulbound 默认直接返回 true。
示例代码
在 OpenZeppelin v5 的 ERC-1155 中通过 _update 钩子强制执行该规则:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
interface IERC5633 {
event Soulbound(uint256 indexed id, bool bounded);
function isSoulbound(uint256 id) external view returns (bool);
}
contract GameItems is ERC1155, IERC5633 {
mapping(uint256 => bool) private _bound;
constructor() ERC1155("https://example.com/items/{id}.json") {}
function isSoulbound(uint256 id) public view returns (bool) {
return _bound[id];
}
function _setSoulbound(uint256 id, bool bounded) internal {
_bound[id] = bounded;
emit Soulbound(id, bounded);
}
function _update(address from, address to, uint256[] memory ids, uint256[] memory values)
internal override
{
if (from != address(0) && to != address(0)) {
for (uint256 i; i < ids.length; ++i) {
require(!_bound[ids[i]], "ERC5633: soulbound");
}
}
super._update(from, to, ids, values);
}
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(IERC5633).interfaceId || super.supportsInterface(interfaceId);
}
}
(为简洁起见,省略了铸造和 _setSoulbound 的访问控制。)