How to Airdrop NFTs
Learn how to airdrop NFTs using both on-chain and off-chain implementations
One of the most common but effective ways of marketing your NFT project is by airdropping NFTs to influencers and backers/community members of another NFT project (which may or may not be owned by you).
Airdropping has also shown application in projects which want to reward existing members with even more utility.
Gutter Cat Gang NFT holders were able to mint Gutter Dogs and Gutter Rats for free
In this article, we will explore how to go about creating the two most common types of airdrops:
- The on-chain direct mint and transfer drop
- The off-chain allowlist
We will build the former using Solidity and standard OpenZeppelin ERC721 contracts. For the latter, we will employ digital signatures and the OpenZeppelin ECDSA library.
Finally, we will also write a script that leverages the [Alchemy NFT API][./] to get a list of all wallets that own an NFT of a particular collection.
Part 1: Direct Mint and Transfer
In the direct mint and transfer model, the creator of the project mints the NFTs directly to a certain selection of wallets.
Step 1: Install Node and npm
If 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 basic NFT smart contract that has built-in airdrop functionality . To do this, we will use the function airdropNfts
, which mints NFTs to a list of wallet addresses.
Open the project in your favorite code editor (e.g., VS Code), and create a new file called NFTAirdrop.sol
in the contracts
folder. Add the following code to this file:
Notice that airdropNfts
has been marked as onlyOwner
. This means that only the owner/creator of the contract will be able to call this function.
Compile the contract and make sure everything is working by running:
Step 4: Create the Airdrop Script
Next, let’s write a script that allows us to mint and airdrop NFTs from the contract above. To do this, create a new file called run.js
in the scripts
folder, then add the following code:
Note that in this script, we are airdropping NFTs to a specific list of addresses. However, if you’d like to airdrop to existing holders of an NFT collection, check out the section below.
Run the script using the following command:
You should see output that looks something like this:
Notice that every wallet we marked as eligible to receive an airdrop now has an NFT balance of 1. The owner/creator of the project was responsible for minting all the NFTs as well as paying the gas fees associated with it.
Part 2: Off-chain Allowlist
The on-chain direct mint and transfer method works well in situations where you need to airdrop sporadically or to only a few wallets. If you plan on distributing NFTs to hundreds or thousands of wallets, this method may not be the best approach.
It can be costly to mint a large number of NFTs yourself, and even if you have the budget, you may still hit Ethereum’s block limits while trying to mint thousands of NFTs at once.
Fortunately, an affordable and effective design is employed by large projects that use off-chain allowlists. In an off-chain allowlist model, the project’s creator stores a list of wallets eligible for an airdrop in an off-chain database.
Wallets that belong to the off-chain database can then interact with the NFT contract and mint the NFTs themselves, thereby saving the creator gas fees associated with minting.
Step 1: Write the smart contract
Let’s write a smart contract that can operate with an off-chain allowlist. In the contracts
folder of your existing project, create a new file called NftAllowlist.sol
and add the following code:
The main function that allows off-chain allowlisting functionality is the recoverSigner
function, which checks who has signed a particular message.
To learn more about how this works in detail, check out our article on How to Create an Off-Chain NFT Allowlist.
Compile the contract and make sure everything is working by running:
Step 2: Create the Off-Chain Allowlist
Now, let’s write a script that lets allowlisted addresses to claim their NFTs . To do this, create a new file called runAllowlist.js
in the scripts
folder, then add the following code:
Again, note that we are only allowlisting default Hardhat wallet addresses. In case you want to allowlist wallets that hold NFTs from another collection, check out the section below.
Run the script using the following command:
You should see output that looks something like this:
Notice that we did not store allowlisted wallets on-chain. Instead, we stored them locally and performed the following steps:
- Check if the selected wallet is allowlisted.
- If yes, sign the hashed version of the wallet’s public address using a secret private key.
- Pass the hashed address and the signature to the minting function of the smart contract.
- In the minting function, recover the signer and check if the signer is the owner of the smart contract. If yes, allow mint. Else, return an error.
Also, note that the address that makes the claim also mints the NFT. Therefore, minting costs are spread across the entire community rather than applied only to the NFT creator.
Airdrop to holders of another collection
Now that you know how to airdrop NFTs to a particular set of wallets, it is worthwhile spending some time deciding which wallets to airdrop to. Besides friends, investors, and early community members, a common group to airdrop NFTs to are holders of a popular NFT collection (like Cryptopunks or Bored Ape Yacht Club).
Getting a list of all wallets that hold an NFT of a particular collection is extremely simple using Alchemy’s NFT API. Sign up for a free Alchemy account, and use the following code snippet to get a list of all wallets that have a BAYC NFT.
To learn more about creating an Alchemy App, and accessing the NFT API using the Alchemy SDK, check out our article on How to get NFT Owners at a Specific Block Height.
Conclusion
Congratulations! You now know how to conduct airdrops using on-chain direct mint and transfer method and the off-chain allowlists method.
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!