我只是在创建我自己的NFT市场。我需要为每个NFT创建唯一的令牌ID,但是当前的令牌ID演示如下所示。
contract ERC721 {
...
mapping(uint256 => address) private _tokenOwners;
function _mint(address to, uint256 tokenID) {
_tokenOwner[tokenID] = to;
...
}
}
contract CryptoDog is ERC721 {
...
string[] public cryptoDogz;
function mint(string memory nftName) public {
cryptoDogz.push(ntfName);
uint256 _tokenID = cryptoDogz.length - 1;
_mint(msg.sender, _tokenID);
...
}
}如果您为我提供一种生成唯一令牌ID的方法,我将不胜感激。
发布于 2022-05-26 21:39:31
您可以使用计数器从ID 1开始,并在创建新NFT时增加计数器。对于每个NFT,您将得到一个不同的增量数字。您可以使用OpenZeppelin中的以下代码:
// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract GameItem is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("GameItem", "ITM") {}
function awardItem(address player, string memory tokenURI)
public
returns (uint256)
{
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, tokenURI);
_tokenIds.increment();
return newItemId;
}
}示例是使用来自OpenZeppelin的计数器库。
您可以检查它是如何实现的这里。
https://ethereum.stackexchange.com/questions/129026
复制相似问题