ERC-6864 · Draft · ERC · 2023-04-05
ERC-6864: Upgradable Fungible Token
Draft ERC-20 extension that lets holders opt in to move balances from an existing token to a new upgraded token contract, and back, without proxies.
What problem does it solve?
Token contracts are immutable. If an ERC-20 has a bug, lacks a feature or must meet new rules, the usual fix is a proxy, but that only works if the token was deployed behind a proxy from day one. Proxy upgrades are also silent and one-way: holders can't refuse them.
ERC-6864 defines a simple upgrade path that works for any existing ERC-20. A new contract (the "ERC-6864 token") wraps a base ERC-20. Holders choose to upgrade by depositing base tokens, and can downgrade again later. Because the new contract is itself an ERC-20, integrators only need to change the address.
How it works
The interface extends IERC20 with:
baseToken(): address of the original ERC-20.upgrade(to, amount): takesamountbase tokens from the caller (who must have approved the new contract) and creditstowith upgraded tokens. EmitsUpgrade.downgrade(from, to, amount): burns upgraded tokens fromfrom(caller must befromor approved) and releases base tokens toto. EmitsDowngrade.
The optional see-through extension adds combinedTotalSupply, combinedBalanceOf and combinedAllowance, which add up the base and upgraded amounts.
Notable design choices:
- Reversible and nestable. Downgrading lets users exit a buggy version or switch between several ERC-6864 contracts built on the same base, and an upgraded token can itself be a base.
- Upgrade-while-transfer. The reference implementation's
transferautomatically upgrades any shortfall from the sender's base balance, and itsbalanceOfreports the combined balance. - The conversion rate is up to the implementation (1:1, linear or non-linear).
Compared with ERC-4931, which uses a separate bridge contract with pre-minted tokens, ERC-6864 mints just in time inside the token itself.
Sample code
A 1:1 implementation built on OpenZeppelin (simplified from the reference, without auto-upgrade):
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
import {IERC20, ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract UpgradedToken is ERC20 {
event Upgrade(address indexed from, address indexed to, uint256 amount);
event Downgrade(address indexed from, address indexed to, uint256 amount);
IERC20 public immutable baseToken;
constructor(address base) ERC20("Token v2", "TKN2") { baseToken = IERC20(base); }
function upgrade(address to, uint256 amount) external {
require(to != address(0), "zero to");
require(baseToken.transferFrom(msg.sender, address(this), amount), "transfer failed");
_mint(to, amount);
emit Upgrade(msg.sender, to, amount);
}
function downgrade(address from, address to, uint256 amount) external {
require(to != address(0), "zero to");
if (from != msg.sender) _spendAllowance(from, msg.sender, amount);
_burn(from, amount);
require(baseToken.transfer(to, amount), "transfer failed");
emit Downgrade(from, to, amount);
}
}
Things to know
- Status: Draft. Interfaces may change, and the reference code in the spec contains typos, so don't copy it verbatim.
- Upgrading requires
approveon the base token. Check the new contract and approve only what you intend to convert, since unlimited approvals combined with auto-upgrade intransferremove that confirmation step. - If
balanceOf/totalSupplyreturn combined values, dashboards and indexers can double-count or disagree, especially with several or nested wrappers. - Non-linear conversion rates expose upgrades and downgrades to front-running and sandwich attacks, as with any AMM curve.