0Pricing
Web3 & DApp Development Fundamentals · Lezione

Foundry e Hardhat a confronto

Strumenti basati su Rust

Foundry e Hardhat a confronto è una lezione Web3 & DApp Development Fundamentals gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Web3 & DApp Development Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Foundry e Hardhat a confronto» è gratuita?

Sì — il testo completo di «Foundry e Hardhat a confronto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Web3 & DApp Development Fundamentals, passa a CoddyKit PRO. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.

Cosa imparerò in «Foundry e Hardhat a confronto»?

Strumenti basati su Rust Eserciti Web3 & DApp Development Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Web3 & DApp Development Fundamentals?

Non è richiesta alcuna esperienza precedente. Web3 & DApp Development Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Foundry e Hardhat a confronto»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Web3 & DApp Development Fundamentals?

Sì. Ogni lezione Web3 & DApp Development Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Foundry e Hardhat a confronto
  2. Test con forge
  3. Fuzzing e invarianti
  4. cast e anvil
← Torna a Web3 & DApp Development Fundamentals