Flattening Smart Contracts with Hardhat
Use Hardhat to deploy and verify smart contract
🧭 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
In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy smart contract on both the XDC Network mainnet and XDC Apothem testnet and verify it on Block Explorer.
What you will do
Install and set up Hardhat
Create a complex smart contract with dependencies (like OpenZeppelin)
Compile the smart contract
Test the smart contract
Deploy the smart contract
Flatten the smart contract
Verify the smart contract
⚒ Starting a new Hardhat Project
There are a few technical requirements before we start. Please install the following:
Node.js v8+ LTS and npm (comes with Node)
Lets start by setting up our folder, we are creating a project called MyCounter
, create a new MyCounter
folder by running on terminal
We can get started with Hardhat by running:
And the following message will show on your console. Hit y
to continue or just press ENTER
:
The following message should log on your console:
Press ENTER
to get started with a new JavaScript Hardhat Project. Then, you will be presented with the following options:
The standard Hardhat project comes with a pre-created Lock.sol
contract and deploy.js
script. Let's clean up our working environment before moving forward:
And your folder files will look like this:
⚒ 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. Let's start by running:
And writing 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 writing:
⚒ Adding Testnet XDC to the Development Wallet
Let's check our Signer's Address on Hardhat by accessing the Hardhat console:
If you get an error that hardhat is not installed locally and is running on a Windows OS you will need to execute:
Once the hardhat console CLI opens, you can run:
This account is on the Ethereum standard format starting with 0x
, but we can simply switch 0x
for xdc
. In this case, our signer wallet address is: xdcA4e66f4Cc17752f331eaC6A20C00756156719519
.
With this account in hand, we can head to the Apothem Faucet and claim some TXDC for development purposes:
💵 Writing Smart Contract
We will be using OpenZeppelin for this guide so let's install it first:
Now let's create simple smart contract called MyCounter.sol
in contracts
folder:
💵 Compiling and Testing
We can now compile our MyCounter.sol
by running:
If everything is correctly configured and there are no errors, you should see the following message on your console:
And your folder should look like this:
Testing
Now, let's create a simple test to see if everything works as intended before we deploy our contract to the live network. This will save us time and gas fees, so it is recommended that you do as many tests as possible for your smart contracts before deploying.
Create file MyCounter.js
under test
directory:
Then run:
Your output should look like this:
💵 Deploying Contract
In order to deploy our newly compiled contract artifacts to the blockchain, we need to create a deployment script into the script folder:
And write the following script to the deploy.js
file:
If the deployment script has no errors, we can go ahead and run the command:
For deployment on XDC mainnet, or:
For deployment on the XDC Apothem Testnet. In either case, you need to have enough funds to pay for gas fees at the address that is being used for development.
If the deployment is successful, the console should log the following message after migrations complete processing:
💵 Flattening Contract
If a smart contract imports external files like ours, we need to flatten it before verifying on Block Explorer.
Now let's flatten our contract:
Then open MyCounterFlatten.sol
and remove every line which starts with // SPDX-License-Identifier
except the first one. We do this because Block Explorer does not accepts contracts with mutliple license definitions.
🔍 Verifying Contracts on the Block Explorer
Once you have successfully deployed your smart contract to the blockchain, it might be interesting to verify your contract on XinFin Block Explorer.
Let's grab the MyCounter.sol
address from the previous step: this address is in the Ethereum standard but we can simply swap the 0x
prefix for xdc
and search for our newly deployed contract on XinFin Block Explorer:
And click on the Verify And Publish
Option.
We will be redirected to the Contract verification page where we need to fill out:
Contract Name: MyCounter
Compiler: Check your
hardhat-config.js
file for Compiler VersionContract Code: Just paste everything from your
MyCounterFlatten.sol
file
❕ Keep in mind that Contract Code
should be MyCounterFlatten.sol
, not MyCounter.sol
. ❕
Once everything is filled out, press Submit!
If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract
:
For more information about Hardhat, Please Visit Hardhat Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook. Resources used during the deployment of the MyCounter can be found at MyCounter Contract Folder.
Last updated