实现令牌
transfer 与 approve
实现令牌 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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
transferchecks balance, updates state, emitsTransferapproverecords an allowance and emitsApproval- Guard against the zero address and rely on 0.8 overflow safety
常见问题解答
「实现令牌」课时是免费的吗?
是的 — 「实现令牌」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「实现令牌」这节课中我会学到什么?
transfer 与 approve 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「实现令牌」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。