
Chapter 0x11: Combining Foundry with Hardhat: A Comprehensive Guide
Introduction
Combining Foundry with Hardhat : If you’re diving into Ethereum smart contract development, you’ve probably heard of both Foundry and Hardhat. These are two powerful tools that can streamline your Solidity development workflow. But what if we could combine the strengths of both? In this guide, we will explore Combining Foundry with Hardhat, integrating Solidity libraries like OpenZeppelin, and performing Gas Optimization and Analysis.
Table of Contents
By the end of this article, you will have a clear understanding of:
- How to use Foundry development alongside Hardhat
- The differences between Foundry and Hardhat
- Integrating Foundry with Solidity libraries like OpenZeppelin
- Gas Optimization and Analysis using Foundry
Let’s get started!
Why Use Foundry and Hardhat Together?

Foundry and Hardhat both have their own advantages:
- Hardhat is excellent for development, debugging, and deploying contracts.
- Foundry is blazing fast, built-in for fuzz testing, and easy to use.
Instead of choosing one over the other, why not combine them?
Hardhat Vs Foundry: Learn How to Use Both in Your Project
- Hardhat: Great for plugins, debugging, and handling complex deployments.
- Foundry: Best for high-performance testing and gas optimization.
By integrating Foundry into your Hardhat workflow, you get the best of both worlds.
Setting Up Foundry in a Hardhat Project

Step 1: Install Foundry
To start using Foundry for Hardhat projects, install Foundry with:
curl -L https://foundry.paradigm.xyz | bash
foundryup
This installs Forge (Foundry’s testing framework) and Cast (its CLI tool).
Step 2: Initialize Foundry inside a Hardhat Project
Navigate to your Hardhat project and initialize Foundry:
forge init
This sets up a Foundry project inside your Hardhat project.
Step 3: Link Hardhat and Foundry
To make them work together, modify your hardhat.config.js:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.26",
};
Then, ensure Foundry runs tests:
forge test
Now, you can use both Hardhat and Foundry together!
Using Foundry with Solidity Libraries Like OpenZeppelin

Installing OpenZeppelin in Foundry
One of the key aspects of smart contract development is using OpenZeppelin for secure contract implementation. To install OpenZeppelin in Foundry, run:
forge install OpenZeppelin/openzeppelin-contracts
This will add OpenZeppelin as a dependency in your project.
Adding Dependencies to Your Contracts in Foundry
Modify your Solidity contract to use OpenZeppelin:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 1000 * 10 ** decimals());
}
}
This is how you integrate Foundry with Solidity libraries like OpenZeppelin.
Foundry Gas Optimization and Analysis

What is Gas Optimization?
Gas optimization is reducing the cost of contract execution on the Ethereum network.
How to Perform Gas Optimization in Foundry?
Foundry provides tools to analyze and optimize gas usage. Use the command:
forge test --gas-report
This generates a gas report showing which functions consume the most gas.
Example of Gas Optimization in Solidity Foundry
Here’s a before-and-after gas optimization example:
Before Optimization:
function addNumbers(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}
After Optimization:
function addNumbers(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly {
c := add(a, b)
}
}
Using inline assembly significantly reduces gas consumption.
Gas Optimization and Analysis Using Foundry Github Example
For real-world gas optimization examples, check out the official Foundry GitHub.
Foundry and Hardhat Integration: Best Practices

- Use Hardhat for deployments and debugging
- Use Foundry for fast testing and gas optimizations
- Keep your dependencies managed properly
- Use OpenZeppelin for secure smart contract development
By following these best practices, you can maximize efficiency and security in your smart contract development.
Conclusion
Combining Foundry with Hardhat gives you the best of both worlds in Solidity development. We covered:
- How to set up Foundry in a Hardhat project
- Using Foundry with Solidity libraries like OpenZeppelin
- Foundry Gas Optimization and Analysis
This guide ensures you maximize the efficiency, security, and performance of your Solidity development workflow.
If you’re serious about smart contract development, start integrating Foundry and Hardhat today!
FAQs
1. What is Foundry development?
Foundry is a fast, Rust-based Solidity development and testing framework.
2. How is Foundry different from Hardhat?
Hardhat is great for debugging and plugins, while Foundry is better for speed and testing.
3. Can I use OpenZeppelin with Foundry?
Yes! You can install OpenZeppelin contracts in Foundry using:forge install OpenZeppelin/openzeppelin-contracts
4. How do I perform gas optimization using Foundry?
Run gas reports with:forge test --gas-report
5. Where can I learn more about Foundry gas optimization and analysis?
Check out Foundry gas optimization and analysis GitHub resources and the official Foundry documentation.
Now you have a complete guide on integrating Foundry with Hardhat and optimizing gas costs in Solidity. Start building better Ethereum smart contracts today! 🚀