EIPs Explained

ERC-7720 · Draft · ERC · 2024-06-09

ERC-7720: Deferred Token Transfer

A standard escrow interface for time-locked ERC-20 payments: deposit tokens for a recipient now, and they can be withdrawn only after a chosen unlock time.

What problem does it solve?

Vesting schedules, escrow, delayed payroll and timed rewards all need the same thing: lock tokens for someone now, release them later. Today each project writes its own time-lock contract with its own function names and events, which makes them hard for wallets, explorers and accounting tools to understand.

ERC-7720 proposes one small interface for "deferred transfers". It works as a separate contract that sits beside any existing ERC-20 token, so tokens do not need to change.

How it works

The deferred-transfer contract exposes three functions:

Function Purpose
transferFrom(token, from, to, amount, unlockTime, reference) Pull amount of token from from into the contract, record it for to, and return a new txnId
withdraw(txnId) After unlockTime, send the tokens to the recorded to
getTransaction(txnId) Read the stored details: token, from, to, amount, unlock time, reference, withdrawn flag

Flow:

  1. The payer approves the deferred-transfer contract on the token.
  2. Someone calls transferFrom(...) with a uint40 unlock timestamp and an arbitrary bytes32 reference (an invoice number, for example). Tokens move into the contract and a Transfer event (with the txnId) is emitted.
  3. Once block.timestamp >= unlockTime, withdraw(txnId) pays out to the recorded recipient and emits Withdraw. Each txnId can be withdrawn only once.

The reference implementation lets anyone call withdraw, but tokens always go to the recipient fixed at deposit time. The security section asks that the contract have no owner, so nobody can redirect locked funds.

Sample code

Locking 1,000 tokens for 30 days with ethers v6:

import { Contract, Wallet, JsonRpcProvider, parseUnits, encodeBytes32String } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const payer = new Wallet(process.env.PRIVATE_KEY!, provider);

const token = new Contract(TOKEN, ["function approve(address,uint256) returns (bool)"], payer);
const deferred = new Contract(DEFERRED, [
  "function transferFrom(address,address,address,uint256,uint40,bytes32) returns (uint256)",
  "function withdraw(uint256)",
  "function getTransaction(uint256) view returns (address,address,address,uint256,uint40,bytes32,bool)",
  "event Transfer(uint256 txnId, address indexed token, address indexed from, address indexed to, uint256 amount, uint40 unlockTime, bytes32 referenceNo)",
], payer);

const amount = parseUnits("1000", 18);
const unlock = Math.floor(Date.now() / 1000) + 30 * 24 * 3600;

await (await token.approve(DEFERRED, amount)).wait();
const tx = await deferred.transferFrom(TOKEN, payer.address, recipient, amount, unlock, encodeBytes32String("INV-0042"));
const receipt = await tx.wait();
const txnId = deferred.interface.parseLog(receipt!.logs.find((l) => l.address === DEFERRED)!)!.args.txnId;

// 30 days later, anyone can trigger the payout to `recipient`
await deferred.withdraw(txnId);

Things to know

  • Status is Draft; function names and events may still change.
  • The interface's transferFrom takes an explicit from. In the reference implementation, anyone can call it for any from that has approved the contract, so an approval to a shared deferred-transfer contract can be used by third parties to lock your tokens for someone else. Implementations should restrict from (for example to msg.sender) or users should approve only exact amounts right before use.
  • The event is also named Transfer, but its signature differs from ERC-20's Transfer, so indexers will not confuse them by topic, but humans reading logs might.
  • There is no cancel or clawback: once deposited, funds go only to the recipient after the unlock time.
  • Fee-on-transfer or rebasing tokens can make the recorded amount differ from what the contract actually holds.

#token #erc20 #escrow #vesting #timelock

Details

Authors
Chen Liaoyuan (@chenly) <[email protected]>
Status
Draft
Type
Standards Track / ERC
Created
2024-06-09
Requires

Machine-readable: Markdown · JSON