Web3 & DApp Development Fundamentals · Lektion

Lokales Netzwerk und Forking

Testumgebungen

Lektion 4 von 413 Schritte

Lokales Netzwerk und Forking ist eine kostenlose Web3 & DApp Development Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Hardhat Network

Hardhat ships with a built-in local Ethereum network designed for development. It runs in-memory, mines blocks instantly, and gives you funded test accounts.

  • No real ETH or gas costs.
  • Deterministic accounts you can reuse.
  • Rich error messages and stack traces.

Starting a Local Node

Run a standalone JSON-RPC server so wallets and front-ends can connect:

npx hardhat node

This prints 20 funded accounts with their private keys and listens on http://127.0.0.1:8545.

npx hardhat node

The localhost Network

To target the running node, define a localhost network in your config:

module.exports = { solidity: "0.8.24", networks: { localhost: { url: "http://127.0.0.1:8545" }, }, };

Then deploy with npx hardhat run scripts/deploy.js --network localhost.

module.exports = {
  solidity: "0.8.24",
  networks: {
    localhost: { url: "http://127.0.0.1:8545" },
  },
};

In-Process vs Standalone

There are two ways to use the Hardhat Network:

  • In-process — a fresh network spun up automatically when you run test or run without --network. State is discarded after.
  • Standalone — started with npx hardhat node and kept alive for external connections.

What Is Forking

Forking creates a local copy of a real network (like Ethereum mainnet) at a given block. You get all the on-chain state — deployed contracts, balances, token data — but can experiment freely.

This is perfect for testing against live protocols such as Uniswap or Aave without spending real funds.

Configuring a Fork

Enable forking by pointing the Hardhat network at an archive RPC URL:

module.exports = { networks: { hardhat: { forking: { url: "https://mainnet.example-rpc.io/KEY", }, }, }, };

You typically need an archive node provider for full historical state.

module.exports = {
  networks: {
    hardhat: {
      forking: {
        url: "https://mainnet.example-rpc.io/KEY",
      },
    },
  },
};

Pinning a Block Number

For reproducible tests, pin the fork to a specific block:

forking: { url: "https://mainnet.example-rpc.io/KEY", blockNumber: 19000000, }

Pinning keeps state stable and lets Hardhat cache responses, so tests run faster and never drift as the chain advances.

forking: {
  url: "https://mainnet.example-rpc.io/KEY",
  blockNumber: 19000000,
}

Impersonating Accounts

On a fork you can act as any address — even one you do not own — to test flows like a whale moving tokens:

await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: ["0xWhaleAddress"], }); const whale = await hre.ethers.getSigner("0xWhaleAddress");
await hre.network.provider.request({
  method: "hardhat_impersonateAccount",
  params: ["0xWhaleAddress"],
});
const whale = await hre.ethers.getSigner("0xWhaleAddress");

Time and Block Manipulation

The local network exposes special RPC methods to control time and blocks, useful for testing vesting or auctions:

// Advance time by 1 day await hre.network.provider.send("evm_increaseTime", [86400]); // Mine a new block await hre.network.provider.send("evm_mine");
// Advance time by 1 day
await hre.network.provider.send("evm_increaseTime", [86400]);
// Mine a new block
await hre.network.provider.send("evm_mine");

Resetting State

You can reset the fork back to a clean snapshot between tests:

await hre.network.provider.request({ method: "hardhat_reset", params: [], });

This restores the original forked state without restarting the process, keeping test runs isolated.

await hre.network.provider.request({
  method: "hardhat_reset",
  params: [],
});

Snapshots and Reverts

Beyond a full reset, you can take lightweight snapshots and revert to them, which is how test fixtures work under the hood:

const id = await hre.network.provider.send("evm_snapshot"); // ... run some transactions ... await hre.network.provider.send("evm_revert", [id]);

Each snapshot id can be reverted to once.

const id = await hre.network.provider.send("evm_snapshot");
// ... run some transactions ...
await hre.network.provider.send("evm_revert", [id]);

Quick Check

Test your understanding of local networks and forking.

Recap

You learned to run and fork local networks.

  • npx hardhat node starts a standalone JSON-RPC node with funded accounts.
  • The in-process network is used automatically for tests and discarded after.
  • Forking copies live mainnet state locally for safe experimentation.
  • Pin blockNumber for reproducibility and caching.
  • Special RPC methods impersonate accounts, advance time, mine blocks, and reset state.
Kostenlos starten

Lerne Web3 & DApp Development Fundamentals mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
29
Lektionen
105

Häufig gestellte Fragen

Ist die Lektion „Lokales Netzwerk und Forking“ kostenlos?

Ja — der vollständige Text von „Lokales Netzwerk und Forking“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Lokales Netzwerk und Forking“?

Testumgebungen Du übst Web3 & DApp Development Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Web3 & DApp Development Fundamentals zu starten?

Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Lokales Netzwerk und Forking“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?

Ja. Jede Web3 & DApp Development Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Hardhat-Einrichtung
  2. Contracts kompilieren
  3. Skripte und Tasks
  4. Lokales Netzwerk und Forking
← Zurück zu Web3 & DApp Development Fundamentals