-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle.js
51 lines (45 loc) · 1.53 KB
/
merkle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import fs from "fs";
import csv from "csv-parser";
const csvFilePath = "./airdrop.csv"; // CSV file of addresses and amount qualified.
const searchAddress = "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"; //One of the address in the csv file This is the address that wants to claim the reward. This is the msg.sender
async function readCSV(filePath) {
return new Promise((resolve, reject) => {
const values = [];
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (row) => {
values.push([row.address, row.amount]);
})
.on("end", () => {
resolve(values);
})
.on("error", reject);
});
}
async function generateMerkleTree() {
try {
const values = await readCSV(csvFilePath);
const tree = StandardMerkleTree.of(values, ["address", "uint256"]);
console.log("Merkle Root:", tree.root);
fs.writeFileSync("tree.json", JSON.stringify(tree.dump()));
} catch (error) {
console.error("Error generating Merkle tree:", error);
}
}
async function generateProof() {
const tree = StandardMerkleTree.load(
JSON.parse(fs.readFileSync("tree.json", "utf8"))
);
for (const [i, v] of tree.entries()) {
if (v[0] === searchAddress) {
const proof = tree.getProof(i);
console.log("Value:", v);
console.log("Proof:", proof);
}
}
}
// Run this to generate the Merkle tree Hash
await generateMerkleTree();
//Run this to generate Proof for based on the selected address.
await generateProof();