How do smart contracts handle upgrades?

July 13th, 2025, 11:49 am
Smart contracts are immutable once deployed to the blockchain, meaning their code cannot be changed directly. However, there are well-established upgrade patterns that allow developers to introduce new functionality or fix bugs

🔁 1. Proxy Pattern (Most Common Approach)


How it works:


  1. A proxy contract delegates calls to a separate logic (implementation) contract using the delegatecall opcode.
  2. The proxy holds the contract's state, while the logic contract holds the code.
  3. When an upgrade is needed, the proxy can be pointed to a new logic contract.


Advantages:


  1. Upgradable without losing state.
  2. Widely used (e.g., OpenZeppelin’s TransparentUpgradeableProxy).


Example:


  1. Proxy -> LogicV1
  2. Upgrade: Proxy -> LogicV2


🧠 2. Eternal Storage Pattern


How it works:


  1. Stores all contract data in a single contract (storage contract).
  2. Logic contracts interact with this storage.
  3. Upgrades involve changing only the logic contract.
  4. Use case: Prevents storage conflicts across upgrades.


🏗️ 3. Modular/Diamond Pattern (EIP-2535)


How it works:


  1. Uses a single contract (the Diamond) with multiple "facets" (modules).
  2. Facets can be added, replaced, or removed.


Advantages:


  1. Highly modular and flexible.
  2. Ideal for large or complex contracts (e.g., DeFi platforms, DAOs).


🛠️ 4. Admin-Controlled Replacement


How it works:

  1. A central admin or DAO can deprecate an old contract and deploy a new one.
  2. Users need to migrate manually or automatically via incentives.


Downsides:


  1. Can be more user-disruptive.
  2. Requires user trust in admin process.


🔐 Security & Governance Considerations


  1. Access Control: Only trusted parties should be able to perform upgrades (e.g., via multi-sig or DAO governance).
  2. Upgradeability Risks: Buggy upgrades, rug pulls, or governance capture.
  3. Transparency: Events and logs should track upgrade history.


🔧 Tools for Upgradeable Contracts


  1. OpenZeppelin Upgrades Plugin
  2. Hardhat + OpenZeppelin libraries
  3. Truffle + OpenZeppelin SDK (deprecated but still seen in older projects)