Web3 & DApp Development Fundamentals · บทเรียน

เครือข่ายภายในเครื่องและการทำฟอร์ก

สภาพแวดล้อมการทดสอบ

บทเรียน 4 จาก 413 ขั้นตอน

เครือข่ายภายในเครื่องและการทำฟอร์ก เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เริ่มต้นได้ฟรี

เรียนรู้ Web3 & DApp Development Fundamentals ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
29
บทเรียน
105

คำถามที่พบบ่อย

บทเรียน “เครือข่ายภายในเครื่องและการทำฟอร์ก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เครือข่ายภายในเครื่องและการทำฟอร์ก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เครือข่ายภายในเครื่องและการทำฟอร์ก”

สภาพแวดล้อมการทดสอบ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เครือข่ายภายในเครื่องและการทำฟอร์ก” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่า Hardhat
  2. การคอมไพล์สัญญา
  3. สคริปต์และงาน
  4. เครือข่ายภายในเครื่องและการทำฟอร์ก
← กลับไปที่ Web3 & DApp Development Fundamentals