top of page
Search

Things you should know about EIP 2535 Diamond contract

  • Writer: Sathya Prakash
    Sathya Prakash
  • Sep 10, 2022
  • 2 min read

A diamond is a contract with external functions supplied by contracts called facets.


Facets are separate, independent contracts that can share internal functions, libraries, and state variables.


  • A diamond stores within it a mapping of function selector to facet address.

  • A diamond has four standard functions called the loupe functions that are used to show what functions a diamond has.

  • A diamond can be upgraded if it has a diamondCut external function or other function(s) that can add/replace/remove functions.

  • Data is stored in the diamond. But different facets have different access to data.

  • Upgrade diamonds to fix bugs, add functionality and implement new standards.


Types of Diamond:

Upgradeable Diamond

An upgradeable diamond has the diamondCut function and/or possibly other functions to add/replace/remove functions. It is useful for iterative development or improving an application over time.


Finished Diamond

A finished diamond was an upgradeable diamond and had a number of upgrades. Then its diamondCut function and/or other upgrade functions were removed and upgrades are no longer possible. It is no longer possible to add/replace/remove functions. It has become an immutable diamond.


Single Cut Diamond

A single cut diamond adds all functions to itself in its constructor function, but it does not add the diamondCut function or any other function that can add/replace/remove functions. This means that a single-cut diamond is fully created in its constructor and once created can never be upgraded. It has the same immutability and trustless guarantees as a regular vanilla contract. Why would someone do this? There may be a number of reasons.


The two use cases below are good reasons.

Your contract hits the max contract size limit. Make it into a single cut diamond. You still break your big contract into smaller facets, modularizing your code.


You start with an upgradeable diamond in your development and testing and upgrade it to your heart’s delight. Reap the advantages of easy upgrading and a stable address as you work out new features, bugs, and kinks. Release the upgradeable diamond on a test network with your application for beta testing and upgrade it when needed. This is iterative development. When it is solid then deploy it as a single cut diamond on the main network.


Diamonds emit an event every time one or more functions are added, replaced, or removed. All source codes can be verified. This enables people and software to monitor changes to a contract. If any bad acting function is added to a diamond then it can be seen.

Security and domain experts can review the history of change of a diamond to detect any history of foul play.


 
 
 

Comments


bottom of page