-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit-orders.js
More file actions
641 lines (538 loc) · 20.6 KB
/
limit-orders.js
File metadata and controls
641 lines (538 loc) · 20.6 KB
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
const Web3 = require("web3");
const { promisify } = require("util");
const axios = require('axios');
const sqlite3 = require("sqlite3").verbose();
const LCDClient = require('@palomachain/paloma.js').LCDClient;
const MsgExecuteContract = require('@palomachain/paloma.js').MsgExecuteContract;
const MnemonicKey = require('@palomachain/paloma.js').MnemonicKey;
const geckoTokens = require("./gecko.json");
const fs = require('fs').promises;
require("dotenv").config();
const PALOMA_LCD = process.env.PALOMA_LCD;
const PALOMA_CHAIN_ID = process.env.PALOMA_CHAIN_ID;
const PALOMA_PRIVATE_KEY = process.env.PALOMA_KEY;
const TELEGRAM_ALERT_API = process.env.TELEGRAM_ALERT_API;
const VETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
const SLIPPAGE = process.env.SLIPPAGE;
const DENOMINATOR = 1000;
const MAX_SIZE = 8;
const PROFIT_TAKING = 2;
const STOP_LOSS = 4;
const EXPIRE = 8;
const BOT_NAME = 'Limit Order';
const WithdrawType = {
SUCCESS: 1, // All success withdrawn type.
EXPIRED: 2, // For Limit order, Stop loss bot type.
REMAINING: 3 // For DCA bot type.
};
let WETH = null;
let web3 = null;
let contractInstance = null;
let COINGECKO_CHAIN_ID = null;
let COINGECKO_COIN_ID = null;
let networkName = null;
let connections = null;
let FROM_BLOCK = null;
let LOB_CW = null;
let DEX = null;
let BOT = "limitOrder";
let ADDRESS = null;
let OLD = false;
const mixpanel = require('mixpanel').init('eaae482845dadd88e1ce07b9fa03dd6b');
let configs = null;
async function setupConnections() {
const data = await fs.readFile('./networks.json', 'utf8');
configs = JSON.parse(data);
}
setupConnections().then(r => { });
let db = new sqlite3.Database(process.env.DB_LOCATION);
db.serialize(() => {
db.run(
`CREATE TABLE IF NOT EXISTS fetched_blocks (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
block_number INTEGER,
network_name TEXT,
dex TEXT,
bot TEXT
);`
);
db.run(
`CREATE TABLE IF NOT EXISTS deposits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
deposit_id INTEGER NOT NULL,
token0 TEXT NOT NULL,
token1 TEXT NOT NULL,
amount0 TEXT NOT NULL,
amount1 TEXT,
depositor TEXT NOT NULL,
deposit_price REAL,
tracking_price REAL,
profit_taking INTEGER,
stop_loss INTEGER,
expire INTEGER,
withdraw_type INTEGER,
withdraw_block INTEGER,
withdraw_amount TEXT,
withdrawer TEXT,
network_name TEXT,
dex_name TEXT,
bot TEXT
);`);
db.run(`CREATE INDEX IF NOT EXISTS deposit_idx ON deposits (deposit_id);`);
db.run(`CREATE TABLE IF NOT EXISTS users(
chat_id TEXT PRIMARY KEY,
address TEXT NOT NULL
)`);
});
db.getAsync = promisify(db.get).bind(db);
db.runAsync = promisify(db.run).bind(db);
// Fetch all deposited order.
// Fetch all withdrawn/canceled order.
// Find pending orders from above.
// Find pools list to fetch information.
// Fetch all pool information from Uniswap V3.
// Find executable IDs.
let processing = false;
async function updateTables() {
// alter tables here for new columns
try {
await db.getAsync(`ALTER TABLE fetched_blocks ADD COLUMN contract_instance TEXT;`);
}
catch (e) {
}
try {
await db.getAsync(`ALTER TABLE deposits ADD COLUMN contract TEXT;`);
}
catch (e) {
}
try {
await db.getAsync(`ALTER TABLE deposits ADD COLUMN old TEXT;`);
}
catch (e) {
}
try {
await db.getAsync(`ALTER TABLE deposits ADD COLUMN tracking_price1 REAL;`);
}
catch (e) {
}
try {
await db.getAsync(`ALTER TABLE deposits ADD COLUMN deposit_price1 REAL;`);
}
catch (e) {
}
}
let prices = {};
async function getLastBlock() {
if (processing) {
return 0
} else {
processing = true;
}
await updateTables();
for (const config of configs) {
web3 = new Web3(config.NODE);
contractInstance = new web3.eth.Contract(JSON.parse(config.ABI), config.VYPER);
ADDRESS = config.VYPER;
COINGECKO_CHAIN_ID = config.COINGECKO_CHAIN_ID;
COINGECKO_COIN_ID = config.COINGECKO_COIN_ID;
networkName = config.NETWORK_NAME;
WETH = config.WETH;
FROM_BLOCK = config.FROM_BLOCK;
LOB_CW = config.CW;
DEX = config.DEX;
OLD = config.OLD || false;
prices[networkName] = [];
try {
const row = await db.getAsync(`SELECT * FROM fetched_blocks WHERE network_name = ? AND dex = ? AND bot = ? AND contract_instance = ? AND ID = (SELECT MAX(ID) FROM fetched_blocks WHERE network_name = ? AND dex = ? AND bot = ? AND contract_instance = ?)`, [networkName, DEX, BOT, ADDRESS, networkName, DEX, BOT, ADDRESS]);
let fromBlock = 0;
if (row === undefined) {
const data = [FROM_BLOCK - 1, networkName, DEX, BOT, ADDRESS];
await db.runAsync(`INSERT INTO fetched_blocks (block_number, network_name, dex, bot, contract_instance) VALUES (?, ?, ?, ?, ?);`, data);
fromBlock = Number(FROM_BLOCK);
} else {
fromBlock = row["block_number"] + 1;
}
await getNewBlocks(fromBlock);
} catch (err) {
console.error(err);
}
await delay(6 * 1000);
}
processing = false;
}
function delay(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
async function retryAxiosRequest(url, method, timeout, headers, maxRetries) {
let error = null;
for (let i = 0; i < maxRetries; i++) {
try {
return await axios({ url, method, timeout, headers });
} catch (err) {
error = err;
console.error(`Attempt ${i + 1} failed. Retrying... in 30 seconds`);
await delay(30 * 1000);
}
}
throw new Error(`Maximum retries exceeded ${error}`);
}
async function canAddDeposit(swap_id) {
const row = await db.getAsync(`SELECT COUNT(*) as count FROM deposits WHERE deposit_id = ? AND network_name = ? AND dex_name = ? AND bot = ?`, [swap_id, networkName, DEX, BOT]);
return row.count === 0;
}
async function getNewBlocks(fromBlock) {
const block_number = Number(await web3.eth.getBlockNumber());
let deposited_events = [];
let withdrawn_events = [];
for (let i = fromBlock; i <= block_number; i += 10000) {
let toBlock = i + 9999;
if (toBlock > block_number) {
toBlock = block_number;
}
const new_deposited_events = await contractInstance.getPastEvents("Deposited", {
fromBlock: i,
toBlock: toBlock,
});
const new_withdrawn_events = await contractInstance.getPastEvents("Withdrawn", {
fromBlock: i,
toBlock: toBlock,
});
deposited_events = deposited_events.concat(new_deposited_events);
withdrawn_events = withdrawn_events.concat(new_withdrawn_events);
}
let responses = [];
for (const deposited_event of deposited_events) {
let token0 = deposited_event.returnValues["token0"];
if (prices[networkName][token0.toLowerCase()] === undefined) {
if (token0 === VETH) {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/price?ids=${COINGECKO_COIN_ID}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
else {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/token_price/${COINGECKO_CHAIN_ID}?contract_addresses=${token0}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
prices[networkName][token0.toLowerCase()] = 0;
}
}
for (const deposited_event of deposited_events) {
let token1 = deposited_event.returnValues["token1"];
if (prices[networkName][token1.toLowerCase()] === undefined) {
if (token1 === VETH) {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/price?ids=${COINGECKO_COIN_ID}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
else {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/token_price/${COINGECKO_CHAIN_ID}?contract_addresses=${token1}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
prices[networkName][token1.toLowerCase()] = 0;
}
}
for (const response of responses) {
Object.keys(response.data).forEach(value => {
let price_index = value;
let value_object = response.data[value];
if (value_object.usd !== undefined) {
prices[networkName][price_index] = value_object.usd
}
});
}
for (const deposited_event of deposited_events) {
let token0 = deposited_event.returnValues["token0"];
deposited_event.returnValues["price"] = prices[networkName][token0.toLowerCase()];
}
for (const deposited_event of deposited_events) {
let token1 = deposited_event.returnValues["token1"];
deposited_event.returnValues["price1"] = prices[networkName][token1.toLowerCase()];
}
if (deposited_events.length !== 0) {
let placeholders = "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
let sql = `INSERT INTO deposits (deposit_id, token0, token1, amount0, depositor, deposit_price, deposit_price1, tracking_price, tracking_price1, profit_taking, stop_loss, expire, network_name, dex_name, bot, contract, old) VALUES ` + placeholders + ";";
for (const deposited_event of deposited_events) {
let flat_array = [];
const profit_taking = deposited_event.returnValues["profit_taking"];
const stop_loss = deposited_event.returnValues["stop_loss"];
const insert_profit_taking = Number(profit_taking) + Number(SLIPPAGE);
const insert_stop_loss = Number(stop_loss) > Number(SLIPPAGE) ? Number(stop_loss) - Number(SLIPPAGE) : 0;
flat_array.push(deposited_event.returnValues["deposit_id"]);
flat_array.push(deposited_event.returnValues["token0"]);
flat_array.push(deposited_event.returnValues["token1"]);
flat_array.push(deposited_event.returnValues["amount0"]);
flat_array.push(deposited_event.returnValues["depositor"]);
flat_array.push(deposited_event.returnValues["price"]);
flat_array.push(deposited_event.returnValues["price1"]);
flat_array.push(deposited_event.returnValues["price"]);
flat_array.push(deposited_event.returnValues["price1"]);
flat_array.push(insert_profit_taking);
flat_array.push(insert_stop_loss);
flat_array.push(deposited_event.returnValues["expire"]);
flat_array.push(networkName);
flat_array.push(DEX);
flat_array.push(BOT);
flat_array.push(ADDRESS);
flat_array.push(OLD);
try {
await db.runAsync(sql, flat_array);
mixpanel.track('bot-add', {
bot: BOT,
dex: DEX,
network: networkName,
price: deposited_event.returnValues["price"],
price1: deposited_event.returnValues["price1"]
});
} catch (e) {
console.log(e);
}
}
}
if (withdrawn_events.length !== 0) {
let sql = `UPDATE deposits SET withdraw_block = ?, withdrawer = ?, withdraw_type = ?, withdraw_amount = ? WHERE deposit_id = ? AND network_name = ? AND bot = ? AND contract = ?;`;
for (const withdrawn_event of withdrawn_events) {
let data = [
withdrawn_event.blockNumber,
withdrawn_event.returnValues["withdrawer"],
withdrawn_event.returnValues["withdraw_type"],
withdrawn_event.returnValues["withdraw_amount"],
withdrawn_event.returnValues["deposit_id"],
networkName,
BOT,
ADDRESS
];
await db.runAsync(sql, data);
try {
const botInfo = await getBot(withdrawn_event.returnValues["deposit_id"]);
const tokenName = await getBotName(botInfo['token1']);
const withdrawType = withdrawn_event.returnValues["withdraw_type"];
if (botInfo && withdrawType !== 'CANCEL') {
axios.get(TELEGRAM_ALERT_API, {
params: {
depositor: botInfo["depositor"],
kind: withdrawType === 'EXPIRE' ? WithdrawType.EXPIRED : WithdrawType.SUCCESS,
tokenName: tokenName,
botType: BOT_NAME,
},
});
}
} catch (error) {
console.log('Telegram alert error', error);
}
}
}
if (fromBlock < block_number) {
let sql = `UPDATE fetched_blocks SET block_number = ? WHERE network_name = ? AND dex = ? AND bot = ? AND contract_instance = ?;`;
let data = [block_number, networkName, DEX, BOT, ADDRESS];
await db.runAsync(sql, data);
}
const deposits = await getPendingDeposits();
const withdrawDeposits = [];
responses = [];
for (const deposit of deposits) {
if (deposit.network_name == networkName) {
let token0 = deposit.token0;
if (prices[networkName][token0.toLowerCase()] === undefined) {
if (token0 === VETH) {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/price?ids=${COINGECKO_COIN_ID}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
else {
responses.push(await retryAxiosRequest(
`https://pro-api.coingecko.com/api/v3/simple/token_price/${COINGECKO_CHAIN_ID}?contract_addresses=${token0}&vs_currencies=usd&x_cg_pro_api_key=${process.env.COINGECKO_API_KEY}`,
'get',
8000,
{
'Content-Type': 'application/json',
},
2
));
}
prices[networkName][token0.toLowerCase()] = 0
}
}
}
for (const response of responses) {
Object.keys(response.data).forEach(value => {
let price_index = value;
let value_object = response.data[value];
if (value_object.usd !== undefined) {
prices[networkName][price_index] = value_object.usd
}
});
}
for (const deposit of deposits) {
try {
let withdrawDeposit = null;
if (deposit.network_name == networkName) {
withdrawDeposit = await processDeposit(deposit);
}
if (withdrawDeposit) {
withdrawDeposit["expected"] = await getMinAmount(withdrawDeposit.deposit_id, withdrawDeposit.withdraw_type);
withdrawDeposits.push(withdrawDeposit);
}
if (withdrawDeposits.length >= MAX_SIZE) {
break;
}
} catch (e) {
console.log(e);
}
}
if (withdrawDeposits.length > 0) {
await executeWithdraw(withdrawDeposits);
}
}
async function getMinAmount(deposit_id, withdraw_type) {
let amount = 0;
try {
amount = await contractInstance.methods.withdraw(deposit_id, withdraw_type).call();
} catch (e) {
console.log('getMinAmount exception:', e);
}
return web3.utils.toBN(amount).mul(web3.utils.toBN(Number(DENOMINATOR) - Number(SLIPPAGE))).div(web3.utils.toBN(DENOMINATOR)).toString();
}
async function processDeposit(deposit) {
let token0 = deposit.token0;
let token1 = deposit.token1;
let price = prices[networkName][token0.toLowerCase()];
let price1 = prices[networkName][token1.toLowerCase()];
await updatePrice(deposit.id, price, price1);
let deposit_price_ratio = deposit.deposit_price1 / deposit.deposit_price;
let current_price_ratio = price1 / price;
if (Number(current_price_ratio) > Number(deposit_price_ratio) * (Number(deposit.profit_taking) + Number(DENOMINATOR)) / Number(DENOMINATOR)) {
return { "deposit_id": Number(deposit.deposit_id), "withdraw_type": PROFIT_TAKING };
} else if (Number(deposit.expire) < Date.now() / 1000) {
return { "deposit_id": Number(deposit.deposit_id), "withdraw_type": EXPIRE };
}
return null;
}
async function executeWithdraw(deposits) {
const lcd = new LCDClient({
URL: PALOMA_LCD,
chainID: PALOMA_CHAIN_ID,
classic: true,
});
const mk = new MnemonicKey({
mnemonic: PALOMA_PRIVATE_KEY,
});
const wallet = lcd.wallet(mk);
const msg = new MsgExecuteContract(
wallet.key.accAddress,
LOB_CW,
{ "put_withdraw": { "deposits": deposits } }
);
let result = null;
try {
const tx = await wallet.createAndSignTx({ msgs: [msg] });
result = await lcd.tx.broadcast(tx);
const telegramAlertApi = process.env.TELEGRAM_ALERT_API || null;
if (telegramAlertApi) {
try {
deposits.forEach(deposit => {
axios.get(telegramAlertApi, {
params: { depositor: deposit.depositor }
});
});
} catch (e) {
console.log(e);
}
}
} catch (e) {
console.log(e);
}
return result;
}
async function getBot(deposit_id) {
const sql = `
SELECT depositor, token1 FROM deposits
WHERE deposit_id = ? AND contract = ?;
`;
const row = await db.getAsync(sql, [deposit_id, ADDRESS]);
return row;
}
async function getBotName(tokenAddress) {
let coinInfo = null;
for (const geckoToken of geckoTokens) {
if (tokenAddress.toLowerCase() !== VETH.toLowerCase()) {
Object.values(geckoToken.platforms).forEach(prop => {
try {
if (prop.toLowerCase() == tokenAddress.toLowerCase()) {
coinInfo = geckoToken;
//break;
}
} catch (e) {
}
});
} else {
if (geckoToken.id == "ethereum") {
coinInfo = geckoToken;
}
}
if (coinInfo) { break; }
}
return coinInfo ? coinInfo['name'] : null;
}
async function updatePrice(id, price, price1) {
await db.runAsync(
`UPDATE deposits SET tracking_price = ? and tracking_price1 = ? WHERE id = ? ;`,
[price, price1, id], null
);
}
function processDeposits() {
setInterval(getLastBlock, 1000 * 1);
}
async function getPendingDeposits() {
let dbAll = promisify(db.all).bind(db);
let dex = DEX;
let bot = BOT;
let nn = networkName;
let contract = ADDRESS;
try {
let rows;
let query = `SELECT * FROM deposits WHERE withdraw_block IS NULL`;
query += ` AND LOWER(network_name) = LOWER(?)`;
query += ` AND LOWER(dex_name) = LOWER(?)`;
query += ` AND LOWER(bot) = LOWER(?)`;
query += ` AND LOWER(contract) = LOWER(?);`
rows = await dbAll(query, nn, dex, bot, contract);
return rows;
} catch (err) {
console.error(err.message);
}
}
module.exports = {
processDeposits,
};