Blockchain Smart Contracts with Solidity · บทเรียน

โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)

สำรวจมาตรฐาน ERC-721 สำหรับสินทรัพย์ดิจิทัลที่มีเอกลักษณ์ และเรียนรู้วิธีสร้างกับจัดการ NFTs

บทเรียน 3 จาก 411 ขั้นตอน

โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs) เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Unique Digital Assets

Welcome to the world of Non-Fungible Tokens (NFTs)! Unlike regular money or cryptocurrencies, NFTs are unique digital items.

Think of them like rare collectibles or one-of-a-kind art pieces, but entirely digital. Each NFT has distinct properties and cannot be replaced by another.

The ERC-721 Standard

On Ethereum, NFTs are defined by a standard called ERC-721. This standard is a set of rules that smart contracts must follow to create, manage, and transfer unique tokens.

It ensures that all compliant NFTs work similarly across different platforms, making them interoperable.

Key Concepts: Token ID & Ownership

Every ERC-721 token has a unique Token ID (a large number). This ID distinguishes it from all other NFTs, even those from the same collection.

The standard also defines functions to track ownership:

  • ownerOf(uint256 tokenId): Returns the address that owns a specific NFT.
  • balanceOf(address owner): Returns the number of NFTs an address owns from that contract.

Building with OpenZeppelin

Creating an ERC-721 contract from scratch can be complex. Luckily, OpenZeppelin provides battle-tested, secure implementations.

We can simply import their contracts and build upon them. This saves time and reduces the risk of vulnerabilities.

Your First ERC-721 Contract

Here's a basic structure for an ERC-721 contract using OpenZeppelin. It defines a name and symbol for our NFT collection in the constructor.

We'll add a minting function next.

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract SimpleNFT is ERC721 {
    constructor() ERC721("MySimpleNFT", "MSN") {
        // Constructor for ERC721 takes name and symbol
    }
}

Creating New NFTs: Minting

To bring new NFTs into existence, we use a process called minting. The _safeMint function (provided by OpenZeppelin's ERC721) creates a new token and assigns it to an owner.

Let's add a public mint function to our contract to create new NFTs.

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract SimpleNFT is ERC721 {
    uint256 private _tokenIdCounter;

    constructor() ERC721("MySimpleNFT", "MSN") {
        _tokenIdCounter = 0;
    }

    function mint(address to) public returns (uint256) {
        uint256 newItemId = _tokenIdCounter;
        _tokenIdCounter++;
        _safeMint(to, newItemId); // Mints token and assigns to 'to' address
        return newItemId;
    }
}

NFT Metadata with Token URI

An NFT's value often comes from what it represents (an image, a song, a game item). This information, called metadata, isn't stored directly on the blockchain due to cost.

Instead, the ERC-721 standard includes a tokenURI(uint256 tokenId) function. This function returns a URL pointing to a JSON file that describes the NFT's metadata (e.g., name, description, image URL).

Transferring Ownership

Once an NFT is minted, it can be transferred between owners. The ERC-721 standard includes functions for this:

  • transferFrom(address from, address to, uint256 tokenId): Transfers ownership of tokenId from from to to.
  • safeTransferFrom(...): A safer version that checks if the recipient contract can receive NFTs.

These functions ensure only the current owner or an approved address can initiate a transfer.

Delegated Transfers: Approvals

Sometimes, you might want to allow another address (like a marketplace) to transfer your NFT on your behalf. This is done using approvals.

The approve(address to, uint256 tokenId) function lets you grant permission to another address to to manage a specific tokenId. This is crucial for decentralized exchanges and other dApps.

ERC-721 Knowledge Check

Let's test your understanding of ERC-721 NFTs. Select all statements that are true.

Recap: ERC-721 NFTs

You've explored the fascinating world of ERC-721 Non-Fungible Tokens!

  • NFTs are unique digital assets, unlike fungible cryptocurrencies.
  • The ERC-721 standard defines how these unique tokens are created and managed on Ethereum.
  • Key features include unique Token IDs, ownership tracking, minting, and linking to off-chain metadata via tokenURI.
  • OpenZeppelin contracts simplify ERC-721 development.

NFTs are a cornerstone of digital ownership and are used in art, gaming, and many other applications!

เริ่มต้นได้ฟรี

เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)”

สำรวจมาตรฐาน ERC-721 สำหรับสินทรัพย์ดิจิทัลที่มีเอกลักษณ์ และเรียนรู้วิธีสร้างกับจัดการ NFTs คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม

ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. มาตรฐานโทเค็นแบบทดแทนกันได้ ERC-20
  2. การสร้างโทเค็น ERC-20
  3. โทเค็นแบบไม่ทดแทนกันได้ ERC-721 (NFTs)
  4. มาตรฐานโทเค็นหลายชนิด ERC-1155
← กลับไปที่ Blockchain Smart Contracts with Solidity