0Pricing
Web3 & DApp Development Fundamentals · 课时

铸造 NFT

唯一令牌 ID

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

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

Creating Unique Tokens

Minting an NFT creates a brand new token with a unique tokenId and assigns it an owner. Unlike ERC-20 minting (which adds to a balance), each NFT mint produces a distinct, identifiable asset.

Ownership Mapping

The core NFT state is a mapping from tokenId to owner address, plus a balance count per owner.

<code>mapping(uint256 => address) private _owners;
mapping(address => uint256) private _balances;</code>

A Basic Mint Function

Minting assigns the token to a recipient, increments their balance, and emits a Transfer from the zero address - just like ERC-20 mint conventions.

<code>function _mint(address to, uint256 tokenId) internal {
    require(to != address(0), 'mint to zero');
    require(_owners[tokenId] == address(0), 'already minted');
    _owners[tokenId] = to;
    _balances[to] += 1;
    emit Transfer(address(0), to, tokenId);
}</code>

Preventing Duplicate IDs

A token id can only be minted once. The check _owners[tokenId] == address(0) ensures you never overwrite an existing token.

<code>require(_owners[tokenId] == address(0), 'token already exists');</code>

Auto-Incrementing IDs

To avoid id collisions, many contracts use a counter that increases with each mint, guaranteeing unique sequential ids.

<code>uint256 private _nextId;

function mint(address to) public {
    uint256 id = _nextId;
    _nextId += 1;
    _mint(to, id);
}</code>

Safe Minting

_safeMint behaves like _mint but, if the recipient is a contract, verifies it implements onERC721Received. This prevents NFTs from being locked in contracts that cannot handle them.

<code>function _safeMint(address to, uint256 tokenId) internal {
    _mint(to, tokenId);
    require(_checkOnERC721Received(to, tokenId), 'unsafe recipient');
}</code>

Access Control

Like ERC-20, an unrestricted mint is dangerous. Gate minting behind ownership or a minter role so only authorized accounts can create tokens.

<code>function mint(address to, uint256 id) public onlyOwner {
    _safeMint(to, id);
}</code>

Public Mint with Payment

Many NFT drops let the public mint by paying a fee. The function is payable and checks the sent value.

<code>uint256 public price = 0.05 ether;

function publicMint() public payable {
    require(msg.value >= price, 'insufficient payment');
    _safeMint(msg.sender, _nextId);
    _nextId += 1;
}</code>

Setting Metadata at Mint

When minting you often record a per-token metadata URI so each NFT can have its own image and attributes.

<code>mapping(uint256 => string) private _tokenURIs;

function mintWithURI(address to, uint256 id, string memory uri) public onlyOwner {
    _safeMint(to, id);
    _tokenURIs[id] = uri;
}</code>

Supply Limits

Collections usually cap the total number of NFTs. Enforce it with a max-supply check during minting.

<code>uint256 public constant MAX_SUPPLY = 10000;

function mint(address to) public onlyOwner {
    require(_nextId < MAX_SUPPLY, 'sold out');
    _safeMint(to, _nextId);
    _nextId += 1;
}</code>

Batch Minting

To mint several NFTs in one transaction, loop the mint logic. Be mindful of the block gas limit for large batches.

<code>function batchMint(address to, uint256 count) public onlyOwner {
    for (uint256 i = 0; i < count; i++) {
        _safeMint(to, _nextId);
        _nextId += 1;
    }
}</code>

Quick Check

Test your understanding of minting NFTs.

Recap

You learned how to mint NFTs:

  • Each mint assigns a unique tokenId to an owner and emits Transfer from address(0)
  • Prevent duplicate ids; auto-increment counters keep ids unique
  • _safeMint protects against locking NFTs in incompatible contracts
  • Add access control, optional payment, metadata, supply caps, and batch minting

常见问题解答

「铸造 NFT」课时是免费的吗?

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

「铸造 NFT」这节课中我会学到什么?

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

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

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

「铸造 NFT」课时需要多长时间?

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

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

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

此课程中的所有课时

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