Mint

試圖限制使用者/地址可以鑄造 NFT 的次數

  • October 17, 2022

我正在嘗試編寫一份契約,以限制使用者可以鑄造我的契約的次數。我自己做了一些研究,發現這個問題也在這個論壇上提出並解決了

我試圖遵循該實現,但我仍然能夠鑄造比我的 USERLIMIT 更多的東西。我是編寫契約的新手,所以我確定這是一個新手問題,並感謝任何幫助。這是我的程式碼片段:


contract MZToken is ERC721, ERC721Enumerable, ERC721URIStorage {
   using Counters for Counters.Counter;

   Counters.Counter private _tokenIdCounter;
   uint256 MAX_SUPPLY = 10000;
   uint256 USERLIMIT = 5;
   mapping(address => uint)  public maxWalletMints;

   constructor() ERC721("MZToken", "MZTOK") {}

   /* 
   Creates a new transaction on the blockchain
   */
   function safeMint(address to, string memory uri) public  {
       uint256 tokenId = _tokenIdCounter.current();

       require(tokenId <= MAX_SUPPLY, "I'm sorry.  The max all NFT's have been minted");
       require(maxWalletMints[to] <= USERLIMIT, "MAX NFT's per wallet and user reached.");
       maxWalletMints[to]+=1;
       
       _tokenIdCounter.increment();
       _safeMint(to, tokenId);
       _setTokenURI(tokenId, uri);
   }

require(balanceOf(msg.sender) < USERLIMIT我的理解是,如果使用者嘗試鑄造此合約超過 5 次,則顯示的行應該引發異常,但這並沒有發生。

我究竟做錯了什麼?

這是一個工作範例,如果您嘗試鑄造超過最大值,它將恢復

pragma solidity 0.8.17;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";

contract Token is ERC721, ERC721Enumerable {
   using Counters for Counters.Counter;

   Counters.Counter private _tokenIdCounter;
   uint256 MAX_SUPPLY = 10000;
   uint256 USERLIMIT = 5;
   mapping(address => uint)  public maxWalletMints;

   constructor() ERC721("MZToken", "MZTOK") {}

   /* 
   Creates a new transaction on the blockchain
   */
   function safeMint(address to, string memory uri) public  {
       uint256 tokenId = _tokenIdCounter.current();

       require(tokenId < MAX_SUPPLY, "I'm sorry.  The max all NFT's have been minted");
       require(maxWalletMints[to] < USERLIMIT, "MAX NFT's per wallet and user reached.");
       maxWalletMints[to]+=1;
       
       _tokenIdCounter.increment();
       _safeMint(to, tokenId);
       
   }

   function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC721,ERC721Enumerable) {
       super._beforeTokenTransfer(from, to, amount);
   }

   function _beforeConsecutiveTokenTransfer(
       address from,
       address to,
       uint256,
       uint96 size
 ) internal override(ERC721,ERC721Enumerable) {}

    function supportsInterface(bytes4 interfaceId)
       public
       view
       override(ERC721, ERC721Enumerable)
       returns (bool)
   {
       return super.supportsInterface(interfaceId);
   }
}

我遇到的問題是由於我對 Remix 的工作原理一無所知 :(。當我編譯/部署對契約的更改時,我沒有選擇新部署的契約版本,所以我總是使用相同的版本進行測試。當使用 Remix IDE,您必須確保您使用的是已部署契約的“正確”版本。這確實是一個新手問題……感謝 MadeInDreams 和 OliverD 對此的支持。

引用自:https://ethereum.stackexchange.com/questions/137555