A zero-deps solidity utility for generating Merkle roots + proofs & testing w/ forge or directly in your contracts. Foundry-ready and optimized
@supersorbet
- Grab package:
forge install supersorbet/onchainmerkle-generator
- Basic usage:
import "merkle-generator/src/MerkleGen.sol";
contract myAirdropOrSumthinLikeThat {
using MerkleGenerator for MerkleGenerator;
bytes32 public merkleRoot;
function setRoot(address[] calldata users, uint256[] calldata amounts) public {
(merkleRoot, ) = MerkleGenerator.generateClaimsRootAndProofs(users, amounts);
}
function claim(address user, uint256 amount, bytes32[] calldata proof) public {
bytes32 leaf = keccak256(abi.encodePacked(
keccak256(abi.encodePacked(user, amount)) // Double-hash!
));
require(verifyProof(merkleRoot, proof, leaf), "Bad proof");
// Your claim logic here
}
}
because rewriting merkle logic for every project is tedious and node packages are a pain in the ass sometimes
- Generate roots + proofs in one call
- Prevents hash collisions with double-hashing
- Gas-optimized tree construction
- Works with any address/amount combos
# Run the proof generation test with verbose logs
forge test --match-test testGenerateProofs -vv
#OR
forge test -vvv
# Example output:
# MERKLE_ROOT: 0x1234...abcd
#
# PROOFS:
# Proof for address 0xBbD9...3849f:
# 0x5678...def1
# 0x9abc...2345
# Generate proofs from command line
./scripts/generate-merkle.sh \
0xYourContractAddress \
"[0x111...,0x222...]" \
"[100,200...]"
- Encoding matters - Keep your leaf format consistent
- Test your hashes - See verification tests
- Gas limits - Works best for <1000 leaves
Foundry is a blazing fast Ethereum development toolkit written in Rust. It includes:
- Forge: Testing & deployment framework
- Cast: CLI for contract interactions
- Anvil: Local testnet node
- Chisel: Solidity REPL
# Build project
forge build
# Run tests
forge test
# Format code
forge fmt
# Gas snapshots
forge snapshot
# Start local node
anvil
# Deploy contracts
forge script script/Deploy.s.sol --rpc-url <RPC_URL> --private-key <PK>
forge --help
anvil --help
cast --help
Full docs: book.getfoundry.sh