Let’s look into the PepsiMicDrop contract. I will explain the contract by its functionality, each functionality is related to some functions and state variables.
The constructor takes 2 inputs:
See more: How to code nft smart contract
- “Pepsi Mic Drop” is the NFT token name
- “PEPSIMICDROP” is the token symbol.
The assignment happens in the ERC721 contract code, which the contract imports at line 51. That is why you cannot find anything related to this constructor body. It is the beauty of inheritance, and we do not need to redo it.
Inside the constructor, we can see 2 state variables get new values, reserveMicDropsId, and micDropsId. Why they are needed? In this NFT drop, Pepsi kept the first 50 NFTs to themselves, so the NFTs that are publicly available start from id 51.
Wait, isn’t NFT an image? Why the here they using numbers to represent NFTs? If you have these questions, I was with you. To understand that, we need to look into what is an NFT.
Now let’s talk about the mint.
Part 1 — Merkle Proof
There are two inputs of the mint function, proof, and leaf. They are used for Merkle Proof from lines 3 to 7. Essentially, it checks if a user is qualified to mint the NFT. There is a concept called whitelist. Sometimes you have to get into this whitelist, to be able to mint NFTs later. I will not explain this part, because someone did a great job. Please check this article, which gives a comprehensive explanation of Merkle Proof.
Part 2 — Prerequisites
The next part, lines 8 to 11, defines the 4 prerequisites of mint. Let’s go through each of them.
(1) Before the NFT started to sell, the state variable saleStarted is set to false. So it will not pass the check-in line 8. When the sale begins, the contract owner can call the function startSale() to change the value of saleStarted.
require(saleStarted == true, “The sale is paused”);
(2) This check makes sure the user address is not 0x0. 0x0 is the Ethereum genesis address, and no user will use it. I am also not sure why this check is necessary. Let me know if you understand.
require(msg.sender != address(0x0), “Public address is not correct”);
(3) In Pepsi Drop, each address can only mint one NFT. alreadyMintedis a state variable in mapping type, like a dictionary, that keeps track of all addresses that minted the NFT.
require(alreadyMinted[msg.sender] == false, “Address already used”);
(4) There is a limited supply, up to 1983 NFTs. This checks if all NFTs have been claimed. micDropsId is the unique token id we discussed above.
require(micDropsId <= maxMint, “Mint limit reached”);
Easy, right? And we are almost done, hanging there.
Part 3 — The actual mint.
This is where mint actually happens. The good news is we do not need to implement it because ERC-721 already did this.
To mint, we call the function _safemint() with the user’s wallet address and the unique token id, and that’s it. Under the hood, there is a state variable (like a dictionary) that keeps track of the ownership of each token. By calling _safemint()function, we update this state variable, assign or change the ownership of the corresponding token.
One note here, the state variable micDropId represents token id, and itincrements by 1 right after each mint.
Part 4 — Update Minted Address
As mentioned in Part 2, each address can only mint one NFT.
alreadyMinted[msg.sender] = true;
This line ensures whoever successfully minted an NFT gets recorded.
Part 5 — Return
After minting, the token id is returned to the frontend.
(3) Update URI
The last part we need to discuss is the function that updates URI.
As you already know, each NFT has its URI. And this is the function used to change the base URI.
The base URI is the mutual part among each NFT’s URI. By default, the URI is baseURI/tokenId. The reason why we set baseURI is that it saves gas fees. Imagine if you set URI for each NFT that is extremely expensive.
You can change how to combine baseURI and tokenId, by overriding the function tokenURIdefined in the ERC-721 contract. For example, you can do something like baseURI/tokenId + ‘.json’if that is how the URI is formatted.
(4) Other Parts
The uncovered parts are simply functions to get or set state variables, I am sure you do not need my help.
Most read: Comprehensive Guide to NFTs for Marketers