This project demonstrates how to implement a smart contract for an airdrop that uses a Merkle tree for whitelisting addresses. The goal is to generate a Merkle root from a list of eligible addresses, implement a Solidity contract that verifies claims against this Merkle root, and manage the airdrop process.
This project consists of three main components:
- Merkle Tree Generation: A script (
merkle.js
) to generate a Merkle root from a CSV file containing addresses and their respective token amounts. - Solidity Smart Contract: A contract (
MerkleAirdrop.sol
) that allows users to claim tokens by providing a valid Merkle proof. - Testing: A test suite to ensure the correct functionality of the airdrop contract.
Before getting started, ensure you have the following installed:
- Node.js (v14 or later)
- npm
- Hardhat (for testing and deployment)
- Solidity
- A code editor (e.g., Visual Studio Code)
-
Clone the repository:
git clone https://github.com/Superior212/Airdrop-merkel-tree cd merkle-airdrop
-
Install the necessary dependencies:
npm install
-
Prepare the CSV file:
Ensure your CSV file containing addresses and token amounts is structured as follows:
address,amount 0x123...abc,100 0x456...def,200
Save this file as
airdrop.csv
in the project root directory.
To generate the Merkle tree:
- Run the
merkle.js
script:This script reads thenode merkle.js
airdrop.csv
file, hashes each entry, constructs a Merkle tree, and outputs the Merkle root. You can find the script in thescripts/
directory.
-
Deploy the contract:
Using Hardhat, you can deploy the
MerkleAirdrop.sol
contract with the generated Merkle root. The deployment script can be found in thescripts/deploy.js
file.To deploy:
npx hardhat run scripts/deploy.js
To claim tokens:
-
Generate the Merkle proof:
Use the
merkletreejs
library to generate a proof for a specific address. Refer to thescripts/proofGenerator.js
file for an example of how to generate a proof. -
Claiming tokens:
Users can interact with the deployed contract's
claim
function using a valid Merkle proof.
Testing is crucial to ensure the contract behaves as expected. The test suite includes cases for:
- Deploying the contract with a sample ERC20 token and Merkle root.
- Simulating valid and invalid claims.
- Ensuring correct handling of double claims and invalid proofs.
You can find the test cases in the test/MerkleAirdrop.test.js
file. To run the tests:
npx hardhat test
- The project assumes a basic understanding of Solidity, Merkle trees, and Hardhat.
- The Merkle tree is generated from a CSV file and assumes that all entries are valid.
- The contract does not include advanced security features like pausable functions or upgradability.