Build and test Smart Contracts on your device (Part I: Hardhat)

April 14, 2023
Intro
How do the pros write smart contracts? They use programming tools to optimise their workflows, debug and test their code.
In this how-to guide, you’ll learn how to install the popular Hardhat library and you’ll see how it can be used to compile, test and deploy your smart contracts.
If you’re ready, let’s get started.
What we’ll cover
- Pre-installation
- Installation
- Test your environment
- Write a contract
- Compile & test a contract
- Deploy to a testnet
Pre-Installation
<aside> 📌 You’ll need the following software packages installed on your laptop to have your setup work. If you have them installed already, you can skip this step.</aside>
- Node (at least version 16 LTS)
- NPM (or Yarn)
Installation
<aside> 📌 This part of the tutorial applies to all operating systems: Windows (WSL is recommended for Windows users), MacOS & Linux.
</aside>
First, create a folder for your project. On any command line, use:
mkdir my-smart-contract
cd my-smart-contract
You should have a new folder created, called my-smart-contract
which you will now open in your command line.
npm init -y

npm install --save-dev hardhat@latest

If you’ve gotten the feedback above, the next step is to execute hardhat with the following command.
npx hardhat
<aside> 📌 Note: you can safely accept all the defaults for a Javascript project</aside>

We’re not done yet, hardhat needs a few more dependencies that you have to install to your project. Use this command:
We’re not done yet, hardhat needs a few more dependencies that you have to install to your project. Use this command:
npm install --save-dev chai @openzeppelin/contracts @nomiclabs/hardhat-ethers ethers @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-chai-matchers
You should see something like what’s showing above. That means those plugins were installed successfully.
Test your environment
We went from an empty folder, to this:

Write a contract
We have our environment ready and all the tools we need to begin writing our first smart contract.
Now, we want to work on our first contract. Hardhat comes with a simple contract called Lock
, you can find it inside the contracts
folder. Also note that all your solidity contract files will be saved inside the contracts
folder.

Lock.sol solidity contract in VS Code
Compile & test a contract
npx hardhat compile
After running the compile
command on the command line, a new file is created in the folder artifacts
this folder won’t exist if you’ve never run the compile
or deploy script.
<aside> 📌 If you created a TypeScript project, this task will also generate TypeScript bindings using TypeChain.</aside>
Testing is an important part of building smart contracts. Hardhat uses the Mocha & Chai libraries for testing smart contracts. To test your default contract with automated testing, use the command:
npx hardhat test

Deploy to a testnet
Remember that a contract has to be compiled and deployed to a network to be used or tested. That’s what we’ll do here.
npx hardhat run scripts/deploy.js
This script looks into the contracts folder, compiles the solidity files and deploys them to the default network, which is a local blockchain network on your computer.

Deploying to a local testnet
It’s a good idea to test your smart contracts, one way is to deploy to a testnet and interact with it there. Hardhat comes with a testnet installed, to activate it and work with it.
npx hardhat node

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

Now, you can see each transaction (in this case, the contract deployed) to the local node.
Conclusion
Congratulations, you’re all setup, you can go deeper into solidity by taking the Smart contract introduction on Mowblox