ERC-20 同质化代币标准
了解 ERC-20 标准的规范和功能,以创建可互换的代币。
ERC-20 同质化代币标准 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Intro to Blockchain Tokens
Welcome to the world of blockchain tokens! Beyond cryptocurrencies like Ether, blockchains host many other digital assets.
These assets, often called 'tokens', can represent anything from digital currency to ownership of real-world items, or even voting rights in a decentralized organization.
Fungibility: Interchangeable Assets
A key concept for tokens is fungibility. What does it mean?
- Fungible: Each unit is identical and interchangeable with another. Like a dollar bill – one $1 bill is the same as any other $1 bill.
- Non-Fungible: Each unit is unique and cannot be replaced by another. Like a piece of art or a specific house.
ERC-20 tokens are fungible, meaning every token is equal to every other token of the same type.
Meet the ERC-20 Standard
ERC-20 (Ethereum Request for Comment 20) is a technical standard for fungible tokens on the Ethereum blockchain.
It defines a common set of rules that all compliant tokens must follow. This ensures they can interact seamlessly with wallets, exchanges, and other smart contracts.
Interoperability Through Standards
Why is a standard so important?
- Compatibility: Any wallet that supports ERC-20 can manage any ERC-20 token.
- Ecosystem Growth: Decentralized applications (dApps) can easily integrate different tokens.
- Predictable Behavior: Developers know exactly how an ERC-20 token will behave, making integration safer and faster.
Core Function: `totalSupply()`
The totalSupply() function returns the total number of tokens in existence. It's a public view function, meaning it doesn't change the blockchain state and costs no gas to call.
Try running this simple contract to see how totalSupply might be represented:
pragma solidity ^0.8.0;
contract SimpleTokenInfo {
uint256 public totalTokens = 1000; // Example supply
function totalSupply() public view returns (uint256) {
return totalTokens;
}
}Core Function: `balanceOf()`
The balanceOf(address account) function returns the token balance of a specific address. It helps you check how many tokens a user owns.
Let's add balanceOf to our example. The deployer will start with all tokens.
pragma solidity ^0.8.0;
contract SimpleTokenInfo {
uint256 public totalTokens = 1000;
mapping(address => uint256) public balances;
constructor() {
balances[msg.sender] = totalTokens; // Assign all to deployer
}
function totalSupply() public view returns (uint256) {
return totalTokens;
}
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
}Core Function: `transfer()`
The transfer(address recipient, uint256 amount) function allows the sender of the transaction to send tokens directly to another address.
This is the most common way to move tokens between users.
pragma solidity ^0.8.0;
contract SimpleTokenInfo {
uint256 public totalTokens = 1000;
mapping(address => uint256) public balances;
constructor() {
balances[msg.sender] = totalTokens;
}
function totalSupply() public view returns (uint256) {
return totalTokens;
}
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
function transfer(address recipient, uint256 amount) public returns (bool) {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[recipient] += amount;
return true;
}
}Delegated Transfers: `approve()` & `allowance()`
ERC-20 also supports delegated transfers, where one user (the owner) allows another address (the spender) to move tokens on their behalf.
approve(address spender, uint256 amount): The owner grants permission to a spender to withdraw a specificamountof tokens.allowance(address owner, address spender): Returns the amount of tokens that thespenderis currently allowed to withdraw from theowner.
Executing Delegated Transfers: `transferFrom()`
Once an approve() call has been made, the designated spender can then use the transferFrom() function.
transferFrom(address sender, address recipient, uint256 amount): Allows a spender to transfer amount tokens from the sender's balance to the recipient, provided the spender has enough allowance from the sender.
Tracking Actions: ERC-20 Events
ERC-20 defines two standard events: Transfer and Approval. These events are crucial for off-chain applications (like wallets or block explorers) to track token movements and approvals without having to read the entire blockchain state.
They emit logs that can be efficiently indexed and queried.
pragma solidity ^0.8.0;
contract EventExample {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function demonstrateTransferEvent(address _to, uint256 _value) public {
// Imagine actual transfer logic here
emit Transfer(msg.sender, _to, _value);
}
function demonstrateApprovalEvent(address _spender, uint256 _value) public {
// Imagine actual approval logic here
emit Approval(msg.sender, _spender, _value);
}
}Check Your Understanding
The ERC-20 standard outlines specific functions that all compliant tokens must implement. Understanding these is crucial for working with tokens.
ERC-20: Your Token Blueprint
You've now learned about the ERC-20 Fungible Token Standard! It's the blueprint for most interchangeable tokens on Ethereum.
- It defines core functions like
totalSupply(),balanceOf(), andtransfer(). - It enables delegated transfers with
approve(),allowance(), andtransferFrom(). - Standard events
TransferandApprovalhelp track token activity.
This standard ensures that all ERC-20 tokens are compatible, making the Ethereum ecosystem robust and easy to navigate. Next, we'll dive into implementing your own ERC-20 token!
常见问题解答
「ERC-20 同质化代币标准」课时是免费的吗?
是的 — 「ERC-20 同质化代币标准」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
「ERC-20 同质化代币标准」这节课中我会学到什么?
了解 ERC-20 标准的规范和功能,以创建可互换的代币。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Blockchain Smart Contracts with Solidity 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「ERC-20 同质化代币标准」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?
能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ERC-20 同质化代币标准
- 实施 ERC-20 代币
- ERC-721 非同质化代币(NFT)
- ERC-1155 多代币标准