ERC-20 标准
令牌接口
ERC-20 标准 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is ERC-20?
ERC-20 is the standard interface for fungible tokens on Ethereum. Fungible means every unit is identical and interchangeable, like currency.
Because all ERC-20 tokens share the same interface, wallets and exchanges can support any of them automatically.
The Required Functions
An ERC-20 token must implement six functions:
totalSupply()balanceOf(account)transfer(to, amount)approve(spender, amount)allowance(owner, spender)transferFrom(from, to, amount)
The Two Events
ERC-20 also requires two events so wallets can track activity:
Transfer- fired on any token movementApproval- fired 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>totalSupply and balanceOf
totalSupply() returns the total number of tokens in existence. balanceOf(account) returns how many tokens a given address holds.
<code>function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);</code>The Interface in Code
Solidity lets you declare the standard as an interface. Implementations inherit from it to guarantee compliance.
<code>interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}</code>Optional Metadata
Three optional functions improve display: name(), symbol(), and decimals(). They are not required by the core standard but are expected by wallets.
<code>function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);</code>Understanding Decimals
The EVM has no fractions, so tokens use decimals to represent fractional amounts. Most tokens use 18 decimals, matching Ether.
So 1 token displayed equals 1 * 10**18 in raw units.
<code>uint8 public constant decimals = 18;
// 1 token = 1_000_000_000_000_000_000 base units</code>Why a Standard Matters
Because ERC-20 defines a common shape, a wallet can display any token, a DEX can trade it, and a contract can integrate it without custom code. Interoperability is the whole point.
Return Values
The mutating functions transfer, approve, and transferFrom return a bool indicating success. Most modern implementations revert on failure rather than returning false.
<code>function transfer(address to, uint256 amount) external returns (bool);
// returns true on success; reverts on insufficient balance</code>Using OpenZeppelin
In practice most developers do not write ERC-20 from scratch. They inherit the battle-tested OpenZeppelin implementation and just set the name, symbol, and initial supply.
<code>// import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
// contract MyToken is ERC20 {
// constructor() ERC20('MyToken', 'MTK') {
// _mint(msg.sender, 1000 * 10 ** decimals());
// }
// }</code>The Allowance Mechanism
A defining ERC-20 feature is allowances: an owner can authorize a spender to move tokens on their behalf via approve and transferFrom. This powers DEXs and DeFi protocols.
You will explore this in detail later in the course.
Quick Check
Test your understanding of the ERC-20 standard.
Recap
You learned the ERC-20 standard:
- It defines fungible tokens with a common interface
- Six required functions plus Transfer and Approval events
- Optional name, symbol, and decimals for display (usually 18 decimals)
- Allowances enable delegated transfers
- OpenZeppelin provides a safe ready-made implementation
常见问题解答
「ERC-20 标准」课时是免费的吗?
是的 — 「ERC-20 标准」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「ERC-20 标准」这节课中我会学到什么?
令牌接口 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「ERC-20 标准」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。