Merkle Airdrop: The Airdrop solution for token issues

Tram Ho

1. Place the problem

Suppose we are about to issue 1 ERC-20 coin on Ethereum and need an airdrop for an initial user base, to increase the number of token holders as well as to broaden the promotion for the upcoming token.

Basically, we will need to take the following steps to build the Airdrop feature:

  1. Of course, you must deploy the ERC-20 contract token first.
  2. Writing Airdrop contract: Contract saves a list of addresses that register to receive tokens. After the Airdrop registration period ends , users can call the contract to claim token.
  3. In addition, we can build more bots (such as Telegram bot) to track users with the condition follow page, share articles. v..v to register for Airdrop .

Merkle Airdrop

With the above Airdrop contract design, we have 1 problem. With airdrops , the number of people who register to receive tokens depending on their size can range from hundreds, thousands or even tens of thousands of people.

Ethereum ‘s storage fees as well as transaction fees are currently not cheap at all, imagine an array of contracts containing thousands of registered addresses will be very expensive, not to mention new transactions. address to the array again. In general, the above Airdrop contract design solution is not at all optimal for your wallet.

With Merkle Airdrop , we will not have to worry about having to save a large number of addresses registered in the contract, while still ensuring the verification of the claim address registered before or not? From there, it saves a lot of costs in Airdrop .

2. Theoretical basis

Merkle Tree

The Merkle Tree is simply a data structure in the form of a binary tree, the value of the nodes, the leaves being the hash of the data.

To create a Merkle Tree , from the data we have, use the hash function to compute the corresponding hash value of the data, which will be the leaf node of the tree. Continue to hash adjacent values ​​until there is a unique hash value (Root of the Merkle tree). How is a Merkle Tree computed in the figure below?

Merkle Tree helps in verifying and verifying data integrity while consuming only a small amount of storage space (due to the small size of the hash). In Blockchain, Merkle Tree is very popular to verify transactions (Used in Bitcoin, Ethereum, etc.)

Merkle Proof

Merkle Proof is used to check whether the input data belongs to the Merkle Tree or not without having to reveal all the data that make up the Merkle Tree .

We will look at the example illustrated in the picture above to be able to understand what Merkle Proof is? In this example we need to prove that the data K belongs to the Merkle Tree . We need to calculate the Hash of K and then gradually climb to the root of the Merkle Tree , if the value of the Merkle Tree root is the same as the given Merkle Root value, it proves that K belongs to the Merkle Tree .

Instead of having to use all the data from the AP to recalculate the Merkle Root to see if it is the same as the original Merkle Root ? We just need to get the back nodes of the tree to verify that the K belongs to the Merkle Tree .

  • The hash of L can then calculate the hash KL
  • HJ ‘s hash can then calculate the IJKL hash
  • The MNOP hash can then calculate the IJKLMNOP hash
  • Hash of ABCDEFGH

From there we can completely calculate the Merkle Root that only needs to know the 4 node values ​​in the Merkle Tree

3. Merkle Airdrop

The basic flow that we will implement Airdrop is as follows

  • For users to register for the airdrop and save the list as follows (We can save it in the server, cloud or IPFS whatever it is). The first value is the registered address and the second is the amount of airdrop tokens for that address.

  • After finishing the airdrop registration time, from the list above, we calculate the Merkle Root and save it on the smart contract.
  • Based on the number of airdrop registrants, we will send the corresponding number of ERC-20 tokens to the Merkle Airdrop smart contract to be able to airdrop for users.
  • The user will then call the Merkle Airdrop contract to claim the amount of registered tokens. Based on Merkle Proof , the contract will calculate whether this address has registered for the airdrop or not and the number of token claim is satisfied or not? If true, the contract will send the corresponding amount of tokens to the user.

Specific example

Our side has also built an Airdrop page and so far it is still working pretty well.

Registration : Users will register to receive airdrops through BOT on Telegram with some conditions such as join box chat Telegram, follow twitter or retweet. When the user completes the registration steps, the BOT will call the Node.js server and store the user’s information in MongoDB .

Claim : After closing the airdrop registration and allowing users to claim token. User will go to the airdrop page. The server will calculate and return the Merkle Proof based on the user’s address. Then, the user signs a transaction to call the smart contract to claim, if the user has registered before, he will receive the token when completing the transaction.

4. Smart contract

Let’s take a look at the logic of the Merkle Airdrop contract

  • The variable tranches stores the id of the Airdrop (we can open many different airdrops)
  • Mapping merkleRoots saves the Merkle Root value of the respective Airdrop.
  • Mapping claimed used to check for specific airdrop during the address that claim or not?
  • The seedNewAllocations function is the init function of the Airdrop, after the airdrop registration is finished, the owner of the contract will call this function to transfer the token to the contract as well as store the Merkle Root value.
  • The private _claimWeek function will check the conditions to see if the address of the user has claimed or not? tranches id valid or not?
  • The _verifyClaim function will rely on the Merkle Proof that the user submits to calculate if the address is correct registered for the airdrop or not?
  • Finally, the _disburse function is the function that will send tokens from the contract to the user when all conditions have been met.

References

Evolution of Airdrop: from Common Spam to the Merkle Tree

Wikipedia

Merkle proofs Explained

Share the news now

Source : Viblo