目 录CONTENT

文章目录

solidity开发代币及NFT Metamask

懿曲折扇情
2025-10-29 / 0 评论 / 1 点赞 / 17 阅读 / 239 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2025-10-29,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

remix上开发合约、编译、发布

ERC20 代币

image-1761728581210

image-1761728461820

NFT发布sepolia测试网



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

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

contract MyNFT is ERC721, Ownable {
    uint256 private _nextTokenId = 1;
    
    // 存储每个 token 的 URI
    mapping(uint256 => string) private _tokenURIs;

    constructor() 
    ERC721("MyArtCollection", "ART")
    Ownable(msg.sender)
    {}

    // 铸造 NFT
    function mintNFT(address to, string memory uri) public onlyOwner returns (uint256) {
        // to = recipient //你的钱包地址。
        // uri = tokenURI //元数据的 IPFS 链接。
        uint256 tokenId = _nextTokenId++;
        _mint(to, tokenId);
        _tokenURIs[tokenId] = uri;
        return tokenId;
    }

    // 重写 tokenURI 方法以返回自定义 URI
    function tokenURI(uint256 tokenId) public view override returns (string memory) {
        _requireOwned(tokenId); // 检查 token 是否存在
        return _tokenURIs[tokenId];
    }

    // 可选:添加批量铸造功能
    function batchMintNFT(address to, string[] memory uris) public onlyOwner returns (uint256[] memory) {
        uint256[] memory tokenIds = new uint256[](uris.length);
        for (uint256 i = 0; i < uris.length; i++) {
            tokenIds[i] = mintNFT(to, uris[i]);
        }
        return tokenIds;
    }
}

{
  "name": "GJSNFT",
  "description": "This is a sample NFT with an image.",
  "image": "https://amber-objective-guppy-906.mypinata.cloud/ipfs/bafybeiamy4c45ekaso5q5orhgtgvv3z53wyx3bbq4vjfbnmpfkjxbqtt5i"
}

image-1761728697931

1

评论区