Add a Mint Function to Your NFT Smart Contract

InstructorRyan Harris

Share this video with your friends

Send Tweet

We need our users to be able to mint their NFT tickets. We also need to limit the number of available tickets.

Start by making sure each NFT has a unique ID. OpenZeppelin gives us the handy utility Counters for this. We will increment the ID each time an NFT is minted.

Once your NFTs have IDs you can use the inherited _safeMint function from OpenZeppelin in your mint function to mint an NFT for the initiator of the function. Also, make sure to increment the ID and decrement the number of remaining NFTs.

mathias gheno
~ 3 years ago

The main function works as expect, but for me the mint function is not avaliable

pragma solidity >=0.8.0 <0.9.0;
//SPDX-License-Identifier: MIT

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract YourContract is ERC721URIStorage {
  using Counters for Counters.Counter;
  Counters.Counter private currentId;

  bool public saleIsActive;
  uint256 public totalTickets = 10;
  uint256 public availabelTickets = 10;
  mapping(address => uint256[]) public holderTokenIds;

  constructor() ERC721("NFTix", "NFTX") {
    currentId.increment();
    console.log(currentId.current());
  }

  function main() public {
    require(availabelTickets > 0, "Not enough tickets");
    _safeMint(msg.sender, currentId.current());
    currentId.increment();
    availabelTickets--;
  }

  function avaliableTicketsCount() public view returns (uint256) {
    return availabelTickets;
  }

  function totalTicketsCount() public view returns (uint256) {
    return totalTickets;
  }

  function openSale() public {
    saleIsActive = true;
  }

  function closeSale() public {
    saleIsActive = false;
  }
}
mathias gheno
~ 3 years ago

Sorry, is mint the function you did not main. My bad.