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 and Hardhat both are smart contracts development frameworks.
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...)
Truffle was founded in 2015.
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.
.
βββ 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
Smart contracts are written in the solidity language, (.sol files) in the contracts
directory.
Command:
npx truffle compile
By default, the compiled contracts will be placed in the build/contracts
directory.
Command:
npx truffle test
Tests can be written in the test
directory. Those tests can be written in javascript as well as in solidity.
At least on my setup, tests take a while to run. About 250ms to 1000ms per 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.
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 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
2020
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.
.
βββ 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
Just like Truffle, smart contracts built using Hardhat are written using the Solidity language.
Command:
npx hardhat compile
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:
According to my experimentation, tests run very quickly! β‘ Moreover, smart contracts will only be recompiled if there were any changes since the last compilation. (Unlike what I've seen with Truffle)
Calling methods on our contracts is a breeze, thanks to Typechain which automatically generates TS types.
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