Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
18
Indexable
Never
const { StandardMerkleTree } = require("@openzeppelin/merkle-tree");
const fs = require("fs");

const treeData = JSON.parse(fs.readFileSync("tree.json", "utf8"));

const sortedTreeData = treeData.sort((a, b) => a.treeIndex - b.treeIndex);

const treeIndexToSortedIndex = {};
for (let i = 0; i < sortedTreeData.length; i++) {
  const entry = sortedTreeData[i];
  treeIndexToSortedIndex[entry.treeIndex] = i;
}

const values = sortedTreeData.map((entry) => [entry.value[0], entry.value[1], entry.value[2]]);

const tree = StandardMerkleTree.of(values, ["address", "uint256", "uint32"]);

console.log('Merkle Root:', tree.root)

const leafTreeIndexToProve = 15567;
const leafIndexToProve = treeIndexToSortedIndex[leafTreeIndexToProve];
const leafToProve = values[leafIndexToProve];

const proof = tree.getProof(leafToProve);

console.log("Proofs:", proof);


const sortedValuesByValue1 = sortedTreeData.slice().sort((a, b) => b.value[1] - a.value[1]);
const top20HighestValues = sortedValuesByValue1.slice(0, 20).map((entry) => entry.value[1]);
console.log("Top 20 highest values of value[1]:", top20HighestValues);
Leave a Comment