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

สคริปต์และงาน

ทำเวิร์กโฟลว์ให้เป็นอัตโนมัติ

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Scripts vs Tasks

Hardhat offers two ways to automate work:

  • Scripts — plain Node.js files you run for one-off jobs like deployment.
  • Tasks — reusable commands integrated into the Hardhat CLI with arguments and help text.

Use scripts for ad-hoc automation, tasks for repeatable commands your team reuses.

A Deployment Script

Scripts live in scripts/. A deploy script uses ethers from the Hardhat Runtime Environment:

const hre = require("hardhat"); async function main() { const Greeter = await hre.ethers.getContractFactory("Greeter"); const greeter = await Greeter.deploy(); await greeter.waitForDeployment(); console.log("Deployed to:", await greeter.getAddress()); } main();
const hre = require("hardhat");

async function main() {
  const Greeter = await hre.ethers.getContractFactory("Greeter");
  const greeter = await Greeter.deploy();
  await greeter.waitForDeployment();
  console.log("Deployed to:", await greeter.getAddress());
}

main();

Running a Script

Run a script through Hardhat so it injects the runtime environment and network:

npx hardhat run scripts/deploy.js

Add --network to target a specific network. Without it, the script runs against an ephemeral in-process network.

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

The Runtime Environment

The Hardhat Runtime Environment (HRE) is injected automatically and gives you:

  • hre.ethers — the ethers.js helpers.
  • hre.network — info about the active network.
  • hre.config — the resolved configuration.

Inside tasks the HRE is the second argument; in scripts it is the hardhat module.

Defining a Custom Task

Define tasks in hardhat.config.js using the task helper:

const { task } = require("hardhat/config"); task("accounts", "Prints the list of accounts", async (taskArgs, hre) => { const accounts = await hre.ethers.getSigners(); for (const account of accounts) { console.log(account.address); } });
const { task } = require("hardhat/config");

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();
  for (const account of accounts) {
    console.log(account.address);
  }
});

Running a Custom Task

Once defined, your task appears in the CLI like any built-in command:

npx hardhat accounts

It also shows up under npx hardhat with its description, so teammates can discover it without reading the config.

npx hardhat accounts

Task Parameters

Tasks accept named parameters with addParam:

task("balance", "Prints an account balance") .addParam("account", "The address") .setAction(async (taskArgs, hre) => { const bal = await hre.ethers.provider.getBalance(taskArgs.account); console.log(hre.ethers.formatEther(bal), "ETH"); });

Run it with npx hardhat balance --account 0x....

task("balance", "Prints an account balance")
  .addParam("account", "The address")
  .setAction(async (taskArgs, hre) => {
    const bal = await hre.ethers.provider.getBalance(taskArgs.account);
    console.log(hre.ethers.formatEther(bal), "ETH");
  });

Overriding Built-in Tasks

You can extend existing tasks like compile using the same name. Use runSuper to call the original behavior:

task("compile", async (args, hre, runSuper) => { console.log("Before compiling..."); await runSuper(args); console.log("Done compiling!"); });

This is handy for adding logging or post-processing steps.

task("compile", async (args, hre, runSuper) => {
  console.log("Before compiling...");
  await runSuper(args);
  console.log("Done compiling!");
});

Subtasks

Subtasks are internal tasks not shown in the main help list. They let you break complex tasks into reusable pieces:

const { subtask } = require("hardhat/config"); subtask("print-network", async (args, hre) => { console.log("Network:", hre.network.name); });

Call subtasks from other tasks with hre.run("print-network").

const { subtask } = require("hardhat/config");

subtask("print-network", async (args, hre) => {
  console.log("Network:", hre.network.name);
});

When to Use Each

Rules of thumb:

  • Need a quick deploy or migration? Write a script.
  • Need a reusable command with parameters and help text? Write a task.
  • Need to compose internal steps? Use subtasks.

Both scripts and tasks have full access to the HRE.

Scripts in package.json

For convenience, wrap common commands as npm scripts:

{ "scripts": { "deploy": "hardhat run scripts/deploy.js --network localhost", "test": "hardhat test" } }

Then teammates run npm run deploy without memorizing flags.

{
  "scripts": {
    "deploy": "hardhat run scripts/deploy.js --network localhost",
    "test": "hardhat test"
  }
}

Quick Check

Test your understanding of scripts and tasks.

Recap

You learned how to automate Hardhat workflows.

  • Scripts in scripts/ run via npx hardhat run for one-off jobs.
  • Tasks defined with task() become first-class CLI commands.
  • The HRE exposes ethers, network, and config.
  • addParam adds CLI arguments; runSuper extends built-in tasks.
  • Subtasks compose internal steps callable with hre.run.

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

บทเรียน “สคริปต์และงาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สคริปต์และงาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “สคริปต์และงาน” ใช้เวลานานแค่ไหน

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

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

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

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

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