Hardhat plugin for integration with hethers.js

Overview

npm hardhat

hardhat-hethers

Hardhat plugin for integration with hethers.js.

What

This plugin brings to Hardhat the Hedera library hethers.js, which allows you to interact with the Hedera hashgraph in a simple way.

Installation

npm install --save-dev 'hardhat-hethers'

And add the following statement to your hardhat.config.js:

require("hardhat-hethers");

Add network configuration in hardhat.config.js:

module.exports = {
  defaultNetwork: 'testnet',  // The selected default network. It has to match the name of one of the configured networks.
  hedera: {
    gasLimit: 300000, // Default gas limit. It is added to every contract transaction, but can be overwritten if required.
    networks: {
      testnet: {      // The name of the network, e.g. mainnet, testnet, previewnet, customNetwork
        accounts: [   // An array of predefined Externally Owned Accounts
          {
            "account": '0.0.123',
            "privateKey": '0x...'
          },
          ...
        ]
      },
      previewnet: {
        accounts: [
          {
            "account": '0.0.123',
            "privateKey": '0x...'
          },
          ...
        ]
      },
      ...
    },
    // Custom networks require additional configuration - for conesensusNodes and mirrorNodeUrl
    // The following is an integration example for the local-hedera package
    customNetwork: {
      consensusNodes: [
        {
          url: '127.0.0.1:50211',
          nodeId: '0.0.3'
        }
      ],
      mirrorNodeUrl: 'http://127.0.0.1:5551',
      chainId: 0,
      accounts: [
        {
          'account': '0.0.1001',
          'privateKey': '0x7f109a9e3b0d8ecfba9cc23a3614433ce0fa7ddcc80f2a8f10b222179a5a80d6'
        },
        {
          'account': '0.0.1002',
          'privateKey': '0x6ec1f2e7d126a74a1d2ff9e1c5d90b92378c725e506651ff8bb8616a5c724628'
        },
      ]
    }
  }
};

Read more about Externally Owned Accounts here.

Tasks

This plugin creates no additional tasks.

Environment extensions

This plugins adds an hethers object to the Hardhat Runtime Environment.

This object has the same API as hethers.js, with some extra Hardhat-specific functionality.

Provider object

A provider field is added to hethers, which is an hethers.providers.BaseProvider automatically connected to the selected network.

Helpers

These helpers are added to the hethers object:

Interfaces

interface Libraries {
  [libraryName: string]: string;
}

interface FactoryOptions {
  signer?: hethers.Signer;
  libraries?: Libraries;
}

Functions

  • function getSigners() => Promise;
const signers = await hre.hethers.getSingers();
  • function getSigner(identifier: any) => Promise;
const signer = await hre.hethers.getSigner({
    "account": "0.0.123",
    "privateKey": "0x..."
});
  • function getContractFactory(name: string, signer?: hethers.Signer): Promise;
const contractFactoryWithDefaultSigner = await hre.hethers.getContractFactory('Greeter');
const signer = (await hre.getSigners())[1];

const contractFactoryWithCustomSigner = await hre.hethers.getContractFactory('Greeter', signer);
  • function getContractFactory(name: string, factoryOptions: FactoryOptions): Promise;
const libraryFactory = await hre.hethers.getContractFactory("contracts/TestContractLib.sol:TestLibrary");
const library = await libraryFactory.deploy();

const contract = await hre.hethers.getContractFactory("Greeter", {
    libraries: {
        "contracts/Greeter.sol:TestLibrary": library.address
    }
});
  • function getContractFactory(abi: any[], bytecode: hethers.utils.BytesLike, signer?: hethers.Signer): Promise;
const greeterArtifact = await hre.artifacts.readArtifact("Greeter");

const contract = await hre.hethers.getContractFactory(greeterArtifact.abi, greeterArtifact.bytecode);
  • function getContractAt(name: string, address: string, signer?: hethers.Signer): Promise;
const Greeter = await hre.hethers.getContractFactory("Greeter");
const deployedGreeter = await Greeter.deploy();

const contract = await hre.hethers.getContractAt("Greeter", deployedGreeter.address);
  • function getContractAt(abi: any[], address: string, signer?: hethers.Signer): Promise;
const greeterArtifact = await hre.artifacts.readArtifact("Greeter");

const contract = await hre.hethers.getContractAt(greeterArtifact.abi, deployedGreeter.address);
  • function getContractFactoryFromArtifact(artifact: Artifact, signer?: hethers.Signer): Promise;
const greeterArtifact = await hre.artifacts.readArtifact("Greeter");

const contractFactoryFromArtifact = await hre.hethers.getContractFactoryFromArtifact(greeterArtifact);
  • function getContractFactoryFromArtifact(artifact: Artifact, factoryOptions: FactoryOptions): Promise;
const greeterArtifact = await hre.artifacts.readArtifact("Greeter");
const libraryFactory = await hre.hethers.getContractFactory(
    "contracts/TestContractLib.sol:TestLibrary"
);
const library = await libraryFactory.deploy();

