Hardhat
What Hardhat is, what it is used for, and how to use it for development on the XDC Network.
🧭 Table of contents
📰 Overview
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. Get Solidity stack traces & console.log.
What you will learn
This article will teach you what Hardhat is, what Hardhat is used for, and how to use the features of Hardhat for development on the XDC Network.
What you will do
What Hardhat is
How to create and configure Hardhat project
Overview of Hardhat tasks
Hardhat plugins
📰 About Hardhat
Hardhat is development which lets you compile and deploy smart contracts on Eethereum-like networks. Hardhat has extensible tooling platform, solidity debugger, plugin ecosystem, Typescript support and more. It is one of the most popular development environments along with Remix and Truffle.
Why do you need development environment
Deploying smart contracts is a very complex task and can cost you a lot of money if you do it wrong, that's why you need to use development environment like Hardhat.
Hardhat comes with lots of tools out of the box which make you life easier and increase your productivity as dApp developer. Hardhat debugging and testing tools allow you to weed out bugs and vulnerabilities in your smart contract before deploying it to live network.
⚒ Create Hardhat Project
To create new Hardhat run this command:
Hardhat gives your three options how to initialize your project:
You can choose to create Javascript or Typescript project, which will create hardhat.config.js
and contracts
, scripts
, test
with some example files. Or you can just create empty hardhat.config.js
file and do everything by yourself.
⚒ Hardhat folder structure
Hardhat has the following folders:
artifacts
: place where smart contract compilation artifacts are storedcache
: folder used for caching internal datacontracts
: folder for smart contract source codescripts
: folder where scripts which interact with Hardhat Runtime Environment are stored including script for deploying your smart contractstest
: place to store tests
⚒ Hardhat Config
Hardhat config is stored in hardhat.config.js
. Config defines which network and accounts hardhat will use, which version of compiler it will run, can be used to configure custom folder structure or configure tests.
Here is an example of hardhat config:
defaultNetwork
: network which is used by default when you run hardhatnetworks
: list of blockchain networks you will useurl
: JSON-RPC url of networkaccounts
: list of accounts you will use to interact with network
solidity
: solidity compiler configurationversion
: compiler versionsettings
: compiler settings with the same schema assettings
in Solidity JSON input interface
You can read more about Hardhat configuration here: Hardhat Configuration.
⚒ Hardhat Tasks
Hardhat task is a command that automates things like deploying, compiling, testing and more. Hardhat comes with some tasks out of box, but you can always define your own tasks using the same Hardhat API.
Open hardhat console:
Compile contracts:
Running tests:
Running scripts (for example deploying):
Clears cache
and deletes all articats
:
To flatten your smart contract in case it uses imports:
You can explore more built-in tasks by running:
Or get full information on a specific task by running
⚒ Hardhat Debugger
You can print messages when testing you smart contract by using console.log()
.
To do this, import hardhat/console.sol
in your project:
And then add console.log
anywhere in your code:
Now you can add:
somewhere in your tests
code and once it reaches this line of code in smart contract it will print hello
in the console!
⚒ Hardhat Plugins
Hardhat plugins are extensions that add new functionality to Hardhat.
Some of popular Hardhat plugins are:
@nomiclabs/hardhat-ethers
: injectsethers.js
into Hardhat Runtime Environment@nomiclabs/hardhat-web3
: injectsWeb3
into Hardhat Runtime Environment@typechain/hardhat
: typechain support for hardhat
You can explore hardhat plugins on Hardhat Plugins homepage.
To add new plugin simply run:
And then add it to Hardhat by putting require
in hardhat.config.js
:
⚒ Using Hardhat to develop on XDC
Hardhat development experience on XDC is mostly indistinguishable from any other Ethereum-like network, but there are still some things you need to learn before starting developing on XDC.
⚒ Configuring XDC Mainnet and Apothem Testnet on Hardhat
In order to get started deploying new contracts on XDC Mainnet and/or Apothem, we need to install a new dependency called dotenv
that will be used in the hardhat.config.js
file:
We will need to configure a .env
file with XDC Mainnet and Apothem Testnet RPC endpoints, plus the Private Key of the wallet we are using for deployment. Lets start by running:
And writting the following info in our .env file:
🚨 Do not use the Private Key in the example above in production or you can risk losing your assets! 🚨
And finally, we can configure the hardhat.config.js
file for both Apothem and XinFin Networks by writting:
And now you can deploy and interact with your contracts on XDC mainnet by running:
or on testnet:
For more information about Hardhat, Please Visit Hardhat Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook.
Last updated