Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
ERC-7720 proposes a mechanism for depositing ERC-20 tokens and allowing their withdrawal once a specified future timestamp is reached.
This article translates, summarizes, and expands on the following proposal.
More EIP explainers:
Overview
The standard allows ERC-20 tokens to be deposited for a designated recipient, with withdrawal restricted until the specified unlock time.
The deposit records the following information:
- token address
- The address of the deposited ERC-20 token contract.
- sender
- The address supplying the tokens.
- recipient
- The address designated to receive the tokens.
- amount
- The quantity of tokens deposited.
- unlock time
- The timestamp at or after which withdrawal becomes possible.
- withdrawal status
- Whether the tokens have already been withdrawn.
For ERC-20, see the following article.
Motivation
Vesting schedules, escrow services, and time-based rewards are examples of applications that may require deferred payments.
A time-locked token transfer can prevent the release of deposited tokens before a specified timestamp. This provides several benefits when implemented securely.
- Structured payments
- Token releases can follow a predefined schedule.
- Security
- Preventing premature withdrawals reduces the risk of a recipient accessing funds before the agreed time.
- Predictability
- A predefined unlock time makes payment schedules easier to reason about.
The unlock time establishes eligibility for withdrawal. It does not automatically execute a transfer at that time.
Specification
Contracts implementing the proposal expose the following interface.
pragma solidity ^0.8.0;
interface ITokenTransfer {
// Event emitted when a transfer is initiated.
event Transfer(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo
);
// Event emitted when tokens are withdrawn.
event Withdraw(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount
);
// Function to initiate a token transfer.
// Parameters:
// - _token: Address of the ERC20 token contract.
// - _from: Address of the sender.
// - _to: Address of the recipient.
// - _amount: Amount of tokens to be transferred.
// - _unlockTime: Time after which the tokens can be withdrawn.
// - _reference: Reference ID for the transaction.
// Returns the transaction ID.
function transferFrom(
address _token,
address _from,
address _to,
uint256 _amount,
uint40 _unlockTime,
bytes32 _reference
) external returns (uint256 txnId);
// Function to withdraw tokens from a transaction.
// Parameters:
// - _txnId: ID of the transaction to withdraw from.
function withdraw(uint256 _txnId) external;
// Function to get transaction details.
// Parameters:
// - _txnId: ID of the transaction.
// Returns the transaction details.
function getTransaction(uint256 _txnId)
external
view
returns (
address token,
address from,
address to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo,
bool withdrawn
);
}Transfer
event Transfer(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo
);Emitted when a deferred transfer is initiated.
txnId- The identifier of the deferred transfer.
token- The ERC-20 token contract address.
from- The sender's address.
to- The recipient's address.
amount- The quantity of tokens being transferred.
unlockTime- The timestamp at or after which the tokens can be withdrawn.
referenceNo- The reference identifier associated with the transfer.
Withdraw
Emitted when the deposited tokens are withdrawn.
event Withdraw(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount
);transferFrom
function transferFrom(
address _token,
address _from,
address _to,
uint256 _amount,
uint40 _unlockTime,
bytes32 _reference
) external returns (uint256 txnId);Initiates a deferred token transfer.
_token- The ERC-20 token contract address.
_from- The sender's address.
_to- The recipient's address.
_amount- The quantity of tokens to transfer.
_unlockTime- The timestamp at or after which the tokens can be withdrawn.
_reference- The reference identifier for the transfer.
The function returns the identifier assigned to the deferred transfer.
withdraw
Withdraws the tokens associated with the specified transfer ID.
function withdraw(uint256 _txnId) external;_txnId- The identifier of the transfer to withdraw.
getTransaction
Retrieves the details of a deferred transfer.
function getTransaction(uint256 _txnId)
external
view
returns (
address token,
address from,
address to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo,
bool withdrawn
);_txnId- The identifier of the transfer.
Additional considerations
Representing unlock time with uint40
Using a uint40 for _unlockTime provides a sufficiently large timestamp range for long-term vesting schedules and escrow arrangements.
A 40-bit unsigned timestamp measured in seconds can represent approximately 35,000 years from the Unix epoch.
Returning txnId from transferFrom
The transferFrom function returns a unique txnId for each deferred transfer. Users and applications can use this identifier to reference and manage a particular deposit.
Compatibility with existing ERC-20 tokens
The standard defines a separate interface rather than extending the ERC-20 token contract itself. Existing ERC-20 token contracts do not need to be modified to participate in this mechanism, subject to their transfer behavior and the deferred-transfer contract's implementation.
Reference implementation
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract TokenTransfer {
using SafeERC20 for IERC20;
struct Transaction {
address token; // Address of the ERC20 token contract.
address from; // Address of the sender.
address to; // Address of the recipient.
uint256 amount; // Amount of tokens to be transferred.
uint40 unlockTime; // Time after which the tokens can be withdrawn.
bytes32 referenceNo; // Reference ID for the transaction.
bool withdrawn; // Flag indicating if the tokens have been withdrawn.
}
// Mapping from transaction ID to Transaction structure.
mapping(uint256 => Transaction) public transactions;
// Variable to keep track of the next transaction ID.
uint256 public lastTxnId = 0;
// Event emitted when a transfer is initiated.
event Transfer(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo
);
// Event emitted when tokens are withdrawn.
event Withdraw(
uint256 txnId,
address indexed token,
address indexed from,
address indexed to,
uint256 amount
);
constructor() {}
// Function to initiate a token transfer.
// Parameters:
// - _token: Address of the ERC20 token contract.
// - _from: Address of the sender.
// - _to: Address of the recipient.
// - _amount: Amount of tokens to be transferred.
// - _unlockTime: Time after which the tokens can be withdrawn.
// - _reference: Reference ID for the transaction.
// Returns the transaction ID.
function transferFrom(
address _token,
address _from,
address _to,
uint256 _amount,
uint40 _unlockTime,
bytes32 _reference
) external returns (uint256 txnId) {
require(_amount > 0, "Invalid transfer amount");
// Transfer tokens from sender to this contract.
IERC20(_token).safeTransferFrom(_from, address(this), _amount);
lastTxnId++;
// Store the transaction details.
transactions[lastTxnId] = Transaction({
token: _token,
from: _from,
to: _to,
amount: _amount,
unlockTime: _unlockTime,
referenceNo: _reference,
withdrawn: false
});
// Emit an event for the transaction creation.
emit Transfer(lastTxnId, _token, _from, _to, _amount, _unlockTime, _reference);
return lastTxnId;
}
// Function to withdraw tokens from a transaction.
// Parameters:
// - _txnId: ID of the transaction to withdraw from.
function withdraw(uint256 _txnId) external {
Transaction storage transaction = transactions[_txnId];
require(transaction.amount > 0, "Invalid transaction ID");
require(block.timestamp >= transaction.unlockTime, "Current time is before unlock time");
// require(transaction.to == msg.sender, "Only the recipient can withdraw the tokens");
require(!transaction.withdrawn, "Tokens already withdrawn");
IERC20(transaction.token).safeTransfer(transaction.to, transaction.amount);
transaction.withdrawn = true;
// Emit an event for the token withdrawal.
emit Withdraw(_txnId, transaction.token, transaction.from, transaction.to, transaction.amount);
}
// Function to get transaction details.
// Parameters:
// - _txnId: ID of the transaction.
// Returns the transaction details.
function getTransaction(uint256 _txnId)
external
view
returns (
address token,
address from,
address to,
uint256 amount,
uint40 unlockTime,
bytes32 referenceNo,
bool withdrawn
)
{
Transaction storage transaction = transactions[_txnId];
require(transaction.amount > 0, "Invalid transaction ID");
return (
transaction.token,
transaction.from,
transaction.to,
transaction.amount,
transaction.unlockTime,
transaction.referenceNo,
transaction.withdrawn
);
}
}The example is reproduced unchanged from the original article. It is illustrative and has not been independently audited. Before production use, review authorization of deposits, compatibility with nonstandard tokens, reentrancy protection, and the handling of withdrawal state around external calls. In particular, an ERC-20 allowance does not by itself establish authorization for every deferred-transfer arrangement.
Security
Ownerless contract design
Omitting a privileged owner can reduce the risk of an administrator redirecting deposited tokens away from their designated recipients. This property depends on the absence of other privileged withdrawal or upgrade mechanisms.
Restricting the destination of withdrawals
The tokens should be transferred only to the recipient recorded when the deposit was created. This ensures that the funds are delivered to the intended address.
In the reference implementation, the recipient-only caller check is commented out. Consequently, another account may trigger a withdrawal once it becomes eligible, but the token destination remains the recorded recipient. Restricting who can initiate a withdrawal and restricting who receives the tokens are separate design decisions.
Citation
Chen Liaoyuan (@chenly) cly@kip.pro, "ERC-7720: Deferred Token Transfer [DRAFT]," Ethereum Improvement Proposals, no. 7720, June 2024. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7720.
Conclusion
ERC-7720 provides a common interface for depositing ERC-20 tokens and releasing them to a designated recipient at or after a specified timestamp.
Questions are welcome via Twitter @cardene777.
Other publications:



