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.
This article examines ERC4931, a proposal that separates the upgrade logic for ERC20 tokens from the token contracts themselves.
This article translates, summarizes, and expands on the proposal below.
I have also covered a range of other EIPs.
Overview
ERC4931 proposes a standard API for upgrading ERC20 tokens. Specifically, it defines an interface for converting tokens from one contract (the source token) into tokens from another contract (the destination token).
It also provides helper methods for retrieving the basic information needed for conversion, such as the addresses of both token contracts and the conversion ratio.
Motivation
Historically, when upgrading ERC20 tokens, each issuer had to provide its own mechanism, and holders had to use that specific interface to exchange their old tokens for new ones.
By adopting the standard interface proposed by ERC4931, holders and centralized and decentralized exchanges can handle upgrades more efficiently. Upgrade scripts can also be reused, reducing the effort required for security reviews.
For issuers, the standard provides clear guidance on how to implement token upgrades effectively.
Specification
IEIP4931
interface IEIP4931 {Methods
upgradeSource
/// @dev A getter to determine the contract that is being upgraded from ("source contract")
/// @return The address of the source token contract
function upgradeSource() external view returns(address)Returns the address of the source token contract from which tokens are upgraded.
- Return value
- The source token contract address, of type
address.
- The source token contract address, of type
upgradeDestination
/// @dev A getter to determine the contract that is being upgraded to ("destination contract")
/// @return The address of the destination token contract
function upgradeDestination() external view returns(address)Returns the address of the destination token contract to which tokens are upgraded.
- Return value
- The destination token contract address, of type
address.
- The destination token contract address, of type
isUpgradeActive
/// @dev The method will return true when the contract is serving upgrades and otherwise false
/// @return The status of the upgrade as a boolean
function isUpgradeActive() external view returns(bool)Returns whether upgrades are currently active.
- Return value
- Type
bool. trueif upgrades are available;falseotherwise.
- Type
isDowngradeActive
/// @dev The method will return true when the contract is serving downgrades and otherwise false
/// @return The status of the downgrade as a boolean
function isDowngradeActive() external view returns(bool)An optional function that returns whether downgrades are active.
- Return value
- Type
bool. - Always returns
falseif downgrades are not implemented.
- Type
ratio
/// @dev A getter for the ratio of destination tokens to source tokens received when conducting an upgrade
/// @return Two uint256, the first represents the numerator while the second represents
/// the denominator of the ratio of destination tokens to source tokens allotted during the upgrade
function ratio() external view returns(uint256, uint256)Returns the conversion ratio from source tokens to destination tokens.
- Return values
- Two
uint256values. - The first is the numerator (
numerator), and the second is the denominator (denominator), representing the ratio(numerator, denominator). - Example
(3, 1)means three destination tokens for each source token.
- Two
totalUpgraded
/// @dev A getter for the total amount of source tokens that have been upgraded to destination tokens.
/// The value may not be strictly increasing if the downgrade Optional Ext. is implemented.
/// @return The number of source tokens that have been upgraded to destination tokens
function totalUpgraded() external view returns(uint256)Returns the total amount of source tokens upgraded so far.
- Return value
- Type
uint256. - The value may decrease if downgrades are supported.
- Type
computeUpgrade
/// @dev A method to mock the upgrade call determining the amount of destination tokens received from an upgrade
/// as well as the amount of source tokens that are left over as remainder
/// @param sourceAmount The amount of source tokens that will be upgraded
/// @return destinationAmount A uint256 representing the amount of destination tokens received if upgrade is called
/// @return sourceRemainder A uint256 representing the amount of source tokens left over as remainder if upgrade is called
function computeUpgrade(uint256 sourceAmount) external view
returns (uint256 destinationAmount, uint256 sourceRemainder)Given an amount of source tokens, calculates how many destination tokens an upgrade would yield and the remainder (the source tokens that cannot be converted).
-
Parameter
sourceAmount (uint256)- The amount of source tokens to convert.
-
Return values
destinationAmount (uint256)- The amount of destination tokens received after the upgrade.
sourceRemainder (uint256)- The remaining source tokens not used in the conversion.
computeDowngrade
/// @dev A method to mock the downgrade call determining the amount of source tokens received from a downgrade
/// as well as the amount of destination tokens that are left over as remainder
/// @param destinationAmount The amount of destination tokens that will be downgraded
/// @return sourceAmount A uint256 representing the amount of source tokens received if downgrade is called
/// @return destinationRemainder A uint256 representing the amount of destination tokens left over as remainder if upgrade is called
function computeDowngrade(uint256 destinationAmount) external view
returns (uint256 sourceAmount, uint256 destinationRemainder)An optional function that calculates the amount of source tokens a downgrade would yield and the remainder, given an amount of destination tokens.
-
Parameter
destinationAmount (uint256)- The amount of destination tokens to convert.
-
Return values
sourceAmount (uint256)- The amount of source tokens received after the downgrade.
destinationRemainder (uint256)- The remaining destination tokens not used in the conversion.
upgrade
/// @dev A method to conduct an upgrade from source token to destination token.
/// The call will fail if upgrade status is not true, if approve has not been called
/// on the source contract, or if sourceAmount is larger than the amount of source tokens at the msg.sender address.
/// If the ratio would cause an amount of tokens to be destroyed by rounding/truncation, the upgrade call will
/// only upgrade the nearest whole amount of source tokens returning the excess to the msg.sender address.
/// Emits the Upgrade event
/// @param _to The address the destination tokens will be sent to upon completion of the upgrade
/// @param sourceAmount The amount of source tokens that will be upgraded
function upgrade(address _to, uint256 sourceAmount) externalConverts source tokens into destination tokens at the specified ratio and sends them to the specified address.
To execute the function, isUpgradeActive() must return true, and approve() must have been called on the source token contract beforehand.
When the conversion would result in fractional amounts, it rounds down and returns any excess to msg.sender.
On success, it emits the Upgrade event.
- Parameters
_to (address)- The address to receive the upgraded tokens.
sourceAmount (uint256)- The amount of source tokens to convert.
downgrade
/// @dev A method to conduct a downgrade from destination token to source token.
/// The call will fail if downgrade status is not true, if approve has not been called
/// on the destination contract, or if destinationAmount is larger than the amount of destination tokens at the msg.sender address.
/// If the ratio would cause an amount of tokens to be destroyed by rounding/truncation, the downgrade call will only downgrade
/// the nearest whole amount of destination tokens returning the excess to the msg.sender address.
/// Emits the Downgrade event
/// @param _to The address the source tokens will be sent to upon completion of the downgrade
/// @param destinationAmount The amount of destination tokens that will be downgraded
function downgrade(address _to, uint256 destinationAmount) externalAn optional function that converts destination tokens back into source tokens.
To execute the function, isDowngradeActive() must return true, and approve() must have been called on the destination token contract beforehand.
Fractional amounts are rounded down, and any excess is returned to msg.sender.
On success, it emits the Downgrade event.
- Parameters
_to (address)- The address to receive the downgraded tokens.
destinationAmount (uint256)- The amount of destination tokens to convert.
Events
Upgrade
Emitted when an upgrade completes.
- Parameters
_from (address)- The address that performed the upgrade.
_to (address)- The address that received the destination tokens.
sourceAmount (uint256)- The amount of source tokens converted.
destinationAmount (uint256)- The amount of destination tokens sent.
Downgrade
Emitted when a downgrade completes.
- Parameters
_from (address)- The address that performed the downgrade.
_to (address)- The address that received the source tokens.
sourceAmount (uint256)- The amount of source tokens sent.
destinationAmount (uint256)- The amount of destination tokens converted.
Additional considerations
Some past ERC20 token upgrades, such as Golem's migration from GNT to GLM, embedded the upgrade functionality directly in a token contract. This design is considered undesirable because it tightly couples the upgrade logic with the existing token functionality.
ERC4931 recommends using a separate, third contract dedicated to upgrades, keeping upgrade logic independent of the tokens' core functionality. Holders and exchanges can then perform token upgrades with simple, reusable scripts, reducing the work required for future migrations.
The interface is deliberately generic and leaves the details of implementation to developers. This avoids interference between token-specific behavior and the upgrade process.
To improve safety and ensure valid conversions, source tokens should be burned during an upgrade if they support burning; otherwise, they are sent to the 0x00 address.
If downgrade functionality is implemented, however, the standard instead calls for locking the source tokens in the upgrade contract to prevent an increase in token supply.
Backwards compatibility
ERC4931 does not break compatibility with existing ERC20 tokens and introduces no backwards compatibility issues.
Reference implementation
//SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./IEIP4931.sol";
contract SourceUpgrade is IEIP4931 {
using SafeERC20 for IERC20;
uint256 constant RATIO_SCALE = 10**18;
IERC20 private source;
IERC20 private destination;
bool private upgradeStatus;
bool private downgradeStatus;
uint256 private numeratorRatio;
uint256 private denominatorRatio;
uint256 private sourceUpgradedTotal;
mapping(address => uint256) public upgradedBalance;
constructor(address _source, address _destination, bool _upgradeStatus, bool _downgradeStatus, uint256 _numeratorRatio, uint256 _denominatorRatio) {
require(_source != _destination, "SourceUpgrade: source and destination addresses are the same");
require(_source != address(0), "SourceUpgrade: source address cannot be zero address");
require(_destination != address(0), "SourceUpgrade: destination address cannot be zero address");
require(_numeratorRatio > 0, "SourceUpgrade: numerator of ratio cannot be zero");
require(_denominatorRatio > 0, "SourceUpgrade: denominator of ratio cannot be zero");
source = IERC20(_source);
destination = IERC20(_destination);
upgradeStatus = _upgradeStatus;
downgradeStatus = _downgradeStatus;
numeratorRatio = _numeratorRatio;
denominatorRatio = _denominatorRatio;
}
/// @dev A getter to determine the contract that is being upgraded from ("source contract")
/// @return The address of the source token contract
function upgradeSource() external view returns(address) {
return address(source);
}
/// @dev A getter to determine the contract that is being upgraded to ("destination contract")
/// @return The address of the destination token contract
function upgradeDestination() external view returns(address) {
return address(destination);
}
/// @dev The method will return true when the contract is serving upgrades and otherwise false
/// @return The status of the upgrade as a boolean
function isUpgradeActive() external view returns(bool) {
return upgradeStatus;
}
/// @dev The method will return true when the contract is serving downgrades and otherwise false
/// @return The status of the downgrade as a boolean
function isDowngradeActive() external view returns(bool) {
return downgradeStatus;
}
/// @dev A getter for the ratio of destination tokens to source tokens received when conducting an upgrade
/// @return Two uint256, the first represents the numerator while the second represents
/// the denominator of the ratio of destination tokens to source tokens allotted during the upgrade
function ratio() external view returns(uint256, uint256) {
return (numeratorRatio, denominatorRatio);
}
/// @dev A getter for the total amount of source tokens that have been upgraded to destination tokens.
/// The value may not be strictly increasing if the downgrade Optional Ext. is implemented.
/// @return The number of source tokens that have been upgraded to destination tokens
function totalUpgraded() external view returns(uint256) {
return sourceUpgradedTotal;
}
/// @dev A method to mock the upgrade call determining the amount of destination tokens received from an upgrade
/// as well as the amount of source tokens that are left over as remainder
/// @param sourceAmount The amount of source tokens that will be upgraded
/// @return destinationAmount A uint256 representing the amount of destination tokens received if upgrade is called
/// @return sourceRemainder A uint256 representing the amount of source tokens left over as remainder if upgrade is called
function computeUpgrade(uint256 sourceAmount)
public
view
returns (uint256 destinationAmount, uint256 sourceRemainder)
{
sourceRemainder = sourceAmount % (numeratorRatio / denominatorRatio);
uint256 upgradeableAmount = sourceAmount - (sourceRemainder * RATIO_SCALE);
destinationAmount = upgradeableAmount * (numeratorRatio / denominatorRatio);
}
/// @dev A method to mock the downgrade call determining the amount of source tokens received from a downgrade
/// as well as the amount of destination tokens that are left over as remainder
/// @param destinationAmount The amount of destination tokens that will be downgraded
/// @return sourceAmount A uint256 representing the amount of source tokens received if downgrade is called
/// @return destinationRemainder A uint256 representing the amount of destination tokens left over as remainder if upgrade is called
function computeDowngrade(uint256 destinationAmount)
public
view
returns (uint256 sourceAmount, uint256 destinationRemainder)
{
destinationRemainder = destinationAmount % (denominatorRatio / numeratorRatio);
uint256 upgradeableAmount = destinationAmount - (destinationRemainder * RATIO_SCALE);
sourceAmount = upgradeableAmount / (denominatorRatio / numeratorRatio);
}
/// @dev A method to conduct an upgrade from source token to destination token.
/// The call will fail if upgrade status is not true, if approve has not been called
/// on the source contract, or if sourceAmount is larger than the amount of source tokens at the msg.sender address.
/// If the ratio would cause an amount of tokens to be destroyed by rounding/truncation, the upgrade call will
/// only upgrade the nearest whole amount of source tokens returning the excess to the msg.sender address.
/// Emits the Upgrade event
/// @param _to The address the destination tokens will be sent to upon completion of the upgrade
/// @param sourceAmount The amount of source tokens that will be upgraded
function upgrade(address _to, uint256 sourceAmount) external {
require(upgradeStatus == true, "SourceUpgrade: upgrade status is not active");
(uint256 destinationAmount, uint256 sourceRemainder) = computeUpgrade(sourceAmount);
sourceAmount -= sourceRemainder;
require(sourceAmount > 0, "SourceUpgrade: disallow conversions of zero value");
upgradedBalance[msg.sender] += sourceAmount;
source.safeTransferFrom(
msg.sender,
address(this),
sourceAmount
);
destination.safeTransfer(_to, destinationAmount);
sourceUpgradedTotal += sourceAmount;
emit Upgrade(msg.sender, _to, sourceAmount, destinationAmount);
}
/// @dev A method to conduct a downgrade from destination token to source token.
/// The call will fail if downgrade status is not true, if approve has not been called
/// on the destination contract, or if destinationAmount is larger than the amount of destination tokens at the msg.sender address.
/// If the ratio would cause an amount of tokens to be destroyed by rounding/truncation, the downgrade call will only downgrade
/// the nearest whole amount of destination tokens returning the excess to the msg.sender address.
/// Emits the Downgrade event
/// @param _to The address the source tokens will be sent to upon completion of the downgrade
/// @param destinationAmount The amount of destination tokens that will be downgraded
function downgrade(address _to, uint256 destinationAmount) external {
require(upgradeStatus == true, "SourceUpgrade: upgrade status is not active");
(uint256 sourceAmount, uint256 destinationRemainder) = computeDowngrade(destinationAmount);
destinationAmount -= destinationRemainder;
require(destinationAmount > 0, "SourceUpgrade: disallow conversions of zero value");
require(upgradedBalance[msg.sender] >= sourceAmount,
"SourceUpgrade: can not downgrade more than previously upgraded"
);
upgradedBalance[msg.sender] -= sourceAmount;
destination.safeTransferFrom(
msg.sender,
address(this),
destinationAmount
);
source.safeTransfer(_to, sourceAmount);
sourceUpgradedTotal -= sourceAmount;
emit Downgrade(msg.sender, _to, sourceAmount, destinationAmount);
}
}Security considerations
The main security concern in ERC4931 is that source tokens might become accessible again after an upgrade. If this happens, the same tokens could be upgraded multiple times, among other abuses, undermining the integrity of the upgrade process.
ERC4931 therefore imposes strict constraints on how source tokens are handled.
Specifically, if source tokens support burning, they must be burned during an upgrade (except when the optional downgrade mechanism requires them to be retained).
Tokens that cannot be burned must be sent to the 0x00 address, an address no one can access, effectively removing them from circulation.
Only when the optional downgrade functionality is implemented may source tokens be locked and retained in the upgrade contract instead. This allows subsequent downgrades. Locking the tokens does not pose a security problem provided safeguards prevent their unauthorized reuse.
Citation
John Peterson (@John-peterson-coinbase), Roberto Bayardo (@roberto-bayardo), David Núñez (@cygnusv), "ERC-4931: Generic Token Upgrade Standard [DRAFT]," Ethereum Improvement Proposals, no. 4931, November 2021. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-4931.
Conclusion
This article covered ERC4931, a proposal that separates the upgrade logic for ERC20 tokens from the tokens themselves. I hope you found it useful.
If you have any questions, feel free to contact me via Twitter DM.
I also publish on other platforms, which you can find below.



