การสร้างและการเผา
การจัดการอุปทาน
การสร้างและการเผา เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Managing Supply
Minting creates new tokens and burning destroys them. Together they let a token's totalSupply grow or shrink over time.
These operations are not part of the core ERC-20 interface, but they are extremely common extensions.
What Minting Does
Minting increases totalSupply and credits new tokens to a recipient's balance. It is how tokens enter circulation - at deployment, as rewards, or on demand.
Implementing Mint
A mint function adds to both totalSupply and the recipient's balance, then emits a Transfer event from the zero address.
<code>function _mint(address to, uint256 amount) internal {
require(to != address(0), 'mint to zero');
totalSupply += amount;
balanceOf[to] += amount;
emit Transfer(address(0), to, amount);
}</code>Mint Emits From Zero
By convention a mint is logged as a Transfer from address(0). Wallets and indexers interpret a transfer originating at the zero address as new tokens being created.
<code>emit Transfer(address(0), to, amount); // signals minting</code>What Burning Does
Burning reduces totalSupply and subtracts from a holder's balance, permanently removing tokens from circulation. It is used for deflationary mechanics, redemptions, and buybacks.
Implementing Burn
A burn function checks the holder's balance, decreases it and totalSupply, then emits a Transfer to the zero address.
<code>function _burn(address from, uint256 amount) internal {
require(balanceOf[from] >= amount, 'burn exceeds balance');
balanceOf[from] -= amount;
totalSupply -= amount;
emit Transfer(from, address(0), amount);
}</code>Burn Emits To Zero
Symmetrically, a burn is logged as a Transfer to address(0). Tokens sent to the zero address are unrecoverable, which is exactly the intent.
<code>emit Transfer(from, address(0), amount); // signals burning</code>Access Control on Mint
Minting must be restricted - an open mint function lets anyone create unlimited tokens, destroying value. Gate it behind an owner or minter role.
<code>address public owner;
modifier onlyOwner() { require(msg.sender == owner, 'not owner'); _; }
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}</code>Burning Your Own Tokens
Holders are usually allowed to burn their own tokens freely. A public burn simply burns from msg.sender.
<code>function burn(uint256 amount) public {
_burn(msg.sender, amount);
}</code>Capped Supply
To guarantee scarcity, a token can enforce a maximum supply by checking against a cap before minting.
<code>uint256 public immutable cap;
function mint(address to, uint256 amount) public onlyOwner {
require(totalSupply + amount <= cap, 'cap exceeded');
_mint(to, amount);
}</code>Burn From Allowance
Like transferFrom, a burnFrom lets an approved spender burn another account's tokens, decrementing the allowance first.
<code>function burnFrom(address from, uint256 amount) public {
require(allowance[from][msg.sender] >= amount, 'allowance');
allowance[from][msg.sender] -= amount;
_burn(from, amount);
}</code>Quick Check
Test your understanding of minting and burning.
Recap
You learned supply management:
- Mint increases totalSupply, credits a balance, emits Transfer from address(0)
- Burn decreases totalSupply, debits a balance, emits Transfer to address(0)
- Minting must be access-controlled; holders can usually burn their own tokens
- Caps enforce a maximum supply; burnFrom uses allowances
เรียนรู้ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- มาตรฐาน ERC-20
- การนำโทเค็นไปใช้งาน
- สิทธิ์การใช้จ่าย
- การสร้างและการเผา