-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestdata.ts
110 lines (91 loc) · 3.16 KB
/
testdata.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from "fs";
import { execSync } from "node:child_process";
import { generateKeys, generateMnemonic } from "sotez";
import { MerkleTree } from "merkletreejs";
import SHA256 from "crypto-js/sha256.js";
import { MichelsonType, packDataBytes, Parser } from "@taquito/michel-codec";
import sandboxName from "./bootstrapped";
// For test purpose,
// 1. generate a JSON containing an array of keys, with a random amount
// 2. generate a test token to be airdropped
// 3. generate merkle root
const NB_DROPS = 400;
const MIN_AMOUNT = 1;
const MAX_AMOUNT = 200;
const TESTDATA_PATH = "./testdata";
// add here some keys that you want to retrieve later
const DETEMINISTIC_KEYS = ["alice", "bob"];
makeDrops(MIN_AMOUNT, MAX_AMOUNT, NB_DROPS).then((drops) => {
const fpath = `${TESTDATA_PATH}/drops.json`;
fs.writeFileSync(fpath, JSON.stringify(drops));
console.info(
`[OK] ${fpath} created with ${
NB_DROPS + DETEMINISTIC_KEYS.length
} drops`
);
});
const tree = buildTree();
const merkleRoot = tree.getHexRoot();
const tokenAddr = makeToken();
console.log(`[OK] ${tokenAddr} deployed`);
fs.writeFileSync(`${TESTDATA_PATH}/token.json`, JSON.stringify(tokenAddr));
fs.writeFileSync(`${TESTDATA_PATH}/merkleRoot.json`, JSON.stringify(merkleRoot));
fs.writeFileSync(
`${TESTDATA_PATH}/airdrop_storage.mligo`,
`
let token = ("${tokenAddr}": address), 0n
let merkle_root = ${merkleRoot}
`
);
function makeToken() {
execSync(`docker exec ${sandboxName} octez-client originate contract token \
transferring 0 from alice running "$(cat testdata/token.tz)" \
--init "$(cat testdata/token_storage.tz)" --burn-cap 0.62175 --force`);
return execSync(
`docker exec ${sandboxName} octez-client show known contract token`
)
.toString()
.trim();
}
async function makeDrops(minAmount: number, maxAmount: number, nbKeys: number) {
const drops = DETEMINISTIC_KEYS.map((name) => ({
pkh: pubkey(name),
amount: between(minAmount, maxAmount),
}));
for (let i = 0; i <= nbKeys; i++) {
const key = await generateKeys(generateMnemonic());
drops.push({
pkh: key.pkh,
amount: between(minAmount, maxAmount),
});
}
return drops;
}
function between(min: number, max: number): number {
return Math.floor(Math.random() * (max - min) + min);
}
// the sandbox key command is used, it gives us deterministic keys
function pubkey(name: string): string {
return execSync(`docker exec ${sandboxName} flextesa key ${name}`)
.toString()
.split(",")[2];
}
function getLeaf(pkh: string, amount: number) {
const p = new Parser();
return SHA256(
packDataBytes(
{
prim: "Pair",
args: [{ string: pkh }, { int: `${amount}` }],
},
p.parseMichelineExpression("(pair address nat)") as MichelsonType
).bytes
);
}
function buildTree() {
const dropsJson = fs.readFileSync(`${TESTDATA_PATH}/drops.json`).toString();
const leaves = JSON.parse(dropsJson).map((drop: any) =>
getLeaf(drop.pkh, drop.amount)
);
return new MerkleTree(leaves, SHA256);
}