Merkle Mint NFTs
Use a Merkle tree of possible artworks and then submit a proof it is valid to mint.
Author: Austin Griffith Source code: https://github.com/scaffold-eth/scaffold-eth-examples/tree/merkle-root-buyer-mints Intended audience: Beginners/Intermediate Topics: Scaffold-eth basics, NFTs, Merkle Tree
Deployer pays around (0.283719 ETH ~$500 at todays gas and price) for the initial contract but then NFTs are only minted once a buyer wants them. (The buyer of the NFT pays the gas to mint. ~$55)
Table of Contents
This branch uses the concept of merkel root to verify on chain NFT minting. So instead of pushing all verfied NFT hashes on-chain which would be quite expensive we generate a merkel root by modifying these scripts to make our life easier.
Let's start our environment for tinkering and exploring how NFT auction would work.
- 1.Clone the repo first
git clone https://github.com/scaffold-eth/scaffold-eth-example.git buyer-mints-nft
- 1.Install dependencies
- 1.Start local chain ina different terminal
- 1.Start your React frontend
- 1.Deploy your smart contracts to a local blockchain
Let's navigate to
packages/hardhat/contractsfolder and check out what contracts we have there.
We use a couple of contracts in this dApp.
All the logic that verifies minting a particular NFT sits here. When you run
yarn deploya merkel root is generated and then passed in this contracts constructor while deploying.
claimis the main function which a buyer can call and mint a particular nft.
function claim(uint256 index, string calldata tokenURI, bytes32 calldata merkleProof)
These are the arguments passed while sending the transaction, the merkel prrof and index are generated for each NFT at the time of the proof, navigate through this file for more info.
require(MerkleProof.verify(merkleProof, merkleRoot, node), 'MerkleDistributor: Invalid proof.');
This is the line where the proof verification takes place so if any NFT which was not a part of the merkel root generation is minted the transaction will revert.
If the proof validates the NFT is then minted to the buyer.
When you navigate to the react app on your browser you'll see the NFT's for which the merkel root was generated
💦 Use the faucet wallet icon in the bottom left of the frontend to give your address $1000 in testnet ETH.
🎫 Try to "Mint" an NFT:
and voilà you see your address as the owner after on-chain verification.