Deploying Smart Contract Using Hardhat
Last updated
Last updated
This section will guide you through deploying a Soulbound Token to the Klaytn Baobab Network using Hardhat.
Hardhat is a smart-contract development environment that will help you:
Develop and compile smart contracts.
Debug, test, and deploy smart contracts and dApps.
Soul-bound tokens(SBTs) are non-transferable NFTs. Meaning once acquired, they cannot be sold or transferred to another user. To learn more about SBTs, how it works and their use case, you can check out this reference article published by Vitalik Buterin.
By the end of this guide you will be able to:
Set up a Hardhat project on Klaytn.
Create a simple soul-bound token.
Compile your smart contract using Hardhat.
Test, deploy, and interact with your smart contract using Hardhat.
Explore Hardhat forking feature.
To follow this tutorial, the following are the prerequisites:
Code editor: a source-code editor such VS-Code.
Metamask: used to deploy the contracts, sign transactions and interact with the contracts.
RPC Endpoint: you can get this from one of the supported Endpoint Providers.
Test KLAY from Faucet: fund your account with sufficient KLAY.
To make use of hardhat, we need to set up our development environment and get hardhat installed. Let's do this in the following steps:
Step 1: Create a project directory
Step 2: Initialize an npm project
Paste this command in your terminal to create a package.json file
Step 3: Install hardhat and other dependencies:
Paste the code below in your terminal to install hardhat
Paste the code below to install other dependencies
Note: This installs other dependencies needed for this project ranging from
hardhat
,klaytn/contract
,dotenv
et al.
Step 4: Initialise a hardhat project:
Run the command below to initiate an hardhat project
For this guide, you'll be selecting a typescript project as seen below:
Note: While initializing the project, you will get a prompt to install
hardhat-toolbox
plugin. The plugin bundles all the commonly used packages and Hardhat plugins recommended to start developing with Hardhat.
After initializing a hardhat project, your current directory should include:
contracts/ – this folder contains smart contract code.
scripts/ – this folder contains code that deploys your contracts on the blockchain network.
test/ – this folder contains all unit tests that test your smart contract.
hardhat.config.js – this file contains configurations important for the work of Hardhat and the deployment of the soul-bound token.
Step 5: Create a .env file
Now create your .env file in the project folder. This file helps us load environment variables from an .env file into process.env.
Paste this command in your terminal to create a .env file
After creating our file, let's configure our .env file to look like this:
Note: You can also choose to use the configuration variable functionality provided by hardhat to configure variables that shouldn't be included in the code repository.
Step 6: Setup Hardhat Configs
Modify your hardhat.config.js
with the following configurations:
Now that we have our development environment all set, let's get into writing our soul-bound token smart contract.
In this section, you will use the Klaytn Contracts: a library for secure smart contract development built on a solid foundation of community-vetted code. It is a fork of open zeppelin contracts.
Note: You already installed this library in step 3 of the
Setting Development Environment
section.
Step 1: Select the contracts folder in the Explorer pane, click the New File button and create a new file named SBT.sol
Step 2: Open the file and paste the following code:
Code Walkthrough
This is your smart contract. line 1 shows that Hardhat uses the Solidity version 0.8.7 or greater. Other than that, it imports KIP17.sol and other supporting contracts. From lines 6-12, a smart contract that inherits KIP17 is been created. Also, the token name and symbol was passed in the constructor.
As you can see in the code above, the token name and symbol have been set to SoulBoundToken and SBT respectively. You can change the token name and symbol to anything you desire.
One major thing in this contract is that it prohibits token transfer, which makes the issued tokens soulbond.
In this section, we would be testing some of our contract functionalities.
Step 1: In the Explorer pane, select the test folder and click the New File button to create a new file named sbtTest.js
Step 2: Copy the code below in the sbtTest.js
file.
In the code you just copied, line 7 & 12 shows you imported expect from Chai and loadFixture from hardhat-network-helpers.
The tests above check the following:
Is the owner of a particular token id the same as who it was minted to?
Did it prohibit transfer of tokens between accounts?
Step 3: To run your test, run the command below:
For more in-depth guide on testing, please check Hardhat testing.
Scripts are JavaScript/Typescript files that help you deploy contracts to the blockchain network. In this section, you will create a script for the smart contract.
Step 1: In the Explorer pane, select the "scripts" folder and click the New File button to create a new file named sbtDeploy.js
.
Step 2: Copy and paste the following code inside the file.
Note: input your MetaMask wallet address in the
deployerAddr
variable.
Step 3: In the terminal, run the following command which tells Hardhat to deploy your SBT token on the Klaytn Test Network (Baobab)
Step 4: Open Klaytnscope to check if the SBT token has been deployed successfully.
Step 5: Copy and paste the deployed contract address in the search field and press Enter. You should see the recently deployed contract.
Hardhat provides developers the functionality of simulating the mainnet (at any given block) to a local development network. One of the major benefit of this feature is that it enables developers to interact with deployed contract and also write test for complex cases.
For this feature to work effectively, you need to connect to an archive node. You can read more about this feature here
Now that we have our Hardhat project set up let’s fork the Klaytn Mainnet using Hardhat. Open your terminal and run this command
You can also configure hardhat.config.js
- Hardhat Network to always do this:
Output
After successfully running this command, your terminal looks like the above image. You'll have 20 development accounts that are pre-funded with 10,000 test tokens.
The forked chain's RPC server is listening at http://127.0.0.1:8545/
. You can verify the forked network by querying the latest block number. Let's try to make a cURL to the RPC to get the block number. Open a new terminal window and use the following command:
Output
The output is an hexadecimal as seen above. To get the block number from the hex, convert the hex to a decimal using this tool. You should get the latest block number from the time you forked the network. You can confirm the block number on klaytnscope.
With hardhat, you can fork the mainnet at a particular block. In that case, let’s fork the chain at block number 105701850
.
To confirm the forked chain at the stated block, open a new terminal window and use the following command:
The output returns hexadecimal which when converted using this tool should be equal to 105701850
.
For more in-depth guide on Hardhat, please refer to Hardhat Docs. Also, you can find the full implementation of the code for this guide on GitHub