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_dbtc_maker.js
528 lines (440 loc) · 20.4 KB
/
icx_bot_dbtc_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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import { rpcMethod, waitConfirmation, waitSPVConnected, createSeedHashPair, 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 btcReceiveAddress = Deno.env.get("SPV_BTC_ADDRESS");
let btcReceiverPubkey = "";
const alarmHook = Deno.env.get("ALARM_HOOK");
let objOfferData = new Object();
let objOfferSpvHtlc = new Object();
let mapOfferSpvClaim = new Map();
let objHashSeed = 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.
let objStatistics = new Object();
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 objOrderHtlcExpire = new Object(); // To help check if can close the order.
let dfcHeaderBlock = 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 canDeleteOrder(orderId) {
if (objOrderHtlcExpire[orderId] != null && Object.keys(objOrderHtlcExpire[orderId]).length > 0) {
return false;
}
const listOrderOffers = (await rpcMethod('icx_listorders', [{"orderTx": orderId}], true)).result;
for (var offerKey in listOrderOffers) {
if (offerKey == "WARNING")
continue;
return false; // Has offer, cannot delete order
}
return true;
}
async function createOrderIfNotExist() {
const res = (await rpcMethod('getblockchaininfo', [], true));
if (res["result"] == null || res["result"]["headers"] == null) {
sendAlarm("[dbtc maker] Failed to getblockchaininfo");
return;
}
dfcHeaderBlock = res["result"]["headers"];
const orders = (await rpcMethod('icx_listorders', [], true)).result;
var foundOrder = false;
var foundedOrder = "";
const accountBalance = (await rpcMethod('getaccount', [ownerAddress])).result;
var btcBalance = 0;
console.log("Account balance " + accountBalance);
accountBalance.forEach((item) => {
if (item.includes("@BTC")) {
btcBalance = parseFloat(item);
return;
}
});
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"] == "INTERNAL" &&
orderDetails["ownerAddress"] == ownerAddress &&
orderDetails["tokenFrom"] == "BTC" &&
orderDetails["chainTo"] == "BTC")
{
console.log(`Order ${key} expiration height ${orderDetails["expireHeight"]}, blockchain header block ${dfcHeaderBlock}`);
if (foundOrder) {
sendAlarm(`[dbtc maker] Already have order ${foundedOrder}, close extra order ${key}`);
const closeTxid = await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true);
sendAlarm(`[dbtc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
} else {
if (orderDetails["expireHeight"] > dfcHeaderBlock + minOrderLife) {
const timeDiffInHours = difference(time().now(), checkOrderSizeTime, { units: ["hours"] })["hours"];
if (timeDiffInHours > checkOrderSizeInterval) {
if (Math.abs(orderDetails["amountToFill"] - btcBalance) > 0.00001) {
const canDel = await canDeleteOrder(key);
// 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 (canDel) {
sendAlarm(`[dbtc maker] Order ${key} size ${orderDetails["amountToFill"]} not match with the dbtc balance ${btcBalance}, close it and recreate new one`);
const closeTxid = await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true);
sendAlarm(`[dbtc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
continue;
}
}
}
console.log("Found order " + key);
objStatistics["dbtcInOrder"] = orderDetails["amountFrom"];
foundOrder = true;
foundedOrder = key;
} else {
sendAlarm(`[dbtc maker] Order ${key} is too old, close it`);
const closeTxid = (await waitConfirmation(await rpcMethod('icx_closeorder', [key]), 0, true));
sendAlarm(`[dbtc maker] Order ${key} is closed in tx ${JSON.stringify(closeTxid)}`);
}
}
}
}
}
if (foundOrder) {
return foundedOrder;
} else {
if (btcBalance <= 0.0001) {
console.error("dBTC balance too low");
return;
}
const orderSize = btcBalance;
console.log("Creating order with size " + orderSize);
const orderTxId = await waitConfirmation(await rpcMethod('icx_createorder',
[{"ownerAddress": ownerAddress,
"tokenFrom": "BTC",
"chainTo": "BTC",
"amountFrom": orderSize,
"orderPrice": 1,
"receivePubkey": btcReceiverPubkey,
"expiry": orderTimeout}]),
0, true);
if (orderTxId["error"] != null) {
sendAlarm("[dbtc maker] icx_createorder failed");
return;
}
checkOrderSizeTime = time().now();
objStatistics["dbtcInOrder"] = orderSize;
Deno.writeTextFileSync("./dbtcmakerstatistics.json", JSON.stringify(objStatistics));
objOrderHtlcExpire[orderTxId] = new Object();
Deno.writeTextFileSync("./orderhtlcexpire.json", JSON.stringify(objOrderHtlcExpire));
sendAlarm(`[dbtc maker] created order ${orderTxId}, dbtc in order: ${orderSize}`);
return orderTxId;
}
}
async function checkExistingDfcHtlc(offerId) {
console.log("checkExistingDfcHtlc for offerId: " + offerId);
if (objOfferData.hasOwnProperty(offerId)) {
console.log("Offer " + offerId + " already has dfc htlc " + objOfferData[offerId]["dfchtlc"])
return;
}
const listHtlcs = (await rpcMethod('icx_listhtlcs', [{"offerTx": offerId}])).result;
console.log("checkExistingDfcHtlc 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") {
const htlcDetails = listHtlcs[key];
const hash = htlcDetails["hash"];
const seed = objHashSeed[hash];
console.log("Seed: " + seed);
let offerData = {
"seed": seed,
"hash": hash,
"dfchtlc": key,
"timeout": htlcDetails["timeout"],
"amount": htlcDetails["amount"],
"expire": dfcHeaderBlock + htlcDetails["timeout"]
};
objOfferData[offerId] = offerData;
Deno.writeTextFileSync("./DBtcOfferData.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;
await checkExistingDfcHtlc(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(`Order detail: ${JSON.stringify(offerDetails)}`);
if (offerDetails["status"] == "OPEN") {
const seedHashPair = createSeedHashPair();
const seed = seedHashPair.seed;
const hash = seedHashPair.hash;
console.log(`Seed/Hash generated: ${seed}/${hash}`);
objHashSeed[hash] = seed;
Deno.writeTextFileSync("./hashseed.json", JSON.stringify(objHashSeed));
const TIMEOUT = 1500; // Must grater than 1439, because CICXSubmitDFCHTLC::MINIMUM_TIMEOUT limit.
const dfcHtlcTxid = await waitConfirmation(await rpcMethod('icx_submitdfchtlc',
[{"offerTx": key, "hash": hash, "amount": offerDetails["amount"], "timeout": TIMEOUT }]), 0, true);
if (dfcHtlcTxid["error"] != null) {
sendAlarm("[dbtc maker] icx_submitdfchtlc failed");
continue;
}
objStatistics["dbtcInHtlc"] += offerDetails["amount"];
Deno.writeTextFileSync("./dbtcmakerstatistics.json", JSON.stringify(objStatistics));
let offerData = {
"seed": seed, "hash": hash,
"dfchtlc": dfcHtlcTxid,
"timeout": TIMEOUT,
"amount": offerDetails["amount"],
"orderid": orderId,
"expire": dfcHeaderBlock + TIMEOUT
};
objOfferData[key] = offerData;
Deno.writeTextFileSync("./DBtcOfferData.json", JSON.stringify(objOfferData));
objOrderHtlcExpire[orderId][dfcHtlcTxid] = dfcHeaderBlock + TIMEOUT;
Deno.writeTextFileSync("./orderhtlcexpire.json", JSON.stringify(objOrderHtlcExpire));
sendAlarm(`[dbtc maker] accepted offer ${key} by call icx_submitdfchtlc with txid: ${dfcHtlcTxid}, amount: ${offerDetails["amount"]}`);
}
}
}
async function checkOfferSpvHtlc(offerId) {
console.log("Checking spv htlc of offer " + offerId);
if (mapOfferSpvClaim.has(offerId)) {
console.log("Offer id: " + offerId + " already claimed in btc tx " + mapOfferSpvClaim.get(offerId));
return;
}
if (objOfferSpvHtlc.hasOwnProperty(offerId)) {
console.log("Offer " + offerId + " already has spv htlc " + objOfferSpvHtlc[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"] == "EXTERNAL" && htlcDetails["status"] == "OPEN") {
// ReCreate the same HTLC 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', [btcReceiverPubkey, htlcDetails["ownerPubkey"], htlcDetails["timeout"].toString(), htlcDetails["hash"]]);
});
if (spvHtlc["error"] != null) {
sendAlarm("[dbtc maker] spv_createhtlc failed");
continue;
}
objOfferSpvHtlc[offerId] = spvHtlc.result["address"];
Deno.writeTextFileSync("./offerspvhtlc.json", JSON.stringify(objOfferSpvHtlc));
sendAlarm("[dbtc maker] spv_createhtlc address result: " + objOfferSpvHtlc[offerId]);
}
}
}
async function checkHtlcOutputAndClaim(offerId) {
if (mapOfferSpvClaim.has(offerId)) {
console.log("Offer id: " + offerId + " already claimed in btc tx " + mapOfferSpvClaim.get(offerId));
return;
}
const spvHtlc = objOfferSpvHtlc[offerId];
console.log("Check output of spv htlc: " + spvHtlc);
const listSpvReceived = (await rpcMethod('spv_listreceivedbyaddress', [1, spvHtlc])).result;
console.log("spv_listreceivedbyaddress result " + JSON.stringify(listSpvReceived));
console.log("listSpvReceived.length: " + Object.keys(listSpvReceived).length);
if (Object.keys(listSpvReceived).length > 0) {
if (!objOfferData.hasOwnProperty(offerId)) {
const msg = `[dbtc maker] offer ${offerId} don't have htlc data for spv htlc ${spvHtlc}`;
sendAlarm(msg);
console.error(msg);
// Delete it so it will goes into infinite loop.
delete objOfferSpvHtlc[offerId];
Deno.writeTextFileSync("./offerspvhtlc.json", JSON.stringify(objOfferSpvHtlc));
return;
}
const offerData = objOfferData[offerId];
if (listSpvReceived[0]["amount"] != offerData["amount"]) {
sendAlarm(`[dbtc maker] The spv received amount ${listSpvReceived[0]["amount"]} not match with offer amount ${offerData["amount"]}!`);
}
const claimInput = [spvHtlc, btcReceiveAddress, offerData["seed"]];
console.log("spv_claimhtlc input: " + JSON.stringify(claimInput));
const claimBtcTxid = await waitSPVConnected(async () => {
return await rpcMethod('spv_claimhtlc', claimInput, false, false);
});
if (claimBtcTxid["error"] != null) {
sendAlarm("[dbtc maker] spv_claimhtlc failed");
return;
}
sendAlarm(`[dbtc maker] SPV claim txid: ${claimBtcTxid.result["txid"]}`);
mapOfferSpvClaim.set(offerId, claimBtcTxid.result["txid"]);
delete objOfferData[offerId];
Deno.writeTextFileSync("./DBtcOfferData.json", JSON.stringify(objOfferData));
sendAlarm(`[dbtc maker] Finished the whole swap process for offer: ${offerId}`);
const orderId = offerData["orderid"];
const dfchtlc = offerData["dfchtlc"];
delete objOrderHtlcExpire[orderId][dfchtlc];
Deno.writeTextFileSync("./orderhtlcexpire.json", JSON.stringify(objOrderHtlcExpire));
// Erase the offer spv htlc, so don't check again.
delete objOfferSpvHtlc[offerId];
Deno.writeTextFileSync("./offerspvhtlc.json", JSON.stringify(objOfferSpvHtlc));
}
}
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 textofferspvhtlc = Deno.readTextFileSync("./offerspvhtlc.json");
if (textofferspvhtlc.length > 0) {
objOfferSpvHtlc = JSON.parse(textofferspvhtlc);
console.log("objOfferSpvHtlc: " + JSON.stringify(objOfferSpvHtlc));
}
}catch (e) {
console.log("Skipped to load offerspvhtlc.json");
}
try {
const textStatistics = Deno.readTextFileSync("./dbtcmakerstatistics.json");
if (textStatistics.length > 0) {
objStatistics = JSON.parse(textStatistics);
console.log("objStatistics: " + JSON.stringify(objStatistics));
}
} catch (e) {
console.log("Skipped to load dbtcmakerstatistics.json");
}
try {
const textOrderHtlcExpire = Deno.readTextFileSync("./orderhtlcexpire.json");
if (textOrderHtlcExpire.length > 0) {
objOrderHtlcExpire = JSON.parse(textOrderHtlcExpire);
console.log("objOrderHtlcExpire: " + JSON.stringify(objOrderHtlcExpire));
}
} catch (e) {
console.log("Skipped to load orderhtlcexpire.json");
}
try {
const textDBtcOfferData = Deno.readTextFileSync("./DBtcOfferData.json");
if (textBtcOfferData.length > 0) {
objOfferData = JSON.parse(textDBtcOfferData);
console.log("objOfferData: " + JSON.stringify(objOfferData));
}
}catch(e) {
console.log("Skipped to load DBtcOfferData.json");
}
}
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 dbtcInHtlc = 0;
if (objStatistics["dbtcInHtlc"] != null) {
dbtcInHtlc = objStatistics["dbtcInHtlc"];
}
sendAlarm(`[dbtc maker] Order size: ${objStatistics["dbtcInOrder"]}, BTC balance: ${btcBalance}, dBTC balance: ${dbtcBalance}, DFI Token balance: ${dfiTokenBalance}, DFI UTXO balance: ${dfiUtxoBalance}, Total dbtc amount in HTLC: ${dbtcInHtlc}`);
}
async function removeExpiredHtlc() {
var deletedItem = false;
for (var orderId in objOrderHtlcExpire) {
if (objOrderHtlcExpire.hasOwnProperty(orderId)) {
for (var dfchtlc in objOrderHtlcExpire[orderId]) {
if (objOrderHtlcExpire[orderId].hasOwnProperty(dfchtlc)) {
if (objOrderHtlcExpire[orderId][dfchtlc] < dfcHeaderBlock) {
sendAlarm(`[dbtc maker] deleted expired dfthtlc ${dfchtlc} of order ${orderId} at block ${dfcHeaderBlock}`);
delete objOrderHtlcExpire[orderId][dfchtlc];
deletedItem = true;
}
}
}
}
}
if (deletedItem) {
Deno.writeTextFileSync("./orderhtlcexpire.json", JSON.stringify(objOrderHtlcExpire));
}
for (var offerId in objOfferData) {
if (objOfferData.hasOwnProperty(offerId)) {
if (objOfferData[offerId]["expire"] < dfcHeaderBlock) {
delete objOfferData[offerId];
Deno.writeTextFileSync("./DBtcOfferData.json", JSON.stringify(objOfferData));
}
}
}
}
(async() => {
try{
if (ownerAddress == null) {
console.error("Please define DFI_ADDRESS in environment variable");
return;
}
if (!btcReceiveAddress || btcReceiveAddress.length <= 0) {
console.error("Please define SPV_BTC_ADDRESS in environment variable");
return;
}
sendAlarm(`[dbtc maker] started bot with version ${BOT_VERSION}`);
loadExistingData();
btcReceiverPubkey = (await rpcMethod('spv_getaddresspubkey', [btcReceiveAddress])).result;
if (btcReceiverPubkey.length <= 0) {
console.error("Don't have ownership of btc address: " + btcReceiveAddress);
return;
}
console.log("Btc pubkey: " + btcReceiverPubkey);
while(true) {
const orderTxId = await createOrderIfNotExist(btcReceiverPubkey);
await acceptOfferIfAny(orderTxId);
for (var offerId in objOfferData) {
if (objOfferData.hasOwnProperty(offerId)) {
checkOfferSpvHtlc(offerId);
}
}
for (var offerId in objOfferSpvHtlc) {
// Need to check here because objOfferSpvHtlc maybe deleted some offers inside checkHtlcOutputAndClaim
if (objOfferSpvHtlc.hasOwnProperty(offerId)) {
await checkHtlcOutputAndClaim(offerId);
}
}
await removeExpiredHtlc();
await outputStatistics();
await sleep(20000);
}
}catch(e) {
console.error(e);
}
})();