-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-merkle-tree.ts
46 lines (39 loc) · 1.62 KB
/
generate-merkle-tree.ts
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
import fs from 'fs';
import { MerkleTree } from 'merkletreejs';
import { utils, BigNumber } from 'ethers';
const chainId = process.env.CHAIN_ID
if (!chainId) {
throw new Error('Set CHAIN_ID environment variable')
}
for (const prefix of ['mandatory_lockup', 'optional_lockup']) {
const accounts = JSON.parse(fs.readFileSync(`./scripts/${chainId}_data/${prefix}_accounts.json`, 'utf-8'))
const nodes = Object.keys(accounts).map((account, index) => {
const { amount } = accounts[account as keyof typeof accounts];
const amountBn = BigNumber.from(amount.hex);
return utils.solidityKeccak256(['uint256', 'address', 'uint256'], [index, account, amountBn]);
});
const merkleTree = new MerkleTree(nodes, utils.keccak256, {sortPairs: true});
const merkleRoot = `0x${merkleTree.getRoot().toString('hex')}`;
const claims = {
merkleRoot,
claims: Object.keys(accounts).reduce(
(claim, account, index) => Object.assign(claim, {
[account]: {
index,
amount: accounts[account as keyof typeof accounts].amount,
split: accounts[account as keyof typeof accounts].split,
proof: merkleTree.getHexProof(
utils.solidityKeccak256(
['uint256', 'address', 'uint256'],
[index, account, accounts[account as keyof typeof accounts].amount]
),
),
},
}),
{}),
};
fs.writeFileSync(
`./scripts/${chainId}_data/${prefix}_claims.json`,
JSON.stringify(claims, null, 2), 'utf-8'
);
}