ERC-1504 · Stagnant · ERC · 2018-10-17
ERC-1504: Upgradable Smart Contract
An early upgradeability pattern that splits a contract into a permanent Data contract and a replaceable Handler contract, with an optional Upgrader that lets registered voters approve each upgrade.
What problem does it solve?
Deployed contract code cannot be changed. When a bug or vulnerability is found, the usual fix in 2018 was to deploy a new contract and migrate all its data by hand. That was slow, expensive and error-prone, and attackers could keep exploiting the old contract in the meantime.
ERC-1504 proposes separating storage from logic so that the logic can be swapped while the data stays put. It also adds an optional voting step so that upgrades are not purely at the owner's discretion.
How it works
The system has three parts:
| Contract | Role |
|---|---|
| Data contract | Holds all state, plus admin data (owner, current Handler address, initialised flag) and upgrade data (Upgrader address, proposal block and period). Only the registered Handler may call its setters. |
| Handler contract | Holds the business logic and reads and writes through the Data contract. Implements initialize, registerUpgrader, isUpgraderRegistered, done (self-destruct after being replaced) and live(), which always returns true. |
| Upgrader contract (optional) | Runs a vote among voters chosen by the owner. If the proposal passes before it expires, it calls setHandler on the Data contract to switch to the new Handler. |
Deploying a system: deploy the Data contract, deploy a Handler, register it with setHandler(), then initialise the data.
Upgrading with voting: deploy the new Handler; deploy an Upgrader that points at the Data contract and both Handlers; register the Upgrader with the new Handler, the old Handler and the Data contract; call startProposal(); voters vote; getResolution() finalises before expiry. The Data contract reports its state through an UpgradingStatus enum (Done, InProgress, Blocked, Expired, Error). Only one proposal can be active at a time.
Sample code
The core idea in modern Solidity: data lives in one contract and only the current handler may write to it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DataStore {
address public owner = msg.sender;
address public handler;
mapping(address => uint256) private balances;
modifier onlyHandler() { require(msg.sender == handler, "not handler"); _; }
function setHandler(address newHandler) external {
require(msg.sender == owner, "not owner"); // ERC-1504 also allows a passed Upgrader
handler = newHandler;
}
function balanceOf(address a) external view returns (uint256) { return balances[a]; }
function setBalance(address a, uint256 v) external onlyHandler { balances[a] = v; }
}
contract HandlerV1 {
DataStore public immutable data;
constructor(DataStore d) { data = d; }
function live() external pure returns (bool) { return true; }
function credit(address to, uint256 amount) external {
data.setBalance(to, data.balanceOf(to) + amount);
}
}
Things to know
- Status is Stagnant. The spec's code targets pre-0.5 Solidity and relies on
selfdestruct, which no longer deletes contracts on mainnet since the Cancun upgrade. - Users interact with the Handler, so its address changes on every upgrade, and frontends and integrators must follow it. Proxy-based patterns avoid this by keeping one stable address.
- The industry largely settled on proxies instead: ERC-1967 storage slots (transparent proxies), ERC-1822 UUPS, and ERC-2535 Diamonds, often with ERC-7201 namespaced storage.
- Any upgrade mechanism means the voters or owner can change the code. Users must trust whoever controls upgrades.