Code Monkey home page Code Monkey logo

diamond's Introduction

Diamond Standard Reference Implementation

This is the gas-optimized reference implementation for the diamond standard.

Specifically this is a gas efficient implementation of the diamondCut function and the Diamond Loupe functions from the diamond standard.

The diamondCut implementation avoids storage read and writes. Fits 8 function selectors in a single storage slot. This is a gas optimization.

The contracts/Diamond.sol file shows an example of implementing a diamond.

The contracts/DiamondStorageContract.sol file shows how to implement Diamond Storage.

The test/diamondTest.js file gives tests for the diamondCut function and the Diamond Loupe functions.

How to Get Started Making Your Diamond

  1. The most important thing is reading and understanding the Diamond Standard. If something is unclear let me know!

  2. The second important thing is using the Diamond Standard reference implementation. You are at the right place because this is the README for the reference implementation.

The reference implementation is more than a reference implementation. It is the boilerplate code you need for a diamond. It is tested and it works. Use it. Also, using the reference implementation makes your diamond compliant with the standard.

Specifically you should copy and use the DiamondFacet.sol and DiamondLoupeFacet.sol contracts as is. They implement the diamondCut function and the loupe functions.

The Diamond.sol contract could be used as is, or it could be used as a starting point and customized. The contract name should be changed to what you want to call your diamond. This contract is the diamond proxy.

The DiamondStorageContract.sol contract could be used as is. It shows how to implement Diamond Storage. This contract includes contract ownership which you might want to change if you want to implement DAO-based ownership or other form of contract ownership. Go for it. Diamonds can work with any kind of contract ownership strategy.

Calling Diamond Functions

In order to call a function that exists in a diamond you need to use the ABI information of the facet that has the function.

Here is an example that uses web3.js:

let myUsefulFacet = new web3.eth.Contract(
  MyUsefulFacet.abi, 
  diamondAddress
)

In the code above we create a contract variable so we can call contract functions with it.

In this example we know we will use a diamond because we pass a diamond's address as the second argument. But we are using an ABI from the MyUsefulFacet facet so we can call functions that are defined in that facet. MyUsefulFacet's functions must have been added to the diamond (using diamondCut) in order for the diamond to use the function information provided by the ABI of course.

Similarly you need to use the ABI of a facet in Solidity code in order to call functions from a diamond. Here's an example of Solidity code that calls a function from a diamond:

string result = MyUsefulFacet(diamondAddress).getResult()

Get Help and Join the Community

If you need help or would like to discuss diamonds then send me a message on twitter, or email me. Or join the Diamond Standard Discord server.

Useful Links

  1. Understanding Diamonds on Ethereum
  2. Solidity Storage Layout For Proxy Contracts and Diamonds
  3. New Storage Layout For Proxy Contracts and Diamonds
  4. Diamond Setter
  5. Upgradeable smart contracts using the Diamond Standard
  6. buidler-deploy supports diamonds

Author

The diamond standard and reference implementation were written by Nick Mudge.

Contact:

License

MIT license. See the license file. Anyone can use or modify this software for their purposes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.