Skip to content

Setting Up Your Environment Using Hardhat#

Hardhat is an environment developers use to test, compile, deploy and debug dapps based on any blockchain compatible Ethereum's EVM. Hardhat is a flexible and extensible task runner that helps you manage and automate the recurring tasks inherent to developing smart contracts and dapps.

This article, partially based on the Hardhat documentation shows you how to set up Hardhat and use it to build, test and deploy smart contracts on Flare.

Guide#

1. Set up the Environment#

Install the following dependencies:

Tip

Check the Official Guide by Hardhat if you have issues installing this package.

Once the above dependencies are installed, create an npm empty project by running the following commands in a terminal:

mkdir flare-tutorial
cd flare-tutorial
npm init

Press Enter on each of the prompts.

Finally, add Hardhat and a few dependencies to the project, since you will use them in this tutorial.

npm install --save-dev \
  hardhat \
  @nomicfoundation/hardhat-toolbox \
  @nomiclabs/hardhat-ethers \
  dotenv

2. Create a Hardhat Project#

Hardhat can quick-start your development by providing a sample project. Just run:

npx hardhat

You should see the following prompt:

Hardhat project creation prompt

Hardhat project creation prompt.

Choose the Create a JavaScript project with the Up and Down keys, and Press Enter. Then press Y for rest of the prompts.

When done, it should print Project created.

If you take a look in the contracts folder, you should find a sample source file called Lock.sol. It is a Solidity smart contract implementing a simple digital lock, where users can only withdraw funds after a given period of time:

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

// 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);
    }
}

3. Compile the Contracts#

To compile the sample project, simply run:

npx hardhat compile

Upon successful compilation it will print Compiled 1 Solidity file successfully.

4. Configure the Project#

In order to be deployed on any of the Flare networks, the project needs to be configured. Edit the hardhat.config.js file and replace its contents with the following:

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

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.17",
  networks: {
    hardhat: {
    },
    coston: {
      url: "https://coston-api.flare.network/ext/bc/C/rpc",
      accounts: [process.env.PRIVATE_KEY],
      chainId: 16
    },
    songbird: {
      url: "https://songbird-api.flare.network/ext/bc/C/rpc",
      accounts: [process.env.PRIVATE_KEY],
      chainId: 19
    },
    coston2: {
      url: "https://coston2-api.flare.network/ext/C/rpc",
      accounts: [process.env.PRIVATE_KEY],
      chainId: 114,
    },
    flare: {
      url: "https://flare-api.flare.network/ext/C/rpc",
      accounts: [process.env.PRIVATE_KEY],
      chainId: 14,
    }
  },
};

Then, create a file called .env at the root of you project (where the hardhat.config.js file resides) to store the private key for the account to use for testing. .env files are useful to store local information which should not be committed into the source repository. In this tutorial, you need to store your test account's private key in this format:

PRIVATE_KEY="0x0000000000000000000000000000000000000000000000000000000000000000"

That is, 64 hexadecimal characters after the 0x.

Caution

Make sure you never upload your .env file to a remote repository.

For this reason, the .gitignore file that Hardhat created for you already ignores .env files.

5. Test the Contract#

In the test folder you should find a ready-made test file that verifies the contract works as expected.

To run tests with Hardhat, you just need to run:

npx hardhat test

You should get:

Lock contract test results

Lock contract test results.

6. Deploy the Contract#

Finally, you will deploy the contract to Flare's test network, Coston2, using a Hardhat script from the scripts folder.

Run this command in the root of the project:

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

You should get an output similar to:

Lock with 1 ETH and unlock timestamp 1705592309 deployed to 0xdC7781FA9fA7e2d0313cd0229a5080B4e30663a5

The last part is the address where the contract has been deployed. You can check the status of the contract by copy and pasting this address in the Block Explorer.


Last update: 2023-06-06