In this article, we’ll break down the MerkleTree proof implementation for your NFT collection’s airdrop/whitelist. (+testing). Using Polygon Mumbai.
If you don’t understand how this algorithm works, I advise you to read the previous article before starting to work (I have explained the Merkle algorithm in detail here).
I also advise you to read this article before you begin. It will help you have a deeper understanding of how the code works.
You will need the following tools installed to successfully crush this build:
First, we will create a simple nft contract using the openzeppelin wizard.
First, we put the possibility of create new nft(Mintable property) and add the AutoIncrementIds property, so that the counter of minted nfts will automatically increase and we don’t need to write additional functions.
Also add the Roles property. So that we have a main admin, as well as a minter role, which we will give to the Airdrop contract (the contract will be able to create nfts and distribute them to suitable addresses). Copy this code, we will need it later.
Important: in new version openzeppelin you need change _grandRole on _setupRole
We will also use the MerkleProof library — it allows us to work conveniently with the merkle tree and verify our proof of candidate in the tree.
Copy the code from the githab into your working directory and install and install all the dependencies:
$ git clone https://github.com/kumancev/nft-merkle-tree.git
$ npm install
Then create & setup your .env file
PRIVATE_KEY="" // your metamask account
POLYGONSCAN_API_KEY="" // your polygon api key
There will be three smart contracts in our project.
The SuperNFT.sol is code copied from the openzeppelin wizard. The IERC721.sol interface of our NFT contract, to be able to use the safeMint() function.
Let’s break down the Airdrop.sol contract:
Now we need to make compile contracts:
$ npx hardhat compile
When writing smart contracts, be sure to write tests for them. Testing smart contracts is just as important as writing them.
Go to the test/nft-airdrop/ folder, Airdrop.test.ts file.
If you make changes to your smart contracts you must recompile them.
$ npx hardhat clean
$ npx hardhat compile
Now let’s break down the test code of our Airdrop.sol contract.
Since we use TypeChain, it automatically generated Factory and Interfaces at compile time. We will need them for testing and future deploy of the contract to the network.
Run the tests on our local hardhat network:
$ npx hardhat test test/nft-airdrop/Airdrop.test.ts
The tests ran successfully, it’s time to upload the contracts to the network.
Deploy smartcontracts to test network
Go to the scripts/nft-airdrop/ folder, deploy.ts file.
The code will be almost identical to what we wrote for the tests.
You will store candidates offchain (tables, database) and proofs will also be generated offchain). Since this is outside the scope of our article, we will generate the proof directly in the deploy script (the proof variable).
First, let’s test our deploy script on the local network.
$ npx hardhat run script/nft-airdrop/deploy.ts
You should see the following:
Proof we will need for the nft minting
Now let’s deploy a deployment to the Mumbai Polygon network:
$ npx hardhat run script/nft-airdrop/deploy.ts --network matic
The last step. NFT Mint.
Go to Polygon Mumbai, enter the airdrop contract address you got when you deploy. On the smart contract page, go to “write contract” tab, connect your wallet.
Then enter in the field (1.claim — merkleProof (bytes32)) 4 hashes without quotes, in square brackets, which you got in the deletion
Next claim NFT, note that the gas price comes out much lower if we had used the confirmation of entry into the whitelist through the “for” method.
Now if we go to the address of our NFT contract and in the tab “Read Contract” in the method “ownerOf” enter 0 (the owner of the first NFT token), we see the address of our wallet.
Now I want to recommend you an article describing the problems of modern solidity development &nft.
The problem of code copying and many other things. A very deep analysis with examples of solutions and code. This article was written by one of the developers of the notorious nft collection Nuclear Neadrs.
If you want to really understand code optimization, take a moment and read this post.
Also while I was writing this article I found probably the only video which describes creation of nft collection with such a big stack of modifications (Free mint + Whitelist Mint (Merkle) + Public Mint + PaymentSplitter). The only problem is the French author, but we write the code in the same language, so you should figure it out.
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing