UNPKG

@alpsfinance/core

Version:

This is the official Alps Finance smart contract repository.

83 lines (73 loc) 2.14 kB
const fs = require("fs"); const path = require("path"); const keccak256 = require("keccak256"); const { MerkleTree } = require("merkletreejs"); const { getAddress, parseUnits, solidityKeccak256, } = require("ethers/lib/utils"); // Output file path const outputPath = path.join(__dirname, "../merkle.json"); class Generator { // Airdrop recipients recipients = []; /** * Setup generator * @param {number} decimals of token * @param {Record<string, number>} airdrop address to token claim mapping */ constructor(decimals, airdrop) { // For each airdrop entry for (const [address, tokens] of Object.entries(airdrop)) { // Push: this.recipients.push({ // Checksum address address: getAddress(address), // Scaled number of tokens claimable by recipient value: parseUnits(tokens.toString(), decimals).toString(), }); } } /** * Generate Merkle Tree leaf from address and value * @param {string} address of airdrop claimee * @param {string} value of airdrop tokens to claimee * @returns {Buffer} Merkle Tree node */ generateLeaf(address, value) { return Buffer.from( // Hash in appropriate Merkle format solidityKeccak256(["address", "uint256"], [address, value]).slice(2), "hex" ); } async process() { console.log("Generating Merkle tree."); // Generate merkle tree const merkleTree = new MerkleTree( // Generate leafs this.recipients.map(({ address, value }) => this.generateLeaf(address, value) ), // Hashing function keccak256, { sortPairs: true } ); // Collect and log merkle root const merkleRoot = merkleTree.getHexRoot(); console.log(`Generated Merkle root: ${merkleRoot}`); // Collect and save merkle tree + root await fs.writeFileSync( // Output to merkle.json outputPath, // Root + full tree JSON.stringify({ root: merkleRoot, tree: merkleTree, }) ); console.log("Generated merkle tree and root saved to Merkle.json."); } } module.exports = Generator;