Performing Merkle Airdrop like Uniswap

Image from https://ccoingossip.com/what-is-airdrop-in-crypto-world/
  1. Admin call a function to send tokens
function airdrop(address address, uint256 amount) onlyOwner {
IERC20(token).transfer(account, amount);
}

Merkle Airdrop

For Merkle airdrop implementation, the same objective is accomplished with the following benefit:

  • The owner only pay the gas fee to create the contract and storing the Merkle root on the contract
  • Whitelisted addresses can call the contract on their own to claim their airdrop — this also opens up the possibility of having a deadline to claim the airdrop.

What is Merkle Airdrop?

Merkle-based Airdrop is based on Merkle Tree data structure.

  • Form second layer: Hash(A+B), Hash(C+D), Hash(E+F), Hash(G+H)
  • Form third layer: Hash(Hash(A+B), Hash(C+D)), Hash( Hash(E+F), Hash(G+H))
  • Finally, the fourth level showed in orange.

Steps on creating a Merkle Airdrop

Reference for the code can be found at https://github.com/steve-ng/merkle-airdrop — 2 main libraries are used

  • Generate the list of whitelisted and amount they are qualified for
  • Generate the Merkle root based on the list
// Generate the list of whitelisted user and amount qualified 
const users = [
{ address: "0x..", amount: 10 },
{ address: "0x..", amount: 15 },
{ address: "0x...", amount: 20 },
{ address: "0x..", amount: 30 },
];
// Encode the datastructure
const elements = users.map((x) =>
utils.solidityKeccak256(
["address", "uint256"], [x.address, x.amount]));
const merkleTree =
new MerkleTree(elements, keccak256, { sort: true });
// Generate the root
const root = merkleTree.getHexRoot();
  • Store all the address that’s eligible for the airdrop, such that when the user comes to your site, they can immediately see if they are eligible
  • If they are eligible, call the smart contract with the proof.

Summary

Once you know how Merkle airdrop works, the implementation is very straightforward. The use case is not only for airdrop, you can also implement this for applications with a whitelisting requirements, eg. IDO or early access to some feature.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store