const contract = await hre.hethers.getContractFactory(greeterArtifact, {
    libraries: {
        "contracts/TestContractLib.sol:TestLibrary": library.address
    }
});
  • function getContractAtFromArtifact(artifact: Artifact, address: string, signer?: hethers.Signer): Promise;
const Greeter = await hre.hethers.getContractFactory("Greeter");
const deployedGreeter = await Greeter.deploy();
const greeterArtifact = await hre.artifacts.readArtifact("Greeter");

const contract = await hre.getContractAtFromArtifact(greeterArtifact, deployedGreeter.address);

The Contract's and ContractFactory's returned by these helpers are connected to the first signer returned by getSigners by default.

Usage

There are no additional steps you need to take for this plugin to work.

Install it and access hethers through the Hardhat Runtime Environment anywhere you need it (tasks, scripts, tests, etc). For example, in your hardhat.config.js:

{ const balance = (await hethers.provider.getBalance('0.0.29631749')).toString(); c onsole.log(`Balance of "0.0.29631749": ${balance} tinybars`); }); module.exports = {};">
require("hardhat-hethers");

// task action function receives the Hardhat Runtime Environment as second argument
task('getBalance', 'Prints the the balance of "0.0.29631749"', async (_, {hethers}) => {
    const balance = (await hethers.provider.getBalance('0.0.29631749')).toString();
  c onsole.log(`Balance of "0.0.29631749": ${balance} tinybars`);
});

module.exports = {};

And then run npx hardhat getBalance to try it.

Read the documentation on the Hardhat Runtime Environment to learn how to access the HRE in different ways to use hethers.js from anywhere the HRE is accessible.

Library linking

Some contracts need to be linked with libraries before they are deployed. You can pass the addresses of their libraries to the getContractFactory function with an object like this:

const contractFactory = await this.env.hethers.getContractFactory("Example", {
    libraries: {
        ExampleLib: "0x...",
    },
});

This allows you to create a contract factory for the Example contract and link its ExampleLib library references to the address "0x...".

To create a contract factory, all libraries must be linked. An error will be thrown informing you of any missing library.

Troubleshooting

Events are not being emitted

Hethers.js polls the network to check if some event was emitted (except when a WebSocketProvider is used; see below). This polling is done every 4 seconds. If you have a script or test that is not emitting an event, it's likely that the execution is finishing before the event is detected by the polling mechanism.

If you are connecting to a Hardhat node using a WebSocketProvider, events should be emitted immediately. But keep in mind that you'll have to create this provider manually, since Hardhat only supports configuring networks via http. That is, you can't add a localhost network with a URL like ws://localhost:8545.

Comments
  • Local Hedera Network

    Local Hedera Network

    In order to enable local smart contract development, hardhat-hethers should support creating a local hedera network. The network must consist of consensus nodes and a mirror node.

    Notes:

    • the first time the network is created is going to be slow
    • secondary network launches should be faster

    Resources:

    • network repo: https://github.com/hashgraph/hedera-network-e2e
    Epic 
    opened by joan41868 1
  • More descriptive error messages for deploy and getDeployTransaciton

    More descriptive error messages for deploy and getDeployTransaciton

    When .deploy or .getDeployTransaction were called with an incorrect number of arguments an non-descriptive error was thrown. This PR changes it so that it has the same behaviour as hardhat-ethers.

    When fewer arguments are passed the error code is: MISSING_ARGUMENT. When more arguments are passed the error code is: UNEXPECTED_ARGUMENT.

    opened by Ivo-Yankov 0
  • feat: Add a workflow that publishes to NPM

    feat: Add a workflow that publishes to NPM

    Add a workflow that triggers when a pre-release is created. It raises the version in package.json, publishes to NPM and converts the pre-release to a normal release.

    • If the pre-release has the word minor or major in the name - a corresponding version type will be bumped, and that word will be removed from the name.
    opened by Ivo-Yankov 0
  • PoC Local Setup

    PoC Local Setup

    1. Run the consensus + mirror node manually
    2. Specify the URLS in the Hardhat Network config
    3. Be able to execute unit tests + deployment scripts against the network
    opened by Daniel-K-Ivanov 0
  • Feature/Network config with provider and signers

    Feature/Network config with provider and signers

    Signed-off-by: nikolay [email protected]

    Tasks:

    • https://github.com/LimeChain/hardhat-hethers/issues/3
    • https://github.com/LimeChain/hardhat-hethers/issues/4
    • https://github.com/LimeChain/hardhat-hethers/issues/5
    opened by natanasow 0
  • Support for `max_automatic_token_associations` at contract.deploy()

    Support for `max_automatic_token_associations` at contract.deploy()

    As a user I'd like to be able to set the property for max automatic token associations somewhere within the deploy() method of a contract.

    Reference for max_automatic_token_associations: This property is supported both for accounts and contracts, but the documentation is not yet updated so I am linking the cryptocreate page for reference instead.

    This is where the property should be populated. I am referencing it as a starting point.

    opened by georgiyazovaliiski 0
