Skip to content

hacktronaut/hardhat-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HardHat Tutorial


HardHat - Ethereum development environment for professionals

Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.

Hardhat Runner is the main component you interact with when using Hardhat. It's a flexible and extensible task runner that helps you manage and automate the recurring tasks inherent to developing smart contracts and dApps.

This tutorial will take your through installation and usage of recommended setup of HardHat.


Installation

Hardhat is used through a local installation in your project. This way your environment will be reproducible, and you will avoid future version conflicts.

To install it, you need to create an npm project by going to an empty folder, running npm init, and following its instructions.

npm init

Install HardHat dependency

npm install --save-dev hardhat

HardHat project creation

With HardHat installed, you can initialize a new project by running npx hardhat init. You'll be presented with options for project creation. Select "Create a JavaScript project" to use a simple project creation wizard.

The initialized project structure will include folders like contracts, scripts, test, and a hardhat.config.js file. These are the default paths for a HardHat project, where your contract source files, tests, and automation scripts reside.


Chapter 2: Compiling Smart Contracts

Compiling Your Contracts

To compile your contracts in your HardHat project, use the built-in compile task. It's as simple as running the following command:

npx hardhat compile

This command will download the necessary compiler and compile your Solidity files, generating the bytecode ready for deployment.


Configuring the Compiler

If you need to customize the Solidity compiler options, you can do so through the solidity field in your hardhat.config.js file. Customizing the compiler settings is essential to ensure your smart contracts behave as expected. Here's an example of configuring the compiler version:

require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.19",
};

Setting the compiler version helps prevent unexpected behavior or compilation errors that might occur with new releases of Solidity.

Chapter 3: Deploying Smart Contracts with HardHat

Now that you've set up your development environment and compiled your smart contracts using HardHat, it's time to deploy them. In this chapter, we'll explore how to run HardHat as a standalone node and deploy your contracts locally.

Running HardHat as a Standalone Node

By default, HardHat operates with an in-memory instance of HardHat Network. However, you can also run HardHat Network in a standalone mode to allow external clients to connect. This is particularly useful for scenarios like wallet integration, Dapp front-ends, or scripts. Running a Local Node

To run a standalone node using HardHat, execute the following command:

npx hardhat node

This command starts a local Ethereum node and provides HTTP and WebSocket JSON-RPC server endpoints, typically located at http://127.0.0.1:8545/.

Warning: These accounts, along with their private keys, are publicly known. Funds sent to these accounts on the Mainnet or any live network will be lost.


Deploying the Contracts

Once you have your standalone node running, you can deploy your smart contracts using HardHat. For example, if you have a deployment script named deploy.js, you can deploy your contracts using the following command:

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

The response from the standalone node provides essential information to confirm the deployment and status of your smart contracts.


Conclusion

With this comprehensive guide, you've learned how to set up the HardHat development environment, compile your smart contracts, and configure the Solidity compiler. HardHat empowers you to build Ethereum projects with efficiency and confidence. Always be cautious with predefined accounts and ensure you use secure accounts when deploying on live networks. Happy deploying!

Stay tuned for more insights and best practices in Ethereum development with HardHat.