2
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);
    }

I have the contract to check and map the wallets address, but I don't know how to write into the mint function how to limit the wallet to only mint 1 nft.

My current guess is it's something along the lines of "require(walletMints[msg.sender] <= userLimit" ?

Any help is appreciated!

3 Answers 3

0
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);
    }
    ```
2
  • There is no need to add state variable. balanceOf function do this Commented Aug 10, 2022 at 6:44
  • how to increment walletMints please ?
    – Frank
    Commented Nov 4, 2022 at 9:01
0

you should use function 'balanceOf' or 'balances' state variable. (they return The number of NFTs owned by an address).

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);
}
-1

using BalanceOf state variable is not an answer since a user can transfer that nft to another account and mint again. There is not any solution to this even if you do something like this:

 uint totalPerWallet = 2;
 struct QtyPerUser{
        address userAddr;
        uint256 balance;
    }

    mapping(address =>QtyPerUser) public QtyPerUserMap ;
    ...  
    require(totalPerWallet >= QtyPerUserMap[msg.sender].balance,"Cannot mint more than Qty allowed");

A user can create many wallet addresses using a bot ahha.

4
  • yet there are NFT projects that have max mint limits. fix your answer.
    – user610620
    Commented Dec 8, 2022 at 4:19
  • What I am saying is you can do that but bots can create more addresses you cannot stop them
    – kool Erick
    Commented Dec 8, 2022 at 7:49
  • in my code it's possible but people can still create many addresses or transfer their NFTs in that case BalanceOf will see zero in the wallet
    – kool Erick
    Commented Dec 8, 2022 at 7:51
  • 1
    This isn't addressing OP question. Commented Dec 8, 2022 at 16:09

Your Answer

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

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