Creating a project with Hardhat

945G...fRid
14 May 2024
71

Intro
When developing DApps, a framework is needed to provide a set of tools and an environment that helps developers more easily write, test, and deploy smart contracts and the front-end applications that interact with them. In addition to Hardhat, which will be introduced this time, there are other common smart contract development frameworks such as Truffle and Embark.


I. What is Hardhat?
Hardhat is an open-source development framework and toolkit for Ethereum smart contract development. It has the following main features and functions:

Smart Contract Compilation and Deployment: Hardhat can compile Solidity smart contracts and provides simple and flexible deployment scripts to automate and repeat the contract deployment process.

Local Testing Environment: Hardhat offers a built-in local testing environment that can quickly run test cases and simulate the behavior of the Ethereum network, allowing developers to quickly verify the behavior of contracts.

Smart Contract Debugging: Hardhat integrates debugging tools to help developers debug smart contracts in a local environment, speeding up error troubleshooting and improving code quality.

Type Safety: Hardhat uses TypeScript to write plugins and scripts, making the code more readable and maintainable and providing stronger type checking.

Plugin System: Hardhat has a flexible plugin system that allows developers to extend and customize its functionality to meet the needs of different projects.

Task Runner: Hardhat provides a task runner that enables developers to write custom tasks to perform various operations, such as compiling contracts, running tests, deploying contracts, etc.

Integrated Testing Support: Hardhat supports integrated testing, allowing developers to write test scripts to simulate the real Ethereum network environment and test the behavior of contracts in actual use scenarios.

Community Support and Documentation: Hardhat has active community support and comprehensive documentation. Developers can get help, exchange experiences in the community, and find various cases and tutorials for using Hardhat.

II. Steps
Installation Create a new empty folder, open the command line, initialize a Node.js project

npm init -y


Download hardhat using npm

npm install --save-dev hardhat


Initialize the Project Create a hardhat project

npx hardhat init


Choose to create a js project or a ts project, or it can be an empty project.

Compile Contracts After the project is created, the following structure will appear. After selecting the first js project, a Lock.sol file will appear in the contracts/folder.

Compile the contract

npx hardhat compile


After the initial compilation, Hardhat will try to do as little work as possible the next time you compile. For example, if no files have changed since the last compilation, nothing will be compiled.

Test Contracts Look in the test/folder, and you will see a test file: Lock.js. Use the following command to test

npx hardhat test


Deploy Contracts In the folder ignition/modules, there is a file named Lock.js. Deploy it using the following command

npx hardhat ignition deploy ./ignition/modules/Lock.js


or

npx hardhat run ./ignition/modules/Lock.js


Connect Wallet to Hardhat Network Start the chain that comes with hardhat, which will expose a JSON-RPC interface.

npx hardhat node


If you want to deploy contracts against the hardhat network, you also need to add --network localhost at the end

npx hardhat ignition deploy ./ignition/modules/Lock.js --network localhost


Add the corresponding network in MetaMask for the interface.

Summary
The above operations have created a project with hardhat, compiled, tested, and deployed a smart contract, and connected the network to the wallet, allowing you to access the funds owned by users created on the hardhat network.

Write & Read to Earn with BULB

Learn More

Enjoy this blog? Subscribe to 666

2 Comments

B
No comments yet.
Most relevant comments are displayed, so some may have been filtered out.