Paj Digital Labs
All articles
Web3 & Crypto Tech

Smart Contract Upgrades: Proxies, Storage Collisions and the Admin Key Problem

Upgradeable contracts trade immutability for flexibility, and the trade has sharp edges. How proxy patterns actually work, the storage layout mistakes that brick contracts, and why the admin key is your biggest risk.

Paj Digital Labs 3 min read
Hardware security key beside a connected circuit board
Editorial illustration · Web3 & Crypto Tech

"Code is law" sounds great until you find a bug in the law and there is no way to amend it. That is why most production smart contracts of any size are upgradeable — and why a meaningful share of the largest losses in the space came not from bugs in the code but from how upgrades were handled.

How a proxy works

An upgradeable contract is really two contracts. The proxy holds the state and the funds, and has a stable address users interact with. The implementation holds the logic. When you call the proxy, it uses delegatecall to run the implementation's code against the proxy's own storage.

To upgrade, you deploy a new implementation and point the proxy at it. The address, balances and state stay put.

solidity
class="text-code-comment">// Simplified — never write your own proxy for production
fallback() external payable {
    address impl = _implementation();
    assembly {
        calldatacopy(0, 0, calldatasize())
        let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
        returndatacopy(0, 0, returndatasize())
        switch ok case 0 { revert(0, returndatasize()) }
        default { return(0, returndatasize()) }
    }
}

Use an audited library implementation. Writing your own proxy is one of the most reliable ways to lose money in this field.

Transparent vs UUPS

| Pattern | Upgrade logic lives in | Trade-off | |---|---|---| | Transparent proxy | The proxy | Simpler to reason about; slightly higher gas per call | | UUPS | The implementation | Cheaper calls; an implementation without upgrade logic bricks upgrades forever | | Beacon | A shared beacon contract | Upgrade many proxies at once |

UUPS is popular for gas reasons, but it puts a sharp edge on every upgrade: deploy an implementation that forgets the upgrade function and you can never upgrade again.

Storage collisions

Because the implementation runs against the proxy's storage, the storage layout must be compatible across versions. Solidity assigns storage slots in declaration order. If version 1 declares:

solidity
uint256 totalSupply;
address owner;

and version 2 inserts a new variable between them, every value after it now reads from the wrong slot. owner suddenly contains garbage, or worse, something an attacker can influence.

Rules I follow:

  • Only append new state variables, never insert or reorder
  • Never change types of existing variables
  • Use storage gaps in base contracts so they can grow later
  • Run a layout check in CI — upgrade tooling can diff storage layouts and fail the build on incompatibility

Initialisers, not constructors

Constructors run in the implementation's context, not the proxy's, so state set there never reaches the proxy. Upgradeable contracts use an initialize function instead — and that function must be protected so it can only run once. Unprotected initialisers have been exploited directly: someone calls initialize on the implementation contract itself and takes ownership of it.

Disable initialisers on the implementation in its constructor. It is one line and closes a whole class of attacks.

The admin key is the real risk

Whoever controls the upgrade can replace the logic with anything, including a function that sends every token to themselves. So the honest description of most upgradeable protocols is: "you trust whoever holds the admin key."

Reduce that trust:

1. Multisig ownership. Upgrades require several independent signers, on separate hardware wallets. 2. Timelock. Upgrades are announced and only executable after a delay (24–72 hours is common), so users can exit if they disagree. 3. Published process. Document who the signers are and how upgrades are reviewed. 4. Eventually, renounce. Once a contract is stable, consider removing upgradeability entirely.

Test the upgrade, not just the contract

Your test suite should deploy version 1, populate realistic state, upgrade to version 2, and assert that every piece of existing state reads correctly afterward. Then run it on a fork of mainnet with real state before the actual upgrade.

Upgradeability is a reasonable trade-off for most projects. Just be honest that it is a trust assumption, protect the key accordingly, and treat every upgrade like the high-risk deployment it is.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading