Hardcore Gas Savings in NFT Minting (Part 3): Save $30,000 in Presale Gas Using Bit Manipulation
This series has a Part 1 and Part 2. If you see a paywall, click here.
In a public mint, people can generally mint as many NFTs as they want to, either by minting over and over or by colluding with other buyers. This is undesirable in a private sale (sometimes known as a whitelist) where each member on the list is guaranteed a specified number of mints. A typical workflow would look something like this in solidity:
(Please note that this does not include the optimizations listed in Part 1 for the sake of brevity. Don’t actually use this code! It doesn’t have require
messages!)
This workflow should look familiar to anyone who has programmed an NFT before. After we implement all the optimizations in Part 1 and Part 2, there is still one more thing to do.
The culprit for unnecessarily high gas costs here is amountMintedSoFar[msg.sender]++
. That variable is a mapping from the addresses to how many that user has minted so far and we want to make…