Deploying a Contract Using Hardhat

Hardhat is a development framework for Ethereum software, providing various tools for smart contract development, compilation, debugging, and deployment. Since dKargo is compatible with Ethereum's EVM, smart contracts can be deployed on the dKargo network.

This guide will walk through the process of building a Wallet dApp using Hardhat and dKargo Warehouse.

When deploying a contract on any blockchain, the native token of that chain is required to pay gas fees. This applies to dKargo Chain as well. Before deploying on the dKargo testnet (Warehouse), developers can obtain $DKA testnet tokens via the faucet.

Development Environment

Prerequisites

  • Node.js & npm/yarn:

  • Hardhat: Development environment for Ethereum.

    • Install with:

    npm install --save-dev hardhat

Step 1 - Set Up a Hardhat Project

  1. Initialize a new Hardhat project

mkdir helloDka
cd helloDka
npx hardhat init
  1. Install dotenv for managing environment variables.

npm i dotenv
touch .env
  1. Below is an example of the Hardhat project folder structure:

/helloDka
├── README.md
├── contracts
|  └── Lock.sol
├── hardhat.config.js
├── ignition
|  └── modules
|     └── Lock.js
├── package-lock.json
├── package.json
└── test
   └── Lock.js

Step 2 - Writing the Counter.sol Contract

  • Create a new file named Counter.sol inside the contracts folder.

  • Copy and paste the following code into the file.

The following contract implements a simple function to set the number variable.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract Counter {
    uint256 public number;
 
    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }
 
    function increment() public {
        number++;
    }
}

Step 3 - Updating the ignition file

  • Create a new file named Counter.js inside the ./ignition/modules folder.

  • Copy and paste the following code into the file.

This script follows the Hardhat Ignition standard and is responsible for deploying the contract.

const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");
 
module.exports = buildModule("Counter", (m) => {
  const lock = m.contract("Counter");
 
  return { lock };
});

Step 4 - Configuring Hardhat for dKargo Chain

  1. Add the necessary plugins and configurations to hardhat.config.js .

  • require('dotenv').config() : Loads environment variables from the .env file.

  • accounts: [process.env.PRIVATE_KEY] : Passes the private key stored in the .env file.

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.24",
  networks: {
    warehouse: {
      url: "<https://it-full.dknote.net>",
      accounts: [process.env.PRIVATE_KEY],
    },
  },
  ignition: {
    requiredConfirmations: 1
  }
};

  1. Add the following code to the file, and ensure that .env is included in the gitignore file to prevent exposing sensitive information.

PRIVATE_KEY=YOUR KEY HERE WITH NO QUOTES

Step 5 - Compile and Deploy

  1. Compile counter.sol.

npx hardhat compile
  1. Deploy the counter.sol contract to the Warehouse testnet:

npx hardhat ignition deploy ./ignition/modules/Counter.js --network warehouse

Step 6 - Viewing the Deployment Transaction

  1. Check the transaction details of the deployed contract.

npx hardhat ignition transactions chain-2465001
  1. Search for the transaction information on dScanner

Verify Contract

In Ethereum and other EVM-based blockchains, smart contracts are often verified using Hardhat's verify function.

However, dKargo Chain utilizes dScanner, a block explorer optimized for logistics services, which is not compatible with Hardhat’s verify function.

To ensure transparency of deployed contracts, the Verify Contract feature provided by dScanner can be used instead.

Last updated