Giter Site home page Giter Site logo

pinkdiamond1 / hardhat-diamond-abi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from projectsophon/hardhat-diamond-abi

0.0 1.0 0.0 126 KB

Hardhat plugin to combine multiple ABIs into a Diamond ABI artifact.

License: GNU General Public License v3.0

JavaScript 3.63% TypeScript 83.24% Solidity 13.13%

hardhat-diamond-abi's Introduction

hardhat-diamond-abi

Hardhat plugin to combine multiple ABIs into a Diamond ABI artifact

What

The Diamond Pattern, or EIP-2535, is an upgrade and proxy pattern that registers functions from many different Smart Contracts, called Facets, into a single Diamond.

Once registered, all functions can be accessed through the Diamond; however, you still need the ABI for the original Facet to make interacting easy. This plugin can be used to combine all the ABIs from your Facets into a single ABI to work with any functions in your Diamond.

This is also very useful if you are using TypeChain and @nomiclabs/hardhat-ethers because we ensure that TypeChain will generate types for your Diamond ABI, which can then be loaded using:

const diamond = await hre.ethers.getContractAt("MyDiamond", CONTRACT_ADDRESS);

Installation

npm install hardhat-diamond-abi

Import the plugin in your hardhat.config.js:

require("hardhat-diamond-abi");

Or if you are using TypeScript, in your hardhat.config.ts:

import "hardhat-diamond-abi";

Tasks

This plugin hooks Hardhat's internal TASK_COMPILE_SOLIDITY_COMPILE_JOBS task, and doesn't provide any additional tasks.

The reason for this is two-fold:

  1. TypeChain hooks this same task, and we want to be able run before it. There aren't better places to hook into the compile pipeline, since most others are subject to the Hardhat cache mechanism.
  2. The Hardhat cache mechanism is very aggressive and will evict our Diamond ABI artifact unless we include our file before it generates the cache file. If we aren't included in the cache file, the Diamond ABI artifact will be removed from the artifacts directory the next time Hardhat is run.

TypeChain

As mentioned above, this plugin uses the same hook as TypeChain. If you load this plugin before TypeChain, it will see the artifact we generate and create TypeScript types for your Diamond ABI!

It is very important that you import this plugin before TypeChain, otherwise it won't see our artifact when it generates types.

+ import "hardhat-diamond-abi";
import "@typechain/hardhat"; // <-- Typechain plugin imported after hardhat-diamond-abi

Basic configuration

Set up your project (we'll use best_dapp_ever/) with the following minimal hardhat.config.js at the root. The only required property is name, which is used to name your Diamond ABI.

module.exports = {
  solidity: "0.8.10",
  diamondAbi: {
    // (required) The name of your Diamond ABI
    name: "BestDappEver",
  },
};

Your project structure should look like this:

j:~/best_dapp_ever/ $ tree
├── hardhat.config.js
└── contracts
    ├── FacetA.sol
    └── FacetB.sol

Now, when you use npx hardhat compile to compile your contracts, a new ABI will exist in your artifacts.

j:~/best_dapp_ever/ $ tree
├── hardhat.config.js
├── artifacts
│   ├── contracts
│   │   ├── FacetA.sol
│   │   │   ├── FacetA.dbg.json
│   │   │   └── FacetA.json
│   │   └── FacetB.sol
│   │       ├── FacetB.dbg.json
│   │       └── FacetB.json
│   └── hardhat-diamond-abi
│       └── BestDappEver.sol
│           └── BestDappEver.json
└── contracts
    ├── FacetA.sol
    └── FacetB.sol

Advanced configuration

If you'd like to adjust details about the included ABIs or individual functions, you can adjust any of these settings:

module.exports = {
  solidity: "0.8.10",
  diamondAbi: {
    // (required) The name of your Diamond ABI.
    name: "BestDappEver",
    // (optional) An array of strings, matched against fully qualified contract names, to
    // determine which contracts are included in your Diamond ABI.
    include: ["Facet"],
    // (optional) An array of strings, matched against fully qualified contract names, to
    // determine which contracts are excluded from your Diamond ABI.
    exclude: ["vendor"],
    // (optional) A function that is called with the ABI element, index, entire ABI,
    // and fully qualified contract name for each item in the combined ABIs.
    // If the function returns `false`, the function is not included in your Diamond ABI.
    filter: function (abiElement, index, fullAbi, fullyQualifiedName) {
      return abiElement.name !== "superSecret";
    },
    // (optional) Whether exact duplicate sighashes should cause an error to be thrown,
    // defaults to true.
    strict: true,
  },
};

hardhat-diamond-abi's People

Contributors

phated avatar github-actions[bot] avatar friskyfoxdk avatar ubinatus avatar

Watchers

 avatar

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.