Building with Hardhat

Building with Hardhat

Deploy a test token with Solidity, Ethers.js, and Hardhat

ยท

4 min read

On deploying Smart contracts, if we consider the tool with ease of use, we can go with remix, the online Ethereum development environment. If we want a more optimized development workflow while working on a bigger codebase, Hardhat is the go-to tool for that. Hardhat affords you a more custom configuration while deploying your Smart Contract. It is a better option for the development of Full Stack Ethereum Applications.

Let's briefly discuss Hardhat, Hardhat is an Ethereum Development Environment and framework used in Web3 Development. You can use Hardhat to deploy, compile and debug your Smart Contract code.

In this tutorial, we will be using Hardhat to deploy a basic Cryptocurrency. The objective of this article is to teach Web3 Development beginners how to configure and build their apps with Hardhat.

Basic Requirements:

  • Basic Command-line knowledge.

  • NodeJS should be installed in your system.

  • Metamask should be installed in your system.

  • Knowledge of JavaScript

INSTALLING THE NEEDED PACKAGES:

Initialize your npm package:

npm init --yes

Install Hardhat package:

npm install --save-dev hardhat

To instantiate the basic Smart Contract file structure we run:

npx hardhat

After running this select 'Create a basic sample project' press enter. Continue to press Enter until your project is created.

Press Enter for the already specified Hardhat Project root

Press Enter for the question on if you want to add a .gitignore

Press Enter for Do you want to install this sample project's dependencies with npm (@nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers)?

If you are using a MacBook the installation of @nomiclabs/hardhat-ethers should run immediately.

Else, if you're using a Windows, run:

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

Next, we install open zeppelin inorder to write our Smart Contract. In the terminal, you run:

npm install @openzeppelin/contracts

This gives you the required folder structure for your Smart Contract Development. Now you can create your Solidity file, DemoCoin.sol

You could write your contract inside your DemoCoin.sol file,

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract DemoCoin is ERC20 {
    constructor() public ERC20("Demo Coin", "DMC")
    {
        //mint function to indicate the address and the tokenID

        _mint(msg.sender, 10000 * 10 ** 18);
    }
}

You can now compile your contract,

npx hardhat compile

There should not be any error, if there is any, check carefully to ensure that you followed the steps till this point.

DEPLOYING YOUR CONTRACT

We will be deploying our contract to the Ropsten network. First, create a deploy.js file under the script folder in your files.

Write this code in the deploy.js file,

const hre = require("hardhat");

async function main() {

  // We get the contract to deploy
  const demoContract = await hre.ethers.getContractFactory("DemoCoin");
  const deployedContract = await demoContract.deploy();

  console.log("Your coin Contract Address:", deployedContract.address);

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
}
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
});

Now install a local environment variable module, this is used to save system specific variables and also to keep those information out from public access.

npm install dotenv

Now create a .env file in the project folder and populate it with this code.

ALCHEMY_API_KEY_URL = "enter your Alchemy url here"
ROPSTEN_PRIVATE_KEY = "enter your ropsten private key here"

We are using Alchemy as a node provider. Alchemy is AWS for Blockchain Technology. It helps us connect to the Blockchain network. It uses our private key to request for gas to mine a block in the Blockchain, that is, to deploy the project to the Blockchain network. You can checkout how to create Alchemy App here

Next we write the configuration code,

require("@nomiclabs/hardhat-waffle");
//to add the dotenv environment
require("dotenv").config({ path: ".env" });

const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL;

const ROPSTEN_PRIVATE_KEY = process.env.ROPSTEN_PRIVATE_KEY;

module.exports = {
  solidity: "0.8.4",
  networks: {
    ropsten: {
      url: ALCHEMY_API_KEY_URL,
      accounts: [ROPSTEN_PRIVATE_KEY],
    },
  },
};

Then deploy using your terminal.

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

This should successfully deploy and generate our Token Contract address.

0xc416253522013394c186319f5218f54BC4CFbBFF

You can check out the token here.

If you worked to this point, Congratulations ๐Ÿš€๐Ÿš€ You've learnt how to deploy your Smart Contract using Hardhat.