Releases(v1.0.2)
  • v1.0.2(Mar 25, 2022)

    What's Changed

    • fix: add a build command in the publish cicd by @Ivo-Yankov in https://github.com/LimeChain/hardhat-hethers/pull/38

    Full Changelog: https://github.com/LimeChain/hardhat-hethers/compare/v1.0.1...v1.0.2

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Mar 24, 2022)

  • v1.0.0(Mar 23, 2022)

    What's Changed

    • v1.0.0 by @Ivo-Yankov in https://github.com/LimeChain/hardhat-hethers/pull/36

    Full Changelog: https://github.com/LimeChain/hardhat-hethers/commits/v1.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
null
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Connect Web Integration illustrates the integration of Connect-Web in various JS frameworks and tooling

Connect Web Integration Connect Web Integration is a repository of example projects using Connect-Web with various JS frameworks and tooling. It provi

Buf 43 Dec 29, 2022
Hardhat plugin to track gas on the transaction level

hardhat-gas-trackooor Hardhat plugin to track gas on the transaction level. Example report Installation npm install hardhat-gas-trackooor --save-dev A

null 16 Jan 3, 2023
Build a Full Stack Marketplace on Ethereum with React, Solidity, Hardhat, and Ethers.js

Building a Digital Marketplace on Ethereum The technologies used in this workshop are React, Next.js, Tailwind CSS, HardHat, Solidity, and Ethers. Get

Nader Dabit 114 Nov 15, 2022
A web3 starter project using Typescript, Hardhat, ethers.js and @web3-react

Starter React Typescript Ethers.js Hardhat Project This repo contains a Hardhat and React Dapp starter project. The React Dapp in the frontend dir of

ChainShot 39 Dec 31, 2022
A Bed and Breakfast dApp run on Ethereum. Includes a token + schedule system (Solidity) and full front-end (React + ethers.js) built with Hardhat.

Hotel ETH - Watch Demo Video Hotel ETH A (fictional) Bed-and-Breakfast run on Ethereum Come Book a Room on Kovan or Rinkeby Networks View the Demo » C

Ryan Lambert 20 Aug 20, 2022
A fully-fledged Hardhat project template based on TypeScript.

Fully-Fledged Hardhat Project Template Based on TypeScript Installation It is recommended to install Yarn through the npm package manager, which comes

Pascal Marco Caversaccio 75 Dec 21, 2022
An NFT Marketplace built with NextJS, Hardhat and Solidity

??️ NFT Marketplace This is a fullstack DApp NFT Marketplace built as a study project to learn more about blockchain and smart contract development. M

Marcelo Kopmann 99 Dec 31, 2022
Solidity starter combining foundry and hardhat because both are great and I can't live without either...

Combination Pizza Hut & Taco Bell Foundry && HardHat starter template. Motivation I like them both. With this set-up we get: Unit tests written in sol

Cache Monet 32 Aug 23, 2022
Decentralized twitter using Solidity, Ethereum, hardhat, ethers, IPFS, Next.JS, TypeScript, TailwindCSS.

DWITTER: Decentralized Twitter Check out the deployed version of this app at https://dwtr.wajeshubham.in Transactions on Ethereum are slow. Therefore,

Shubham Waje 12 Sep 2, 2022
Use Hardhat & Foundry in the same project

Hardhat Foundry Starter This is a solidity starter template which lets you use both, Hardhat and Foundry. Why use both the tools? Foundry has some awe

Rajdeep Bharati 12 Aug 23, 2022
Build your zkp app with typescript, hardhat, circom, and snarkjs!

Zk app boilerplate Pre requisites Install rust and circom2 Getting started Clone or fork this template repository. git clone https://github.com/wanseo

Wanseob Lim 114 Dec 20, 2022
Build your zkp app with typescript, hardhat, circom, and snarkjs!

Zk app boilerplate Pre requisites Install rust and circom2 Getting started Clone or fork this template repository. git clone https://github.com/wanseo

null 33 May 18, 2022
Introductory fullstack ethereum dapp using: solidity, hardhat, react.js, ethers.js

Intro to Fullstack Ethereum Development Check out deployed dapp here! (Make sure you're connected to the Rinkeby Testnet) This article will help you e

Christian Chiarulli 77 Dec 21, 2022
A Typescript Hardhat-based template to develop evm-based smart contracts with all the tooling you need.

EVM-based Smart Contract Scaffold A Typescript Hardhat-based template to develop evm-based smart contracts with all the tooling you need. Features Use

Flair 8 Oct 24, 2022
Build your zkp app with typescript, hardhat, circom, and snarkjs!

Zk app boilerplate Pre requisites Install rust and circom2 Getting started Clone or fork this template repository. git clone https://github.com/wanseo

Privacy & Scaling Explorations (formerly known as appliedzkp) 35 May 29, 2022
Minimal template to get started with Foundry + Hardhat

Hardhat x Foundry Template Template repository for getting started quickly with Hardhat and Foundry in one project Getting Started Use Foundry: forge

Foundry 158 Jan 3, 2023