ERC-5700 · Draft(草案) · ERC · 2022-09-22
ERC-5700: Bindable Token Interface
一个草案接口,让 ERC-721 或 ERC-1155 代币可以"绑定"到某个 NFT 上,从而在该 NFT 转移时随之转移,直到其所有者解除绑定。
它解决什么问题?
有时几枚代币是属于一起的:一件实体与数字结合(phygital)的服装 NFT,加上随附的音乐、头像和数字孪生;或者一块虚拟土地及其装饰。移动这样一个组合通常需要逐个转移每枚代币,而链上也没有任何记录说明它们属于一起。
ERC-5700 定义了"可绑定"代币。可绑定的 ERC-721 或 ERC-1155 代币可以绑定到某个特定的 ERC-721 NFT。一旦绑定,它就归 NFT 本身所有,而不是归某个钱包所有,因此每当 NFT 易主时,被绑定的代币就会隐式地随之转移,不需要额外成本。当前 NFT 所有者可以随时解除绑定,拿回普通的、可转移的代币。
它如何运作
- 绑定。
bind(from, bindAddress, bindId, tokenId)把代币从from转移到bindAddress处的 NFT 合约,并记录它属于 NFTbindId。如果目标合约不是 ERC-721,或该 NFT 不存在,必须回滚。会触发Transfer和Bind事件。 - 解除绑定。
unbind(from, to, bindAddress, bindId, tokenId)只能由代币所绑定的 NFT 的所有者(或被授权的 operator)调用。所有权转给to,如果to是合约,则执行常规的 safe-transfer 接收方检查。会触发Unbind和Transfer事件。 - 查询。
binderOf(tokenId)返回代币所绑定的 NFT(未绑定则为零地址),boundBalanceOf(bindAddress, bindId)统计绑定到某个 NFT 的代币数量。 - ERC-1155 版本增加了数量和批量变体:
batchBind、batchUnbind、boundBalanceOfBatch,以及BindBatch/UnbindBatch事件。
两种接口都通过 ERC-165 检测;ERC-721 版本的接口 ID 是 0x82a34a7d。
示例代码
把一个配饰代币绑定到"衣橱"NFT,之后再取回(ERC-721 版本):
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
interface IERC721Bindable {
event Bind(address indexed operator, address indexed from,
address indexed bindAddress, uint256 bindId, uint256 tokenId);
event Unbind(address indexed operator, address indexed from, address to,
address indexed bindAddress, uint256 bindId, uint256 tokenId);
function bind(address from, address bindAddress, uint256 bindId, uint256 tokenId) external;
function unbind(address from, address to, address bindAddress, uint256 bindId, uint256 tokenId) external;
function binderOf(uint256 tokenId) external view returns (address, uint256);
function boundBalanceOf(address bindAddress, uint256 bindId) external view returns (uint256);
}
contract Example {
function attach(IERC721Bindable accessory, address wardrobe, uint256 wardrobeId, uint256 hatId) external {
// caller must own (or be approved for) hatId
accessory.bind(msg.sender, wardrobe, wardrobeId, hatId);
}
function detach(IERC721Bindable accessory, address wardrobe, uint256 wardrobeId, uint256 hatId) external {
// caller must own (or be approved for) the wardrobe NFT
accessory.unbind(msg.sender, msg.sender, wardrobe, wardrobeId, hatId);
}
}