Introduction
Diamonds are a novel approach to how smart contracts can interact with one another. They enable splitting responsibilities or functions across multiple contracts, but keeping all of those functions accessible from the same, single place.
A single smart contract can act as a directory to many others, enabling access to plenty of functionality. Another smart contract may be referenced in many such directories. Today’s write-up explains how Diamonds enable this interoperable network of smart contracts. Enjoy.
Sections
How Can Smart Contracts Speak to Each other?
How Can Diamonds enable more Interoperable Contracts?
How Do Diamonds (EIP-2535) Work?
What is the Purpose behind Facets?
What are the Benefits of Diamonds?
Diamonds on Ethereum Today
In Conclusion
*Thank you to Ziad for proofreading.
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. This is not used often, as contracts tend to have all the information they use contained within them.
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 (as well as other benefits). delegateCall continues to be the communication method, but the system for how these contracts are structured is what Diamonds introduces through EIP-2535.
EIP-2535 is a framework with two types of smart 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. You can look at a single diamond and observe many of its facets, and with EIP-2535 you can use a Diamond contract to use many different functions (Facets).
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 applications, and can be used by any or all of them. Contracts need not repeat code that already exists; they can instead reference Facets.
Facets can serve as public libraries of code any contract can use, making Diamonds modular. They contain code and code only, all the data they compute is stored in the Diamonds that call them.
Learn more about contract data storage here.
The Benefits of Diamonds
Diamonds make contracts increasingly interoperable. Diamonds themselves can become super-contracts people refer to for a variety of uses. A single destination for functionality can exist in place of a series of smart contracts that work together. Diamonds abstract such series of smart contracts into a single program.
Diamonds can also make contract data storage more efficient. By designing Facets so they only contain code, Diamonds become a universal source for the data a transaction uses. If a transaction provides data that more than one facet needs, that data can be accessed by several Facets from the same Diamond. Instead of calling multiple contracts and providing that data separately to each (like repeating unnecessarily who the “from” address is), a single source for the data exists that all facets can use.
Smart contracts are limited in size to 24kb. This is the size of a contract’s code, not the data it creates or uses when executing that code. The Solidity program itself, when compiled and stored on the blockchain, is limited to 24kb. Anything greater would be enormously difficult to include in a single block due to the size limitations of blocks themselves. Diamonds solve this problem by allowing functionality to be distributed over several contracts and called upon as facets.
Facets can also be a method of introducing new functionality to contracts over time. Diamonds can be either immutable or update-able. A Diamond can launch with a single Facet as it’s functionality, and expand over time, then become immutable when all Facets have been added and the changes are complete.
Diamonds on Ethereum Today
Unlike other EIPs covered in this newsletter, this one is already live and usable on Ethereum today! Development on EIP-2535 lasted 4 years, and completed in the past year. Since the EIP doesn’t introduce any new functionality to Ethereum but instead creates a standard for how to do things, it is already live. There are several implementations on Ethereum already.
Another thing that stands out with EIP-2535 is how strongly the proposal has been championed by its creator, Nick Mudge. There is a Discord server, a Substack, and plenty of documentation he’s created to support implementations of the Diamonds standard (find them in the Additional Reading section below!). It really goes above and beyond.
In Conclusion
Wherever a limitation exists on Ethereum, there are brilliant people working on fixing it. Increasingly interoperable contracts can enhance user experience, reduce on-chain bloat, and create specialized services for creating Facets as reusable repositories, or Diamonds as directories of convenience. The possibilities are exciting, and still in early days! I’ll recommend you subscribe to Nick Mudge’s Substack below if you’re interested in more.
Thank You & Additional Reading!
Thanks a lot for reading! Here are some more resources if you'd like to dive deeper.
Please like this post and sign up for more simple write-ups on blockchain concepts.
If you’d like me to cover any topics or have any questions, reach out in the comments!
Share a Summary
Thanks again, please consider sharing this write-up below!
Stay kind. Stay curious.