-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
594 lines (474 loc) · 14.2 KB
/
db.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
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
import * as afx from './global.js'
import mongoose from 'mongoose';
const { ObjectId } = mongoose.Types;
const userSchema = new mongoose.Schema({
chatid: String,
username: String,
init_eth: Number,
init_usd: Number,
block_threshold: Number,
max_fresh_transaction_count: Number,
min_fresh_wallet_count: Number,
min_whale_balance: Number,
min_whale_wallet_count: Number,
min_kyc_wallet_count: Number,
min_dormant_wallet_count: Number,
min_dormant_duration: Number,
min_sniper_count: Number,
lp_lock: Number,
honeypot: Number,
contract_age: Number,
from_chatid: String,
type: String,
wallet: String,
permit: Number,
kickmode: Number,
slippage: Number,
account: String,
pkey: String,
fee:Number,
autobuy: Number,
autosell:Number,
autosell_hi: Number,
autosell_lo: Number,
autosell_hi_amount: Number,
autosell_lo_amount: Number,
autobuy_amount: Number,
vip:Number
});
const cexWalletSchema = new mongoose.Schema({
address: String,
cex_name: String,
distinct_name: String
});
const txHistorySchema = new mongoose.Schema({
chatid: String,
username: String,
account: String,
mode: String,
eth_amount: Number,
token_amount: Number,
token_address: String,
ver:String,
tx: String,
timestamp: Date
});
const feeAccumSchema = new mongoose.Schema({
chatid: String,
fee:Number,
});
const whitelistSchema = new mongoose.Schema({
username: String
});
const tokenSchema = new mongoose.Schema({
chatid: String,
address: String,
dex: Number,
symbol: String,
decimal: Number
});
const autoTradeTokenSchema = new mongoose.Schema({
chatid: String,
address: String,
name: String,
symbol: String,
decimal: Number,
price: Number,
poolAddress: String,
version: String
});
const gainerHistorySchema = new mongoose.Schema({
chatid: String,
token_address: String,
token_name: String,
token_symbol: String,
pair_address: String,
dex: Number,
pair_base_token_symbol: String,
token_price: Number,
market_cap: Number,
timestamp: Date
});
const callHistorySchema = new mongoose.Schema({
chatid: String,
messageId: String,
token_address: String,
pair_address: String,
content0: String,
content1: String,
timestamp: Date
});
// const groupSchema = new mongoose.Schema({
// chatid: String,
// groupname: String,
// });
const poolHistorySchema = new mongoose.Schema({
pool_id: Number,
token_address: String,
pair_address: String,
timestamp: Date
});
const tokenReportSchema = new mongoose.Schema({
address: String,
name: String,
symbol: String,
decimal: Number
});
const User = mongoose.model('users', userSchema);
const PoolHistory = mongoose.model('pool_history', poolHistorySchema);
const TxHistory = mongoose.model('tx_history', txHistorySchema);
const CexWallet = mongoose.model('cex_wallets', cexWalletSchema);
const Whitelist = mongoose.model('whitelists', whitelistSchema);
const Token = mongoose.model('tokens', tokenSchema);
const TokenReport = mongoose.model('token_reports', tokenReportSchema);
const GainerHistory = mongoose.model('gainer_history', gainerHistorySchema);
const CallHistory = mongoose.model('call_history', callHistorySchema);
const AutoTradeToken = mongoose.model('auto_trade_token', autoTradeTokenSchema);
export const init = () => {
return new Promise(async (resolve, reject) => {
mongoose.connect('mongodb://localhost:27017/tbot')
.then(() => {
console.log('Connected to MongoDB...')
resolve();
})
.catch(err => {
console.error('Could not connect to MongoDB...', err)
reject();
});
});
}
export const updateFee = (params) => {
return new Promise(async (resolve, reject) => {
User.findOne({ chatid: params.chatid }).then(async (user) => {
if (user) {
user.fee = params.fee
await user.save();
}
resolve(user);
});
});
}
export const updateUser = (params) => {
return new Promise(async (resolve, reject) => {
User.findOne({ chatid: params.chatid }).then(async (user) => {
if (!user) {
user = new User();
}
user.chatid = params.chatid
user.username = params.username
user.init_eth = params.init_eth
user.init_usd = params.init_usd
user.block_threshold = params.block_threshold
user.max_fresh_transaction_count = params.max_fresh_transaction_count
user.min_fresh_wallet_count = params.min_fresh_wallet_count
user.min_whale_balance = params.min_whale_balance
user.min_whale_wallet_count = params.min_whale_wallet_count
user.min_kyc_wallet_count = params.min_kyc_wallet_count
user.min_dormant_wallet_count = params.min_dormant_wallet_count
user.min_dormant_duration = params.min_dormant_duration
user.min_sniper_count = params.min_sniper_count
user.lp_lock = params.lp_lock
user.contract_age = params.contract_age
user.honeypot = params.honeypot
user.wallet = params.wallet;
user.from_chatid = params.from_chatid;
user.type = params.type;
user.permit = params.permit;
user.kickmode = params.kickmode
user.slippage = params.slippage
user.account = params.account
user.pkey = params.pkey
user.autobuy = params.autobuy
user.autosell = params.autosell
user.autosell_hi = params.autosell_hi
user.autosell_lo = params.autosell_lo
user.autosell_hi_amount = params.autosell_hi_amount
user.autosell_lo_amount = params.autosell_lo_amount
user.autobuy_amount = params.autobuy_amount
//user.vip = params.vip
await user.save();
resolve(user);
});
});
}
export const removeUser = (params) => {
return new Promise((resolve, reject) => {
User.deleteOne({ chatid: params.chatid }).then(() => {
resolve(true);
});
});
}
// export const updateGroup = (params) => {
// return new Promise(async (resolve, reject) => {
// Group.findOne({ chatid: params.chatid }).then(async (group) => {
// if (!group) {
// group = new Group();
// }
// group.chatid = params.chatid
// group.groupname = params.groupname
// await group.save();
// resolve(group);
// });
// });
// }
// export const removeGroup = (params) => {
// return new Promise((resolve, reject) => {
// Group.deleteOne({ chatid: params.chatid }).then(() => {
// resolve(true);
// });
// });
// }
export async function selectUsers(params = {}) {
return new Promise(async (resolve, reject) => {
User.find(params).then(async (users) => {
resolve(users);
});
});
}
export async function addPoolHistory(params = {}) {
return new Promise(async (resolve, reject) => {
try {
let count = await PoolHistory.countDocuments({})
let item = new PoolHistory();
item.pool_id = count + 1
item.token_address = params.primaryAddress
item.pair_address = params.poolAddress
item.timestamp = new Date()
await item.save();
resolve(item.pool_id);
} catch (err) {
console.log(err)
resolve(-1);
}
});
}
export async function selectPoolHistory(params) {
return new Promise(async (resolve, reject) => {
PoolHistory.findOne(params).then(async (user) => {
resolve(user);
});
});
}
export async function selectUser(params) {
return new Promise(async (resolve, reject) => {
User.findOne(params).then(async (user) => {
resolve(user);
});
});
}
export async function existInWhitelist(username) {
return new Promise(async (resolve, reject) => {
Whitelist.findOne({'username': username}).then(async (item) => {
resolve(item);
});
});
}
export function checkCEXWallet(address) {
return new Promise(async (resolve, reject) => {
CexWallet.find({ address: address.toLowerCase() }).then((result) => {
if (result.length > 0) {
resolve(true);
} else {
resolve(false);
}
}).catch(err => {
console.error(err)
reject(false);
});
});
}
export async function getAllTokens() {
return new Promise(async (resolve, reject) => {
Token.find({}).then(async (tokens) => {
resolve(tokens);
});
});
}
export async function getTokens(chatid) {
return new Promise(async (resolve, reject) => {
Token.find({chatid}).then(async (tokens) => {
resolve(tokens);
});
});
}
export async function addToken(chatid, address, symbol, decimal) {
// export async function addToken(chatid, address, dex, symbol, decimal) {
return new Promise(async (resolve, reject) => {
Token.findOne({chatid, address}).then(async (token) => {
if (!token) {
token = new Token();
}
token.chatid = chatid;
token.address = address.toLowerCase();
//token.dex = dex;
token.symbol = symbol;
token.decimal = decimal;
await token.save();
resolve(token);
});
});
}
export async function addGainerHistory(tokenAddress, tokenName, tokenSymbol, pairAddress, dex, pairBaseTokenSymbol, tokenPrice, marketCap) {
// export async function addGainerHistory(chatid, tokenAddress, tokenSymbol, pairAddress, dex, pairBaseTokenSymbol, tokenPrice, marketCap) {
//console.log(tokenAddress, tokenSymbol, pairAddress, pairBaseTokenSymbol, tokenPrice, marketCap)
return new Promise(async (resolve, reject) => {
let item = new GainerHistory();
// item.chatid = chatid
item.token_address = tokenAddress
item.token_name = tokenName
item.token_symbol = tokenSymbol
item.pair_address = pairAddress
item.dex = dex
item.pair_base_token_symbol = pairBaseTokenSymbol
item.token_price = tokenPrice
item.market_cap = marketCap
item.timestamp = new Date()
await item.save();
resolve(item);
});
}
export async function removeToken(_id) {
return new Promise(async (resolve, reject) => {
Token.findByIdAndDelete(new ObjectId(_id)).then(async () => {
resolve(true);
});
});
}
export async function removeTokenByUser(chatid) {
return new Promise(async (resolve, reject) => {
Token.deleteMany({chatid}).then(async (result) => {
resolve(result);
});
});
}
export const selectGainerFrom = (pairAddress, from) => {
return new Promise(async (resolve, reject) => {
GainerHistory.find({pair_address: pairAddress, timestamp: {$gte: from}}).sort({timestamp: 1}).limit(1).then(async (gainer) => {
if (gainer && gainer.length > 0)
resolve(gainer[0]);
else
resolve(null);
});
});
}
export const selectGainerBetween = async (pairAddress, fromTime, toTime) => {
return new Promise(async (resolve, reject) => {
if (!fromTime || !toTime) {
resolve(null);
return
}
try {
let from = null, to = null
let gainers1 = await GainerHistory.find({pair_address: pairAddress, timestamp: {$gte: fromTime, $lte: toTime}}).sort({timestamp: -1}).limit(1)
if (gainers1 && gainers1.length > 0)
from = gainers1[0];
else {
resolve(null);
return
}
let gainers2 = await GainerHistory.find({pair_address: pairAddress, timestamp: {$gte: fromTime, $lte: toTime}}).sort({timestamp: 1}).limit(1)
if (gainers2 && gainers2.length > 0)
to = gainers2[0];
else {
resolve(null);
return
}
resolve({from, to})
} catch (error) {
afx.error_log('selectGainerBetween', error)
resolve(null);
}
})
}
export const selectGainerLatest = (pairAddress) => {
return new Promise(async (resolve, reject) => {
GainerHistory.find({pair_address: pairAddress}).sort({timestamp: -1}).limit(1).then(async (gainer) => {
if (gainer && gainer.length > 0)
resolve(gainer[0]);
else
resolve(null);
});
});
}
export async function addCallHistory(chatid, messageId, tokenAddress, pairAddress, content0, content1) {
//console.log(tokenAddress, tokenSymbol, pairAddress, pairBaseTokenSymbol, tokenPrice, marketCap)
return new Promise(async (resolve, reject) => {
let item = new CallHistory();
item.chatid = chatid;
item.messageId = messageId;
item.token_address = tokenAddress;
item.pair_address = pairAddress;
item.content0 = content0;
item.content1 = content1;
item.timestamp = new Date()
await item.save();
resolve(item);
});
}
export async function addTokenReport(address, name, symbol, decimal) {
return new Promise(async (resolve, reject) => {
TokenReport.findOne({address}).then(async (token) => {
if (!token) {
token = new TokenReport();
}
token.address = address.toLowerCase();
token.name = name;
token.symbol = symbol;
token.decimal = decimal;
await token.save();
resolve(token);
});
});
}
export async function selectTokenReports(params = {}) {
return new Promise(async (resolve, reject) => {
User.find(params).then(async (users) => {
resolve(users);
});
});
}
export async function addAutoTradeToken(chatid, address, name, symbol, decimal, price) {
return new Promise(async (resolve, reject) => {
AutoTradeToken.findOne({chatid, address}).then(async (token) => {
if (!token) {
token = new AutoTradeToken();
}
token.chatid = chatid;
token.address = address.toLowerCase();
token.name = name;
token.symbol = symbol;
token.decimal = decimal;
token.price = price
await token.save();
resolve(token);
});
});
}
export async function getAutoTradeTokens(chatid) {
return new Promise(async (resolve, reject) => {
AutoTradeToken.find({chatid}).then(async (tokens) => {
resolve(tokens);
});
});
}
export async function selectAutoTradeTokens(params = {}) {
return new Promise(async (resolve, reject) => {
AutoTradeToken.find(params).then(async (users) => {
resolve(users);
});
});
}
export async function removeAutoTradeToken(_id) {
return new Promise(async (resolve, reject) => {
AutoTradeToken.findByIdAndDelete(new ObjectId(_id)).then(async () => {
resolve(true);
});
});
}
export async function removeAutoTradeTokensByUser(chatid) {
return new Promise(async (resolve, reject) => {
AutoTradeToken.deleteMany({chatid}).then(async (result) => {
resolve(result);
});
});
}