Solidity
你如何設置錢包可以鑄造的最大 NFT 數量?
contract xyz is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; uint256 public constant maxSupply = 9999; uint256 public constant userLimit = 1; mapping(address => uint) public walletMints; constructor() ERC721("xyz", "xyz") {} function safeMint(address to, string memory uri) public onlyOwner { require(_tokenIdCounter.current() <= maxSupply, "I'm sorry we reached the cap"); uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); _safeMint(to, tokenId); _setTokenURI(tokenId, uri); }
我有檢查和映射錢包地址的契約,但我不知道如何在 mint 函式中寫入如何將錢包限制為僅 mint 1 nft。
我目前的猜測是它類似於“require(walletMints
$$ msg.sender $$<= 使用者限制“? 任何幫助表示讚賞!
contract xyz is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; uint256 public constant maxSupply = 9999; uint256 public constant userLimit = 1; mapping(address => uint) public walletMints; constructor() ERC721("xyz", "xyz") {} function safeMint(address to, string memory uri) public onlyOwner { require(_tokenIdCounter.current() <= maxSupply, "I'm sorry we reached the cap"); require(walletMints[msg.sender] <= userLimit, "I'm sorry only one NFT per user"); uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); _safeMint(to, tokenId); _setTokenURI(tokenId, uri); }
你應該使用函式’balanceOf’或’balances’狀態變數。(它們返回一個地址擁有的 NFT 數量)。
contract xyz is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; uint256 public constant maxSupply = 9999; uint256 public constant userLimit = 1; mapping(address => uint) public walletMints; constructor() ERC721("xyz", "xyz") {} function safeMint(address to, string memory uri) public onlyOwner { require(_tokenIdCounter.current() <= maxSupply, "I'm sorry we reached the cap"); require(balanceOf(to) <= userLimit ,'user limit error maessage') uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); _safeMint(to, tokenId); _setTokenURI(tokenId, uri); }