nft-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

NFT Development Skill

NFT开发技能

Master NFT development with token standards, metadata design, marketplace integration, and on-chain generative art.
精通NFT开发,涵盖代币标准、元数据设计、交易市场集成与链上生成艺术。

Quick Start

快速开始

python
undefined
python
undefined

Invoke this skill for NFT development

调用此技能进行NFT开发

Skill("nft-development", topic="standards", standard="ERC721A")
undefined
Skill("nft-development", topic="standards", standard="ERC721A")
undefined

Topics Covered

涵盖主题

1. Token Standards

1. 代币标准

Choose the right standard:
  • ERC-721: Standard NFTs, one token per ID
  • ERC-721A: Gas-optimized batch minting
  • ERC-1155: Multi-token, semi-fungible
  • DN404: Divisible NFT hybrid
选择合适的标准:
  • ERC-721: 标准NFT,每个ID对应一个代币
  • ERC-721A: 优化Gas的批量铸造
  • ERC-1155: 多代币、半同质化
  • DN404: 可拆分NFT混合标准

2. Metadata Design

2. 元数据设计

Structure your NFT data:
  • On-chain: Base64 JSON, SVG
  • IPFS: Content-addressed storage
  • Arweave: Permanent storage
  • Dynamic: Evolving traits
构建你的NFT数据结构:
  • 链上存储: Base64 JSON、SVG
  • IPFS: 内容寻址存储
  • Arweave: 永久存储
  • 动态元数据: 可进化的属性特征

3. Marketplace Integration

3. 交易市场集成

List and sell NFTs:
  • OpenSea: Seaport protocol
  • Royalties: EIP-2981
  • Operator Filter: Royalty enforcement
  • Collection Verification: Metadata standards
上架并售卖NFT:
  • OpenSea: Seaport协议
  • 版税: EIP-2981
  • 操作符过滤器: 版税强制执行
  • 集合验证: 元数据标准

4. On-Chain Art

4. 链上艺术

Generate art in Solidity:
  • SVG Generation: Dynamic shapes
  • Seed-based: Deterministic randomness
  • Traits: Rarity distribution
  • Base64: Data URI encoding
用Solidity生成艺术作品:
  • SVG生成: 动态图形
  • 种子驱动: 确定性随机
  • 属性特征: 稀有度分布
  • Base64: 数据URI编码

Code Examples

代码示例

ERC-721A Mint

ERC-721A铸造

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

import "erc721a/contracts/ERC721A.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyNFT is ERC721A, Ownable {
    uint256 public constant MAX_SUPPLY = 10000;
    uint256 public constant PRICE = 0.08 ether;

    error MaxSupplyReached();
    error InsufficientPayment();

    constructor() ERC721A("MyNFT", "MNFT") Ownable(msg.sender) {}

    function mint(uint256 quantity) external payable {
        if (_totalMinted() + quantity > MAX_SUPPLY) revert MaxSupplyReached();
        if (msg.value < PRICE * quantity) revert InsufficientPayment();

        _mint(msg.sender, quantity);
    }

    function _startTokenId() internal pure override returns (uint256) {
        return 1;
    }
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "erc721a/contracts/ERC721A.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyNFT is ERC721A, Ownable {
    uint256 public constant MAX_SUPPLY = 10000;
    uint256 public constant PRICE = 0.08 ether;

    error MaxSupplyReached();
    error InsufficientPayment();

    constructor() ERC721A("MyNFT", "MNFT") Ownable(msg.sender) {}

    function mint(uint256 quantity) external payable {
        if (_totalMinted() + quantity > MAX_SUPPLY) revert MaxSupplyReached();
        if (msg.value < PRICE * quantity) revert InsufficientPayment();

        _mint(msg.sender, quantity);
    }

    function _startTokenId() internal pure override returns (uint256) {
        return 1;
    }
}

On-Chain SVG

链上SVG

solidity
function tokenURI(uint256 tokenId) public view returns (string memory) {
    uint256 seed = seeds[tokenId];

    string memory svg = string(abi.encodePacked(
        '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">',
        '<rect width="400" height="400" fill="#', _getColor(seed), '"/>',
        '<circle cx="200" cy="200" r="80" fill="#', _getColor(seed >> 24), '"/>',
        '</svg>'
    ));

    string memory json = string(abi.encodePacked(
        '{"name":"Art #', tokenId.toString(),
        '","image":"data:image/svg+xml;base64,', Base64.encode(bytes(svg)), '"}'
    ));

    return string(abi.encodePacked(
        "data:application/json;base64,",
        Base64.encode(bytes(json))
    ));
}
solidity
function tokenURI(uint256 tokenId) public view returns (string memory) {
    uint256 seed = seeds[tokenId];

    string memory svg = string(abi.encodePacked(
        '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">',
        '<rect width="400" height="400" fill="#', _getColor(seed), '"/>',
        '<circle cx="200" cy="200" r="80" fill="#', _getColor(seed >> 24), '"/>',
        '</svg>'
    ));

    string memory json = string(abi.encodePacked(
        '{"name":"Art #', tokenId.toString(),
        '","image":"data:image/svg+xml;base64,', Base64.encode(bytes(svg)), '"}'
    ));

    return string(abi.encodePacked(
        "data:application/json;base64,",
        Base64.encode(bytes(json))
    ));
}

EIP-2981 Royalties

EIP-2981版税

solidity
import "@openzeppelin/contracts/token/common/ERC2981.sol";

