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

ส่วนขยายโทเค็น

Pausable, Burnable

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

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

Extending Tokens

The base ERC-20 only handles transfers and approvals. OpenZeppelin provides extensions that add features by inheritance:

  • Pausable — freeze transfers in emergencies.
  • Burnable — let holders destroy tokens.
  • Capped — enforce a maximum supply.
  • Permit — gasless approvals via signatures.

Combining via Inheritance

You mix extensions by listing multiple base contracts:

import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol"; contract MyToken is ERC20, ERC20Burnable { constructor() ERC20("MyToken", "MTK") {} }
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract MyToken is ERC20, ERC20Burnable {
    constructor() ERC20("MyToken", "MTK") {}
}

Burnable Tokens

ERC20Burnable adds two functions:

  • burn(amount) — destroys the caller's own tokens.
  • burnFrom(account, amount) — burns from an account that approved you.

Burning reduces totalSupply, which can be used for deflationary mechanics.

// holder destroys their tokens
token.burn(ethers.parseEther("100"));

Pausable Tokens

ERC20Pausable lets an authorized account halt all transfers — useful during a security incident:

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract MyToken is ERC20, ERC20Pausable, Ownable { function pause() public onlyOwner { _pause(); } function unpause() public onlyOwner { _unpause(); } }
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, ERC20Pausable, Ownable {
    function pause() public onlyOwner { _pause(); }
    function unpause() public onlyOwner { _unpause(); }
}

How Pause Works

When paused, transfers revert with EnforcedPause. The Pausable extension hooks into the token's internal transfer logic, so transfer, transferFrom, mint, and burn are all blocked until unpause is called.

Use pausing sparingly — it centralizes control.

Resolving Function Conflicts

When two parents define the same internal hook, Solidity requires you to override it and specify the order:

function _update(address from, address to, uint256 value) internal override(ERC20, ERC20Pausable) { super._update(from, to, value); }

This tells the compiler how to linearize the inheritance.

function _update(address from, address to, uint256 value)
    internal override(ERC20, ERC20Pausable) {
    super._update(from, to, value);
}

Capped Supply

ERC20Capped enforces a hard maximum supply set at deployment:

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol"; contract MyToken is ERC20Capped { constructor() ERC20("MyToken", "MTK") ERC20Capped(1000000 * 10 ** 18) {} }

Any mint that would exceed the cap reverts.

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";

contract MyToken is ERC20Capped {
    constructor()
        ERC20("MyToken", "MTK")
        ERC20Capped(1000000 * 10 ** 18) {}
}

Gasless Approvals with Permit

ERC20Permit lets users approve spending with an off-chain signature instead of an on-chain approve transaction:

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol"; contract MyToken is ERC20, ERC20Permit { constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {} }

This improves UX by removing one transaction.

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";

contract MyToken is ERC20, ERC20Permit {
    constructor()
        ERC20("MyToken", "MTK")
        ERC20Permit("MyToken") {}
}

Extensions for NFTs Too

ERC-721 has parallel extensions:

  • ERC721Enumerable — iterate over all tokens.
  • ERC721URIStorage — per-token metadata URIs.
  • ERC721Burnable / ERC721Pausable — same ideas as ERC-20.

The pattern of composing through inheritance is the same.

Compose Only What You Need

Each extension adds code, gas cost, and surface area. Include only the features your token actually requires:

  • Fewer extensions mean cheaper deployment and a smaller attack surface.
  • Document why each extension is present.

Votes Extension

ERC20Votes tracks historical voting power by snapshotting balances at each block, enabling on-chain governance:

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";

Holders can delegate their votes, and proposals can query past voting power to prevent flash-loan manipulation.

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";

Quick Check

Test your understanding of token extensions.

Recap

You learned to compose token features with extensions.

  • Extensions add behavior by multiple inheritance.
  • Burnable destroys tokens; Capped enforces max supply.
  • Pausable freezes transfers in emergencies.
  • Permit enables gasless, signature-based approvals.
  • Resolve hook conflicts by overriding (e.g. _update) and include only what you need.

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

บทเรียน “ส่วนขยายโทเค็น” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ส่วนขยายโทเค็น”

Pausable, Burnable คุณปฏิบัติ 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. เหตุใดจึงใช้ OpenZeppelin
  2. การควบคุมการเข้าถึง
  3. ส่วนขยายโทเค็น
  4. สัญญาที่อัปเกรดได้
← กลับไปที่ Web3 & DApp Development Fundamentals