ERC-4931 General-purpose token upgrade standard (Generic Token Upgrade Standard)
Stagnant · Tokens · created 2021-11-02
An upgrade-only interface that allows one ERC20 token (source) to be exchanged for another ERC20 token (target) in a standardized way.
Why it is needed
When a token issuer switches to a new contract, developers typically build a custom screen and script on an ad hoc basis to swap the existing token for the new one. With this approach, holders and exchanges must analyze and trust unfamiliar contract code from scratch for every upgrade, creating a heavy verification burden, and the upgrade logic is mixed into the token contract itself rather than being separated from the token's functionality. Furthermore, if how the source token is handled (whether it is burned or sent somewhere) is not standardized, it becomes difficult to prevent the same token from being upgraded multiple times, which can call into question the reliability of the upgrade itself.
What to implement
function upgradeSource() external view returns(address): Returns the address of the original (source) token contract being upgraded.function upgradeDestination() external view returns(address): Returns the address of the target token contract received through the upgrade.function isUpgradeActive() external view returns(bool): Returns true/false for whether the upgrade function is currently active.function isDowngradeActive() external view returns(bool): Returns true/false for whether the downgrade function is currently active, and is always false if downgrade is not implemented.function ratio() external view returns(uint256, uint256): Returns how many units of the target token are given per unit of the source token, as a numerator/denominator pair.function totalUpgraded() external view returns(uint256): Returns the total quantity of tokens upgraded from source to target so far.function computeUpgrade(uint256 sourceAmount) external view returns (uint256 destinationAmount, uint256 sourceRemainder): Precomputes the quantity of target tokens that would be received and the leftover, unconverted quantity of source tokens if a given amount of source tokens were upgraded.function computeDowngrade(uint256 destinationAmount) external view returns (uint256 sourceAmount, uint256 destinationRemainder): Precomputes the quantity of source tokens that would be received and the leftover, unconverted quantity of target tokens if a given amount of target tokens were downgraded.function upgrade(address _to, uint256 sourceAmount) external: Converts source tokens into target tokens according to the ratio and sends them to the _to address.function downgrade(address _to, uint256 destinationAmount) external: Converts target tokens back into source tokens according to the ratio and sends them to the _to address.event Upgrade(address indexed _from, address indexed _to, uint256 sourceAmount, uint256 destinationAmount): Records the caller, recipient address, and source/target token amounts when upgrade is executed.event Downgrade(address indexed _from, address indexed _to, uint256 sourceAmount, uint256 destinationAmount): Records the caller, recipient address, and source/target token amounts when downgrade is executed.
In depth
The source/destination pair and ratio
upgradeSource and upgradeDestination each return the address of the token contract before and after the upgrade. ratio returns the exchange rate of target tokens relative to source tokens as a (numerator, denominator) tuple; for example, (3, 1) means 3 target tokens are given per 1 source token. This ratio is the baseline value applied consistently across computeUpgrade, computeDowngrade, and the execution of both upgrade and downgrade.
Remainder calculation in computeUpgrade and computeDowngrade
computeUpgrade(sourceAmount) is a lookup function that precomputes the result without actually calling upgrade, returning both the destinationAmount to be received and the sourceRemainder that does not divide evenly into the ratio and thus is not converted. The specification states that at a ratio of (3, 2), when sourceAmount is 5, computeUpgrade(5) must return (6, 1). In other words, 4 units of source are converted to give 6 units of target, leaving 1 unit of source as remainder. computeDowngrade calculates the reverse direction the same way; at the same (3, 2) ratio, when destinationAmount is 13, computeDowngrade(13) returns (4, 1), converting 12 units of target into 4 units of source and leaving 1 unit of target as remainder.
Conditions for executing upgrade and handling of the source token
upgrade(_to, sourceAmount) converts the caller's source tokens into destination tokens according to the ratio and sends them to the _to address. approve must have been called on the source contract beforehand, and the function must throw if the caller's source token balance is insufficient or if isUpgradeActive is false. The function must emit the Upgrade event, and the source token must be locked in the upgrade contract or burned. If the downgrade extension is implemented, it must be locked instead of burned.
Optional downgrade extension
downgrade is an Optional Ext., a feature that does not need to be implemented. If implemented, downgrade(_to, destinationAmount) must unwrap the destination token back into the source token and send it to the _to address. Before the call, approve must have already been called on the destination contract, and it must throw if the caller has insufficient destination tokens or if isDowngradeActive is false, and it must emit a Downgrade event. If downgrade is not implemented, isDowngradeActive must always return false to indicate that downgrade is unavailable.
The increase/decrease behavior of totalUpgraded
totalUpgraded returns the total amount upgraded from source to destination so far. If the downgrade extension is not implemented, only upgrade is possible, so this value only ever increases. Conversely, if downgrade is implemented, calling downgrade can decrease this value, so it may decrease between calls.
The rationale for the source token burn/lock principle
The Rationale views prior cases like Golem (GNT to GLM), which built the upgrade function directly into the token contract itself, as a suboptimal approach that couples the upgrade and token functionality too tightly. This standard separates the upgrade into a separate third-party contract so that the token contract's implementation does not interfere with the upgrade process. There is an agreed-upon rule for handling the source token: tokens that can be burned must be burned via burn, and tokens that cannot be must be sent to the 0x00 address to be effectively burned. If the downgrade extension is implemented, the source token is instead locked in the upgrade contract, to prevent the supply of the source or destination token from being artificially inflated by repeatedly calling upgrade and downgrade.
Backward compatibility
This standard has no backward compatibility issues that break existing functionality. However, it notes that existing token upgrades already implemented are likely not to follow this standard, and in such cases it is desirable for the asset issuer to disclose that their upgrade does not comply with EIP-4931.
Proposal status
This ERC is currently Stagnant, meaning discussion has stalled. It is not yet a finalized standard, so its content may change or may not be adopted.
Things to watch out for
- The risk of duplicate upgrades of the source token: If the source token remains accessible elsewhere after an upgrade, the same token could be upgraded multiple times, which calls into question the validity of the upgrade itself.
- Burnable tokens must use burn: A source token with a burn function must be burned via burn during the upgrade process, while a token without a burn function must be sent to the 0x00 address.
- Constraints relaxed when downgrade is implemented: If the downgrade extension is implemented, the upgrade contract must hold the source token rather than burning it, so in this case the full-burn constraint is relaxed to a locking approach.