Skip to content

Commit

Permalink
Merge pull request #3 from viguenstepanyan/feat/drop-source
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbir authored Oct 22, 2024
2 parents f6bc15e + f8b726b commit 1dda41c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP_SOURCE=./p2p_eigen_drop.json
50 changes: 27 additions & 23 deletions 01-generate-merkle-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@

import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import fs from "fs";
import "dotenv/config";

import { Address, Data } from "./ts/types";
import { dropsToTreeEntries } from "./ts/helpers/drops-to-tree-entries";

async function main() {
try {
const values = [
["0x1111111111111111111111111111111111111111", "5000000000000000000"],
["0x2222222222222222222222222222222222222222", "2500000000000000000"],
["0x3333333333333333333333333333333333333333", "100500000000000000000000"]
];

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

console.log('Merkle Root:', tree.root);
fs.writeFileSync("tree.json", JSON.stringify(tree.dump()));

for (const [i, v] of tree.entries()) {
console.log('Claimer:', v[0]);
console.log('Amount:', v[1]);
console.log('Proof:', tree.getProof(i));
}

console.log('Done.')
} catch (err) {
console.log(err)
try {
const tree = StandardMerkleTree.of(
dropsToTreeEntries(loadDrops(process.env.DROP_SOURCE!)),
["address", "uint256"]
);

console.log("Merkle Root:", tree.root);
fs.writeFileSync("tree.json", JSON.stringify(tree.dump()));

for (const [i, v] of tree.entries()) {
console.log("Claimer:", v[0]);
console.log("Amount:", v[1]);
console.log("Proof:", tree.getProof(i));
}

console.log("Done.");
} catch (err) {
console.log(err);
}
}

const loadDrops = (path: string): Record<Address, Data> =>
JSON.parse(fs.readFileSync(path, "utf8"));

main().catch((error) => {
console.error(error);
process.exitCode = 1;
console.error(error);
process.exitCode = 1;
});
9 changes: 9 additions & 0 deletions ts/helpers/drops-to-tree-entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Address, Data } from "../types";

import { ethToWei } from "./eth-to-wei";

export const dropsToTreeEntries = (drops: Record<Address, Data>) =>
Object.entries(drops).map(([address, { alloc }]) => [
address,
ethToWei(alloc),
]);
17 changes: 17 additions & 0 deletions ts/helpers/eth-to-wei.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const ethToWei = (eth: string | number): string => {
// Convert input to string and handle scientific notation
const ethString = eth.toString();

// Handle decimal points
let [whole, decimal] = ethString.split(".");
decimal = decimal || "0";

// Pad with zeros or truncate to 18 decimal places
decimal = decimal.padEnd(18, "0").slice(0, 18);

// Remove leading zeros from whole number
whole = whole.replace(/^0+/, "") || "0";

// Combine whole and decimal parts
return whole + decimal;
};
2 changes: 2 additions & 0 deletions ts/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Address = `0x${string}`;
export type Data = Record<string, unknown> & { alloc: number };

0 comments on commit 1dda41c

Please sign in to comment.