James Bachini

Hardhat vs Foundry

Hardhat vs Foundry

I started developing solidity with Truffle, then moved to Hardhat, then moved to Foundry, then moved back to Hardhat before eventually settling using both on a regular basis.

In this article I’ll talk about the benefits and disadvantages of Hardhat and Foundry so you can make an informed decision about which framework is better for you.

  1. Hardhat vs Foundry Video
  2. The Hardhat Ecosystem
  3. Introduction To Foundry
  4. Usability & Complexity
  5. A Performance Comparison
  6. A Feature Comparison
  7. Solidity vs Javascript
  8. Building vs Auditing
  9. Installing Hardhat & Foundry Together
  10. Concluding Thoughts Hardhat vs Foundry

Hardhat vs Foundry Video Summary

James On YouTube

The Hardhat Ecosystem

Hardhat is a powerful, flexible and extensible framework, designed to make the development process of Ethereum smart contracts as seamless and efficient as possible. It is built and managed by the Nomic Foundation as open source code.

Hardhat Solidity

Hardhat is a javascript module which can be installed via npm and runs on NodeJS.

npm install hardhat
npx hardhat init
npx hardhat test

The framework includes a scripting and testing environment for writing unit tests (chai) in Javascript or Typescript and executing transactions on local and remote EVM blockchains.

There are a range of 3rd party plugins and extensions so developers can customize Hardhat to better suit their specific needs, such as integrating with Ethereum libraries and tools.

Hardhat docs: https://hardhat.org/docs


Introduction To Foundry

Foundry is an open-source framework built and maintained by Paradigm.

Foundry Solidity

Foundry runs on Rust and offers various tools to develop and test Solidity smart contracts

  • forge – a testing framework for writing unit tests in Solidity
  • cast – a command line tool for constructing transaction and executing on-chain functions
  • anvil – a local EVM node used for testing
  • chisel – a Solidity REPL (Read, Evaluate, Print, & Loop)

Foundry manages dependencies via direct imports from Github with forge install @author/repo

curl -L https://foundry.paradigm.xyz | bash
foundryup

In my experience Foundry runs better on Unix based operating systems so if you are using Windows I would recommend installing Windows Subsystem For Linux.


Usability & Complexity

Foundry is marketed as “the framework for pros” which is maybe semi-true in that it does include more tools for sophisticated smart contract analysis.

I wouldn’t be fooled into thinking that it is more professional software however. The naming conventions are quirky at best and it has the feel of a personal project rather than a professional framework developed by a professional team.

Hardhat is more what you’d expect in that department and I tend to find it more intuitive to use. A lot of the time you are working within an ethers.js environment so if you are a full stack developer then it will seem very familiar to frontend code.

Foundry tests are written in Solidity which keeps the testing and code in the same language which I like a lot.

With both tools you’ll need to get familiar with using the command line.


A Performance Comparison

Foundry excels at performance executing tests and scripts faster than hardhat.

In everyday use this isn’t likely to cause an issue and both scripts will run a standard set of unit tests in seconds.

However the improved performance allows foundry to integrate additional capabilities such as native fuzz testing of function inputs. This means that it will send various different inputs to a function to test it can handle min values, zero values, max values, various other data that might cause errors, edge cases and highlight bugs in the code.


A Feature Comparison

Here are some of the unique features of both frameworks

HardhatFoundry
Javascript Scripts & Unit TestsSolidity Scripts & Unit Tests
Extensive plugin and dependencies ecosystem via npmFuzz Testing Function Inputs
console.log() debugging within solidity codeTraces which are similar to Tenderly transaction debugger breakdowns
Superior deployment scriptingCast command line transaction client
Cheatcodes for manipulating state during tests

Solidity vs Javascript

A big part of your decision will come down to if you hate Javascript.

For full stack developers that are going to be writing frontend code Hardhat is very intuitive and useful for writing unit tests in the same code. In the past I’ve given frontend developers the unit test files so they can see how all the smart contract functions work and just copy and paste the JS code into their frontend.

In Hardhat you also have the all the Javascript native functions and npm libraries at your disposal. If you want to put something into an array and shuffle it or do something else weird within a unit test it’s going to be a whole lot simpler to write with Hardhat.

If you love Rust and low level languages then Foundry is a better option. The ability to write tests in Solidity and execute them in a minimalist, fast environment is going to be appealing.

For developers that don’t know Javascript then Foundry also offers the ability to write, test and deploy smart contract code using only Solidity and not needing to learn a second language.


Building vs Auditing

I might be biased because of how I’ve ended up using both frameworks but I feel that Hardhat might be more suited towards Solidity developers who are building on Ethereum, while Foundry might be more suited towards smart contract auditors and security specialists.

What I tend to use Hardhat for is writing initial unit tests and deployment scripts. I find Foundry is good for is poking and prodding at my own smart contracts to create “scenario tests” and fuzz inputs.

There’s no reason (with some careful remapping) you can’t use both in the same repository when creating production code. Sometimes having a different toolset can help you look at a smart contract through a different lens and identify issues that weren’t picked up on standard function unit tests.

Installing Hardhat & Foundry Together

There are many Hardhat/Foundry boilerplates, I use my own minimalist one which is available here: SolidStart

Fork or download the SolidStart.js file, (make sure you have NodeJS and Rust installed) and then run:

node SolidStart.js

This will install all the following in one go:

  • Hardhat
  • Foundry (forge, cast, anvil, chisel)
  • OpenZeppelin Libraries
  • DotEnv for credentials

Concluding Thoughts Hardhat vs Foundry

My general advice is to start with Remix at https://remix.ethereum.org

Use this for proof of concepts and deploying simple tokens etc.

Then use Hardhat (unless you hate Javsacript) for writing unit tests and a deployment script. For testing and deployment I find Hardhat more intuitive and the performance speed has never caused an issue. I can write unit tests faster in Hardhat which more than makes up for shaving a couple of seconds of the test execution time.

Then if you are working on something that is going into a production environment and is security critical. Take some time to do an “internal audit” on your own code using foundry. Foundry is a very powerful tool that has a lot of features well suited to security focused work.



Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.