A comprehensive set of smart contracts and tools for gas-efficient merkle tree token distributions. This project provides multiple distribution mechanisms including cumulative merkle drops and signature-based QR code distributions.
The Merkle Distribution project provides efficient mechanisms for distributing tokens to multiple recipients using merkle trees. It supports two main distribution types:
- Cumulative Merkle Drops: Sequential drops where each new merkle root contains cumulative balances
- Signature-Based Drops: MEV-resistant distributions using private key signatures for claim verification
- 🌳 Gas-efficient merkle tree implementation - Optimized for minimal gas consumption
- 🔐 MEV-resistant signature-based claims - Prevents front-running attacks
- 📱 QR code generation - Easy distribution via QR codes
- 🔗 Claim link generation - Direct URLs for claiming tokens
- ✅ Comprehensive verification tools - Validate claims before and after deployment
- 📦 Batch processing - Handle thousands of recipients efficiently
- 🚀 Multi-chain support - Deploy on Ethereum, BSC, Base, and other EVM chains
# Clone the repository
git clone https://github.com/1inch/merkle-distribution.git
cd merkle-distribution
# Install dependencies
yarn install
# Compile contracts
yarn build
Each new merkle tree root replaces the previous one and contains cumulative balances of all participants. This allows for multiple distribution rounds while preventing double-claiming.
Key features:
- Updateable merkle root
- Cumulative balance tracking
- Owner-controlled root updates
Uses private key signatures to secure claim links, preventing MEV bots from stealing transactions. Each claim requires a valid signature from the corresponding private key in the merkle tree.
Key features:
- 128-bit merkle tree optimization
- Private key signature verification
- MEV-resistant claiming mechanism
- Fixed tree depth for gas optimization
Generate QR codes for token distribution:
yarn qr:create <version> -a <amounts> -n <quantities>
Parameters:
version
: Drop version identifier-a, --amounts
: Comma-separated token amounts (in tokens, not wei)-n, --numbers
: Comma-separated quantities for each amount tier
Example:
# Generate:
# - 100 QR codes with 5 tokens each
# - 50 QR codes with 10 tokens each
# - 20 QR codes with 20 tokens each
yarn qr:create 1 -a 5,10,20 -n 100,50,20
Additional options:
-t, --testcodes
: Test codes in format "count,amount" (default: "10,1")-c, --cleanup
: Clean directories before generation-z, --zip
: Create zip archives of generated QR codes-b, --chainid
: Target chain ID (default: 1)
Deploy a merkle drop contract with a pre-computed merkle root:
yarn deploy:qr <network> --v <version> --r <root> --h <height>
Parameters:
--v
: Deployment version number--r
: Merkle root (hex string)--h
: Merkle tree height
Example:
# Deploy on mainnet
yarn deploy:qr --v 35 --r 0xc8f9f70ceaa4d05d893e74c933eed42b --h 9
# Deploy on Base network
yarn deploy:qr --v 42 --r 0xabcdef1234567890 --h 10
Verify a single claim link against a merkle root:
yarn lk:check -x -u <url> -r <root> [-b <chainid>]
Example:
yarn lk:check -x -u "https://drop.1inch.io/#/r1/..." -r 0xabcdef... -b 1
Verify all generated links against a deployed merkle drop contract:
yarn verify:links <network> --v <version>
Parameters:
network
: Target network (mainnet, base, bsc, etc.)--v
: Deployment version number
This command will:
- Read the deployment file for the specified version
- Load all links from
generated-data/{version}-qr-links.json
- Verify each link against the deployed contract
- Display progress with colored indicators (green ■ for valid, red ■ for invalid)
- Report verification statistics
Example:
# Verify links for version 61 on Base network
yarn verify:links base --v 61
# Verify links for version 42 on mainnet
yarn verify:links mainnet --v 42
Requirements:
- Deployment file must exist:
deployments/{network}/MerkleDrop128-{version}.json
- Link files must exist:
generated-data/{version}-qr-links.json
- Network must have RPC access configured
Execute a complete merkle drop deployment (generation + deployment + verification):
yarn drop <network> --v <version> --a <amounts> --n <counts> [--debug]
This command will:
- Generate claim links with specified amounts
- Create merkle tree from generated data
- Deploy the merkle drop contract
- Verify all generated links
Example:
# Deploy on Base with 3 tiers
yarn drop base --v 53 --a 100,250,500 --n 50,30,20
# Test without deployment
yarn drop hardhat --v 54 --a 10,20 --n 5,5 --debug
merkle-distribution/
├── contracts/ # Solidity smart contracts
│ ├── CumulativeMerkleDrop.sol
│ ├── SignatureMerkleDrop128.sol
│ └── interfaces/
├── src/ # TypeScript source code
│ ├── cli/ # CLI tools
│ ├── services/ # Core services
│ ├── tasks/ # Hardhat tasks
│ └── types/ # TypeScript types
├── test/ # Test files
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── e2e/ # End-to-end tests
└── deploy/ # Deployment scripts
# Compile contracts
yarn build
# Watch mode for development
yarn build:watch
# Clean build artifacts
yarn clean
# Run all linters
yarn lint
# Fix linting issues
yarn lint:fix
# Individual linters
yarn lint:sol # Solidity
yarn lint:js # JavaScript
yarn lint:ts # TypeScript
# Run all tests
yarn test
# Run specific test suites
yarn test:unit # Unit tests only
yarn test:integration # Integration tests only
yarn test:e2e # End-to-end tests only
# Run with coverage
yarn coverage
yarn test:coverage # NYC coverage for TypeScript tests
- Unit Tests: Test individual components and functions
- Integration Tests: Test component interactions
- E2E Tests: Test complete workflows including deployment
- Always verify merkle roots before deployment
- Test on testnets first before mainnet deployment
- Use signature-based drops for public distributions to prevent MEV
- Audit generated claim links before distribution
- Monitor contract events for claim activity
This codebase should be audited before production use. Key areas to review:
- Merkle proof verification logic
- Signature validation in SignatureMerkleDrop128
- Access control in CumulativeMerkleDrop
- Integer overflow/underflow protections
Supported networks (configured in hardhat.config.ts):
- Ethereum Mainnet
- BSC (Binance Smart Chain)
- Base
- Local Hardhat Network
The contracts are optimized for gas efficiency:
- SignatureMerkleDrop128 uses 128-bit arithmetic where possible
- Fixed tree depth reduces proof verification costs
- Bitmap tracking for claimed indices
- Optimized compiler settings (1M runs)
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE.md file for details.
- Built by 1inch Network
- Uses OpenZeppelin contracts for security
- Merkle tree implementation based on merkletreejs
For issues and feature requests, please use the GitHub issue tracker.