Skip to content

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It is written in JavaScript language.

Notifications You must be signed in to change notification settings

vibhudawar/testing_contracts_hardhat

Repository files navigation

Hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It is written in JavaScript language.

Hardhat vs Truffle

Hardhat:

  • Faster migration process.
  • Has low gas consumption
  • Debugging is easy

Hardhat Installation

To install hardhat:

npm install --save-dev hardhat
npx hardhat (choose JavaScript project)

After this install the dependencies that are listed by the hardhat as additional dependencies.

Hardhat Compilation

Now make contracts in the contracts folder of the hardhat. Use the following command to first compile the contract.

cd contracts
npx hardhat compile

Hardhat Contract Testing

Then run the tests in the test folder, while use the script folder to deploy the contract on the testnet. For testing use command:

npx hardhat test

Hardhat Contract Debugging

First import the following file in the test. Then write the statements and then check using following.

import "hardhat/console.sol"
npx hardhat test

Hardhat Contract Deployment

First write deployment code in the deploy folder. In the deploy script, this code is universal:

main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });

npx hardhat run scripts/deploy.js

Hardhat Contract Deployment on testnet

First use the alchemy, to take the GOERLI (chain) URL and API_KEY and save them in .env file, using names:

GOERLI_URL
GOERLI_API_KEY
PRIVATE_KEY (from Metamask wallet)

And in hardhat config.js, run following:

require("dotenv").config();

/** @type import('hardhat/config').HardhatUserConfig */
require("@nomiclabs/hardhat-waffle");
const GOERLI_URL = process.env.GOERLI_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

module.exports = {
  solidity: "0.8.17", [use lastest version]
  networks: {
    goerli: {
      url: GOERLI_URL,
      accounts: [PRIVATE_KEY],
    },
  },
};

Also, remember to first install .env package in the project and then include this:

npm i dotenv
GOERLI_URL=[URL_FROM_ALCHEMY]
GOERLI_API_KEY= [API_KEY_FROM_ALCHEMY]
PRIVATE_KEY= [wallet PRIVATE_KEY]

Then run following command, to deploy on the testnet

npx hardhat run scripts/deploy.js --network goerli

Check the address of the contact on the goerli etherscan, by pasting token address.

About

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It is written in JavaScript language.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published