SolidityIn

Architecture: Upgradeable Smart Contracts: A Deep Dive into Diamond and Proxy Patterns

November 7, 2026 · admin · Uncategorized
Smart Contract Architecture

 

A Deep Dive into Diamond and Proxy Patterns

 

 

One of the core tenets of blockchain technology is immutability. However, in software development, immutability is a double-edged sword. While it provides security and trust, it makes fixing bugs or adding features to deployed code impossible. To solve this, the Ethereum community developed Upgradeable Smart Contracts.

 

This article explores the two most prominent architectural patterns used today: the Proxy Pattern and the Diamond Pattern (EIP-2535).

 

 

The Core Mechanism: delegatecall

 

Before diving into patterns, we must understand delegatecall. This opcode allows Contract A to execute code from Contract B, but the execution happens in the context of Contract A’s storage, balance, and address.

 

“Think of delegatecall as a company hiring a consultant. The consultant (Implementation) brings the expertise, but they use the company’s (Proxy) office, files, and bank account.”

 

 

1. The Proxy Pattern

 

The standard Proxy Pattern involves two contracts: a Proxy Contract (which holds the state and funds) and a Logic/Implementation Contract (which holds the code).

 

Transparent Proxy Pattern

 

In this model, the upgrade logic resides within the Proxy contract. To prevent “function selector clashing” (where a user accidentally calls an administrative function), the proxy identifies whether the caller is an admin or a regular user.

 

UUPS (Universal Upgradeable Proxy Standard)

 

UUPS is more gas-efficient. The upgrade logic is placed in the Implementation contract rather than the proxy. If you accidentally deploy a logic contract without upgrade functions, the contract becomes “frozen” and non-upgradeable, which serves as a safety feature for some projects.

 

// Simplified Proxy logic
fallback() external payable {
    address _impl = implementation();
    assembly {
        calldatacopy(0, 0, calldatasize())
        let result := delegatecall(gas(), _impl, 0, calldatasize(), 0, 0)
        returndatacopy(0, 0, returndatasize())
        switch result
        case 0 { revert(0, returndatasize()) }
        default { return(0, returndatasize()) }
    }
}

 

 

2. The Diamond Pattern (EIP-2535)

 

As decentralized applications grew in complexity, they hit the Spurious Dragon limit: a smart contract cannot exceed 24KB. The Diamond Pattern was created to bypass this limit and bring modularity to contract architecture.

 

Facets and the Diamond

 

Instead of one implementation contract, a “Diamond” can delegate calls to multiple contracts, called Facets. The Diamond maintains a mapping of function selectors to facet addresses.

 

    • The Diamond: The main proxy contract.

 

    • Facets: Independent contracts containing specific logic (e.g., a Governance Facet, a Swap Facet).

 

    • DiamondCut: A standard interface to add, replace, or remove facets.

 

    • Loupe: Functions that allow you to “look into” the diamond to see what functions it supports.

 

 

The Diamond Pattern is essentially a multi-proxy. It allows for a single point of entry for an ecosystem of dozens of micro-contracts.

 

 

Comparing the Patterns

 

Feature Standard Proxy (UUPS/Transparent) Diamond Pattern (EIP-2535)
Contract Size Limited to 24KB. Virtually unlimited (modular).
Complexity Low to Medium. High.
Storage Management Inherited storage or Proxiable. Diamond Storage (Namespaced).
Gas Cost Slightly lower for single calls. Slightly higher due to mapping lookup.
Use Case Simple apps, standard tokens. Complex DEXs, Games, DAOs.

 

 

Critical Risk: Storage Collisions

 

In both patterns, storage management is the most dangerous aspect. If the new implementation defines a variable in a storage slot already occupied by a different variable in the old implementation, the data will be corrupted.

 

To mitigate this, developers use Unstructured Storage (storing the implementation address at a random slot like keccak256("eip1967.proxy.implementation")) or Diamond Storage, which uses structs at specific hash-based positions to ensure facets don’t overwrite each other’s data.

 

 

Conclusion

 

Choosing between a standard Proxy and a Diamond depends on the scale of your project. If you are building a simple DeFi utility, a UUPS Proxy is often the best balance of safety and efficiency. However, if you are building an ambitious protocol that will evolve over years and likely exceed the 24KB limit, the Diamond Pattern provides the modularity and longevity required for enterprise-grade blockchain architecture.

 

 

 

 

Tags: , , ,