ERC-7721 · Draft(草案) · ERC · 2023-05-25
ERC-7721: Lockable Extension for ERC-1155
ERC-1155 的扩展,允许持有者在自己的钱包中将一定数量的代币锁定给指定的 locker,无需托管即可实现借贷和租赁。
它解决什么问题?
如今,要把 NFT 用作贷款抵押品或出租,通常需要把它转入协议的托管合约。在它被托管期间,你会失去它的功能(游戏访问权限、Discord 身份组等),无法将其与其他金融工具组合使用,还会暴露于该托管合约的漏洞风险之下。
ERC-7721 为 ERC-1155 代币添加了原生锁定功能。代币仍留在所有者的钱包中,但由选定的 locker 地址控制指定数量何时可以再次转移。ERC-721 上的同类思路是 ERC-7066。
它如何运作
锁定按 tokenId、account、locker 以及数量逐项进行(ERC-1155 的余额是数量):
lock(tokenId, account, locker, amount):所有者锁定amount,并指定另一个locker。lock(tokenId, account, amount):所有者或获批的 operator 锁定amount,调用者成为 locker。unlock(tokenId, account, amount):只有 locker 才能解锁。setApprovalForId(tokenId, operator, amount):比 ERC-1155 要么全有要么全无的setApprovalForAll粒度更细的授权。transferAndLock(from, to, tokenId, amount, setApprove):一步完成向接收方转账并在其处锁定,还可以选择给调用者授权,使其之后能把代币收回(适用于租赁或先买后付)。getLocked(tokenId, account, operator)返回被锁定的数量。
如果 safeTransferFrom 会移动被锁定的单位,则必须 revert:最大可转数量为 balance - locked。Lock 和 Unlock 事件让索引器能够跟踪状态。
示例代码
接口,以及一个强制执行转账规则的辅助逻辑:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
interface IERC7721 is IERC1155 {
event Lock(uint256 indexed tokenId, address account, address _locker, uint256 amount);
event Unlock(uint256 indexed tokenId, address account, address _locker, uint256 amount);
function lock(uint256 tokenId, address account, uint256 amount) external;
function lock(uint256 tokenId, address account, address _locker, uint256 amount) external;
function unlock(uint256 tokenId, address account, uint256 amount) external;
function transferAndLock(address from, address to, uint256 tokenId, uint256 amount, bool setApprove) external;
function getLocked(uint256 tokenId, address account, address operator) external view returns (uint256);
function setApprovalForId(uint256 tokenId, address operator, uint256 amount) external;
}
// Inside an implementation's transfer hook:
// uint256 free = balanceOf(from, id) - totalLocked[id][from];
// require(amount <= free, "ERC7721: amount locked");
用 TypeScript 检查被锁定的数量(ethers v6):
const lockable = new Contract(tokenAddr, [
"function getLocked(uint256,address,address) view returns (uint256)",
], provider);
const locked = await lockable.getLocked(tokenId, holder, lenderContract);
需要注意
- 状态:Draft。 函数签名仍可能变化。
getLocked的接口注释提到返回钱包,而规范正文说它返回数量;实现遵循"返回数量"的理解。- 如果 locker 是无法访问的地址,或是没有解锁路径的合约,代币可能被永久锁定。锁定之前请先审查 locker。
- 市场在上架前应检查被锁定的数量,因为转移被锁定的单位会 revert。