Web3 & DApp Development Fundamentals · 课时

ERC-721 标准

非同质化令牌

第 1 / 4 课13 个步骤

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

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

What Is ERC-721?

ERC-721 is the standard for non-fungible tokens (NFTs). Unlike ERC-20, each token is unique and identified by a distinct tokenId.

NFTs represent ownership of one-of-a-kind items: art, collectibles, in-game assets, and more.

Fungible vs Non-Fungible

The key contrast:

  • ERC-20 - all tokens identical and interchangeable, tracked by balance
  • ERC-721 - each token unique, tracked by owner per tokenId

You own specific token #42, not a fraction of a pool.

Core Ownership Functions

ERC-721 tracks ownership with two key views:

  • ownerOf(tokenId) - who owns a specific token
  • balanceOf(owner) - how many tokens an address owns
<code>function ownerOf(uint256 tokenId) external view returns (address);
function balanceOf(address owner) external view returns (uint256);</code>

Transfer Functions

ERC-721 provides transferFrom and the safer safeTransferFrom, which checks that a receiving contract can handle NFTs.

<code>function transferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId) external;</code>

Approval Functions

Owners can authorize others to transfer specific tokens with approve, or grant blanket permission over all their tokens with setApprovalForAll.

<code>function approve(address to, uint256 tokenId) external;
function setApprovalForAll(address operator, bool approved) external;
function getApproved(uint256 tokenId) external view returns (address);
function isApprovedForAll(address owner, address operator) external view returns (bool);</code>

Required Events

ERC-721 defines three events: Transfer, Approval, and ApprovalForAll. Note the tokenId is indexed so transfers of a specific NFT are easy to track.

<code>event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);</code>

safeTransferFrom and onERC721Received

safeTransferFrom protects against NFTs getting stuck. If the recipient is a contract, it must implement onERC721Received and return the correct magic value, otherwise the transfer reverts.

<code>// receiving contract must implement:
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data)
    external returns (bytes4);</code>

The Metadata Extension

Most NFTs implement the optional metadata extension adding name(), symbol(), and crucially tokenURI(tokenId), which points to the token's off-chain JSON metadata.

<code>function name() external view returns (string memory);
function symbol() external view returns (string memory);
function tokenURI(uint256 tokenId) external view returns (string memory);</code>

ERC-165 Interface Detection

ERC-721 builds on ERC-165, which lets contracts advertise which interfaces they support via supportsInterface. Marketplaces use it to detect NFTs.

<code>function supportsInterface(bytes4 interfaceId) external view returns (bool);</code>

Using OpenZeppelin ERC721

As with ERC-20, the practical approach is to inherit OpenZeppelin's audited ERC721 implementation and add your minting logic.

<code>// import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
// contract MyNFT is ERC721 {
//     constructor() ERC721('MyNFT', 'MNFT') {}
// }</code>

Where the Asset Lives

An NFT on-chain is just an id and an owner. The actual image or media usually lives off-chain (often on IPFS), referenced by the tokenURI. The chain proves ownership, not the file itself.

Quick Check

Test your understanding of the ERC-721 standard.

Recap

You learned the ERC-721 standard:

  • NFTs are unique, identified by tokenId, tracked via ownerOf
  • Transfers use transferFrom and safer safeTransferFrom
  • Approvals: per-token approve and blanket setApprovalForAll
  • Metadata extension adds tokenURI; ERC-165 enables interface detection
  • Assets usually live off-chain, referenced by the URI
免费开始

用 AI 导师学习 Web3 & DApp Development Fundamentals — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
29
课程
105

常见问题解答

「ERC-721 标准」课时是免费的吗?

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

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

非同质化令牌 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「ERC-721 标准」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. ERC-721 标准
  2. 铸造 NFT
  3. 令牌元数据
  4. 市场
← 返回 Web3 & DApp Development Fundamentals