0Pricing
Web3 & DApp Development Fundamentals · 课时

ERC 标准(ERC-20、ERC-721)

学习常见的以太坊评议请求(ERC)标准,例如用于同质化代币的 ERC-20 和用于非同质化代币(NFT)的 ERC-721。

ERC 标准(ERC-20、ERC-721) 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

What are ERC Standards?

ERC stands for Ethereum Request for Comments. These are official proposals that define specific rules and functionalities for smart contracts on the Ethereum blockchain.

Think of them as blueprints or common agreements. They ensure that different contracts and applications can 'speak the same language' and interact seamlessly.

Understanding ERC-20 Tokens

An ERC-20 token is a standard for fungible tokens. Fungible means each unit is identical and interchangeable with another, just like regular currency.

If you have one dollar, it's the same as any other dollar. ERC-20 tokens are used for cryptocurrencies, stablecoins, and utility tokens.

ERC-20: Basic Operations

The ERC-20 standard defines a set of functions and events that all compliant tokens must implement. Key functions include:

  • totalSupply(): Returns the total number of tokens.
  • balanceOf(address account): Gets the balance of an address.
  • transfer(address recipient, uint256 amount): Sends tokens from the caller to a recipient.

ERC-20: Simple Transfer

Here's a simplified contract demonstrating the core idea of an ERC-20 token's transfer function. It manages balances and allows sending tokens.

Try deploying this in Remix and transferring tokens!

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;

    contract SimpleToken {
        string public name = "MySimpleToken";
        string public symbol = "MST";
        uint8 public decimals = 18;
        uint256 public totalSupply = 1000000 * (10 ** decimals);

        mapping(address => uint256) public balances;

        constructor() {
            balances[msg.sender] = totalSupply;
        }

        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;
        }

        function balanceOf(address account)
            public view returns (uint256)
        {
            return balances[account];
        }
    }

ERC-20: Approve & TransferFrom

To allow other contracts (like decentralized exchanges) to spend tokens on your behalf, ERC-20 uses allowances:

  • approve(address spender, uint256 amount): Allows a spender to withdraw up to amount from your account.
  • transferFrom(address sender, address recipient, uint256 amount): Transfers tokens from sender to recipient, usually called by a spender who has an allowance.

Introducing ERC-721 NFTs

ERC-721 tokens are for non-fungible tokens (NFTs). Non-fungible means each token is unique and cannot be replaced by another.

Think of a unique piece of art or a specific collectible card. NFTs are used for digital art, unique game items, land in virtual worlds, and more.

ERC-721: Ownership & Transfers

The ERC-721 standard also defines specific functions:

  • ownerOf(uint256 tokenId): Returns the owner of a specific token ID.
  • balanceOf(address owner): Returns the number of NFTs an owner has.
  • safeTransferFrom(address from, address to, uint256 tokenId): Safely transfers a specific NFT from one address to another.

ERC-721: Simple NFT Minting

This contract shows a basic way to 'mint' (create) unique NFTs and track their ownership. Each NFT has a unique tokenId.

After deploying, try calling the mint function multiple times!

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;

    contract SimpleNFT {
        string public name = "MySimpleNFT";
        string public symbol = "SNFT";
        uint256 private _nextTokenId;

        mapping(uint256 => address) public ownerOf;
        mapping(address => uint256) public balanceOf;

        constructor() {
            _nextTokenId = 0;
        }

        function mint(address recipient)
            public returns (uint256)
        {
            _nextTokenId++;
            uint256 newTokenId = _nextTokenId;
            ownerOf[newTokenId] = recipient;
            balanceOf[recipient]++;
            return newTokenId;
        }
    }

ERC-20 vs. ERC-721: Key Differences

Here's a quick comparison:

  • Fungibility: ERC-20 tokens are fungible (interchangeable); ERC-721 tokens are non-fungible (unique).
  • Uniqueness: ERC-20 tokens are tracked by quantity; ERC-721 tokens are tracked by unique IDs.
  • Use Cases: ERC-20 for currencies, voting rights; ERC-721 for digital art, collectibles, deeds.

Identify the Standards

Which of the following statements correctly describe the characteristics or use cases of ERC-20 and ERC-721 tokens?

Recap: ERC Standards

We've explored two foundational Ethereum standards:

  • ERC-20 for fungible tokens, ideal for currencies and utility tokens.
  • ERC-721 for non-fungible tokens (NFTs), perfect for unique digital assets like art and collectibles.

These standards are crucial for interoperability and building a wide range of DApps on Ethereum.

常见问题解答

「ERC 标准(ERC-20、ERC-721)」课时是免费的吗?

是的 — 「ERC 标准(ERC-20、ERC-721)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。

「ERC 标准(ERC-20、ERC-721)」这节课中我会学到什么?

学习常见的以太坊评议请求(ERC)标准,例如用于同质化代币的 ERC-20 和用于非同质化代币(NFT)的 ERC-721。 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。

「ERC 标准(ERC-20、ERC-721)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. ERC 标准(ERC-20、ERC-721)
  2. 合约安全最佳实践
  3. 可升级合约
← 返回 Web3 & DApp Development Fundamentals