0Pricing
Web3 & DApp Development Fundamentals · 课时

本地网络与分叉

测试环境

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

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

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.

常见问题解答

「本地网络与分叉」课时是免费的吗?

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

「本地网络与分叉」这节课中我会学到什么?

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

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

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

「本地网络与分叉」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 设置 Hardhat
  2. 编译合约
  3. 脚本与任务
  4. 本地网络与分叉
← 返回 Web3 & DApp Development Fundamentals