How to Create an On-Chain NFT Allowlist
Learn how to create on-chain allowlists for NFT PFP projects
If you’re launching your very own NFT PFP collection, chances are that you want to generate some hype and create a community that is interested in your project.
It may also be the case that your project has already generated a huge amount of interest, and the demand for your NFTs far outstrips supply. In such cases, you would want to implement a system that only allows the most active or desirable members to mint first before opening the sale to the public.
Cryptopunks, one of the world’s most popular NFT PFP project
An allowlist is an excellent solution for both the aforementioned cases. An allowlist is a mechanism that allows only certain wallets to mint NFTs from a collection for a certain period of time. Allowlist mints almost always precede public sales.
In this tutorial, we will write an NFT collectible smart contract using Solidity and Hardhat that implements an allowlist on-chain (i.e within the contract itself).
Creating the NFT Allowlist Contract
Step 1: Install Node and npm
In case you haven’t already, install node and npm on your local machine.
Make sure that node is at least v14 or higher by typing the following in your terminal:
Step 2: Create a Hardhat project
We’re going to set up our project using Hardhat, the industry-standard development environment for Ethereum smart contracts. Additionally, we’ll also install OpenZeppelin contracts.
To set up Hardhat, run the following commands in your terminal:
Choose Create a Javascript project
from the menu and accept all defaults. To ensure everything is installed correctly, run the following command in your terminal:
To install OpenZeppelin:
Step 3: Write the smart contract
Let’s now write a smart contract that implements an allowlist. To do this on-chain, we need three things:
- A Solidity mapping
isAllowlistAddress
that keeps track of all allowed addresses. - A function
allowListAddresses
that allows the contract owner to add addresses to the allowlist. - A function
preSale
that only allows a wallet to mint if it is part of the allowlist.
Open the project in your favorite code editor (e.g., VS Code), and create a new file called NFTAllowlist.sol
in the contracts
folder. Add the following code to this file:
To learn more about the code, check out the commented sections above.
Compile the contract and make sure everything works by running:
Step 4: Test the allowlist locally
Next, write a script that allows us to test the allowlist locally. To do this, create a new file called run.js
in the scripts folder, then add the following code::
Run this script by running the following command in your terminal:
You should see output that looks like this:
Above, you can see that the allowlisted address (Address 1
) was able to mint the NFTs but the other address (Address 2
) wasn’t!
Conclusion
Congratulations! You now know how to implement an on-chain NFT allowlist.
If you enjoyed this tutorial about creating on-chain allowlists, tweet us at @Alchemy and give us a shoutout!
Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!
Ready to start building your NFT collection?
Create a free Alchemy account and do share your project with us!