[英]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 的最簡單方法。
ERC721
mintable
and burnable
,您將獲得mintable
和burnable
的 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:
// 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.