Smart Contract Frameworks - Foundry vs Hardhat

Smart Contract Frameworks - Foundry vs Hardhat
Do not index
Do not index
When it comes to developing smart contracts on the Ethereum blockchain, two tools stand out as the dynamic duo for Solidity developers: Foundry and Hardhat. Both of these smart contract development frameworks provide a streamlined and efficient way to write, test, and deploy smart contracts.
However, there are some key differences between Foundry and Hardhat that developers should consider before choosing which one to use...
In this blog post, we will compare and contrast these two popular options, highlighting their strengths and weaknesses and providing code samples to showcase their capabilities. By the end, you'll have a better understanding of which tool is best suited for your Solidity development needs.

Understanding the Basics of Solidity and Its Development Tools

Smart contract development frameworks like Foundry and Hardhat provide essential tools for writing, testing, and deploying solidity contracts.
When it comes to testing, both frameworks offer testing language tests that allow developers to write tests for their contracts. Additionally, Foundry allows developers to automatically generate random values for testing, which can be incredibly useful during the early development phase.
In terms of managing dependencies, Foundry excels by allowing developers to easily install and compile time foundry, providing a seamless and efficient experience.
Overall, understanding the basics of solidity and its development tools is crucial for developers looking to maximize their efficiency and ensure the reliability of their smart contracts.

Foundry vs Hardhat

What is Foundry?

notion image
Foundry is a development environment, testing framework, and Ethereum smart contract task runner. Aimed at providing developers with a robust set of tools to streamline the smart contract development process, Foundry emphasizes test isolation, repeatability, and the inclusion of “Cheatcodes” to aid in rapid testing and deployment.
One great feature of Foundry is the ability to write tests in Solidity, making it a breeze to ensure the functionality and correctness of smart contracts.
With Foundry, developers can expect differently for different inputs, allowing for thorough testing scenarios. Another benefit is how Foundry manages dependencies, making it simple to install and compile time Foundry.
This streamlined process reduces context switching and allows developers to focus more on writing code and gaining even more Solidity knowledge.

Key Features of Foundry

  1. Isolated Testing: One of the primary characteristics of Foundry is its approach to test isolation. Every test is run in a completely isolated environment, ensuring that each test's state doesn't interfere with another. This results in more reliable and consistent tests, particularly crucial in a domain like blockchain where mistakes can be costly.
  1. Cheatcodes for Faster Development: Foundry introduces the concept of “Cheatcodes,” which are essentially shortcuts or functionalities that provide developers with a rapid way to perform common tasks during testing. These can range from deploying contracts to manipulating the blockchain's state.
  1. Integrated Development: Like other popular frameworks in the Ethereum ecosystem, Foundry integrates the complete smart contract lifecycle – from writing and compiling to testing and deployment.
  1. Evolving Ecosystem: As with many tools in the rapidly advancing blockchain space, Foundry is continuously evolving. It may receive regular updates, new features, and optimizations based on community feedback and the changing landscape of Ethereum development.
In essence, Foundry is designed to be a one-stop-shop for Ethereum developers, providing them with the tools necessary to accelerate development, improve code quality, and minimize errors.
Foundry's emphasis on testing and its unique features make it a noteworthy addition to the roster of Ethereum development tools.

What is Hardhat?

notion image
Hardhat is a comprehensive Ethereum development environment designed to facilitate the processes of smart contract development, testing, debugging, and deployment.
With a focus on developer experience, Hardhat provides an ecosystem of tools and plugins that aim to improve the efficiency and effectiveness of building on Ethereum.

Key Features of Hardhat

  1. Hardhat Network: One of the standout features of Hardhat is its built-in Ethereum network that simulates mainnet or any other network for testing. It offers advanced mining and network control, enabling developers to recreate a variety of blockchain scenarios.
  1. Solidity Stack Traces: Hardhat revolutionized debugging in Ethereum development by introducing meaningful error stack traces for Solidity code. This makes it significantly easier to pinpoint and rectify errors.
  1. Extensible: Hardhat is designed to be extensible through plugins. The community and ecosystem around Hardhat have developed a wide array of plugins to integrate with other tools, perform specialized tasks, or extend core functionalities.
  1. TypeScript Support: Hardhat's codebase is written in TypeScript, offering developers a typed environment that can be beneficial for catching errors at compile-time and improving code quality.
  1. Integration with Ethers.js and Waffle: Hardhat is seamlessly integrated with the Ethers.js library and the Waffle testing framework, making it easy to write, test, and deploy smart contracts.
  1. Console Logging: Developers can directly insert console logs into their Solidity smart contract code, similar to traditional programming, providing a simple and intuitive debugging method.
  1. Task Automation: Hardhat allows developers to define tasks, automating repetitive actions that can be executed with simple commands. This helps in streamlining the development workflow.
