我发现NFT合同通常用于按顺序创建NFT。我是否可能将minting函数更改为包含NFT的id作为输入变量,以便客户可以创建他们想要的指定的NFT?有谁知道怎么写这种造币功能吗?非常感谢!
发布于 2022-06-07 07:02:59
是的,它可以通过创建继承自ERC721PresetMinterPauserAutoId.sol预置的契约来完成。然后可以使用内部_mint(address to, uint256 tokenId)函数来指定id。如果不需要预置,也可以使用普通的ERC721合同。ERC1155标准也是如此。您可以在OpenZeppelin回购中找到这些合同标准。
实心的示例代码:
// 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);
}
}https://ethereum.stackexchange.com/questions/129704
复制相似问题