10.7 C
Los Angeles
Friday, March 31, 2023
HomeCryptoNFT airdrop/whitelist implementation with...

NFT airdrop/whitelist implementation with MerkleTree. | by Alexandr Kumancev | Coinmonks | Aug, 2022

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.

exmpl erc721 token
An example of what your wizard should look like

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

Code review

There will be three smart contracts in our project.

contracts-|
|-nft-airdrop-|
|-Airdrop.sol
|-IERC721.sol
|-SuperNFT.sol

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.

Testing smartcontract

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.

connect wallet

Then enter in the field (1.claim — merkleProof (bytes32[])) 4 hashes without quotes, in square brackets, which you got in the deletion

add hashes

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

Source link

Most Popular

LEAVE A REPLY

Please enter your comment!
Please enter your name here

More from Author

Covering over well at Indian temple collapses, killing 35

Thirty-five bodies have been found in a well after a structure built on top of it collapsed at a temple in central India where Hindus had gathered to pray at a festival for the much-revered deity Rama.ByASHOK SHARMA Associated PressNEW DELHI -- Army soldiers and other responders...

Prosecutors Asked For Friday Surrender—But Trump Team Rejected Plan, Report Says

Above The Manhattan district attorney's office asked former President Donald Trump's legal team to schedule a deposition for Friday — one day after the grand jury voted to impeach him — but Trump's lawyers rejected the plan on security grounds, Trump's lawyer says, according to New. York...

Watch Hunter Renfroe’s Outrageous No-Look Catch That Stunned Shohei Ohtani

The MLB kicked off Opening Day Thursday, and amidst some incredible team and individual performances, the final game between the A's and Angels in Oakland was the enduring highlight of the first day of the season. In a rather shocking turn of events, Mike Trout or someone...

Pence Calls Trump Indictment An ‘Outrage’—But Sidesteps Questions About 2024 Impact

Above Vice President Mike Pence predicted that the impeachment of former President Donald Trump, his one-term president and 2024 rival, "will only serve to further divide our country" and cast doubt on the legality of the charges during a CNN interview. Thursday - joining a group of...

Why 2023 is the year to visit Mongolia

Editor's note: This CNN Travel series is, or was, sponsored by the featured country. CNN retains complete editorial control over the subject matter, reporting...

Keeley & Roy’s Ted Lasso Split Hit Brett Goldstein The Hardest

Brett Goldstein has never been shy about singing the praises of co-star Juno Temple, and has also been open about dealing with the news of Roy and Keeley's breakup. "As an actor, I found it devastating and heartbreaking," Goldstein told Town & Country magazine. He added:...

Today’s Wordle #650 Hints, Clues And Answer For Friday, March 31st

Look, it's the last day of March already! There will not be another March in 2023 again. This is it. This is the end. Only nine months left of the year. Soon, it will be summer. I can have that. This weekend...

Boom or bust? What does Trump indictment mean for 2024 bid

WASHINGTON: The historic indictment of Donald Trump it's a seismic moment for the 2024 US presidential campaign, but it's impossible to predict whether the political earthquake will cause lasting damage or settle in his favor.On Thursday a New York grand jury voted to indict the twice-impeached Republican...

Christine Quinn Talks About Life After ‘Selling Sunset’

Christine Quinn's life after Sunset is anything but dull. The former reality star recently appeared on "The Masked Singer," wrote a book, is committed to her acting and modeling careers, and is living the life of a mom with her 22-month-old son, Christian. Life is booming for Kristen...

DAO treasuries top $25 billion for the first time: DeepDAO

Decentralized Autonomous Organization (DAO) treasuries are growing rapidly and have just passed a major milestone, according to DeepDAO.According to data from the DAO data platform, on March 31, the total assets of all decentralized autonomous organizations reached a record $25.1 billion.Treasury is the total amount of assets...