# 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.

{% hint style="info" %}
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.
{% endhint %}

## Development Environment

Prerequisites

* **Node.js & npm/yarn:**
  * Installed from [Node.js](https://nodejs.org/).
* **Hardhat**: Development environment for Ethereum.

  * Install with:

  ```bash
  npm install --save-dev hardhat
  ```

## Step 1 - Set Up a Hardhat Project

1. Initialize a new Hardhat project

```solidity
mkdir helloDka
cd helloDka
npx hardhat init
```

2. Install [dotenv](https://github.com/motdotla/dotenv) for managing environment variables.

```bash
npm i dotenv
touch .env
```

3. Below is an example of the **Hardhat project folder structure**:

```bash
/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 <mark style="color:blue;">`Counter.sol`</mark> 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.

```solidity
// 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 <mark style="color:blue;">`Counter.js`</mark> inside the <mark style="color:blue;">`./ignition/modules`</mark> folder.&#x20;
* Copy and paste the following code into the file.

This script follows the [**Hardhat Ignition standard**](https://hardhat.org/ignition/docs/getting-started#overview) and is responsible for deploying the contract.

```jsx
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 <mark style="color:blue;">`hardhat.config.js`</mark> <mark style="color:blue;"></mark><mark style="color:blue;">.</mark>

* &#x20;<mark style="color:blue;">`require('dotenv').config() :`</mark> Loads environment variables from the <mark style="color:blue;">`.env`</mark> file.
* `accounts: [process.env.PRIVATE_KEY] :`  Passes the private key stored in the <mark style="color:blue;">`.env`</mark> file.

```jsx
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
  }
};

```

2. Add the following code to the file, and ensure that <mark style="color:blue;">`.env`</mark> **is included in the** <mark style="color:blue;">`gitignore`</mark> **file** to prevent exposing sensitive information.

```bash
PRIVATE_KEY=YOUR KEY HERE WITH NO QUOTES
```

## Step 5 - Compile and Deploy

1. Compile <mark style="color:blue;">`counter.sol`</mark>.

```bash
npx hardhat compile
```

2. Deploy the <mark style="color:blue;">`counter.sol`</mark> contract to the Warehouse testnet:

```bash
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.

```jsx
npx hardhat ignition transactions chain-2465001
```

2. Search for the transaction information on [dScanner](https://warehouse.dscanner.io/)

## Verify Contract

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

However, **dKargo Chain** utilizes [dScanner](https://warehouse.dscanner.io/), 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](https://warehouse.dscanner.io/verify-contract) feature provided by [dScanner](https://warehouse.dscanner.io/) can be used instead.
