What is hardhat?
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate the recurring tasks that are inherent to the process of building smart contracts and dApps, as well as easily introducing more functionality around this workflow.
What are hardhat's main functionalities?
Compiling Smart Contracts
Hardhat can compile your smart contracts using the Solidity compiler. You can specify the version of Solidity you want to use in the configuration file.
module.exports = {
solidity: "0.8.4",
};
Running Tests
Hardhat allows you to write tests for your smart contracts using popular testing frameworks like Mocha and Chai. This example shows a simple test for a token contract.
const { expect } = require("chai");
describe("Token contract", function () {
it("Deployment should assign the total supply of tokens to the owner", async function () {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
});
Deploying Contracts
Hardhat makes it easy to deploy your smart contracts to the Ethereum network. This script deploys a token contract and logs the address to which it was deployed.
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
console.log("Token deployed to:", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Debugging
Hardhat provides a powerful debugging tool that allows you to inspect the state of your contracts at any point in time. This example shows a custom task that prints the list of accounts.
const { task } = require("hardhat/config");
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
Other packages similar to hardhat
truffle
Truffle is a development environment, testing framework, and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. It offers similar functionalities to Hardhat, such as compiling, deploying, and testing smart contracts. However, Hardhat is often praised for its flexibility and the ease of integrating with other tools.
embark
Embark is a framework for serverless Decentralized Applications using Ethereum, IPFS, and other platforms. It allows for easy development and deployment of smart contracts and dApps. Compared to Hardhat, Embark offers more out-of-the-box integrations with decentralized storage and communication protocols.
brownie
Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine. It is similar to Hardhat in terms of functionalities but is more suited for developers who prefer Python over JavaScript.
HardHat
Scaffolding utility for blue-collar programmers.
About / Why?
There are a few other
scaffolding libraries out there, some more recently
updated than others. One thing most of them share in common is that they all do
a little too much. I don't need option parsing, I can use optimist.
I don't need a command prompt library, prompt
already takes care of that. I don't need cli routing, I'm happy to use
flatiron/director.
HardHat aims to solve specific problems and be another tool in your overall
command-line application.
Features
scaffold()
: Copy a directory of scaffold folders & files (templates) to a
new destination.
- Your templates can be written using any engine that consolidate supports
(currently 14 engines including jade, handlebars, etc.)
- More? Pull requests welcome :)
Installation
Install via npm (node package manager)
$ [sudo] npm install hardhat
Usage
hardhat.scaffold
The scaffold() method copies files and directories from one location to
another, optionally applying template data to them.
The templating is powered by consolidate, so
you can use any of its supported engines (jade, handlebars, ejs, etc.).
var hardhat = require('hardhat');
hardhat.scaffold(<srcDir>, <destDir>, options, function(err) {
});
The available options (and their defaults) are:
var options = {
engine: 'handlebars',
ext: 'tpl',
encoding: 'utf8',
data: {}
};
In order to trigger templating on your source files, you shoud add an extension
prefix matching the options passed or the default. So for example, if your
source file is index.html
but you want templating to be applied, you should
name it index.tpl.html
. Scaffold will apply the templating and write the
file to the destination as index.html
, stripping the extension prefix.
If you are happy with the default options and do not have any template data to
pass, you can omit options from the method call like:
var hardhat = require('hardhat');
hardhat.scaffold(<srcDir>, <destDir>, function(err) {
});
Examples
Coming Soon
Running Tests
Install dev dependencies:
$ [sudo] npm install -d
Run the tests:
$ make test