Blockchain development space is exploding. There has been tremendous demand for developers to build web3-based applications and infrastructures.
However, the problem here is an overwhelming pool of libraries, tools, and frameworks available, which can be confusing for beginners entering the industry.
You have to believe me here. I know how you feel if you are just starting.
I recently added Solidity to my stack, but I didn’t find the idea of an online IDE championed by most Solidity beginner tutorials to be a good fit for me. (Most of the tutorials used remix online IDE.)
Hence, the focus on this online IDE made it very difficult for beginners to find good set-up content on alternative IDE.
However, I have created this guide to help you take your baby step in smart contract development. So, let’s begin with setting up your development environment.
Prerequisite: Download the VS Code and install npm & node.JS on your machine.
Install The Hardhat On Your Machine.
For you to build and deploy solidity smart contracts on the VS Code IDE, you might need to use an Ethereum software development environment such as Hardhat or Truffle. But for the purpose of this tutorial, we would play around Hardhat alone.
Here are the steps to get Hardhat installed and running on your machine.
Step 1
Open a new terminal on your VS Code and check for the availability of npm & Node.Js on your machine. Run the following commands:
//This will display the version of Node.js installed on your machine.//
node --version
//check the version of npm with the following command//
npm -v
Step 2
Install the Hardhat framework using the following command:
npm install --save-dev hardhat
This might take a few minutes to complete, depending on the speed of your machine.
Step 3
Run npx hardhat to Create a Sample Project.
After installation is completed, do the following.
— Run npx hardhat
on your terminal.
— Select the “Create a Sample Project” option.
— Select your preferred project root and enter “y” to add a .gitignore.
— Enter “y” to install this sample project’s dependencies with npm (@nomicfoundation/hardhat-toolbox)
Ready To deploy Build Smart Contracts.
Now, you are fully set to begin to build and deploy your smart contract. But wait a minute. You need to familiarize yourself with some of the commands within the hardhat framework.
To access these commands, enter npx hardhat
to see the available commands.
For example, if we enter the command npx hardhat compile
on our terminal, our contracts folder is immediately compiled into a new folder called artifacts.
Hardhat Task — Accounts Case study.
In Hardhat, all actions you perform are encapsulated as tasks. The predefined functionalities that come with Hardhat are considered built-in tasks. Interestingly, these built-in tasks are constructed using the APIs that are at your disposal. This means that the tools and capabilities available can also be leveraged to define and implement custom tasks to suit your needs.
In the context of Hardhat, a “task” is essentially a JavaScript asynchronous function that gets access to the Hardhat Runtime Environment and comes with additional metadata that automates certain processes for you. Specifically, it handles tasks such as parsing, validating arguments, and providing helpful messages.
For this tutorial, we’re going to take a look at Accounts in Tasks. This will enable us to access your available accounts to deploy your contacts.
Find this Hardhat’s config API, which is available in the global scope of hardhat.config.js.
The file looks like this →
require("@nomicfoundation/hardhat-toolbox");
task("balance", "Prints an account's balance").setAction(async () => {});
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.23",
};
However, we will need to reconfigure this in order to get access to the Account metadata. Replace the code above with this and enter npx hardhat accounts
on your terminal to print the list of available accounts.
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
module.exports = {
solidity: "0.8.19",
};
— →→→ —
The Hardhat Network
Hardhat is equipped with an Ethereum network node tailored for development purposes. This feature enables you to deploy your smart contracts, execute tests, and troubleshoot your code — all within the environment of your local machine.
So you can run or test your smart contract against a fake blockchain built into the hardhat network.
For example, you can run a hardhat network node using npx hardhat node
to spin up the node you can work with. when you run this command, you will get a list of the attached addresses, their private keys, and their balance.
Forking Configuration of Hardhat network
Forking the Hardhat Network refers to the process of creating a copy or replica of the Ethereum mainnet (or another network) within the Hardhat development environment. This allows developers to interact with a snapshot of the blockchain at a specific block height, providing a simulated environment for testing and development.
By forking the network, developers can work with realistic data and test their smart contracts against historical or predefined states. This is particularly useful for scenarios where interaction with real-world data or specific contract states is required during development and testing.
To do this,
1. update your **hardhat.config.js**
to include network configurations.
networks: {
hardhat: {
forking: {
url: `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`,
},
},
},
In our case, we are going to use the Alchemy environment variable.
To fork the Hardhat Network with Alchemy,
2. Set the **ALCHEMY_URL**
environment variable to the appropriate Alchemy API URL.
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
module.exports = {
solidity: "0.8.19",
networks: {
hardhat: {
forking: {
url: `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`,
},
},
},
}
3. Run Hardhat with the **--network hardhat**
flag:
npx hardhat --network hardhat
You are all set.
Now that your Hardhat environment is configured, you can start developing your Ethereum smart contracts. Open the .sol
files in your Hardhat project's contracts
folder. This is where you’ll define the logic and functionality of your decentralized applications.
Compiling Your Smart Contracts
After writing your smart contracts, it’s time to compile them using the Hardhat framework. Open your terminal or command prompt and run the following command:
npx hardhat compile
This command instructs Hardhat to compile your Solidity contracts, generating the corresponding bytecode that will be deployed to the Ethereum blockchain.
Once the compilation process is complete, Hardhat will generate an artifacts folder containing the compiled bytecode, ABI (Application Binary Interface), and other artifacts. These artifacts are essential for deploying and interacting with your smart contracts.
Don't hesitate to reach out if you encounter any difficulties or have questions about the process. Drop a message in the comments section, and I’ll gladly assist you, whether it’s related to writing Solidity code or navigating compilation errors.
If you find this article thrilling, discover extra thrilling posts like this on Learnhub Blog; we write a lot of tech-related topics from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain. Take a look at How to Build Offline Web Applications.
Written by Mayowa Olatunji
Top comments (0)