From f8b726beb106f4a662c13737d4e11ec490cdc57d Mon Sep 17 00:00:00 2001 From: viguenstepanyan Date: Tue, 22 Oct 2024 15:53:11 +0300 Subject: [PATCH] feat(drop-source): drop source by path added --- .env.example | 1 + 01-generate-merkle-tree.ts | 50 ++++++++++++++++------------- ts/helpers/drops-to-tree-entries.ts | 9 ++++++ ts/helpers/eth-to-wei.ts | 17 ++++++++++ ts/types/index.ts | 2 ++ 5 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 .env.example create mode 100644 ts/helpers/drops-to-tree-entries.ts create mode 100644 ts/helpers/eth-to-wei.ts create mode 100644 ts/types/index.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5358ec4 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +DROP_SOURCE=./p2p_eigen_drop.json \ No newline at end of file diff --git a/01-generate-merkle-tree.ts b/01-generate-merkle-tree.ts index be405a3..6f19aba 100644 --- a/01-generate-merkle-tree.ts +++ b/01-generate-merkle-tree.ts @@ -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 => + JSON.parse(fs.readFileSync(path, "utf8")); + main().catch((error) => { - console.error(error); - process.exitCode = 1; + console.error(error); + process.exitCode = 1; }); diff --git a/ts/helpers/drops-to-tree-entries.ts b/ts/helpers/drops-to-tree-entries.ts new file mode 100644 index 0000000..0bd08b6 --- /dev/null +++ b/ts/helpers/drops-to-tree-entries.ts @@ -0,0 +1,9 @@ +import type { Address, Data } from "../types"; + +import { ethToWei } from "./eth-to-wei"; + +export const dropsToTreeEntries = (drops: Record) => + Object.entries(drops).map(([address, { alloc }]) => [ + address, + ethToWei(alloc), + ]); diff --git a/ts/helpers/eth-to-wei.ts b/ts/helpers/eth-to-wei.ts new file mode 100644 index 0000000..f34f08f --- /dev/null +++ b/ts/helpers/eth-to-wei.ts @@ -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; +}; diff --git a/ts/types/index.ts b/ts/types/index.ts new file mode 100644 index 0000000..9d1e9b2 --- /dev/null +++ b/ts/types/index.ts @@ -0,0 +1,2 @@ +export type Address = `0x${string}`; +export type Data = Record & { alloc: number };