Is this smart contract mint function going to work?

Hi, I'm very new to smart contract development - wondering if someone can have a look at my mint function - I Have edited a tutorials mint function to be passed an array of IDs instead of just incrementing the ID within the mint function.

Idea is that people can go to my site and choose which NFTs they want to buy

Thanks

    function mint(uint256[] mintIDs_) public payable {
        require(isPublicMintEnabled, 'minting not enabled');
        require(msg.value == mintIDs_.length * mintPrice, 'wrong mint value');
        for (i = 0, i < mintIDs_.length, i++) {
           require(!_exists(mintIDs_[i]), 'One or more of the of your nfts have already sold')
        } 
        require(totalSupply + mintIDs_.length <= maxSupply, 'sold out');
        
        for (uint256 i = 0; i < mintIDs_.length; i++) {
            uint256 newTokenId = mintIDs_[i]
            totalSupply++;
            _safeMint(msg.sender, newTokenId);
        }
    }

Your code contains a few syntax errors:

  • missing data location of the function parameter mintIDs_
  • check the first for loop - the statements should be divided by semicolons, not by commas
  • missing type of the i variable in the first for loop
  • missing semicolon after the 3rd require() condition
  • missing semicolon after newTokenId declaration

Apart from that, assuming that all variables not declared in this function (isPublicMintEnabled, mintPrice, ...) are visible in this scope, and have such values that won't make the require() conditions fail, you'll be able to successfully run the code.


Example:

Pass [1,2,3] as the mint() function param. Since we declared the value of mintPrice as 0, you don't need to set any ETH value of the transaction in this case.

pragma solidity ^0.8;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract MyCollection is ERC721("MyCollection", "MyC") {
	bool isPublicMintEnabled = true;
	uint256 mintPrice = 0;
	uint256 totalSupply = 0;
	uint256 maxSupply = type(uint256).max;

	function mint(uint256[] calldata mintIDs_) public payable {
        require(isPublicMintEnabled, 'minting not enabled');
        require(msg.value == mintIDs_.length * mintPrice, 'wrong mint value');
        for (uint256 i = 0; i < mintIDs_.length; i++) {
           require(!_exists(mintIDs_[i]), 'One or more of the of your nfts have already sold');
        } 
        require(totalSupply + mintIDs_.length <= maxSupply, 'sold out');
        
        for (uint256 i = 0; i < mintIDs_.length; i++) {
            uint256 newTokenId = mintIDs_[i];
            totalSupply++;
            _safeMint(msg.sender, newTokenId);
        }
    }
}

Thanks a lot! And yes all variables are declared

_safeMint already checks that the token being created doesn't already exist. So you don't been to check that yourself.

This would work just as well and be cheaper

function mint(uint256[] calldata mintIDs_) public payable {
        require(isPublicMintEnabled, 'minting not enabled');

        uint256 count = mintIDs_.length;
        totalSupply += count;

        require(msg.value == count * mintPrice, 'wrong mint value');
        require(totalSupply <= maxSupply, 'sold out');
        
        for (uint256 i = 0; i < count; ++i) {
            _safeMint(msg.sender, mintIDs_[i]);
        }
    }