This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicx_bot_btc_maker.js
506 lines (421 loc) · 19.7 KB
/
icx_bot_btc_maker.js
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import { rpcMethod, waitConfirmation, waitSPVConnected, sleep, BOT_VERSION } from './util.js';
import { difference } from "https://deno.land/[email protected]/datetime/mod.ts";
import { time } from "https://deno.land/x/[email protected]/mod.ts";
const orderTimeout = 5000;
const minOrderLife = 1500;
const ownerAddress = Deno.env.get("DFI_ADDRESS");
const btcMakerAddress = Deno.env.get("SPV_BTC_ADDRESS");
let btcMakerPubkey = "";
const alarmHook = Deno.env.get("ALARM_HOOK");
let objOfferData = new Object();
let mapOfferDfcClaim = new Map();
let objHashSeed = new Object();
let objSpvHtlcExpire = new Object();
let objSpvHtlcAmount = new Object();
let objStatistics = new Object();
const checkOrderSizeInterval = 1; // every hour check order size
let checkOrderSizeTime = new Date("2021-01-01"); // Set to an old time so when restart the script will check first.
const outputStatisticsInterval = 6; // very 6 hours output statistics
let outputStatisticsTime = new Date("2021-01-01"); // Set to an old time so when restart the script will output first.
let btcBlock = 0;
async function sendAlarm(msg) {
console.log(msg);
if (alarmHook == null)
return;
fetch(alarmHook, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ "text": msg }),
});
}
async function createOrderIfNotExist() {
const res = (await rpcMethod('getblockchaininfo', [], true));
if (res["result"] == null || res["result"]["headers"] == null) {
sendAlarm("[btc maker] Failed to getblockchaininfo");
return;
}
const headerBlock = res["result"]["headers"];
const orders = (await rpcMethod('icx_listorders', [], true)).result;
var foundOrder = false;
var foundedOrder = "";
const btcBalance = parseFloat((await rpcMethod('spv_getbalance', [])).result);
console.log("BTC balance " + btcBalance);
for (var key in orders) {
if (key == "WARNING")
continue;
if (orders.hasOwnProperty(key)) {
console.log(key + " -> " + JSON.stringify(orders[key]));
const orderDetails = orders[key];
if (orderDetails["type"] == "EXTERNAL" &&
orderDetails["ownerAddress"] == ownerAddress &&
orderDetails["chainFrom"] == "BTC" &&
orderDetails["tokenTo"] == "BTC")
{
console.log(`Order ${key} expiration height ${orderDetails["expireHeight"]}, blockchain header block ${headerBlock}`);
if (foundOrder) {
sendAlarm(`[btc maker] Already have order ${foundedOrder}, close extra order ${key}`);
const closeTxid = await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true);
sendAlarm(`[btc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
} else {
if (orderDetails["expireHeight"] > headerBlock + minOrderLife) {
const timeDiffInHours = difference(time().now(), checkOrderSizeTime, { units: ["hours"] })["hours"];
if (timeDiffInHours > checkOrderSizeInterval) {
if (Math.abs(orderDetails["amountToFill"] - btcBalance) > 0.00001) {
const listOrderOffers = (await rpcMethod('icx_listorders', [{ "orderTx": key }], true)).result;
let hasOffer = false;
for (var offerKey in listOrderOffers) {
if (offerKey == "WARNING")
continue;
hasOffer = true;
}
// If the order size not match with balance and it don't have offer now, then close the order and recreate a new order.
if (!hasOffer) {
sendAlarm(`[btc maker] Order ${key} size ${orderDetails["amountToFill"]} not match with the btc balance ${btcBalance}, close it and recreate new one`);
const closeTxid = await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true);
sendAlarm(`[btc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
continue;
}
}
}
console.log("Found order " + key);
objStatistics["btcInOrder"] = orderDetails["amountFrom"];
foundOrder = true;
foundedOrder = key;
} else {
sendAlarm(`[btc maker] Order ${key} is too old, close it`);
const closeTxid = await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true);
sendAlarm(`[btc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
}
}
}
}
}
if (foundOrder) {
return foundedOrder;
}else {
if (btcBalance <= 0.0001) {
console.error("BTC balance too low");
return;
}
const orderSize = btcBalance;
console.log("Creating order with size " + orderSize);
const orderTxId = await waitConfirmation(await rpcMethod('icx_createorder',
[{"ownerAddress": ownerAddress,
"chainFrom": "BTC",
"tokenTo": "BTC",
"amountFrom": orderSize,
"orderPrice": 1,
"expiry": orderTimeout}]), 0, true);
if (orderTxId["error"] != null) {
sendAlarm("[btc maker] icx_createorder failed");
return;
}
checkOrderSizeTime = time().now();
objStatistics["btcInOrder"] = orderSize;
Deno.writeTextFileSync("./btcmakerstatistics.json", JSON.stringify(objStatistics));
sendAlarm(`[btc maker] created order ${orderTxId}, btc in order: ${orderSize}`);
return orderTxId;
}
}
async function checkExistingExtHtlc(offerId) {
console.log("checkExistingExtHtlc for offerId: " + offerId);
if (objOfferData.hasOwnProperty(offerId)) {
console.log("Offer " + offerId + " already has ext htlc " + objOfferData[offerId]["exthtlc"])
return;
}
const listHtlcs = (await rpcMethod('icx_listhtlcs', [{"offerTx": offerId}])).result;
console.log("checkExistingExtHtlc icx_listhtlcs result: " + JSON.stringify(listHtlcs));
for (var key in listHtlcs) {
if (key == "WARNING") {
continue;
}
const htlcDetails = listHtlcs[key];
if (htlcDetails["type"] == "EXTERNAL" && htlcDetails["status"] == "OPEN" && htlcDetails["offerTx"] == offerId) {
const htlcDetails = listHtlcs[key];
const hash = htlcDetails["hash"];
const seed = objHashSeed[hash];
console.log("Seed: " + seed);
let offerData = {
"seed": seed,
"hash": hash,
"exthtlc": key,
"timeout": htlcDetails["timeout"],
"amount": htlcDetails["amount"],
"expire": btcBlock + htlcDetails["timeout"]
};
objOfferData[offerId] = offerData;
Deno.writeTextFileSync("./BtcOfferData.json", JSON.stringify(objOfferData));
}
}
}
async function acceptOfferIfAny(orderId) {
if (orderId == null || orderId.length <= 0) {
console.error("empty order id input");
return;
}
const listOrderOffers = (await rpcMethod('icx_listorders', [{"orderTx": orderId}], true)).result;
for (var key in listOrderOffers) {
if (key == "WARNING")
continue;
// Check if accepted the offer already. Because may restarted the script.
await checkExistingExtHtlc(key);
}
for (var key in listOrderOffers) {
if (key == "WARNING")
continue;
if (objOfferData.hasOwnProperty(key)) {
console.log("Order " + orderId + " already has offer " + key);
continue;
}
console.log(key + " -> " + listOrderOffers[key]);
const offerDetails = listOrderOffers[key];
console.log(`Offer detail: ${JSON.stringify(offerDetails)}`);
if (offerDetails["status"] == "OPEN") {
sendAlarm(`[btc maker] received offer ${key} with amount ${offerDetails["amount"]}`);
const btcTakerPubkey = offerDetails["receivePubkey"];
const SPV_TIMEOUT = 80; // Must greater than CICXSubmitEXTHTLC::EUNOSPAYA_MINIMUM_TIMEOUT = 72;
// Create the on maker side. Note. the timeout input is a string but not a number
// If input number, will have "JSON value is not a string as expected" error.
const spvHtlc = await waitSPVConnected(async () => {
return await rpcMethod('spv_createhtlc', [btcTakerPubkey, btcMakerPubkey, SPV_TIMEOUT.toString()]);
});
if (spvHtlc["error"] != null) {
sendAlarm("[btc maker] spv_createhtlc failed");
continue;
}
sendAlarm("[btc maker] spv_createhtlc result: " + JSON.stringify(spvHtlc.result));
const seed = spvHtlc.result["seed"];
const hash = spvHtlc.result["seedhash"];
console.log(`Seed/Hash generated: ${seed}/${hash}`);
objHashSeed[hash] = seed;
Deno.writeTextFileSync("./hashseed.json", JSON.stringify(objHashSeed));
// Fund the spv htlc
const spvFundRpy = (await waitSPVConnected(async () => {
return await rpcMethod('spv_sendtoaddress', [spvHtlc.result["address"], offerDetails["amountInFromAsset"]])
}));
// const spvFundTxid = (await rpcMethod('spv_sendtoaddress', [spvHtlc.result["address"], offerDetails["amountInFromAsset"]])).result;
if (spvFundRpy["error"] != null) {
sendAlarm("[btc maker] spv_sendtoaddress failed");
continue;
}
const spvFundTxid = spvFundRpy.result;
if (spvFundTxid["txid"] == null) {
sendAlarm("[btc maker] spv_sendtoaddress returns null");
continue;
}
objStatistics["btcInHtlc"] += offerDetails["amountInFromAsset"];
Deno.writeTextFileSync("./btcmakerstatistics.json", JSON.stringify(objStatistics));
objSpvHtlcExpire[spvHtlc.result["address"]] = btcBlock + SPV_TIMEOUT + 2;
Deno.writeTextFileSync("./spvhtlcexpire.json", JSON.stringify(objSpvHtlcExpire));
objSpvHtlcAmount[spvHtlc.result["address"]] = offerDetails["amountInFromAsset"];
Deno.writeTextFileSync("./spvhtlcamount.json", JSON.stringify(objSpvHtlcAmount));
sendAlarm(`[btc maker] Fund spv htlc ${spvHtlc.result["address"]} with txid result: ${spvFundTxid["txid"]}`);
const extHtlcTxid = await waitConfirmation(await rpcMethod('icx_submitexthtlc',
[{"offerTx": key, "hash": hash, "amount": offerDetails["amountInFromAsset"],
"htlcScriptAddress": spvHtlc.result["address"], "ownerPubkey": btcMakerPubkey, "timeout": SPV_TIMEOUT}]), 0, true);
if (extHtlcTxid["error"] != null) {
sendAlarm("[btc maker] icx_submitexthtlc failed");
continue;
}
sendAlarm(`[btc maker] icx_submitexthtlc txid: ${extHtlcTxid}`);
let offerData = {
"seed": seed,
"hash": hash,
"exthtlc": extHtlcTxid,
"timeout": SPV_TIMEOUT,
"amount": offerDetails["amountInFromAsset"],
"expire": btcBlock + SPV_TIMEOUT
};
objOfferData[key] = offerData;
Deno.writeTextFileSync("./BtcOfferData.json", JSON.stringify(objOfferData));
}
}
}
async function checkOfferDfcHtlc(offerId) {
console.log("Checking dfc htlc of offer " + offerId);
if (mapOfferDfcClaim.has(offerId)) {
console.log("Offer id: " + offerId + " already claimed in dBTC tx " + mapOfferDfcClaim.get(offerId));
return;
}
const listHtlcs = (await rpcMethod('icx_listhtlcs', [{"offerTx": offerId}])).result;
console.log("icx_listhtlcs result: " + JSON.stringify(listHtlcs));
for (var key in listHtlcs) {
if (key == "WARNING")
continue;
const htlcDetails = listHtlcs[key];
if (htlcDetails["type"] == "DFC" && htlcDetails["status"] == "OPEN" && offerId == htlcDetails["offerTx"]) {
const offerData = objOfferData[offerId];
const dfcClaimRes = await rpcMethod('icx_claimdfchtlc', [{"dfchtlcTx": key, "seed": offerData["seed"]}]);
if (dfcClaimRes["error"] != null) {
continue;
}
const dfcClaimTxid = dfcClaimRes.result;
sendAlarm("[btc maker] Claimed dBTC in txid: " + JSON.stringify(dfcClaimTxid));
mapOfferDfcClaim.set(offerId, dfcClaimTxid.txid);
sendAlarm(`[btc maker] Finished the whole swap process for offer: ${offerId}`);
delete objOfferData[offerId];
Deno.writeTextFileSync("./BtcOfferData.json", JSON.stringify(objOfferData));
}
}
}
async function loadExistingData() {
try {
const texthashseed = Deno.readTextFileSync("./hashseed.json");
if (texthashseed.length > 0) {
objHashSeed = JSON.parse(texthashseed);
console.log("objHashSeed: " + JSON.stringify(objHashSeed));
}
} catch (e) {
console.log("Skipped to load hashseed.json");
}
try {
const textspvHtlcExpire = Deno.readTextFileSync("./spvhtlcexpire.json");
if (textspvHtlcExpire.length > 0) {
objSpvHtlcExpire = JSON.parse(textspvHtlcExpire);
console.log("objSpvHtlcExpire: " + JSON.stringify(objSpvHtlcExpire));
}
} catch (e) {
console.log("Skipped to load spvhtlcexpire.json");
}
try {
const textspvHtlAmount = Deno.readTextFileSync("./spvhtlcamount.json");
if (textspvHtlAmount.length > 0) {
objSpvHtlcAmount = JSON.parse(textspvHtlAmount);
console.log("objSpvHtlcAmount: " + JSON.stringify(objSpvHtlcAmount));
}
} catch (e) {
console.log("Skipped to load spvhtlcamount.json");
}
try {
const textStatistics = Deno.readTextFileSync("./btcmakerstatistics.json");
if (textStatistics.length > 0) {
objStatistics = JSON.parse(textStatistics);
console.log("objStatistics: " + JSON.stringify(objStatistics));
}
} catch (e) {
console.log("Skipped to load btcmakerstatistics.json");
}
try {
const textBtcOfferData = Deno.readTextFileSync("./BtcOfferData.json");
if (textBtcOfferData.length > 0) {
objOfferData = JSON.parse(textBtcOfferData);
console.log("objOfferData: " + JSON.stringify(objOfferData));
}
}catch(e) {
console.log("Skipped to load BtcOfferData.json");
}
}
async function getBtcBlock() {
const res = (await rpcMethod('spv_syncstatus'));
if (res["result"] == null || !res["result"]["connected"]) {
console.warn("spv not connected");
sendAlarm("[btc maker] spv not connected");
return;
}
if (res["result"]["current"] != res["result"]["estimated"]) {
console.warn("spv not full synced");
sendAlarm("[btc maker] spv not full synced");
return;
}
btcBlock = res["result"]["current"];
}
async function claimExpiredSpvHtlc() {
let spvHtlcToRemove = Array();
for (const spvHtlc in objSpvHtlcExpire) {
const expireBlock = objSpvHtlcExpire[spvHtlc];
if (btcBlock > expireBlock) {
spvHtlcToRemove.push(spvHtlc);
const res = (await rpcMethod('spv_refundhtlc', [spvHtlc, btcMakerAddress]));
if (res["error"] != null) {
console.log(`SPV HTLC ${spvHtlc} already claimed`);
}else if (res["result"] != null && res["result"]["txid"]) {
sendAlarm(`[btc maker] Successfully claimed back btc in SPV HTLC ${spvHtlc} in txid ${res["result"]["txid"]}`);
}
objStatistics["btcInHtlc"] -= objSpvHtlcAmount[spvHtlc];
Deno.writeTextFileSync("./btcmakerstatistics.json", JSON.stringify(objStatistics));
}
}
if (spvHtlcToRemove.length > 0) {
// Remove the already checked one
spvHtlcToRemove.forEach((spvHtlc) => {
delete objSpvHtlcExpire[spvHtlc];
Deno.writeTextFileSync("./claimedspvhtlc.json", spvHtlc + "\n", { append: true});
console.log(`Delete spv htlc expire ${spvHtlc}`);
});
if (Object.keys(objSpvHtlcExpire).length > 0) {
Deno.writeTextFileSync("./spvhtlcexpire.json", JSON.stringify(objSpvHtlcExpire));
}else {
console.log(`Remove file spvhtlcexpire.json`);
Deno.removeSync("./spvhtlcexpire.json");
}
}
for (var offerId in objOfferData) {
if (objOfferData.hasOwnProperty(offerId)) {
if (objOfferData[offerId]["expire"] < btcBlock) {
delete objOfferData[offerId];
Deno.writeTextFileSync("./BtcOfferData.json", JSON.stringify(objOfferData));
}
}
}
}
async function outputStatistics() {
const timeDiffInHours = difference(time().now(), outputStatisticsTime, { units: ["hours"] })["hours"];
if (timeDiffInHours < outputStatisticsInterval) {
return;
}
outputStatisticsTime = time().now();
const accountBalance = (await rpcMethod('getaccount', [ownerAddress])).result;
var dbtcBalance = 0, dfiTokenBalance = 0;
console.log("Account balance " + accountBalance);
accountBalance.forEach((item) => {
if (item.includes("@BTC")) {
dbtcBalance = parseFloat(item);
}else if (item.includes("@DFI")) {
dfiTokenBalance = parseFloat(item);
}
});
const dfiUtxoBalance = (await rpcMethod('getbalance')).result;
const btcBalance = parseFloat((await rpcMethod('spv_getbalance', [])).result);
var btcInHtlc = 0;
if (objStatistics["btcInHtlc"] != null) {
btcInHtlc = objStatistics["btcInHtlc"];
}
sendAlarm(`[btc maker] Order size: ${objStatistics["btcInOrder"]}, BTC balance: ${btcBalance}, dBTC balance: ${dbtcBalance}, DFI Token balance: ${dfiTokenBalance}, DFI UTXO balance: ${dfiUtxoBalance}, Total btc amount in HTLC: ${btcInHtlc}`);
}
(async() => {
try{
if (ownerAddress == null) {
console.error("Please define DFI_ADDRESS in environment variable");
return;
}
if (!btcMakerAddress || btcMakerAddress.length <= 0) {
console.error("Please define SPV_BTC_ADDRESS in environment variable");
return;
}
sendAlarm(`[btc maker] started bot with version ${BOT_VERSION}`);
loadExistingData();
btcMakerPubkey = (await rpcMethod('spv_getaddresspubkey', [btcMakerAddress])).result;
if (btcMakerPubkey.length <= 0) {
console.error("Don't have ownership of btc address: " + btcMakerAddress);
return;
}
console.log("Btc pubkey: " + btcMakerPubkey);
while(true) {
const orderTxId = await createOrderIfNotExist(btcMakerPubkey);
await getBtcBlock();
await acceptOfferIfAny(orderTxId);
for (var offerId in objOfferData) {
if (objOfferData.hasOwnProperty(offerId)) {
checkOfferDfcHtlc(offerId);
}
}
await claimExpiredSpvHtlc();
await outputStatistics();
await sleep(20000);
}
}catch(e) {
console.error(e);
}
})();