Diamond Proxy Pattern

Diamond Proxy Pattern

Diamonds are for ever, so should your application be...

The software has to change, as it has to support business operations which change based on consumer requirements. To support changes in blockchain smart contracts, proxy patterns have evolved and the developer community embraced it, while duly noting that data should remain immutable while business logic could change.

Proxy Patterns

Transparent Proxy and UUPS Proxy patterns are widely accepted patterns adopted by many web3 projects. The underlying principle is on two key elements.

a) fallback

All proxy patterns rely on the fallback function to route the call to the correct function. For this purpose, the fallback function uses a delegate call to route the call to another contract providing the functionality.

b) delegate call

The delegate call is key to proxy patterns as it allows the implementation to operate on the data storage of the proxy. This is how the storage layout is retained after repeated upgrades.

Transparent and UUPS patterns work great for upgradeability but have limitations as described below. Some are due to the limitations of EVM itself.

a) The contract cannot exceed 24KB

b) Works with single implementation contract

c) partial upgrade of functionality is not possible, it is either all or nothing kind of approach.

Diamond Pattern

Diamond patterns take the same principles as proxy patterns leveraging the fallback and delegate functions. But, it adds a layer in the middle that supercharges the abilities of this pattern. It stores a function identifier in a mapping, using which the diamond pattern can delegate calls to functions across different implementation contracts. The diamond pattern is a single proxy that deals will many implementation contracts.

Not only does it support multiple implementation contracts, but it also provisions for incremental upgrades of functionality using the diamond-cut function.

The key components of a diamond proxy are as follows:

  • Diamond: The central contract that acts as a proxy and routes function calls to the appropriate facets. It contains a mapping of function selectors to facet addresses.

  • Facets: Individual contracts that implement specific functionality. Each facet contains a set of functions that can be called by the diamond.

  • Diamond Loupe: A set of standard functions defined in EIP-2535 that provide information about the facets and function selectors used in the diamond. The diamond loupe allows developers and users to inspect and understand the structure of the diamond.

  • DiamondCut: A function used to add, replace, or remove facets and their corresponding function selectors in the diamond. Only an authorized address (e.g., the diamond's owner or a multi-signature contract) can perform a diamond cut.

Refer to the EIP to get a detailed understanding about diamond pattern.

https://eips.ethereum.org/EIPS/eip-2535

Facet reuse

Benefits of using Diamond Pattern:

a) A single contract that can expand on functionality incrementally.

b) Ability to start small and grow incrementally by adding new features using the diamond-cut function.

c) Emit events for each update of functionality to track changes, logs are reference.

d) Solves the 24KB maximum contract size limitation, as the data and logic are now spread across many contracts.

e) Reusability of code

f) fine grain control on data access

Motivation

Like a diamond, where each facet is shaped slowly and carefully, you can use the diamond proxy pattern to shape your application, incrementally.

This is relatively new, but it is being actively adopted by many projects for the wide range of benefits it offers. This is an area that all solidity developers to watch.

To learn more about this pattern and different flavours, refer to the below links.

https://github.com/mudgen/diamond-1-hardhat

https://github.com/mudgen/diamond-2-hardhat

https://github.com/mudgen/diamond-3-hardhat

A wealth of information at AuditOne

AuditOne has a repository of information that helped me through the journey to grasp some of these key concepts.

Please refer to the below link.

auditone.notion.site/auditone/bb52d390910e4..

To learn more about AuditOne, visit their website.

auditone.io

They also conduct quizzes and CTF challenges which help in verifying the progress, and also a great place to meet like-minded people. Join their discord channel to explore more.