ERC-7444 · Draft(草案) · ERC · 2023-06-05
ERC-7444: Time Locks Maturity
一个只有一个函数的接口 getMaturity(id),让任何人都能获知被时间锁定的头寸解锁的 Unix 时间戳,使锁定资产可以被一致地估值和交易。
它解决什么问题?
时间锁在 DeFi 中无处不在:归属(vesting)计划、投票托管代币、锁定的流动性、债券。锁定头寸的权利本身往往可以转让,例如以 ERC-721 或 ERC-1155 代币的形式。要为这样的头寸定价,你需要知道它何时解锁,因为它的价值在很大程度上取决于剩余时间。如今每个协议公开这个日期的方式各不相同,甚至根本不公开,因此市场、借贷协议和估值工具必须为每一个协议编写定制代码。
ERC-7444 把它们都需要的那一条信息标准化:到期日。
它如何运作
符合规范的合约支持 ERC-165 并实现:
function getMaturity(bytes32 id) external view returns (uint256 maturity);
id标识某一个具体的锁。ID 如何生成由实现决定(哈希、转换为bytes32的代币 ID 等)。maturity是以秒为单位的 Unix 时间戳,与block.timestamp的单位相同。- 对于以单一计划锁定一种同质化资产的单例合约,应忽略
id,调用者应传入0x0。
可以通过部署一个单独的合约来为现有基于 NFT 的锁定系统回答 getMaturity,从而把该标准附加到它们上面。
其设计理由指出,距到期时间是 Black-Scholes 等期权式定价模型的关键输入,这就是为什么需要一种通用的读取方式。
示例代码
一个实现该接口的最小锁仓合约:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IERC7444 {
function getMaturity(bytes32 id) external view returns (uint256 maturity);
}
contract SimpleLocker is IERC7444 {
struct Lock { address owner; uint256 amount; uint256 maturity; }
IERC20 public immutable token;
mapping(bytes32 => Lock) public locks;
uint256 private nonce;
constructor(IERC20 _token) { token = _token; }
function deposit(uint256 amount, uint256 period) external returns (bytes32 id) {
id = keccak256(abi.encode(msg.sender, nonce++));
locks[id] = Lock(msg.sender, amount, block.timestamp + period);
require(token.transferFrom(msg.sender, address(this), amount), "transfer");
}
function withdraw(bytes32 id) external {
Lock memory l = locks[id];
require(msg.sender == l.owner, "owner");
require(block.timestamp >= l.maturity, "locked");
delete locks[id];
require(token.transfer(msg.sender, l.amount), "transfer");
}
function getMaturity(bytes32 id) external view returns (uint256) {
return locks[id].maturity;
}
function supportsInterface(bytes4 i) external pure returns (bool) {
return i == type(IERC7444).interfaceId || i == 0x01ffc9a7; // ERC-165
}
}
需要注意
- 状态为 Draft。
- 有些协议允许延长锁定。
getMaturity只报告当前值,因此在交易或以头寸为抵押借贷之前,请先检查它。 - 规范文本中的参考实现有拼写错误,并且
withdraw中的到期检查写反了;请把它当作示意,不要直接复制。 - 返回值
0通常表示该 ID 不存在,但规范并未定义这一点,因此不要在不同实现之间依赖它。