---
eip: 7818
label: ERC-7818
title: Expirable ERC-20
status: Final
type: Standards Track
category: ERC
created: 2024-11-13
requires: 20
official: https://eips.ethereum.org/EIPS/eip-7818
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7818.md
canonical: https://eips.huatuo.xyz/zh/eip-7818/
lang: zh
tldr: 一个 ERC-20 扩展，代币归属于各个纪元（epoch），并在经过设定数量的纪元后过期，适用于应当自动失效的积分、预付额度、游戏道具或债券。
tags: [token, erc20, expiration, epochs, loyalty]
---

# ERC-7818: Expirable ERC-20

> An ERC-20 extension for creating fungible tokens with expiration, supporting time-limited use cases.

**TL;DR:** 一个 ERC-20 扩展，代币归属于各个纪元（epoch），并在经过设定数量的纪元后过期，适用于应当自动失效的积分、预付额度、游戏道具或债券。

## 它解决什么问题？

现实中的许多余额并不打算永久存在：忠诚度积分、促销额度、手机流量额度、游戏货币，或有到期日的工具。使用普通的 [ERC-20](/eip-20/)，要让它们过期就得运行一个销毁余额的任务，成本高、中心化，而且容易出错。

ERC-7818 把过期变成代币的一部分。过期的代币直接不再被计入，无需任何写入交易。

## 它如何运作

时间被划分为**纪元（epoch）**，以区块数（`BLOCKS_BASED`）或秒数（`TIME_BASED`）计量。在某个纪元收到的代币在 `validityDuration()` 个纪元内保持有效。之后该纪元就过期了，其中的代币不可使用。

由于当前纪元可以通过读取计算得出，过期是"惰性"的：代币过期时不需要运行任何东西。

标准的 ERC-20 函数含义略有变化：

- `balanceOf` 只返回**未过期**的代币。
- `transfer` / `transferFrom` 只能转移未过期的代币，否则必须回滚或返回 `false`。实现可以优先花费最先到期的代币。
- 当代币悄悄过期时，`totalSupply` 很难计算，因此规范规定它**应当**返回 `0` 或 `type(uint256).max`。

新增函数：

| 函数 | 用途 |
| --- | --- |
| `currentEpoch()` | 最新的纪元 |
| `epochLength()`, `epochType()` | 纪元的长度和单位 |
| `validityDuration()` | 代币保持有效的纪元数 |
| `isEpochExpired(epoch)` | 过期检查 |
| `balanceOfAtEpoch(epoch, account)` | 某一纪元的余额（已过期则为 0） |
| `transferAtEpoch` / `transferFromAtEpoch` | 转移指定纪元的代币 |

规范中的例子：纪元 1、2、3 的余额分别为 100、150 和 200，当前纪元为 3，有效期可回溯一个纪元。可用余额为 150 + 200 = 350。

## 示例代码

读取可过期余额（ethers v6）：

```ts
import { Contract, JsonRpcProvider } from "ethers";

const abi = [
  "function balanceOf(address) view returns (uint256)",
  "function currentEpoch() view returns (uint256)",
  "function validityDuration() view returns (uint256)",
  "function balanceOfAtEpoch(uint256 epoch, address account) view returns (uint256)",
];
const token = new Contract(tokenAddress, abi, new JsonRpcProvider(process.env.RPC_URL));

const usable = await token.balanceOf(user);           // non-expired only
const now: bigint = await token.currentEpoch();
const validity: bigint = await token.validityDuration();

for (let e = now; e >= 0n && now - e <= validity; e--) {
  console.log(`epoch ${e}:`, await token.balanceOfAtEpoch(e, user)); // 0 once expired
}
console.log("usable:", usable);
```

## 需要注意

- **状态：Final。**
- 假定余额只会通过 `Transfer` 事件变化的工具会出错：纪元过期时余额会悄悄减少。`totalSupply` 同样没有意义。
- DEX 资金池或借贷协议中的可过期代币可能在存入期间过期；大多数 DeFi 集成并没有为此做设计。
- 跨许多小的纪元桶花费代币可能消耗大量 gas，余额过于碎片化有触及区块 gas 上限的风险。
- 基于时间的纪元依赖 `block.timestamp`。参考实现改用区块号，部分原因是担心 L2 停机。


---

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