Diamonds & Facets: Increasing Interoperability on Ethereum - A Simple Explanation
Diamonds are a novel approach to how smart contracts can interact with one another. They enable splitting functions across multiple contracts, but keeping all of those functions accessible from the same, single place.
Here's a summary with everything you should know.
How Smart Contracts Communicate
Contracts on Ethereum today can communicate with each other using a function named “delegateCall.” This is a function in some contracts that calls on a function in a different contract, and executes it.
How Diamonds Increase Interoperability
This same delegateCall method can be used to instead spread functionality over multiple smart contracts, enabling people to reuse functions in different contracts like libraries. The system for how these contracts are structured is what Diamonds introduces through EIP-2535.
EIP-2535 is a framework with two types of contracts: Diamonds and Facets.
The metaphor is helpful here; diamonds are the main object, and facets are their various faces. Diamonds are the directories of smart contracts, each with potentially many facets; Each smart contract referenced in a diamond is a facet.
How Diamonds Work
Diamonds are contracts that only contain a fallback function. Fallback functions are most commonly used as a failsafe; when a contract receives a transaction with a request that does not match what it can do, it calls the fallback function. Since Diamonds only contain a fallback function and nothing else, every transaction it receives will be one that does not match what it can do. The Diamond can do nothing, it can only fallback.
Inside a Diamond’s fallback function is the directory of Facets it can call upon. The data inside the transaction a Diamond receives determines which of these facets should be called upon. The facet is then executed, and any relevant data is stored with the Diamond contract.
What Facets Do
Facets are smart contracts that exist to be called by Diamonds. They provide specific functionality that can be useful to a variety of apps, and can be used by any or all of them.
They contain code only, all the data they compute is stored in the Diamonds that call them.
This is a summary of a full write-up from my free newsletter!
Visit using the link below to learn more about blockchain, and consider signing up to be notified when a new piece is up!
Stay kind. Stay curious.