-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo.ts
316 lines (291 loc) · 11.5 KB
/
demo.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import Big from "big.js";
import { GrpcClient } from "grpc-bchrpc-node";
import { prompt } from "inquirer";
import * as readline from "readline";
import { List } from "./src/list";
import { Config } from "./src/config";
const bchaddr = require("bchaddrjs-slp");
const Spinner = require("cli-spinner").Spinner;
const args = process.argv.slice(2);
let client: GrpcClient;
let customUrl, certPath: string;
if (args.includes("--bchd-rootcert") && args.includes("--bchd-url")) {
certPath = args[args.indexOf("--bchd-rootcert") + 1];
customUrl = args[args.indexOf("--bchd-url") + 1];
client = new GrpcClient({ url: customUrl });
} else if (args.includes("--bchdurl")) {
customUrl = args[args.indexOf("--bchd-url") + 1];
client = new GrpcClient({ url: customUrl });
} else {
client = new GrpcClient();
}
(async () => {
const slpdbHosts: {[key: string]: string[]} = {
mainnet: ["https://slpdb.fountainhead.cash", "https://slpserve.imaginary.cash", "http://localhost:3000"],
testnet: ["https://tslpdb.bitcoin.com", "http://localhost:3000"],
};
const appMode: string = (await prompt([
{
type: "list",
name: "mode",
message: "What would you like to do?",
choices: [
{
value: "slp_shareholder_list",
name: "Token holders and balances.",
},
{
value: "bch_dividend_list_prorata",
name: "Token holders and pro-rata BCH dividend to distribute.",
},
{
value: "slp_coin_age_list",
name: "Token coin listing with addresses and coin age.",
},
{
value: "slp_airdrop_list_prorata",
name: "Token holders and SLP airdrop pro-rata.",
},
],
},
])).mode;
const bch_network: string = (await prompt([
{
type: "list",
name: "bch_network",
message: "Choose BCH network:",
choices: ["Mainnet", "Testnet"],
filter(net: string) { return net.toLowerCase(); },
},
])).bch_network;
let minHeight = 543375;
let minMtp = 1534250155;
if (bch_network === "testnet") {
if (!customUrl) {
client = new GrpcClient({testnet: true});
}
minHeight = 1253801;
minMtp = 1535262813;
}
let spinner = new Spinner("Getting Network Info.. %s");
spinner.setSpinnerString("|/-\\");
spinner.start();
let bestHeight: number;
let currentMtp: number;
try {
bestHeight = (await client.getBlockchainInfo()).getBestHeight();
currentMtp = (await client.getBlockInfo({ index: bestHeight })).getInfo()!.getMedianTime();
} catch (e) {
spinner.stop(true);
print("Network error:", e.message);
process.exit();
}
spinner.stop(true);
print("------------NETWORK INFO------------");
print("Current Height:", bestHeight!);
print("Current MTP-11:", currentMtp!);
print("------------------------------------");
const time_mode = (await prompt([
{
type: "list",
name: "time_mode",
message: "Time selection method:",
choices: [
{
name: "Use current best height",
value: "best_block",
},
{
name: "Enter block height (must be > " + minHeight + ")",
value: "height",
},
{
name: "Enter MTP-11 unix timestamp (must be > " + minMtp + ")",
value: "mtp",
},
{
name: "Use mempool state",
value: "mempool",
},
],
},
])).time_mode;
let userHeight = -1;
if (time_mode === "mtp") {
const userMtp = (await prompt([
{
type: "input",
name: "mtp",
message: "Enter desired MTP-11:",
validate(t) {
if (t < minMtp || t > currentMtp) { return false; }
return /^\d+$/.test(t);
},
},
])).mtp;
const updateProgress = (h: number) => {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, 0);
process.stdout.write("Searching for block with MTP.. " + h);
};
// download block info
clearScreen();
for (let i = bestHeight!; bestHeight! > 0; i--) {
const info = (await client.getBlockInfo({ index: i })).getInfo()!;
updateProgress(info!.getHeight());
if (userMtp > info!.getMedianTime()) {
userHeight = i + 1;
break;
}
}
clearScreen();
} else if (time_mode === "height") {
userHeight = parseInt((await prompt([{
type: "input",
name: "height",
message: "Enter desired block height:",
choices: [],
validate(h) {
if (h < minHeight || h > bestHeight) { return false; }
return /^\d+$/.test(h);
},
},
])).height, 10);
} else if (time_mode === "best_block") {
userHeight = bestHeight!;
}
const answers = await prompt([
{
type: "list",
name: "slpdbHost",
message: "Choose SLPDB host:",
choices: slpdbHosts[bch_network],
},
{
type: "input",
name: "slpTokenId",
message: "Enter token ID:",
validate(id) { return /^([A-Fa-f0-9]{2}){32,32}$/.test(id); },
},
]);
if (typeof userHeight !== "number") { throw Error("userHeight must be a number."); }
print("------------------------------------------------------");
if (appMode === "slp_shareholder_list") {
spinner = new Spinner("processing.. %s");
spinner.setSpinnerString("|/-\\");
spinner.start();
Config.SetUrl(answers.slpdbHost);
const bals = await List.GetAddressListFor(
answers.slpTokenId,
userHeight,
) as Map<string, Big>;
spinner.stop(true);
const slpTotal = Array.from(bals.values()).reduce((a, c) => a.plus(c), new Big(0));
bals.forEach((v, k) => {
if (v.gt(0)) {
print(k + ",", v.toFixed());
}
});
print("------------------------------------------------------");
print(`Block Height: ${userHeight}`);
print(`Address Count (includes 0 balances): ${bals.size}`);
print(`Address Count (not including 0 balances): ${Array.from(bals.values()).filter((v) => v.gt(0)).length}`);
print(`Tokens Circulating: ${slpTotal.toFixed()}`);
} else if (appMode === "bch_dividend_list_prorata") {
const bchAmount = (await prompt([{
type: "input",
name: "bch_amount",
message: "Enter BCH amount to distribute:",
validate(id) { return /^\d+(\.\d{0,8})?$/.test(id); }, // up to 8 decimal places
}])).bch_amount;
spinner = new Spinner("processing.. %s");
spinner.setSpinnerString("|/-\\");
spinner.start();
Config.SetUrl(answers.slpdbHost);
const bals = await List.GetAddressListFor(answers.slpTokenId, userHeight) as Map<string, Big>;
spinner.stop(true);
const slpTotal = Array.from(bals.values()).reduce((a, c) => a.plus(c), new Big(0));
bals.forEach((v, k) => {
const d = v.div(slpTotal).mul(bchAmount);
if (d.gt(0.00000000)) {
print(`${bchaddr.toCashAddress(k)}, ${d.toFixed(8)}`);
}
});
print("-----------------COPY/PASTE TO ELECTRON CASH-------------------\n");
print(`Block Height: ${userHeight}`);
print(`Address Count (includes 0 balances): ${bals.size}`);
print(`Address Count (not including 0 balances): ${Array.from(bals.values()).filter((v) => v.gt(0)).length}`);
print(`Receiver Count (not including 0 outputs): ${Array.from(bals.values()).filter((v) => v.round(8).gte(0.00000001)).length}`);
print("Tokens Circulating:", slpTotal.toFixed());
} else if (appMode === "slp_coin_age_list") {
const coinAgeFrom = parseInt((await prompt([{
type: "input",
name: "coin_age_from",
message: "Coin Age should be calculated from block:",
default: 0,
validate(id) { return /^[0-9]\d*$/.test(id); },
}])).coin_age_from, 10);
spinner = new Spinner("processing.. %s");
spinner.setSpinnerString("|/-\\");
spinner.start();
Config.SetUrl(answers.slpdbHost);
const coins = await List.GetCoinListFor(answers.slpTokenId, userHeight, coinAgeFrom);
spinner.stop(true);
coins.forEach((v, i) => {
const a = v.slpAmount;
const b = v.coinAge;
print(`${v.address}, ${a} ${a !== "1" ? "tokens" : "token"}, ${b} ${b! > 1 ? "blocks" : "block"} old, ${v.txid}:${v.vout}`);
});
} else if (appMode === "slp_airdrop_list_prorata") {
const slpDivisibility = parseInt((await prompt([{
type: "input",
name: "slp_divisibility",
message: "Airdrop token divisibility (must be between 0-9):",
validate(id) { return /^(?:[0-9])$/.test(id); },
}])).slp_divisibility, 10);
const regex = "^\\d+(\\.\\d{0," + slpDivisibility + "})?$";
const re = new RegExp(regex);
const slp_amount = (await prompt([
{
type: "input",
name: "slp_amount",
message: "Enter airdrop amount:",
validate(id) { return re.test(id); }, // only 8 decimal places
},
])).slp_amount;
let counter = 0;
const thresh = new Big(1).div(slpDivisibility);
spinner = new Spinner("processing.. %s");
spinner.setSpinnerString("|/-\\");
spinner.start();
Config.SetUrl(answers.slpdbHost);
const bals = await List.GetAddressListFor(answers.slpTokenId, userHeight) as Map<string, Big>;
spinner.stop(true);
const slpTotal = Array.from(bals.values()).reduce((a, c) => a.plus(c), new Big(0));
bals.forEach((v, k) => {
const d = v.div(slpTotal).mul(slp_amount);
if (d.round(slpDivisibility).gte(thresh)) {
print(k + ",", d.toFixed(slpDivisibility));
counter += 1;
if (counter % 19 === 0) {
print("------------------- SLP TXN OUTPUT LIMIT ---------------------");
}
}
});
print("---------- COPY/PASTE TO ELECTRON CASH SLP v3.4.15+ ----------\n");
print(`Block Height: ${userHeight}`);
print(`Address Count (includes 0 balance): ${bals.size}`);
print(`Address Count (not including 0 balances): ${Array.from(bals.values()).filter((v) => v.gt(0)).length}`);
print(`Receiver Count (not including 0 outputs): ${counter}`);
print(`Tokens Circulating: ${slpTotal.toFixed()}`);
}
})();
function clearScreen() {
const blank = "\n".repeat(process.stdout.rows!);
print(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
}
function print(s: any, ...optionalArgs: any[]) {
console.log(s, ...optionalArgs);
}