How to Implement Lazy Minting in your NFT Contract
Minting every item. One transaction at a time. And you’ll pay zero gas fees.
Lazy minting is a technique that is used more and more in NFT contracts. It is a solution to one single problem: insanely high gas fees on Ethereum.
The simple explanation is changing the mint
function from onlyOwner
to payable
. If you want to receive extra payments at least. Otherwise, you would only need to change the mint
function and keep some other details in your mind.
The code
We’re going to use a simple OpenZeppelin Wizard contract for our example. By default, only the owner can mint new tokens. That is useful if you don’t have a limit to the number of tokens available. But since most NFT collections have a limit, we need to check if the limit isn’t reached.
A regular safeMint
function would look something like this:
Only the owner of the contract (you) could call it. This would cost you gas fees. And you’d also need to know who to mint it to for the to
parameter. You also couldn’t accept payment for your unique and beautiful tokens.
We need to change the parameters as we would mint the item to the caller, or msg.sender
inside the function itself.
If we want the user to be able to mint multiple tokens we only need uint256 _amount
as a parameter, otherwise, we don’t need any parameters. We would also need to create a simple for
loop to mint the number of tokens selected in the _amount
parameter.
We need to make it a payable
function if we want to accept a payment, and we need to remove the onlyOwner
modifier because we want our users to mint their own tokens.
This would give us a safeMint
function similar to this:
The pitfalls
The safeMint
function so far looks good, but it has some pitfalls in it that we really need to fix. I’ll explain why and how we could do this.
The user can mint all available tokens in one single transaction if he or she would like to. Especially on networks such as Polygon, this could be a problem since gas fees aren’t so much that a user won’t do this. For this, we need to add a require
statement that only lets the user mint a certain amount in a single transaction.
Another pitfall, and arguably a more important one, we haven’t set a price. This will require a similar piece of code as above. We need to check the msg.value
with our price.
It is looking better and better now, but we might have another issue. Most NFT collections or dApps have a limit to how many tokens there could be. This statement will be similar to the number of tokens per sale, but we need to check it with the total supply.
We have now tackled the most common issues and negated their effects if properly implemented. The whole safeMint
function could look like this now (I have also implemented a variable to toggle the sale):
Conclusion
Smart contracts are a great way to use the blockchain, but since fees are high, we want to put that on our users in some cases. Chains such as Polygon make it a lesser issue.
We also need to work on the security and try to safeguard our dApp for any malicious attempts that a user might try such as minting all available tokens.
Thank you very much for reading and have a wonderful day.
Check me out on Twitter and check out my first NFT collection on the Polygon network.