0

I'm trying to require the number of contract NFTs in a wallet < declared uint256 variable in order to proceed with safemint function. I have the following code but I can't work out what is happening, or how to pass the result of balanceOf to a variable which I can later check against MAX_InWallet. Can anyone help please. Thanks

    uint256 MAX_SUPPLY = 20;
    uint256 MAX_InWallet = 5;

    function safeMint(address to, string memory uri) public {
        uint256 tokenId = _tokenIdCounter.current();
        require(tokenId <= MAX_SUPPLY, "Error, Minted Out");

        mapping (address => uint256) public view balanceOf; // week1 challenge
        require(balanceOf(msg.sender) < MAX_InWallet, "error"); // week1 challenge

        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

2 Answers 2

0

My two cents:

    import "@path/to/openzeppelin/contracts/token/IERC721.sol"
   
    uint256 MAX_SUPPLY = 20;
    uint256 MAX_InWallet = 5;
    IERC721 ThisNFT = IERC721(address(this));

    function safeMint(address to, string memory uri) public {
        uint256 tokenId = _tokenIdCounter.current();
        require(tokenId <= MAX_SUPPLY, "Error, Minted Out");
        require(ThisNFT.balanceOf(msg.sender) < MAX_InWallet, "5 tokens per wallet");

        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

You should learn about ERC721 and how to create a instance for it inside the contract :)

4
  • Thanks, contract compiles and deploys to EVM now, I've been calling safemint until "Error, Minted Out" is thrown at 21 in wallet, but safemint never reverted with "5 tokens per wallet" message. Just kept minting to max.
    – 0xCas
    Jan 4 at 20:16
  • I'm made an update. Try now! Jan 5 at 7:10
  • Thank for your help, "ERC20" changed to "IERC721" on line 5? I caught that and made that change too, to get it to compile. Still same lack of error thrown.
    – 0xCas
    Jan 6 at 19:44
  • Lemme try It here. Jan 6 at 21:12
0

This can be simplified a bit further since you're already inheriting from an ERC-721 to the following:

    uint256 MAX_SUPPLY = 20;
    uint256 MAX_InWallet = 5;

    function safeMint(address to, string memory uri) public {
        uint256 tokenId = _tokenIdCounter.current();
        require(tokenId <= MAX_SUPPLY, "Error, Minted Out");

        // you can change the require statement to this instead
        require(balanceOf(to) <= MAX_InWallet, "Exceeds max wallet count");

        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

recap:

  • removed the import statement of IERC721
  • removed setting up ThisNFT variable
  • simplified the require check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.