Deploying with Hardhat

What is Hardhat?

Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.For more information please visit: Hardhat Webpage

Setting up the development environment

Prerequisites:

SIX EVM on mainnet and testnet currently support solidity below version 0.8.20

Quick Start

To create the sample project, run npx hardhat init in your project folder:

$ npx hardhat init
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.22.12 👷‍

? What do you want to do? …
❯ Create a JavaScript project
  Create a TypeScript project
  Create a TypeScript project (with Viem)
  Create an empty hardhat.config.js
  Quit

Let’s create the JavaScript or TypeScript project and go through these steps to compile, test and deploy the sample contract. We recommend using TypeScript, but if you are not familiar with it just pick JavaScript.

Hardhat Config

if you take a look in the root folder, you'll see hardhat.config.ts :

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.18",
  networks: {
    fivenet: {
      url: "https://rpc-evm.fivenet.sixprotocol.net:443",
      accounts: ["0xPrivateKey1", "0xPrivateKey2"],
      gas: "auto",
      allowUnlimitedContractSize: true,
      blockGasLimit: 10000000,
    },
    sixnet: {
      url: "https://sixnet-rpc-evm.sixprotocol.net:443",
      accounts: ["0xPrivateKey1", "0xPrivateKey2"],
      gas: "auto",
      allowUnlimitedContractSize: true,
      blockGasLimit: 10000000,
    },
  },
  etherscan: {
    apiKey: {
      fivenet: "ANY STRING VALUE",
      sixnet: "ANY STRING VALUE",
    },
    customChains: [
      {
        network: "fivenet",
        chainId: 150,
        urls: {
          apiURL: "https://fivenet.evm.sixscan.io/api",
          browserURL: "https://fivenet.evm.sixscan.io/",
        },
      },
      {
        network: "sixnet",
        chainId: 98,
        urls: {
          apiURL: "https://evm.sixscan.io/api",
          browserURL: "https://evm.sixscan.io/",
        },
      }
    ],
  },
};

export default config;

Compiling SmartContract

if you take a look in the contracts/ folder, you'll see Lock.sol:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
    uint public unlockTime;
    address payable public owner;

    event Withdrawal(uint amount, uint when);

    constructor(uint _unlockTime) payable {
        require(
            block.timestamp < _unlockTime,
            "Unlock time should be in the future"
        );

        unlockTime = _unlockTime;
        owner = payable(msg.sender);
    }

    function withdraw() public {
        // Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
        // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

        require(block.timestamp >= unlockTime, "You can't withdraw yet");
        require(msg.sender == owner, "You aren't the owner");

        emit Withdrawal(address(this).balance, block.timestamp);

        owner.transfer(address(this).balance);
    }
}

To compile it, simply run:

npx hardhat compile

Deploying your contract

Next, to deploy the contract we will use a Hardhat Ignition module.Inside the ignition/modules folder you will find a file with the following code:

// This setup uses Hardhat Ignition to manage smart contract deployments.
// Learn more about it at https://hardhat.org/ignition

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const JAN_1ST_2030 = 1893456000;
const ONE_GWEI: bigint = 1_000_000_000n;

const LockModule = buildModule("LockModule", (m) => {
  const unlockTime = m.getParameter("unlockTime", JAN_1ST_2030);
  const lockedAmount = m.getParameter("lockedAmount", ONE_GWEI);

  const lock = m.contract("Lock", [unlockTime], {
    value: lockedAmount,
  });

  return { lock };
});

export default LockModule;

You can deploy it using npx hardhat ignition deploy ./ignition/modules/Lock.ts --network fivenet

Verify your contract

You can verify Lock.sol:Lock contract by using npx hardhat ignition verify chain-150 --include-unrelated-contracts

Last updated