How to Create an NFT Allowlist Based on Ownership
Learn how to create on-chain allowlists based on previously owned NFTs
The most successful NFT PFP projects usually like to reward their community with continuing utility. One of the most common ways of doing this is by allowing existing holders to mint free NFTs from their subsequent collections.
BAYC NFT holders were able to mint MAYC NFTs for free at the time of release
Another fairly common design paradigm is to allow NFT holders of a project you do not own to mint NFTs from your collection for free or at a discounted rate. This is usually done to incentivize some of the most active collectors in the NFT ecosystem to participate in your project.
Fortunately, building allowlists that query ownership data on already deployed contracts is fairly simple on Ethereum and other EVM-based blockchains. In this article, we will write an NFT collectible smart contract using Solidity and Hardhat that allows users to mint NFTs only if they own an NFT from another pre-determined collection.
Creating the 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 original NFT smart contract
In order to create an NFT smart contract that allows mints based on ownership of another NFT collection, we need to either get the deployed address of the original NFT collection or write one from scratch.
Since we are working in a local development environment, we will do the latter. Create a file called OGCollection.sol
in the contracts
folder and add the following code:
Step 4: Write the NFT Smart Contract with Allowlist
Let’s now write an NFT smart contract that allows mint only if a wallet holds an NFT from the OG collection above. In order to do this, we will have to invoke the balanceOf()
function of OGCollection
provided to it by OpenZeppelin’s ERC-721 implementation.
To make sure that our contract can actually call the aforementioned function, we need to provide it with the function’s signature. We will do this using Solidity interfaces.
In the contracts
folder, create a new file called NftWithAllowlist.sol
and add the following code:
Compile the contracts and make sure everything is working by running:
Step 5: Test the allowlist functionality locally
Next, write a script that allows us to test the allowlist locally. To do this, create a new file called run.jsin 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 an address with the OG NFT was able to mint but an address without the OG NFT was not.
Conclusion
Congratulations! You now know how to implement an on-chain NFT allowlist based on previously owned NFTs.
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!