ERC-7589 · Draft(草案) · ERC · 2023-12-28
ERC-7589: Semi-Fungible Token Roles
一个出借 ERC-1155 代币使用权的标准草案:所有者提交(commit)一部分余额,然后在不转移所有权的情况下,把基于该余额的、有时限且可选择可撤销的角色授予其他账户。
它解决什么问题?
ERC-1155 记录每种代币谁拥有多少,但没有说明谁可以使用它们。游戏和应用常常希望出租游戏内物品、让公会成员使用通行证,或在固定期限内委托权利,而不必把代币本身送出去。
ERC-7589 为半同质化代币增加了一个标准的角色层,其理念与面向 NFT 的 ERC-7432 类似。每个角色分配只授予一个被授权者,并会自动过期。
它如何运作
授权分两步进行:
- 提交代币。
commitTokens(grantor, tokenAddress, tokenId, amount)锁定特定数量(通过存入角色合约或冻结),并返回一个commitmentId。 - 授予角色。
grantRole(commitmentId, role, grantee, expirationDate, revocable, data)在该承诺上分配一个bytes32角色。永久角色推荐使用type(uint64).max。
其他部分:
revokeRole结束一次分配。被授权者随时可以放弃;授权者只有在该角色可撤销或已过期时才能撤销。releaseTokens(commitmentId)返还已提交的代币,但只要还存在任何不可撤销且未过期的角色,就必须回滚。setRoleApprovalForAll(tokenAddress, operator, approved)让操作员(例如租赁市场)代表所有者提交和授权。这些授权与 ERC-1155 的授权是分开的。- 视图函数:
grantorOf、tokenAmountOf、roleExpirationDate、isRoleRevocable、roleData等。 - 建议角色 ID 取角色名称和参数的
keccak256,例如keccak256("Player(uint256)")。 - 可选扩展:
commitTokensAndGrantRole(一笔交易完成)和roleBalanceOf(针对某个角色授予某用户的总量)。此外还有一个供 ERC-1155 元数据 JSON 使用的可选roles部分。 - 核心接口的 ERC-165 接口 ID:
0xc4c8a71d。
示例代码
把某物品的 5 个单位出租 7 天(ethers v6):
import { Contract, Wallet, JsonRpcProvider, id } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const owner = new Wallet(process.env.PRIVATE_KEY!, provider);
const roles = new Contract(rolesAddress, [
"event TokensCommitted(address indexed _grantor, uint256 indexed _commitmentId, address indexed _tokenAddress, uint256 _tokenId, uint256 _tokenAmount)",
"function commitTokens(address,address,uint256,uint256) returns (uint256)",
"function grantRole(uint256,bytes32,address,uint64,bool,bytes)",
], owner);
const receipt = await (await roles.commitTokens(owner.address, itemAddress, 42n, 5n)).wait();
const commitmentId = roles.interface.parseLog(receipt!.logs.find(l => l.address === rolesAddress)!)!.args._commitmentId;
const expires = BigInt(Math.floor(Date.now() / 1000) + 7 * 24 * 3600);
await roles.grantRole(commitmentId, id("Player()"), renterAddress, expires, false, "0x");