Hardhat vs Truffle

Posted on

Hardhat and Truffle are two of the dominant development environments for projects that interact with blockchains based on the Ethereum Virtual Machine (EVM). Thus, it is important to have an overview of how they differ on certain aspects in order to be able to choose the most suitable one for each project of interest.

At first glance, it seems that they offer the same functionalitiesediting, compiling, testing and deployingand that both have clear and organized documentation. However, we can go deeper on how each development environment tackles each functionality.

Smart Contracts interaction

The first point to compare is the interaction with smart contracts. 

Truffle defaults to utilizing the web3.js library, which is injected as a global dependency and provides built-in contract abstractions. However, it only supports providers (abstractions which are used to connect to the network) created via web3.js. While there is ongoing work to support ethers.js, the main thing that can be done in this regard is having developers install ethers.js and use it a dependency for Truffle tests.

On the other hand, Hardhat has ethers.js as the default library for interacting with smart contracts, it is available in the global scope, and has the possibility of using web3.js through a plugin. 

However, comparing this smart contracts interaction aspect based on which library is leveraged by which environment would end up in comparing ethers.js and web3.js, which is not the aim of this article.

Local Ethereum Networks and Forking

Hardhat comes built-in with a local ethereum network named Hardhat Network and Truffle comes with one named Ganache.

Both can be run as an in-process or as a stand-alone daemon, enabling external clients to connect to it (metamask, dapp front-end, etc). Ganache has the convenience of having, besides the CLI, a graphical user interface (Ganache UI) which can be useful for a more visual experience for beginners. Truffle has the downside that Ganache needs to be installed in order to be run as a stand-alone process, while Hardhat does not require any extra installation to run Hardhat Network as a standalone daemon. 

Hardhat and Truffle allow configuration of block mining intervals and support Ethereum network forking and account impersonation.

Nonetheless, Hardhat has an easier way of forking than truffle. Truffle requires the installation of Ganache and only allows forking through Ganache-CLI by executing the appropriate command (taking Alchemy as a node provider in this example):

ganache-cli --fork https://eth-rinkeby.alchemyapi.io/v2/<key>

and then configuring the network on truffle-config.js like this:

networks:{
  rinkebyFork: {
      host: "127.0.0.1",    
      port: 8545,        
      network_id: "999",    
 },
}

On Hardhat, developers do not need to go through any installation process and can create a blockchain fork by starting a node from the command line with:

npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key>

Once the hardhat network is booted, it will connect to that fork without the need to change any configuration.

The other way a fork can be achieved is by configuring hardhat network to always fork from mainnet:

networks: {
  hardhat: {
    forking: {
      url: "https://eth-mainnet.alchemyapi.io/v2/<key>",
    }
  }
}

In this case, starting the default hardhat network will use the specified fork. Additional parameters can be added, such as starting block number for the fork.

Contracts Deployment

Hardhat enables developers to write their own scripts for deployment, giving them more flexibility in the processes. 

See also  PayPal Dives Into Crypto: Support for Stablecoin Transactions Announced

Truffle’s fixed way of deploying contracts through what are called migration files compromises developers’ freedom of choice, as they specify how contracts need to be written and named. The process in general can result arguably convoluted, particularly for people unfamiliar with it.

Error Management

Tools for dealing with errors is an important aspect to focus on when comparing both development environments. 

Hardhat Network allows printing messages and contract variables through console.log from the solidity code and it only requires importing hardhat/console.sol to use it. 

Truffle does not have native console.log support (currently, it is reportedly in progress), but the console.log feature can be used by installing Ganache’s console.log library and importing @ganache/console.log/console.sol on the solidity file of interest. 

Whenever a transaction fails, Hardhat will throw an exception containing a Solidity stack trace (which includes a complete Solidity call stack), enabling the developer to know exactly why tests or transactions are failing. Truffle does not display the actual stack trace, and only shows the error message. The development team plans to add the feature of displaying the stack trace in the future.

Hardhat Network also offers logging functionality that facilitates development and debugging of smart contracts. This functionality is enabled by default when running Hardhat networks node (through npx hardhat node) and is disabled when using hardhat network in-process. Furthermore, Hardhdat Network has the ability of creating automatic error messages for transactions which fails without a reason. 

Something to highlight about Truffle is that it includes a debugger with step-in/out, breakpoints, variables inspection, etc which can be used to debug specific transactions (through truffle debug <transaction hash>) or inside tests files to debug specific operations by capturing the contract operation in question with debug() and running truffle test --debug.

Plugins

Hardhat is much more flexible than Truffle due to the existence of plugins, which enable developers to have a more customizable environment.

Hardhat plugins aim to extend Hardhat Runtime environment with more tasks or by modifying existing ones. There is a wide offer of plugins that tackles different needs and each developer can also make their own plugins; more on this can be found here. Theres is quite a bit of movement and activity in this regards.

Truffle plugin support is in early stages, but they have what’s called “Truffle Boxes”. These boxes are boilerplates which contain modules, contracts, libraries (and more) and can also be created by each developer based on their own needs. However, it has an important drawback which is that boxes need to be used as a whole; it is not possible to use part of one box and this makes it considerably more complex than using plugins on hardhat.

Conclusion

Several parameters have been laid out in order for each developer to be able to decide if Hardhat or Truffle is the best-suited environment for them. Nevertheless, besides from researching both options, developers should also try them out in order to find which one they feel more comfortable with and determine the best fit for their needs.

Posted in Blockchain, Smart Contract, Software Development, SolidityTagged , , ,

Romina
By Romina