0

I am currently testing my NFT contract, and I have run into a few problems when it comes to cost. The cost in predefined like this

contract Optinauts is ERC721Enumerable, Ownable {
  using Strings for uint256;

  string public baseURI;
  string public baseExtension = ".json";
  string public notRevealedUri;
  ***uint256 public cost = .2 ether;***

Then I have a function that can change the price by replacing the value of cost.

function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }

My issue is that I am aiming to do a whitelist mint and a public mint, and this is a great way to control that. However, when testing on Rinkeby it does not automatically charge my wallet the price of the NFT. I can go into the Remix IDE deploy tab and specify the value in gwei for the amount of the total NFTs that I want to mint. How can I do this automatically? Do I need to specify my cost in wei? Or should I just hardcode a mint for public and whitelist?

Here is my mint function as well.

    function mint(uint256 _mintAmount) public payable {
    require(!paused, "the contract is paused");
    uint256 supply = totalSupply();
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");

    if (msg.sender != owner()) {
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "user is not whitelisted");
            uint256 ownerMintedCount = addressMintedBalance[msg.sender];
            require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "max NFT per address exceeded");
        }
        require(msg.value >= cost * _mintAmount, "insufficient funds");
    }
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
        addressMintedBalance[msg.sender]++;
      _safeMint(msg.sender, supply + i);
    }
  }

1 Answer 1

0

I figured that creating a single function that can change one price variable and another function that controls the state of the sale is the best way to go about it. Previously, I had two state variables for defining whitelist and public price and two state variables for defining the state of the whitelist and public sale. This seemed to be very gas inefficient as each state variable costs 25k gwei. I was also able to remove 2-3 functions that could alter these state variables which also would increase the gas fees for deployment.

Here is my public mint function: function publicMint( uint256 amount ) public payable nonReentrant { address sender = _msgSender();

    require(isActive, "Public sale is not open");
    require(amount <= maxMint - _alreadyMinted[sender], "Insufficient mints left");
    require(msg.value == mintPrice * amount, "Incorrect payable amount");
    
    _alreadyMinted[sender] += amount;
    _internalMint(sender, amount);
  }

Here is my whitelist mint function:

function whitelistMint(
    uint256 amount,
    bytes32[] calldata merkleProof
  ) public payable nonReentrant {
    address sender = _msgSender();

    require(isActive, "Whitelist sale is not open");
    require(_verify(merkleProof, sender, maxWhitelistMint), "You are not whitelisted");
    require(amount <= maxWhitelistMint - _alreadyMinted[sender], "Insufficient mints left");
    require(msg.value == mintPrice * amount, "Incorrect payable amount");

    _alreadyMinted[sender] += amount;
    _internalMint(sender, amount);
  }

  function ownerMint(address to, uint256 amount) public onlyOwner {
    _internalMint(to, amount);
  }

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.