# Quickly making airdrop setup FE and BE

By [N00b21337](https://paragraph.com/@n00b21337) · 2023-01-11

---

To make airdrop you need to have a list of addresses that are eligible for that as well as put them into Merkle tree get hashes of them, store them into a smart contract, and then check if they are on the list.

For the node JS part we can use this code to install libraries quickly

    const { MerkleTree } = require("merkletreejs");
    const keccak256 = require("keccak256");
    
    let whitelistAddress = [
      "0x6271217bE872a0F65Bcd8C90d3D0A80B305B8906",
      "0xB1620c0542744DeDD30F30a863c09D1964532F8C",
      "0x139Fb3997Ea9d45129b08BC9d8aac5adC0bB3B09",
    ];
    
    const leafNodes = whitelistAddress.map((addr) => keccak256(addr));
    const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
    
    const rootHash = merkleTree.getRoot();
    const rootHex = merkleTree.getRoot().toString("hex");
    
    let index = whitelistAddress.indexOf(
      "0x139Fb3997Ea9d45129b08BC9d8aac5adC0bB3B09"
    );
    
    console.log(index); // returns -1 if not found
    const claimingAddress = leafNodes[index];
    const hexProof = merkleTree.getHexProof(claimingAddress);
    
    console.log(merkleTree.verify(hexProof, claimingAddress, rootHash));
    

So we check if the address is in the Merkle tree, we could just check the index on FE part in the list with indexOf() function, we would get index if it is in the list and -1 if the entry is not in the list.  But in this example, we will use verify function from Merkle tree library to check if it is there.

In code where we interact with smart contract, we will send these 3 parameters so they will be used directly on the blockchain

    pragma solidity ^0.8.9;
    
    import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
    
    contract MerkleTreeExample {
    
        bytes32 public merkleRoot =
            0x2e35b61278fbcec3f3b0bb361d928e373e089a61758af09690ce0a5391078ff2;
    
        mapping(address => bool) public whitelistClaimed;
    
        function whitelistMint(bytes32[] calldata _merkleProof) public {
            require(!whitelistClaimed[msg.sender], "Address already claimed");
            bytes32 leaf = keccak256(abi.encodePacked(msg.sender));
            require(
                MerkleProof.verify(_merkleProof, merkleRoot, leaf),
                "Invalid Merkle Proof."
            );
           // Add here some MINT function call depending on smart contract
            whitelistClaimed[msg.sender] = true;
        }
    }
    

So as seen above we need to set merkle tree root hash, which we get in \*\*rootHex \*\*in JS and then we call \*\*whitelistMint \*\*where we check did we already claimed out token and if not we then check sender address and make leaf out of his hex and also pass in merkle proof that needs to be sent as part of transaction. This is something we get from JS library as hexProof variable.

This will all work for situations like NFT where you want jus to check if address is eligible or not, for erc20 tokens aka fungable tokens you will need amounts also to mint/claim in this situation you need to add JS object as data point and process them in conjunction with addresses.We can do it like this

    const { MerkleTree } = require("merkletreejs");
    const keccak256 = require("keccak256");
    
    let whitelistAddress = {
      "0x6271217bE872a0F65Bcd8C90d3D0A80B305B8906": 100,
      "0xB1620c0542744DeDD30F30a863c09D1964532F8C": 22,
      "0x139Fb3997Ea9d45129b08BC9d8aac5adC0bB3B09": 44,
    };
    
    const leafNodes = Object.entries(whitelistAddress).map(([key, val] = entry) => {
      return keccak256(key + val);
    });
    
    const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
    
    const rootHash = merkleTree.getRoot();
    
    //console.log(rootHash);
    const rootHex = merkleTree.getRoot().toString("hex");
    
    const claimingAddress = keccak256(
      "0x139Fb3997Ea9d45129b08BC9d8aac5adC0bB3B09" + 44
    );
    const hexProof = merkleTree.getHexProof(claimingAddress);
    
    console.log(merkleTree.verify(hexProof, claimingAddress, rootHash));
    

and in out smart contract we will also need to create leaf where we combine address with amount.

    bytes32 leaf = keccak256(abi.encodePacked(msg.sender, amount));
    

Below links might also help on this task

[https://thecibrax.com/using-merkle-trees-for-bulk-transfers-in-ethereum](https://thecibrax.com/using-merkle-trees-for-bulk-transfers-in-ethereum) [https://steveng.medium.com/performing-merkle-airdrop-like-uniswap-85e43…](https://steveng.medium.com/performing-merkle-airdrop-like-uniswap-85e43543a592) [https://forum.openzeppelin.com/t/how-to-create-a-merkle-tree-and-get-me…](https://forum.openzeppelin.com/t/how-to-create-a-merkle-tree-and-get-merkle-root-for-merkle-distributor-contract/4990/4) [https://github.com/Anish-Agnihotri/merkle-airdrop-starter](https://github.com/Anish-Agnihotri/merkle-airdrop-starter)

---

*Originally published on [N00b21337](https://paragraph.com/@n00b21337/quickly-making-airdrop-setup-fe-and-be)*
