侧边栏壁纸
博主头像
拾起行

行动起来,活在当下

  • 累计撰写 3 篇文章
  • 累计创建 8 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

ERC721标准的实现

栋栋拐
2025-06-13 / 0 评论 / 0 点赞 / 39 阅读 / 0 字

创建项目

创建nft项目,初始化 默认值创建node项目

npm init -y

安装hardhat

npm install hardhat -D

创建hardhat项目 全部默认创建即可

npx hardhat init

安装openzeppelin

npm install -D @openzeppelin/contracts

代码实现

可以先在openzeppelin官网中去获取ERC721标准代码

https://www.openzeppelin.com/solidity-contracts

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.27;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ERC721Burnable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import {ERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable, Ownable {
    uint256 private _nextTokenId;

    constructor(address initialOwner)
        ERC721("MyToken", "MTK")
        Ownable(initialOwner)
    {}

    function safeMint(address to, string memory uri)
        public
        onlyOwner
        returns (uint256)
    {
        uint256 tokenId = _nextTokenId++;
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
        return tokenId;
    }

    // The following functions are overrides required by Solidity.

    function _update(address to, uint256 tokenId, address auth)
        internal
        override(ERC721, ERC721Enumerable)
        returns (address)
    {
        return super._update(to, tokenId, auth);
    }

    function _increaseBalance(address account, uint128 value)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._increaseBalance(account, value);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable, ERC721URIStorage)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

最大的NFT交易平台:opensea

https://opensea.io/zh-CN

使用Metadata 定义NFT的信息

https://docs.opensea.io/docs/metadata-standards

将数据信息存储在去中心化网络上,目前最大的去中心化存储网络IPFS:

Why IPFS?

Our peer-to-peer content delivery network is built around the innovation of content addressing: store, retrieve, and locate data based on the fingerprint of its actual content rather than its name or location.

IPFS的第三方服务应用filebase

获取到IPFS Gateway URL

在代码中添加参数

string constant META_DATA = "https://grotesque-beige-dormouse.myfilebase.com/ipfs/xxxxx";
function safeMint(address to) public onlyOwner returns (uint256)
    {
        uint256 tokenId = _nextTokenId++;
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, META_DATA);
        return tokenId;
    }

在remix中可进行测试,铸造完成后在opensen测试网中查看NFT

CCIP跨链

使用ChainLink的CCIP服务

https://docs.chain.link/ccip/tutorials/evm/send-arbitrary-data

可获取到相关的代码实现,复制粘贴

安装CCIP插件

npm install -D @chainlink/contracts-ccip

NFT跨链示意图

编写NFT跨链代码

安装hardhat-deploy插件,完成单元测试

npm install --save-dev @nomicfoundation/hardhat-ethers ethers hardhat-deploy hardhat-deploy-ethers

引入hardhat-deploy所需要的插件

require("@nomicfoundation/hardhat-ethers");
require("hardhat-deploy");
require("hardhat-deploy-ethers");

0

评论区