Using Hardhat

Setting up and launching an DRC20 Token Smart Contract on the Deelance Testnet using Hardhat.

Using Hardhat

What is Hardhat ?

Hardhat is a popular development framework and tool suite designed for building, testing, and deploying smart contracts and decentralized applications (DApps) on blockchain platforms. Like Truffle, Hardhat streamlines the development process and provides a range of features to aid developers in working with blockchain technologies.

Configuring the Development Workspace

Before we dive in, ensure you have the following essentials installed: Requirements:

Initialize Project

Our first task is to set up a Hardhat project.

  • Create a new directory for your Hardhat project

mkdir PepeDeelance
cd PepeDeelance
  • Next, execute npm init -y to generate a package.json file.

  • Afterward, install Hardhat with the following command:

npm install --save-dev hardhat

Now, If you run npx hardhat, you will be shown some options to facilitate project creation:

$ npx hardhat
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.17.1

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

We recommend selecting the option "Create a JavaScript project."

After selecting the "Create a JavaScript project" option, you will be prompted to install the following dependencies.

npm install --save-dev "hardhat@^2.17.1" "@nomicfoundation/hardhat-toolbox@^3.0.0"

Finally, the Hardhat project is initialized.

To initially familiarize yourself with the available features and the current status, execute npx hardhat within your project directory. This will produce the following output in the terminal:

$ npx hardhat
Hardhat version 2.17.1

Usage: hardhat [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]

GLOBAL OPTIONS:

  --config              A Hardhat config file.
  --emoji               Use emoji in messages.
  --flamegraph          Generate a flamegraph of your Hardhat tasks
  --help                Shows this message, or a task's help if its name is provided
  --max-memory          The maximum amount of memory that Hardhat can use.
  --network             The network to connect to.
  --show-stack-traces   Show stack traces (always enabled on CI servers).
  --tsconfig            A TypeScript config file.
  --typecheck           Enable TypeScript type-checking of your scripts/tests
  --verbose             Enables Hardhat verbose logging
  --version             Shows hardhat's version.


AVAILABLE TASKS:

  check                 Check whatever you need
  clean                 Clears the cache and deletes all artifacts
  compile               Compiles the entire project, building all artifacts
  console               Opens a hardhat console
  coverage              Generates a code coverage report for tests
  flatten               Flattens and prints contracts and their dependencies. If no file is passed, all the contracts in the project will be flattened.
  gas-reporter:merge
  help                  Prints this message
  node                  Starts a JSON-RPC server on top of Hardhat Network
  run                   Runs a user-defined script after compiling the project
  test                  Runs mocha tests
  typechain             Generate Typechain typings for compiled contracts
  verify                Verifies a contract on Etherscan

To get help for a specific task run: npx hardhat help [task]

The things you can do are shown in a list. Some are already there, and some come when you add extra tools/packages. Learn More.

After initialization, your Hardhat project's folder structure will look like this.

project-root/
├── hardhat.config.js
├── contracts/
│   └── YourContract.sol
├── scripts/
│   └── yourScript.js
├── test/
│   └── yourTest.js
├── node_modules/
├── package.json
├── package-lock.json
└── .gitignore

Create an DRC20 Token Contract on Deelance Testnet using Hardhat.

  • Make a file named "PepeDeelance.sol" in the "contracts/" folder of your project.

  • Copy and paste the code provided below.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

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

contract PepeDeelance is ERC20, Ownable {
    constructor() ERC20("PepeDeelance", "PEPEDLANCE") {
        _mint(msg.sender, 1000000000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

Ensure you have the @openzeppelin/contracts package since our Solidity code relies on it. To get it, execute: npm i @openzeppelin/contracts in terminal.

  • Now, in your terminal, run the command npx hardhat compile , This will compile the contracts located in the "contracts/" folder.

  • Once the contract compiles without errors, generate a deploy.js file within the scripts/ directory and insert the following code:

const hre = require("hardhat");

async function main() {
  console.log("Deploying Contract....");

  const PepeDeelanceFactory = await hre.ethers.getContractFactory(
    "PepeDeelance"
  );
  const PepeDeelance = await PepeDeelanceFactory.deploy();

  console.log("waiting for Transaction Confirmations");
  const res = await PepeDeelance.deploymentTransaction().wait(2);

  console.log(`==============================`);
  console.log(`Contract Deployed : ${res.contractAddress}`);
  console.log(`Transaction Hash : ${res.hash}`);
  console.log(`Gas Used : ${res.gasUsed}`);
  console.log(`Gas Price : ${res.gasPrice}`);
  console.log(`==============================`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
  • Next, copy the following code into the hardhat.config.js file.

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

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.19",
  networks: {
    deelanceTestnet: {
      url: "https://testnet-rpc.deelance.com/",
      chainId: 455214,
      accounts: ["<PRIVATE_KEY>"],
    },
  },
};

Ensure you replace <PRIVATE_KEY> with the actual private key of your Wallet, essential for executing transactions.

Deploying on Deelance Testnet

  • We're now set to deploy our Smart Contract on the Deelance Testnet. To proceed with the deployment, execute the following command in your terminal:

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

Executing this will produce an output in the terminal similar to the following:

$ npx hardhat run --network deelanceTestnet scripts/deploy.js
Deploying Contract....
waiting for Transaction Confirmations
==============================
Contract Deployed : 0xFe52162DCe322Ba797FB0329b2a9409CCe29Eb32
Transaction Hash : 0xffc881d291732891006fbda071cdeF124d1a6E139bc30550daf36453411eb9bd
Gas Used : 1518972
Gas Price : 7
==============================

Keep in mind that your address, transaction_hash, and other details might vary. The above is merely an illustration of the expected structure.

Well done! You've successfully deployed the PepeDeelance DRC20 Token Smart Contract. You can now engage with the Smart Contract.

Last updated