EIPs Explained

ERC-7818 · Final · ERC · 2024-11-13

ERC-7818: Expirable ERC-20

An ERC-20 extension where tokens belong to epochs and expire after a set number of them, for loyalty points, prepaid credits, game items or bonds that should lapse automatically.

What problem does it solve?

Many real-world balances are not meant to last forever: loyalty points, promotional credits, mobile data allowances, game currencies or instruments with a maturity date. With a plain ERC-20, expiring them means running a job that burns balances, which is costly, centralised and easy to get wrong.

ERC-7818 makes expiry part of the token. Expired tokens simply stop counting, with no write transaction needed.

How it works

Time is divided into epochs, measured either in blocks (BLOCKS_BASED) or in seconds (TIME_BASED). Tokens received in an epoch stay valid for validityDuration() epochs. After that the epoch is expired and its tokens are unusable.

Because the current epoch can be computed with a read, expiry is "lazy": nothing has to run when tokens expire.

The standard ERC-20 functions change meaning slightly:

  • balanceOf returns only non-expired tokens.
  • transfer / transferFrom may only move non-expired tokens, and must revert or return false otherwise. Implementations may spend the soonest-expiring tokens first.
  • totalSupply is hard to compute when tokens silently expire, so the spec says it should return 0 or type(uint256).max.

New functions:

Function Purpose
currentEpoch() The latest epoch
epochLength(), epochType() Epoch size and unit
validityDuration() How many epochs tokens stay valid
isEpochExpired(epoch) Expiry check
balanceOfAtEpoch(epoch, account) Balance from one epoch (0 if expired)
transferAtEpoch / transferFromAtEpoch Move tokens from a specific epoch

Example from the spec: balances of 100, 150 and 200 in epochs 1, 2 and 3, with the current epoch 3 and validity reaching one epoch back. The usable balance is 150 + 200 = 350.

Sample code

Reading an expirable balance (ethers v6):

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);

Things to know

  • Status: Final.
  • Tools that assume balances only change through Transfer events will be wrong: balances drop silently when epochs expire. totalSupply is not meaningful either.
  • Expiring tokens inside DEX pools or lending protocols can expire while deposited; most DeFi integrations are not built for this.
  • Spending across many small epoch buckets can use a lot of gas, and very fragmented balances risk hitting the block gas limit.
  • Time-based epochs depend on block.timestamp. The reference implementation uses block numbers instead, partly because of concerns about L2 halts.

#token #erc20 #expiration #epochs #loyalty

Details

Authors
sirawt (@MASDXI), ADISAKBOONMARK (@ADISAKBOONMARK)
Status
Final
Type
Standards Track / ERC
Created
2024-11-13
Requires

Machine-readable: Markdown · JSON