首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写NFT合同以创建指定的NFT?

如何编写NFT合同以创建指定的NFT?
EN

Ethereum用户
提问于 2022-06-07 03:01:05
回答 1查看 72关注 0票数 0

我发现NFT合同通常用于按顺序创建NFT。我是否可能将minting函数更改为包含NFT的id作为输入变量,以便客户可以创建他们想要的指定的NFT?有谁知道怎么写这种造币功能吗?非常感谢!

EN

回答 1

Ethereum用户

回答已采纳

发布于 2022-06-07 07:02:59

是的,它可以通过创建继承自ERC721PresetMinterPauserAutoId.sol预置的契约来完成。然后可以使用内部_mint(address to, uint256 tokenId)函数来指定id。如果不需要预置,也可以使用普通的ERC721合同。ERC1155标准也是如此。您可以在OpenZeppelin回购中找到这些合同标准。

实心的示例代码:

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@openzeppelin/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol";

contract NFTtest is ERC721PresetMinterPauserAutoId {

    constructor()
        ERC721PresetMinterPauserAutoId(
            "NonFungibleToken",
            "NFT",
            "https://example.com"
        )
    {}

    // Mint function with ID as an input
    function mint(uint256 _id) public {
        // Here you can add additional logic or pre-conditions.
        // _mint function already checks if the _id has been
        // used so no need to check again

        // Mint the NFT with the specified _id and set as the owner
        // the sender of the transaction
        _mint(msg.sender, _id);
    }
}
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/129704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档