ERC-7589 · Draft(초안) · ERC · 2023-12-28
ERC-7589: Semi-Fungible Token Roles
ERC-1155 토큰의 사용권을 빌려주기 위한 표준 초안. 소유자가 잔액을 커밋(commit)한 뒤, 소유권을 이전하지 않고 그 위에 기한이 있고 선택적으로 취소 가능한 역할(role)을 다른 계정에 부여한다.
어떤 문제를 해결하나요?
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은 할당을 종료합니다. 피부여자는 언제든 포기할 수 있고, 부여자는 취소 가능(revocable)하거나 이미 만료된 경우에만 취소할 수 있습니다.releaseTokens(commitmentId)는 커밋한 토큰을 돌려주지만, 취소 불가능하고 만료되지 않은 역할이 남아 있는 동안에는 revert해야 합니다.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");