Web3 & DApp Development Fundamentals · 课时

Foundry 与 Hardhat 对比

基于 Rust 的工具

第 1 / 4 课13 个步骤

Foundry 与 Hardhat 对比 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Is Foundry

Foundry is a fast, Rust-based toolkit for Ethereum development. It lets you write tests, scripts, and deployments entirely in Solidity, with no JavaScript layer required.

Its speed and Solidity-native testing have made it the dominant choice for many professional smart contract teams.

The Four Tools

Foundry ships as four command-line tools:

  • forge — build, test, and deploy contracts
  • cast — interact with chains and contracts from the CLI
  • anvil — a local development blockchain
  • chisel — a Solidity REPL

Together they cover the full development lifecycle.

What Is Hardhat

Hardhat is a JavaScript and TypeScript based development environment. Tests and scripts are written in JS/TS using libraries like ethers.js or viem, and it has a rich plugin ecosystem.

It is well suited to teams comfortable in the Node.js world and projects with heavy off-chain tooling.

Language for Tests

The biggest difference is the testing language:

  • Foundry — tests are Solidity contracts; no context switch from your contract code
  • Hardhat — tests are JavaScript/TypeScript using a chai-style assertion library

Solidity tests give Foundry direct access to internal state and low-level cheatcodes.

// Foundry test (Solidity)
function testIncrement() public {
    counter.increment();
    assertEq(counter.number(), 1);
}

Speed

Foundry's Rust implementation and native EVM make it dramatically faster than Hardhat's Node-based runner, often by an order of magnitude on large suites.

Fast feedback loops matter: when tests run in milliseconds, developers run them constantly, catching bugs earlier.

Project Layout

A Foundry project has a conventional structure:

  • src/ — your contracts
  • test/ — Solidity test files (suffix .t.sol)
  • script/ — deployment scripts (suffix .s.sol)
  • lib/ — dependencies installed as git submodules
  • foundry.toml — configuration
myproject/
  src/Counter.sol
  test/Counter.t.sol
  script/Deploy.s.sol
  lib/forge-std/
  foundry.toml

Dependency Management

Foundry installs dependencies as git submodules via forge install, with import remappings instead of an npm registry. Hardhat uses npm packages.

Submodules keep exact source pinned in your repo, while npm offers familiar versioning and a vast package ecosystem.

$ forge install OpenZeppelin/openzeppelin-contracts
# adds to lib/ and updates remappings

Cheatcodes

Foundry's killer feature is cheatcodes: special calls via the vm object that manipulate EVM state in tests. You can warp time, set balances, impersonate addresses, and expect reverts.

Hardhat offers similar capabilities through helper plugins, but Foundry's are built in and accessible directly from Solidity.

vm.warp(block.timestamp + 1 days);
vm.prank(alice);
vm.expectRevert("not owner");

Scripting and Deployment

Foundry deployments are Solidity scripts run with forge script, using vm.startBroadcast() to send real transactions. Hardhat deployments are JS scripts run with hardhat run.

Solidity scripting keeps deployment logic in the same language as the contracts, reducing context switching.

function run() external {
    vm.startBroadcast();
    new Counter();
    vm.stopBroadcast();
}

Choosing Between Them

There is no universal winner:

  • Choose Foundry for speed, fuzzing, and Solidity-native testing
  • Choose Hardhat for rich JS/TS tooling and a large plugin ecosystem

Many teams use both: Foundry for unit and fuzz tests, Hardhat for complex deployment and integration tasks. They can coexist in one repo.

Installing Foundry

Foundry installs via foundryup, a version manager that fetches the latest binaries. Once installed, forge init scaffolds a new project ready to build and test.

$ curl -L https://foundry.paradigm.xyz | bash
$ foundryup
$ forge init my_project

Quick Check

In what language are Foundry tests written?

Recap

You compared the two main toolkits:

  • Foundry is Rust-based with Solidity tests; Hardhat is Node-based with JS/TS tests
  • Foundry tools: forge, cast, anvil, chisel
  • Foundry is faster and has built-in cheatcodes and fuzzing
  • Dependencies via git submodules vs npm

Next we dive into writing tests with forge.

免费开始

用 AI 导师学习 Web3 & DApp Development Fundamentals — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
29
课程
105

常见问题解答

「Foundry 与 Hardhat 对比」课时是免费的吗?

是的 — 「Foundry 与 Hardhat 对比」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

「Foundry 与 Hardhat 对比」这节课中我会学到什么?

基于 Rust 的工具 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Foundry 与 Hardhat 对比」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Foundry 与 Hardhat 对比
  2. forge 测试
  3. 模糊测试与不变量
  4. cast 与 anvil
← 返回 Web3 & DApp Development Fundamentals