-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake-tree.js
46 lines (38 loc) · 1.36 KB
/
make-tree.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
const {MerkleTree} = require("merkletreejs");
const ethers = require("ethers");
const keccak256 = require("keccak256");
const path = require("path");
const fs = require("fs-extra");
const {utils} = require("ethers");
const winners = require("../data/raffleWinners");
const data = [];
for (let winner of winners) {
const myBytes32 = ethers.utils.defaultAbiCoder.encode(["uint256"], [winner.tokenId]);
data.push(`${winner.wallet}${myBytes32.substring(2)}`);
}
const leaves = data.map((e) => keccak256(e));
const tree = new MerkleTree(leaves, keccak256, {sort: true});
const root = tree.getRoot().toString("hex");
fs.writeFileSync(path.resolve(__dirname, "../data/merkleTree.json"), JSON.stringify(tree));
const proofs = {
root,
children: [],
};
for (let i = 0; i < leaves.length; i++) {
let proof = tree.getHexProof(leaves[i]);
let leaf = leaves[i];
let wallet = data[i].substring(0, 42);
let tokenId = parseInt(data[i].substring(42));
proofs.children.push({
winner: {
tokenId,
wallet,
},
data: data[i],
leaf: utils.hexZeroPad(utils.hexlify(leaf), 32),
proof,
});
}
fs.writeFileSync(path.resolve(__dirname, "../data/rootLeavesAndProofs.json"), JSON.stringify(proofs, null, 2));
// uncomment if need to modify the fixtures
// fs.writeFileSync(path.resolve(__dirname, "../test/fixtures/proofs.json"), JSON.stringify(proofs, null, 2));