Blockchain Smart Contracts with Solidity · Lektion

ERC-20-Standard für fungible Tokens

Verstehen Sie die Spezifikationen und Funktionen des ERC-20-Standards zur Erstellung austauschbarer Tokens.

Lektion 1 von 412 Schritte

ERC-20-Standard für fungible Tokens ist eine kostenlose Blockchain Smart Contracts with Solidity-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Blockchain Smart Contracts with Solidity-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 specific amount of tokens.
  • allowance(address owner, address spender): Returns the amount of tokens that the spender is currently allowed to withdraw from the owner.

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(), and transfer().
  • It enables delegated transfers with approve(), allowance(), and transferFrom().
  • Standard events Transfer and Approval help 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!

Kostenlos starten

Lerne Blockchain Smart Contracts with Solidity mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „ERC-20-Standard für fungible Tokens“ kostenlos?

Ja — der vollständige Text von „ERC-20-Standard für fungible Tokens“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Blockchain Smart Contracts with Solidity-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „ERC-20-Standard für fungible Tokens“?

Verstehen Sie die Spezifikationen und Funktionen des ERC-20-Standards zur Erstellung austauschbarer Tokens. Du übst Blockchain Smart Contracts with Solidity mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Blockchain Smart Contracts with Solidity zu starten?

Keine Vorkenntnisse erforderlich. Blockchain Smart Contracts with Solidity auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „ERC-20-Standard für fungible Tokens“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Blockchain Smart Contracts with Solidity-Lektion Code schreiben und ausführen?

Ja. Jede Blockchain Smart Contracts with Solidity-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. ERC-20-Standard für fungible Tokens
  2. Einen ERC-20-Token implementieren
  3. ERC-721 Non-Fungible Tokens (NFTs)
  4. ERC-1155-Multi-Token-Standard
← Zurück zu Blockchain Smart Contracts with Solidity