What is Diamond Pattern?
The diamond pattern, defined in EIP-2535, is an advanced proxy architecture that allows a single proxy contract to delegate calls to multiple implementation contracts called facets. This modular approach overcomes the 24KB contract size limit, enables granular upgrades, and provides flexible access control for different functionality sets.
How it Works
A diamond consists of a central proxy contract that routes function calls to the appropriate facet based on the function selector. The diamond stores a mapping of function selectors to facet addresses, allowing unprecedented modularity.
Key components include:
- Diamond Proxy: The central contract users interact with
- Facets: Multiple implementation contracts, each handling specific functions
- DiamondCut Facet: Manages adding, replacing, and removing facets
- DiamondLoupe Facet: Provides introspection of available functions
- Selector to Facet Mapping: Routes each function to its implementing facet
Upgrades can target specific facets without affecting others, enabling surgical updates.
Practical Example
Aavegotchi uses the diamond pattern for their gaming protocol, allowing them to add new game features, items, and mechanics without redeploying the entire contract system. The modular nature means new facets can be added for seasonal content while core game logic remains stable. Louper is a popular tool for exploring diamond contracts, showing all facets and their functions. Several complex DeFi protocols have adopted diamonds when traditional proxies prove too limiting.
Why it Matters
The diamond pattern suits complex protocols that require extensive functionality exceeding single-contract limits. However, this complexity introduces additional security considerations. Each facet upgrade must maintain storage compatibility, and the expanded attack surface requires thorough auditing. Understanding diamonds helps users evaluate whether protocol complexity is warranted and properly managed.
Fensory analyzes complex protocol architectures including diamond patterns, helping users understand the modular structure and upgrade capabilities of sophisticated DeFi systems.