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

การนำโทเค็นไปใช้งาน

transfer และ approve

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

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

Building a Token

Now you will implement the core of an ERC-20 token from scratch. Understanding the internals helps you debug and audit real tokens, even when you later use libraries.

We will focus on the state, transfer, and approve.

Core State Variables

A token needs to track balances and total supply. Balances are stored in a mapping from address to amount.

<code>contract Token {
    string public name = 'MyToken';
    string public symbol = 'MTK';
    uint8 public decimals = 18;
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;
}</code>

The Constructor

The constructor mints an initial supply to the deployer. We increase totalSupply and credit the creator's balance.

<code>constructor(uint256 initialSupply) {
    totalSupply = initialSupply;
    balanceOf[msg.sender] = initialSupply;
}</code>

The Events

Declare the two required events. We will emit Transfer on every movement and Approval when an allowance is set.

<code>event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);</code>

Implementing transfer

The transfer function moves tokens from the caller to a recipient. It must check the caller has enough balance and emit the Transfer event.

<code>function transfer(address to, uint256 amount) public returns (bool) {
    require(balanceOf[msg.sender] >= amount, 'insufficient balance');
    balanceOf[msg.sender] -= amount;
    balanceOf[to] += amount;
    emit Transfer(msg.sender, to, amount);
    return true;
}</code>

Guarding Against Zero Address

Sending tokens to address(0) effectively destroys them by accident. Many implementations reject it explicitly.

<code>function transfer(address to, uint256 amount) public returns (bool) {
    require(to != address(0), 'transfer to zero');
    require(balanceOf[msg.sender] >= amount, 'insufficient');
    balanceOf[msg.sender] -= amount;
    balanceOf[to] += amount;
    emit Transfer(msg.sender, to, amount);
    return true;
}</code>

The allowance Mapping

To support delegated transfers we add a nested mapping: owner to spender to approved amount.

<code>mapping(address => mapping(address => uint256)) public allowance;</code>

Implementing approve

The approve function lets the caller authorize a spender to move up to a certain amount of their tokens. It records the allowance and emits Approval.

<code>function approve(address spender, uint256 amount) public returns (bool) {
    allowance[msg.sender][spender] = amount;
    emit Approval(msg.sender, spender, amount);
    return true;
}</code>

Overflow Safety

Since Solidity 0.8, arithmetic reverts on overflow and underflow automatically. The subtraction in transfer is safe: if the balance is too low it reverts, though the explicit require gives a clearer error.

Why Return bool

The standard specifies a bool return for compatibility. We return true on success. On failure we revert, which is the modern convention and safer than silently returning false.

Putting It Together

With state, events, transfer, and approve in place you have a functioning fungible token. The next lesson adds transferFrom to complete the allowance flow.

<code>// state + constructor + events + transfer + approve
// = a minimal working ERC-20</code>

Quick Check

Test your understanding of implementing a token.

Recap

You implemented the core of an ERC-20 token:

  • State: name, symbol, decimals, totalSupply, balanceOf mapping
  • Constructor mints the initial supply to the deployer
  • transfer checks balance, updates state, emits Transfer
  • approve records an allowance and emits Approval
  • Guard against the zero address and rely on 0.8 overflow safety

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

บทเรียน “การนำโทเค็นไปใช้งาน” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การนำโทเค็นไปใช้งาน”

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

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

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

บทเรียน “การนำโทเค็นไปใช้งาน” ใช้เวลานานแค่ไหน

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

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

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

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

  1. มาตรฐาน ERC-20
  2. การนำโทเค็นไปใช้งาน
  3. สิทธิ์การใช้จ่าย
  4. การสร้างและการเผา
← กลับไปที่ Web3 & DApp Development Fundamentals