繁体   English   中英

如何在 NFT 的智能合约中编写烧录 function?

[英]How to write a burn function in smart contract for NFT?

我正在学习如何为 NFT collections 编写智能合约,以下是我阅读的教程给出的示例 function:

    function _burn(uint256 tokenId) internal virtual override {
        super._burn(tokenId);
        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }
    }

我认识到这个 function 不会完全从区块链中删除令牌。 相反,它将删除令牌的 URI(无论谁拥有它)。 因此,代币仍会在集合中并显示在交易平台上,但元数据会消失(但可能需要一段时间才能生效,因为平台不经常刷新元数据)。

我想知道这是否是刻录 function 的正确做法。 如果有人可以为我提供一个如何在其他 NFT 智能合约上实现销毁 function 的示例,那对我将非常有帮助。

这是将burn function 添加到 NFT 的最简单方法。

  • GO 到Openzepplin 向导
  • 点击ERC721
  • 给你的令牌一个名字和符号。
  • 点击mintable and burnable ,您将获得mintableburnable的 NFT 代币合约。

这是一个示例:

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

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

contract MyToken is ERC721, ERC721Burnable, Ownable {
    constructor() ERC721("MyToken", "MTK") {}

    function safeMint(address to, uint256 tokenId) public onlyOwner {
        _safeMint(to, tokenId);
    }
}

对应的 OZ 向导界面如下所示:

在此处输入图像描述

您将获得以下public burn function:

在此处输入图像描述

来自Openzepplin ERC721 可烧合约

更新

你可以让合约既可enumarable burnable 在此处输入图像描述

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

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

contract MyToken is ERC721, ERC721Enumerable, ERC721Burnable, Ownable {
    constructor() ERC721("MyToken", "MTK") {}

    function safeMint(address to, uint256 tokenId) public onlyOwner {
        _safeMint(to, tokenId);
    }

    // The following functions are overrides required by Solidity.

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM