Hardhat
Detailed guide by Tanssi
Hardhat is an Ethereum development environment that helps developers manage and automate the recurring tasks inherent to building smart contracts and dApps. Hardhat can be used with any EVM appchain to build, compile, and deploy smart contracts, thanks to the seamless compatibility of Parodychain.
This guide will cover how to use Hardhat to compile, deploy, and interact with Ethereum smart contracts deployed to the Parodychain.
Checking Prerequisites
For this guide, you'll need to have MetaMask installed and configured to work with Parodychain.
Creating a Hardhat Project
You must create a Hardhat project if you don't already have one. You can create one by completing the following steps:
Create a directory for your project
Initialize the project, which will create a
package.json
fileInstall Hardhat
Create a project
Note
npx
is used to run executables installed locally in your project. Although Hardhat can be installed globally, installing it locally in each project is recommended so you can control the version on a project-by-project basis.A menu will appear allowing you to create a new project or use a sample project. For this example, you can choose Create an empty hardhat.config.js
npx hardhat init888 888 888 888 888888 888 888 888 888888 888 888 888 8888888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888888 888 "88b 888P" d88" 888 888 "88b "88b 888888 888 .d888888 888 888 888 888 888 .d888888 888888 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.2 👷 What do you want to do? … Create a JavaScript project Create a TypeScript project Create a TypeScript project (with Viem) Quit
This will create a Hardhat config file (hardhat.config.js
) in your project directory.
Once you have your Hardhat project, you can also install the Ethers plugin. This provides a convenient way to use the Ethers.js library to interact with the network. To install it, run the following command:
Additionally, you'll need to install the hardhat-ignition-ethers
plugin to enable deployment of smart contracts with Hardhat Ignition. You can install it with the following command:
The Contract File
With your empty project created, you will create a contracts
directory next. You can do so by running the following command:
The smart contract that you'll deploy as an example will be called Box
, it will let you store a value that can be retrieved later. In the contracts
directory, you can create the Box.sol
file:
Open the file and add the following contract to it:
The Hardhat Configuration File
Before you can deploy the contract to your Tanssi appchain, you'll need to modify the Hardhat configuration file and create a secure file to store your private key in.
You can modify the hardhat.config.js
file to use Parodychain:
Congratulations! You are now ready for deployment!
Compiling Solidity
To compile the contract you can simply run:
npx hardhat compileCompiled 8 Solidity files successfully (evm target: paris).
After compilation, an artifacts
directory is created: it holds the bytecode and metadata of the contract, which are .json
files. Adding this directory to your .gitignore
is a good idea.
Deploying the Contract
To deploy the contract, you'll use Hardhat Ignition, a declarative framework for deploying smart contracts. Hardhat Ignition is designed to make managing recurring tasks surrounding smart contract deployment and testing easy. For more information, be sure to check out the Hardhat Ignition docs.
To set up the proper file structure for your Ignition module, create a folder named ignition
and a subdirectory called modules
. Then add a new file to it called Box.js
. You can take all three of these steps with the following command:
Next, you can write your Hardhat Ignition module. To get started, take the following steps:
Import the
buildModule
function from the Hardhat Ignition moduleExport a module using
buildModule
Use the
getAccount
method to select the deployer accountSpecify custom gas price and gas limit settings for the deployment
Deploy the
Box
contractReturn an object from the module. This makes the
Box
contract accessible for interaction in Hardhat tests and scripts
To run the script and deploy the Box.sol
contract, use the following command, which requires you to specify the network name as defined in your hardhat.config.js
. Hardhat will deploy the contract to a local hardhat network by default if you don't specify a network.
You'll be prompted to confirm the network you wish to deploy to. After a few seconds after you confirm, the contract is deployed, and you'll see the contract address in the terminal. If you're deploying to another Tanssi appchain, make sure that you specify the correct network. The network name must match how it's defined in hardhat.config.js
. After a few seconds, the contract is deployed, and you should see the address in the terminal.
npx hardhat ignition deploy ./ignition/modules/Box.js --network dancebox ✅ Confirm deploy to network parodychain (2078)? … yesHardhat Ignition 🚀 Deploying [ BoxModule ] Batch #1Executed BoxModule#Box [ BoxModule ] successfully deployed 🚀 Deployed Addresses BoxModule#Box - 0xa84caB60db6541573a091e5C622fB79e175E17be
Congratulations, your contract is live! Save the address, as you will use it to interact with this contract instance in the next step.
Interacting with the Contract
To interact with your newly deployed contract on your Tanssi appchain, you can launch the Hardhat console
by running:
Next, you can take the following steps, entering one line at a time:
Create a local instance of the
Box.sol
contractConnect the local instance to the deployed contract, using the address of the contract
Interact with the attached contract. For this example, you can call the
store
method and store a simple value
Your EVM account will sign the transaction and broadcast it to the network. The output should look similar to:
npx hardhat console --network dancebox Welcome to Node.js v20.9.0.Type ".help" for more information. const Box = await ethers.getContractFactory('Box');undefined const box = await Box.attach('0xa84caB60db6541573a091e5C622fB79e175E17be');undefined await box.store(5);ContractTransactionResponse { provider: HardhatEthersProvider { ... }, blockNumber: null, blockHash: null, index: undefined, hash: '0x1c49a64a601fc5dd184f0a368a91130cb49203ec0f533c6fcf20445c68e20264', type: 2, to: '0xa84caB60db6541573a091e5C622fB79e175E17be', from: '0x3B939FeaD1557C741Ff06492FD0127bd287A421e', nonce: 87, gasLimit: 45881n, gasPrice: 1107421875n, maxPriorityFeePerGas: 1n, maxFeePerGas: 1107421875n, data: '0x6057361d0000000000000000000000000000000000000000000000000000000000000005', value: 0n, chainId: 5678n, signature: Signature { r: "0x9233b9cc4ae6879b7e08b9f1a4bfb175c8216eee0099966eca4a305c7f369ecc", s: "0x7663688633006b5a449d02cb08311569fadf2f9696bd7fe65417860a3b5fc57d", yParity: 0, networkV: null }, accessList: [], blobVersionedHashes: null } await box.retrieve();5n
Notice your address labeled from
, the contract's address, and the data
being passed. Now, you can retrieve the value by running:
You should see 5
or the value you initially stored.
Note
If you run the retrieve command immediately after storing the value, you may see the old value. Rerunning the retrieval command after waiting a moment will return the correct value.
Congratulations, you have successfully deployed and interacted with a contract using Hardhat!
Last updated