Skip to content

πŸ”— Blockchain Development Frameworks Comparison

Notifications You must be signed in to change notification settings

vcheeney/truffle-vs-hardhat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


Truffle VS Hardhat

This projects aims to compare the Truffle Framework and the Hardhat Framework when it comes to smart contracts development. A big focus is put on developer experience. The smart contract used for the purpose of this comparison is a simple ERC20 token.

Truffle VS Hardhat

Truffle and Hardhat both are smart contracts development frameworks.


Truffle

Details

Truffle is part of the Truffle Suite, a suite of four tools designed to help blockchain developers build, test and deploy dapps.

  • Truffle: the smart contract development framework
  • Ganache: local blockchain that runs on your PC (available as a UI or CLI)
  • Drizzle: frontend library based on redux
  • Teams: (TODO: explain and link.. can't seem to find it though...)

Release date

Truffle was founded in 2015.

Getting started

The approach they recommend in their installation guide is to install truffle globally using npm:

npm install -g truffle

Then, you can use the truffle CLI to initialize a project.

Typical Project Structure

.
β”œβ”€β”€ build/contracts/    # Default location for the compiled smart contracts
β”œβ”€β”€ contracts/          # Smart contracts files (.sol)
β”œβ”€β”€ migrations/         # Migrations scripts (what is used to deploy the contracts)
β”œβ”€β”€ test/               # Tests for the smart contracts (javascript files)
β”œβ”€β”€ truffle-config.js   # Configuration
└── package.json        # No explanations needed

Writing smart contracts

Smart contracts are written in the solidity language, (.sol files) in the contracts directory.

Compilation

Command:

npx truffle compile

By default, the compiled contracts will be placed in the build/contracts directory.

Testing

Command:

npx truffle test

Tests can be written in the test directory. Those tests can be written in javascript as well as in solidity.

Remarks

At least on my setup, tests take a while to run. About 250ms to 1000ms per test.

truffle-test

TypeScript is supported, but documentation on the subject is not extensive.

As of my experimentation, contracts are recompiled before EVERY tests run. This takes some time and makes the developer experience less than optimal.

Tests written in JavaScript in a Truffle project extend the mocha testing framework with special utilities.

Deploying

Command:

npx truffle migrate

To deploy smart contracts, migration scripts must be written in the migrations directory.

In migrations scripts, use artifacts.require() to retrieve the compiled smart contracts. Ex:

const MyContract = artifacts.require("MyContract");

See documentation for more info.


Hardhat

Details

Hardhat is a newer smart contracts development framework. It focuses on providing a great developer experience through best practices and native support for TypeScript, along with exclusive features like the ability to write console.log statements in smart contracts.

Hardhat also has an emphasis on speed through its much faster feedback loop

Release date

2020

Getting started

Hardhat advocates using a local installation of the package to prevent potential version conflicts that occur as tools evolve and update.

Therefore, the recommended approach to start a project is to first create an empty directory and install Hardhat locally using the following command:

npm install --save-dev hardhat

Then, initialize the project by running the following command:

npx hardhat

Hardhat will then suggest a few installation options but I would recommended going with the Create an advanced sample project that uses TypeScript option. The project is not that complicated and all the configs are already perfectly setup to start building smart contracts and testing them using TypeScript.

For further information on the installation process, make sure to check the documentation.

The thing to remember is that the installation process is smooth and advocates using best practices to improve maintainability.

Typical Project Structure

.
β”œβ”€β”€ artifacts/          # Default location for the compiled smart contracts
β”œβ”€β”€ contracts/          # Smart contracts files (.sol)
β”œβ”€β”€ scripts/            # User scripts
β”œβ”€β”€ test/               # Tests for the smart contracts (javascript files)
β”œβ”€β”€ typechain/          # Directory containing the generated TS types for our smart contracts
β”œβ”€β”€ .env                # Environment variables (mostly used in hardhat.config.ts)
β”œβ”€β”€ .eslintrc.js        # Linting rules
β”œβ”€β”€ .gitignore          # Specifies files that should not be tracked by Git
β”œβ”€β”€ .npmignore          # Specifies files that should not be included in our NPM package (if we would ever publish it)
β”œβ”€β”€ .prettierignore     # Specifies files that our code formatter (Prettier) should ignore
β”œβ”€β”€ .solhint.json       # Solhint configuration (linter for smart contracts)
β”œβ”€β”€ .solhintignore      # Specifies files that should not be linted by solhint
β”œβ”€β”€ hardhat.config.ts   # Hardhat Configuration
β”œβ”€β”€ package.json        # NPM project file detailing dependencies and other information
└── tsconfig.json       # TypeScript Configuration

Writing smart contracts

Just like Truffle, smart contracts built using Hardhat are written using the Solidity language.

Compilation

Command:

npx hardhat compile

Testing

Command:

npx hardhat compile

Testing is done in TypeScript files in the test directory. Traditional mocha is used as the testing framework, although the Waffle package which provides blockchain specific Chai matchers is highly recommended as a complement. Here's an example of a test using Waffle and the same test without Waffle: chai

According to my experimentation, tests run very quickly! ⚑ hardhat-test Moreover, smart contracts will only be recompiled if there were any changes since the last compilation. (Unlike what I've seen with Truffle) compile

Calling methods on our contracts is a breeze, thanks to Typechain which automatically generates TS types.

Deploying

Deploying smart contracts using Hardhat is done through user scripts (most often written in the scripts directory).

The command to run a user script is:

npx hardhat run scripts/scriptName.ts