contract NFTWithRoyalty is ERC721A, ERC2981 {
    constructor() {
        _setDefaultRoyalty(msg.sender, 500); // 5%
    }

    function supportsInterface(bytes4 interfaceId)
        public view override(ERC721A, ERC2981)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}
solidity
import "@openzeppelin/contracts/token/common/ERC2981.sol";

contract NFTWithRoyalty is ERC721A, ERC2981 {
    constructor() {
        _setDefaultRoyalty(msg.sender, 500); // 5%
    }

    function supportsInterface(bytes4 interfaceId)
        public view override(ERC721A, ERC2981)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

Metadata Schema

元数据 Schema

ERC-721 Standard

ERC-721标准

json
{
  "name": "Cool NFT #1",
  "description": "A very cool NFT",
  "image": "ipfs://QmXxx.../1.png",
  "animation_url": "ipfs://QmXxx.../1.mp4",
  "external_url": "https://example.com/1",
  "attributes": [
    { "trait_type": "Background", "value": "Blue" },
    { "trait_type": "Rarity", "value": "Legendary" },
    { "display_type": "number", "trait_type": "Power", "value": 100 }
  ]
}
json
{
  "name": "Cool NFT #1",
  "description": "A very cool NFT",
  "image": "ipfs://QmXxx.../1.png",
  "animation_url": "ipfs://QmXxx.../1.mp4",
  "external_url": "https://example.com/1",
  "attributes": [
    { "trait_type": "Background", "value": "Blue" },
    { "trait_type": "Rarity", "value": "Legendary" },
    { "display_type": "number", "trait_type": "Power", "value": 100 }
  ]
}

Standard Comparison

标准对比

StandardBest ForGas (1 mint)Gas (5 mints)
ERC-721Simple NFTs~100k~500k
ERC-721APFP collections~100k~120k
ERC-1155Game items~50k~80k
标准适用场景Gas消耗(1次铸造)Gas消耗(5次铸造)
ERC-721简单NFT~100k~500k
ERC-721APFP系列~100k~120k
ERC-1155游戏道具~50k~80k

Common Pitfalls

常见陷阱

PitfallIssueSolution
Metadata not showingBad tokenURIValidate JSON, check CORS
Gas too highStandard ERC-721Use ERC-721A for batches
Royalties not paidMarketplaces ignoreUse operator filter
Reveal brokenWrong base URITest on testnet first
陷阱问题解决方案
元数据不显示tokenURI错误验证JSON格式,检查CORS设置
Gas消耗过高使用标准ERC-721批量铸造时使用ERC-721A
版税未到账交易市场忽略版税使用操作符过滤器
揭露功能失效基础URI错误先在测试网测试

Troubleshooting

故障排除

"Metadata not showing on OpenSea"

"OpenSea上元数据不显示"

  1. Verify tokenURI returns valid JSON
  2. Check IPFS gateway accessibility
  3. Refresh metadata via API:
bash
curl "https://api.opensea.io/api/v2/chain/ethereum/contract/{addr}/nfts/{id}/refresh"
  1. 验证tokenURI返回有效的JSON
  2. 检查IPFS网关的可访问性
  3. 通过API刷新元数据:
bash
curl "https://api.opensea.io/api/v2/chain/ethereum/contract/{addr}/nfts/{id}/refresh"

"Batch mint out of gas"

"批量铸造Gas不足"

  • Use ERC-721A instead of ERC-721
  • Limit batch size to 10-20 per tx
  • 用ERC-721A替代ERC-721
  • 限制每笔交易的批量大小为10-20个

"Royalties not enforced"

"版税未强制执行"

Implement operator filter:
solidity
import "operator-filter-registry/src/DefaultOperatorFilterer.sol";

function setApprovalForAll(address op, bool approved)
    public override onlyAllowedOperatorApproval(op)
{
    super.setApprovalForAll(op, approved);
}
实现操作符过滤器:
solidity
import "operator-filter-registry/src/DefaultOperatorFilterer.sol";

function setApprovalForAll(address op, bool approved)
    public override onlyAllowedOperatorApproval(op)
{
    super.setApprovalForAll(op, approved);
}

Gas Optimization

Gas优化

TechniqueSavings
ERC721A batching~80% for batches
Merkle whitelist~50% vs mapping
Packed storage~20k per slot
Custom errors~200 per error
优化技巧节省比例
ERC721A批量铸造批量铸造时节省~80%
Merkle白名单比映射节省~50%
打包存储每个插槽节省~20k
自定义错误每个错误节省~200

Cross-References

交叉引用

  • Bonded Agent:
    07-nft-development
  • Related Skills:
    solidity-development
    ,
    web3-frontend
  • 关联Agent:
    07-nft-development
  • 相关技能:
    solidity-development
    ,
    web3-frontend

Resources

资源

  • ERC-721: eips.ethereum.org/EIPS/eip-721
  • ERC-721A: erc721a.org
  • OpenSea Metadata: docs.opensea.io/docs/metadata-standards
  • ERC-721: eips.ethereum.org/EIPS/eip-721
  • ERC-721A: erc721a.org
  • OpenSea元数据标准: docs.opensea.io/docs/metadata-standards

Version History

版本历史

VersionDateChanges
2.0.02025-01Production-grade with standards, on-chain
1.0.02024-12Initial release
版本日期变更内容
2.0.02025-01生产级内容,涵盖标准与链上功能
1.0.02024-12初始版本