Hardhat's array of features, combined with a strong emphasis on testing and debugging, make it an invaluable tool for both novice and experienced Ethereum developers.
Whether you're developing a new decentralized application, writing complex smart contracts, or just experimenting with Ethereum, Hardhat offers a suite of functionalities tailored to make the development process as smooth as possible.

Unpacking Hardhat: What Makes it Unique and Relevant Code Examples

Hardhat, like Foundry, is a powerful tool for Solidity developers, but it brings its own unique features and benefits to the table.
One standout feature is its ability to easily write and execute tests for smart contracts. With Hardhat, developers can expect differently for different inputs, allowing for thorough testing scenarios.
This level of flexibility is especially useful when working with complex contracts.
Additionally, Hardhat shares the same contracts and dependencies as Truffle, making it easier for developers to transition between the two frameworks.
Overall, Hardhat offers a seamless and efficient development experience with less context switching, allowing developers to focus on writing code and gaining even more Solidity knowledge.

Testing in Hardhat

In Hardhat, test files typically use the `.js` extension and are written using the Mocha testing framework along with the Chai assertion library.
const { expect } = require("chai");

describe("Token Contract", function() {

  it("Should return the total supply", async function() {

    const Token = await ethers.getContractFactory("Token");

    const token = await Token.deploy();

    expect(await token.totalSupply()).to.equal(1000000);



Scripting Deployment with Remix¶

Remix is a web-based IDE for Ethereum. Although it's not directly related to Hardhat or Foundry, it's worth noting how deployment scripts can be created. Using Remix's JavaScript VM environment, you can deploy smart contracts directly onto a simulated Ethereum environment.

Deploying and Interacting with Smart Contracts in Hardhat¶

Hardhat makes deploying and interacting with contracts straightforward. Using Hardhat scripts, you can deploy a contract like this:
const hre = require("hardhat");

async function main() {

  const Token = await hre.ethers.getContractFactory("Token");

  const token = await Token.deploy();

  await token.deployed();

  console.log("Token deployed to:", token.address);


Interacting is just as simple. Once a contract is deployed, you can interact with it using Ethers.js which comes integrated with Hardhat.

Testing in Foundry

Pros & Cons of Foundry

Foundry is newer compared to other frameworks but has its advantages:
  • Isolation: Foundry runs each test in isolation, ensuring a clean state for each test case.
  • Cheats: Foundry introduces “cheats” which are functionalities that provide developers shortcuts for common tasks during testing.
  • Learning curve: Being newer, it might be less familiar to developers compared to other frameworks.
  • Documentation: As of my last update, documentation might not be as extensive as more established tools.

Testing in Foundry

In Foundry, writing tests might look a bit different. Given its isolated environment and the Cheatcodes, a typical test might look like this:
describe("Token Contract", function() {

  it("Should return the total supply", async function() {

    const token = await deploy("Token"); // Using Foundry's deploy cheat

    expect(await token.totalSupply()).to.equal(1000000);



Understanding Foundry Cheatcodes for testing

As mentioned, one of the unique features of Foundry is its "Cheatcodes". These are shortcuts provided to developers for common tasks. Some cheats include:
  • `deploy`: Deploys a contract.
  • `mineBlock`: Advances block numbers.
  • `increaseTime`: Advances time in the local Ethereum environment.

Summary of differences

  • Mature with extensive plugins.
  • Integrates seamlessly with Ethers.js and Waffle.
  • Has a built-in local Ethereum environment.
  • Isolates each test case.
  • Provides cheats for faster testing.
  • Is newer with potential for growth but may have a steeper learning curve initially.

The Foundry vs Hardhat Verdict: Choosing the Right Tool for Your Needs

When it comes to choosing between Foundry and Hardhat for your Solidity development needs, it ultimately comes down to your specific requirements and preferences. Both tools offer a seamless and efficient development experience, with the ability to write and execute tests for smart contracts.
If you prioritize thorough testing and the ability to expect differently for different inputs, Foundry is the way to go. Its "expect differently" function allows for comprehensive testing scenarios, ensuring that your contracts can handle various edge cases without vulnerabilities.
On the other hand, if you're working with complex contracts or looking for an easier transition from Truffle, Hardhat is the better choice. Its flexibility and compatibility with Truffle contracts and dependencies make it a powerful tool for developers.
Consider your testing needs, contract complexity, and desired development workflow when making your decision. Either way, you can't go wrong with either Foundry or Hardhat as your Solidity development companion.

Stay ahead of the Web3 security curve!

Learn tips and tricks from top auditors and stay up-to-date on the latest news.