ERC-5501 · Stagnant(停滞) · ERC · 2022-08-18
ERC-5501: Rental & Delegation NFT - EIP-721 Extension
提出带有到期时间和 `isBorrowed` 标志的 ERC-721 `user` 角色,使 NFT 可以在留在所有者钱包中的同时委托给热钱包或出租,并且所有者无法提前取消租约。
它解决什么问题?
许多 NFT 很有用(游戏道具、土地、门票、会员资格),而使用它的人并不总是所有者。ERC-4907 增加了有时间限制的 user 角色,但所有者可以随时重置 user。因此,租赁市场为了保护租户,不得不托管 NFT(把它质押到合约里)。质押期间,所有者会失去空投、免费铸造等持有者福利,也不能挂牌出售该 NFT。
EIP-5501 增加了一个标志 isBorrowed,在租期内锁定 user 角色,这样 NFT 可以全程留在所有者的钱包中。
它如何运作
IERC5501 扩展(ERC-165 ID 为 0xf808ec37)增加了:
| 函数 | 用途 |
|---|---|
setUser(tokenId, user, expires, isBorrowed) |
所有者或获批准的操作员设置 user、到期时间戳和借用标志 |
userOf(tokenId) |
当前 user;未设置或已过期时应 revert |
userExpires(tokenId) |
user 角色何时结束 |
userIsBorrowed(tokenId) |
该角色是否为被锁定的租赁 |
由此产生两种模式:
- 委托(
isBorrowed = false):例如把 user 角色指向热钱包。所有者可以随时更改,转移时会被清除。 - 租赁(
isBorrowed = true):在expires之前setUser必须 revert,并且 user 在转移后依然保留,所以所有者在租赁期间甚至可以出售该 NFT。
绝不能把 user 当作所有者对待:不能转移,不能授权。可选扩展增加了 userBalanceOf、tokenOfUserByIndex,以及一个 Terminable 扩展:出租方和借用方各自调用 setBorrowTermination,之后任何人都可以调用 terminateBorrow 来提前结束租赁。
示例代码
核心规则的草图,基于 OpenZeppelin ERC-721(v5):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
abstract contract RentableSketch is ERC721 {
struct UserInfo { address user; uint64 expires; bool isBorrowed; }
mapping(uint256 => UserInfo) internal _users;
event UpdateUser(uint256 indexed tokenId, address indexed user, uint64 expires, bool isBorrowed);
function setUser(uint256 tokenId, address user, uint64 expires, bool isBorrowed) public {
require(user != address(0), "zero user");
_checkAuthorized(ownerOf(tokenId), msg.sender, tokenId);
UserInfo memory u = _users[tokenId];
require(!(u.isBorrowed && u.expires >= block.timestamp), "borrowed");
_users[tokenId] = UserInfo(user, expires, isBorrowed);
emit UpdateUser(tokenId, user, expires, isBorrowed);
}
function _update(address to, uint256 tokenId, address auth) internal virtual override returns (address from) {
from = super._update(to, tokenId, auth);
if (from != to && !_users[tokenId].isBorrowed) { // delegation is cleared on transfer
delete _users[tokenId];
emit UpdateUser(tokenId, address(0), 0, false);
}
}
}
需要注意
- 状态:Stagnant。ERC-4907(Final)是更广为人知的租赁标准,读取
userOf和userExpires的应用可以兼容两者。 - 如果租赁的
expires非常大,除非实现了 Terminable 扩展,否则所有者将被无限期锁定而无法调用setUser。 - 由于所有者和 user 同时处于活跃状态,应用必须决定哪些福利归哪个角色,以避免同一福利被"重复使用"。
- 市场应该显示挂牌的 NFT 当前是否处于被借用状态。