Hardhat

Create and deploy your Smart Contract using Hardhat.

Requirements

To complete the tutorial npm has to be installed.

npm install -g npm

Step 1: Project init

First, we’ll need to create a folder for our project. Navigate to your command line and type

mkdir hello-world
cd hello-world

Now that we’re inside our project folder, we’ll use npm init to initialize the project.

npm init # (or npm init --yes)

It doesn’t matter how you answer the installation questions. Approve the package.json, and we’re good to go!

Step 2: Install Hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers build smart contracts and dApps locally before deploying to the live chain.

Inside our hello-world project run:

npm install --save-dev hardhat

Check out this page for more details on installation instructions.

Inside our hello-world project folder, run:

npx hardhat

You should then see a welcome message and the option to select what you want to do. Select “create an empty hardhat.config.js”:

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

👷 Welcome to Hardhat v2.10.1 👷‍

? What do you want to do? … 
  Create a JavaScript project
  Create a TypeScript project
> Create an empty hardhat.config.js
  Quit

This command will generate a hardhat.config.js file, where we’ll specify all the configurations for our project (in step 13). You can choose to create the project in JavaScript or TypeScript, but remember that the deployment and interaction files will have to be changed accordingly!

If the installation does not require you to install the plugin @nomicfoundation/hardhat-toolbox, which has everything you need to develop smart contracts, install it with the following command:

npm install --save-dev @nomicfoundation/hardhat-toolbox

Add the highlighted line to your hardhat.config.js so that it looks like this:

require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.15",
  
};

Step 3: Write our smart contract

Create a folder to keep our smart contract code file. In the command line, type:

mkdir contracts
cd contracts

Open the hello-world project in your favourite editor (e.g., VSCode, IntelliJ IDEA). Smart contracts are written in a language called Solidity, which we will use to write our smart contract.‌

  1. Below, there are three sample smart contracts that we can choose for this tutorial.

  2. Navigate to the “contracts” folder and create a new file with the name of the smart contract you choose (e.g., Greeter.sol).

  3. Copy and paste the contents below into your file, and be sure to read the comments to understand what this contract does:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;
    }
}

Step 4: Connect Metamask

See Create a Metamask account.

Step 5: Update hardhat.config.js

We need to update hardhat.config.js so that our project knows the details about the 1DLT node. Add the 1DLT node information into the network list in hardhat.config.js

require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */

module.exports = {
  solidity: "0.8.15",
  networks: {
    node_1DLT: {
      url: "http://20.224.30.75:8545",
      chainId: 637707033, //change it with your node chainID
      accounts: ["YOUR ACCOUNT PRIVATE KEY"],
      },
  },
};

Step 6: Write the deployment script

Now that our contract is written and our configuration file is good to go, it’s time to write our contract deploy script.

Navigate to the scripts/ folder and create a new file called deploy.js, adding the following contents to it:

cd ..
mkdir scripts
cd scripts
const {ethers} = require("hardhat");

async function main() {
   const Greeter = await ethers.getContractFactory("Greeter");

   // Start deployment, returning a promise that resolves to a contract object
   const greeter = await Greeter.deploy("Hello World!");   
   console.log("Contract deployed to address:", greeter.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Hardhat does an amazing job explaining each of these lines of code in their Contracts tutorial. We’ve adopted their explanations here.

const Greeter = await ethers.getContractFactory("HelloWorld");

A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts, so Greeter here is a factory for instances of our hello world contract. When using the hardhat-ethers plugin ContractFactory andContract, instances are connected to the first signer (owner) by default.

const greeter = await Greeter.deploy();

Calling deploy() on a ContractFactory will start the deployment and return a Promise that resolves to a Contract object. This object has a method for each of our smart contract functions.

Step 7: Deploy the contract

Let's compile our contract to make sure everything is working so far. The compile task is one of the built-in Hardhat tasks.

From the command line, run:

npx hardhat compile

You might be warned about the SPDX license identifier not provided in the source file, but there is no need to worry about that.

We're finally ready to deploy our smart contract! Navigate to the command line and run:

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

You should then see something like:

Contract deployed to address: 0x6cd7d44516a20882cEa2DE9f205bF401c0d23570

Last updated