npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gas-optimized-diamond-1

v1.1.3

Published

Gas-optimized diamond for adding/replacing/removing functions.

Downloads

7

Readme

Diamond Standard Gas Optimized Diamond

This is the gas-optimized reference implementation for EIP-2535 Diamond Standard.

Specifically this is a gas efficient implementation of the diamondCut function. Adding/replacing/removing is optimized to take the least gas.

The loupe functions are NOT gas optimized. In this implementation the facets, facetFunctionSelectors, facetAddresses loupe functions are not meant to be called on-chain and may use too much gas or run out of gas when called in on-chain transactions. In this implementation these functions should be called by off-chain software like websites and Javascript libraries etc., where gas costs do not matter.

However the facetAddress loupe function is gas efficient and can be called in on-chain transactions.

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/facets/DiamondCutFacet.sol file shows how to implement the diamondCut external function.

The contracts/facets/DiamondLoupeFacet.sol file shows how to implement the four standard loupe functions.

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

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

A simpler implementation of a diamond exists here: https://github.com/mudgen/diamond

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 gas-optimized 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 DiamondCutFacet.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. This contract is the diamond. Its deployment creates a diamond. It's address is a stable diamond address that does not change.

The LibDiamondStorage.sol library 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.

The LibDiamondCut.sol library contains an internal function version of diamondCut that can be used in the constructor of a diamond or other places.

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. EIP-2535 Diamond Standard
  2. Understanding Diamonds on Ethereum
  3. Solidity Storage Layout For Proxy Contracts and Diamonds
  4. New Storage Layout For Proxy Contracts and Diamonds
  5. Diamond Setter
  6. Upgradeable smart contracts using the Diamond Standard
  7. 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.