Forget Truffle, we have HardHat from now on

Tram Ho

Probably for those who are just starting to learn Ethereum in particular as well as EVM-based platforms in general know Truffle (a framework supporting compile, deploy smart contract). However, when it comes to developing larger, more complex smart contracts, it exposes some limitations such as consuming a lot of gas, long deploying time, … With Hardhat , the framework gives us options, somewhat superior features to do with more complex smart contract systems.

1. General introduction

Like Truffle , Hardhat is a development environment for compiling, deploying, testing and debugging Ethereum Dapps.

Some of Hardhat ‘s standout feature points

  • Integrated local hardhat network, easy to run and debug code on local.
  • Debug is easier: With Hardhat , we can debug Solidity code more easily when we can console.log out variables (Solidity doesn’t support console.log)
  • Plugins system: Help developers to add functionality, depending on each specific project
  • TypeScript support

2. Installation

We also set up a trial of a small sample project with hardhat . We perform the operations below

We will choose option 1 (create a sample project)

Continue to select the necessary options

In the end, Hardhat ‘s sample project directory structure should look like this

Here we take the corresponding structure of the Truffle Box

Basically the project structure of the 2 tools is relatively similar

  • The contracts folder contains the source code of the smart contracts
  • The scripts and migrations directory contains scripts written in Javascript for compiling and deploying smart contracts.
  • The test directory contains the unit tests written for the contract
  • 2 The files hardhat.config.js and truffle-config.js contain hardhat.config.js such as networks, wallet address, compiler …

3. Use

Greeter.sol

We have a smart contract Greeter.sol file in the contracts folder

hardhat.config.js

The hardhat.config.js file is where we define networks, the compiler, the build file directory, …

A more complete version

scripts/sample-script.js

Unlike Truffle , Hardhat does not need to deploy “buffer” to add contract Migration.sol , so it can save 1 amount of gas

Deploy

If we want to deploy the code contract in other networks, we add the --network option

So the console.log we put in the constructor function has been executed

4. More advanced features

Compile contracts in different Solidity versions

hardhat.config.js

Hardhat in this case would use the 0.5.5 compiler for pragma solidity ^0.5.0 and 0.6.7 contracts with pragma solidity ^0.6.0 contracts

In TH contract defines the compiler pragma solidity >=0.5.0 , Hardhat will choose the highest compiler 0.6.7

Or we can configure details for which contracts to use which compiler

Run a private blockchain network such as ganache

In the past, I used to use ganache to deploy smart contract locally with truffle for testing, of course I can use truffle develop but ganache has more options and ways to configure parameters. With Hardhat , I can also use Hardhat , it also supports the simulation of local versions of hardforks of Ethereum.

By default the network will run at port 8545 , we can add parameters to adjust as desired

Conclude

In general, Hardhat is a very powerful, full-featured tool needed to help developers deploy smart contracts on Ethereum. Hardhat usage is quite similar to Truffle , in addition it also overcomes some bad points from the opponent.

References

https://hardhat.org/getting-started/

https://hardhat.org/tutorial/

Share the news now

Source : Viblo