Reusable On-Chain Facets for Smart Contract Platforms
Diamonds enable on-chain reusable smart contracts for on-chain libraries and platforms.
One of the exciting things about EIP2535 Diamonds is that it makes reusable on-chain smart contracts possible.
It has been very common to redeploy the same Solidity smart contract code over and over and over again.
For example, there are many, many copies of OpenZeppelinโs ERC721 smart contract code on blockchains.
What if we could deploy one ERC721 smart contract and then just reuse it on-chain without ever having to deploy it again? What if we could do that with lots of kinds of smart contracts? Well that is possible, and diamonds is something that makes that possible.
How? Very simple. Implement an ERC721 facet and deploy it. Use Diamond Storage so that the ERC721 facet handles its own management of the state that it declares. The facet can store its state at a unique location in the diamond proxy so that it will not conflict with the state defined by other facets. Remember that facets store state in a diamond proxy contract, not in themselves. Other facets that want to extend or build on the ERC721 facet can do so by importing the same struct that the ERC721 facet uses and accessing it at the location specified for it.
Defining and managing the state from different facets in a diamond can be simple and easy as long as it is well organized โ Diamond Storage makes that possible.
And so one ERC721 facet gets deployed to a blockchain, and its verified source code is made public. And then anyone can add the functions of that on-chain facet to any of their diamonds. It is really simple and obvious. One deployed ERC721 facet and unlimited on-chain use by an unlimited number of diamonds.
I have been hinting or saying this on-chain reusable nature of facets for awhile:
Some people and projects have started to run with this. Checkout this diagram from the article Smart Contracts as Apps:
And here is quote from the article:
DAOs allow people to make decisions democratically, we could take advantage of this beautiful fact to develop a decentralized repository of certified facets. The task of this hypothetical DAO would be the same thing as what the Apple employees are doing on apps in the review process while publishing an app on the App Store.
Then each time a facet is being cut, the diamond will check if that facet is certified. I imagine that multiple repositories will flourish creating different โfacets storesโ, this reminds me of the Linux ecosystem in which every distro has its own repository containing the packages available to the users.
Anyone can create a diamond and use the facets already certified by the DAO or propose their own facet to be certified and added to the Facets stores, without worrying about compatibility issues.
To be clear this article is talking about creating a repository of on-chain certified facets that work together that can be used to create custom diamonds.
I suggest you open a platform for discussion like github discussion tab, so other developer can provide guides. So we can help.