---
eip: 1132
label: ERC-1132
title: Extending ERC20 with token locking capability
status: Stagnant
type: Standards Track
category: ERC
created: 2018-06-03
official: https://eips.ethereum.org/EIPS/eip-1132
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1132.md
canonical: https://eips.huatuo.xyz/zh/eip-1132/
lang: zh
tldr: ERC-20 的一项扩展，允许在代币合约内部以指定原因对代币进行时间锁定，持有者无需把代币转入托管合约，仍保留在自己的余额中。
tags: [token, erc20, locking, vesting, governance]
---

# ERC-1132: Extending ERC20 with token locking capability

> An extension to the ERC20 standard with methods for time-locking of tokens within a contract.

**TL;DR:** ERC-20 的一项扩展，允许在代币合约内部以指定原因对代币进行时间锁定，持有者无需把代币转入托管合约，仍保留在自己的余额中。

## 它解决什么问题？

许多应用需要用户把代币锁定一段时间：归属（vesting）计划、治理投票，或作为理赔评估者展示"利益相关"。通常的做法是把代币转入质押或托管合约（[ERC-900](/eip-900/) 风格）。这样做有代价：

- 用户必须信任托管合约，并且要先对它授权，
- 额外的转账要消耗 gas，
- 代币从 `balanceOf` 中消失，钱包显示的余额具有误导性，
- 用户必须记得把代币取回，并且无法在同一应用的其他地方使用被锁定的代币。

ERC-1132 让代币留在持有者的账户中，并在代币合约内部记录锁定，每个锁定都带有一个 `bytes32` 类型的**原因**（reason），例如 `"GOV"` 或 `"CLAIM_ASSESS"`。

## 它如何运作

该扩展为 ERC-20 代币增加以下函数：

| 函数 | 用途 |
| --- | --- |
| `lock(reason, amount, time)` | 以 `reason` 为原因，把调用者的 `amount` 数量代币锁定 `time` 秒 |
| `tokensLocked(of, reason)` | 某个原因下当前被锁定的数量 |
| `tokensLockedAtTime(of, reason, time)` | 在未来某个时间戳仍将被锁定的数量 |
| `totalBalanceOf(of)` | 锁定余额 + 可转账余额 |
| `extendLock(reason, time)` | 把解锁时间进一步推后 |
| `increaseLockAmount(reason, amount)` | 向已有的锁定中追加代币 |
| `tokensUnlockable(of, reason)` / `getUnlockableTokens(of)` | 锁定已到期的代币 |
| `unlock(of)` | 把所有已到期的锁定释放回可转账余额 |

`Locked` 和 `Unlocked` 事件让钱包和索引器可以跟踪锁定状态，就像 `Transfer` 跟踪余额一样。

由于同一个锁定可以被应用的多个部分读取，锁定代币 *n* 天的用户，例如可以在这段时间内投多次票，而一次性的质押转账不容易做到这一点。

## 示例代码

从另一个合约中使用可锁定的代币：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface ILockableToken {
    function lock(bytes32 reason, uint256 amount, uint256 time) external returns (bool);
    function tokensLocked(address of_, bytes32 reason) external view returns (uint256);
    function totalBalanceOf(address of_) external view returns (uint256);
    function unlock(address of_) external returns (uint256);
}

contract Voting {
    ILockableToken public immutable token;
    bytes32 public constant GOV = "GOV";

    constructor(ILockableToken t) { token = t; }

    // Voting weight = tokens the voter has locked for governance.
    function weight(address voter) external view returns (uint256) {
        return token.tokensLocked(voter, GOV);
    }
}
```

用户直接在代币上锁定（被锁定的是 `msg.sender` 自己的代币）：

```ts
await token.lock(encodeBytes32String("GOV"), parseUnits("100", 18), 7n * 24n * 3600n);
```

## 需要注意

- 状态为 **Stagnant**。它没有最终定稿，因此实际使用中的接口可能与上面的不同。
- 规范在事件签名中，对 reason 混用了 `uint256` 和 `bytes32`；请检查你要集成的实际合约。
- 必须重写 `transfer`，使得只有余额中未锁定的部分才能转移。规范把这一点留给实现者，因此请仔细审计。
- 锁定按原因区分；若要修改已有的锁定，请使用 `increaseLockAmount` 或 `extendLock`。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1132
