Coder Social home page Coder Social logo

paulrberg / hardhat-packager Goto Github PK

View Code? Open in Web Editor NEW
35.0 3.0 5.0 4.6 MB

Hardhat plugin for preparing the contract artifacts and the TypeChain bindings for registry deployment

License: MIT License

Shell 0.84% TypeScript 95.04% Solidity 4.13%
hardhat hardhat-plugin npm-package typechain

hardhat-packager's Introduction

Hardhat Packager GitHub Actions Coverage Status Styled with Prettier License: MIT

Hardhat plugin for preparing the contract artifacts and the TypeChain bindings for registry deployment.

Description

This plugin builds on top the TypeChain plugin to prepare the contract artifacts and TypeChain bindings for being deployed to a package registry (e.g. npmjs.org). More specifically, it deletes all artifacts and bindings that are not in an allowlist of contracts, minifying the directory structure in the process.

Installation

First, install the plugin and its peer dependencies. If you are using Ethers or Waffle, run:

yarn add --dev hardhat-packager typechain @typechain/hardhat @typechain/ethers-v5

Or if you are using Truffle, run:

yarn add --dev hardhat-packager typechain @typechain/hardhat @typechain/truffle-v5

Second, import the plugin in your hardhat.config.js:

require("@typechain/hardhat");
require("hardhat-packager");

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

import "@typechain/hardhat";
import "hardhat-packager";

Required plugins

Tasks

This plugin adds the prepare-package task to Hardhat:

Prepares the contract artifacts and the TypeChain bindings for registry deployment

Environment Extensions

This plugin does not extend the Hardhat Runtime Environment.

Configuration

This plugin extends the HardhatUserConfig object with an optional packager object. This object contains one field, contracts. This is an array of strings that represent the names of the smart contracts in your project. The plugin uses this array as an allowlist for the artifacts and the bindings that should be kept for registry deployment.

An example for how to set it:

module.exports = {
  packager: {
    // What contracts to keep the artifacts and the bindings for.
    contracts: ["MyToken", "ERC20"],
    // Whether to include the TypeChain factories or not.
    // If this is enabled, you need to compile the TypeChain files with the TypeScript compiler before shipping to the registry.
    includeFactories: true,
  },
};

Usage

To use this plugin you need to decide which contracts you would like to be part of the package deployed to the registry. Refer to the configuration section above.

Then run this:

yarn hardhat prepare-package

And go look what you got in the artifacts and the typechain directory.

Tips

  • You may want to add the /artifacts, /contracts and /typechain globs to the files field in your package.json file.
  • You may want to blocklist some files, such as test contracts. You can do this via an .npmignore file.
  • See how the plugin is integrated in @hifi/protocol, and how the artifacts and the bindings are used in @hifi/deployers.

License

MIT © Paul Razvan Berg

hardhat-packager's People

Contributors

fvictorio avatar paulrberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hardhat-packager's Issues

Expose the class and call new() on it

At first, I thought that the idea of the packager was to expose classes from the typechain to a npm package way in order to import the typechain anywhere (another hh project maybe) and just call new on a factory right away. But this plugin doesn't (yet ?). Any plans on that in the future ? Cause I see that you are exporting abi, bytecode and then recreating the factory in your exemples. I've tried myself to publish a hardhat project to npm and exposing as well the typechain directly but it doesn't seems to work when importing from another hardhat project. All i'm getting is either SyntaxError: Unexpected token 'export' or SyntaxError: Cannot use import statement outside a module. It's a common error, I know, but I cannot make it work

Discussion: Integrate deployed contract address

Background

Right now, the deployment of contracts includes both the types and the artifacts (e.g. ABIs) needed to interact with the smart contract. Unfortunately, the deployed contract address is still something that has to be tracked manually somehow, either via documentation or via some proxy registry. I brought up this topic to @PaulRBerg on the following tweet, and documentation was brought again, but I feel it's not really a full solution (e.g. I tried to play around with the hifi-protocol contracts and I couldn't find the address in the repository's README for any network).

What's the problem?

I'm trying to refactor the way scaffold-eth uses the smart contracts in the front-end to highlight the benefits of hardhat-packager (see @austintgriffith's tweet). Right now it expects a tightly coupled monorepo where artifacts are easily built or assembled within the front-end, but more often than not, this isn't the case. This is a great example of how hardhat-packager can help by getting the contracts into a neatly npm package.

That being said, while trying to add hardhat-packager, I realized that the smart contract address was written into the file disk after deployment, and then fetched in the front-end. Unfortunately, hardhat-packager has no mechanisms to write the contract address on file disk or provide the front-end with a way to fetch the deployed and thus, need further work on top of the types and ABI import.

Describe the feature you'd like to discuss

I would like to open a discussion about how to tackle the problem in question. Here are some ideas:

  • Provide hardhat-packager with an additional task able to write a deployed contract address into file disk so it's also tracked as part of the npm package.
  • Provide hardhat-packager with the ability to use a Singleton Factory approach to either fetch or deploy the bytecode to a specific network.
  • Provide hardhat-packager with a proxy-like (e.g. ERC1820Registry) smart contract that can keep track of the contract addresses given a specific contract.

I'm aware that hardhat-packager isn't necessarily the best place to solve this, and I envision some hardhat.config.ts with another plugin could look something like this:

import "@typechain/hardhat";
import "hardhat-packager";
import "hardhat-deployer";
import "hardhat-registry";

where packager makes sure you have all the contracts' types/artifacts, deployer ensures they are deployed to a given network, and registry registers the address being used to fetch a specific contract version.

Looking forward to some feedback, thanks!

Error HH201: Could not set param noTypechain for task compile because its name is already used.

  1. Initialized a new Hardhat Typescript project with
    npx hardhat --> Create an advanced sample project that uses TypeScript

  2. Ran the installation command from README:
    yarn add --dev hardhat-packager typechain @typechain/hardhat @typechain/ethers-v5

  3. Running any hardhat task fails with the following error:

    Screenshot 2022-02-16 at 4 47 56 PM
  4. Searching for noTypechain yields the following results:

    Screenshot 2022-02-16 at 4 48 14 PM
  5. Removed @typechain/hardhat from package.json and reinstalled

  6. Works as expected

Looks like some kind of conflict for the @typechain/hardhat package

Related dependencies used:

"devDependencies": {
  "@typechain/ethers-v5": "^9.0.0",
  "@typechain/hardhat": "^4.0.0",
  "ethers": "^5.5.4",
  "hardhat": "^2.8.4",
  "hardhat-packager": "^1.2.1",
  "typechain": "^7.0.0",
}

SyntaxError: Cannot use import statement outside a module

When importing an npm package prepared with hardhat-packager, and attempting to construct a contract factory, users get this error:

SyntaxError: Cannot use import statement outside a module

Example script:

import { YourContract__factory} from "your-package/typechain/factories/YourContract__factory";
import { task } from "hardhat/config";

task("deploy:YourContract").setAction(async function (_, { ethers }) {
  const signers: SignerWithAddress[] = await ethers.getSigners();
  const chainlinkOperatorFactory: ChainlinkOperator__factory = new ChainlinkOperator__factory(signers[0]);
  ...
}

A potential solution would be to set "type": "module" in package.json, but I have to look into it. I'm not confident that that's all it takes.

Discovered thanks to @sshmaxime's report.

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.