-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
154 lines (112 loc) · 4.9 KB
/
index.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
import dotenv from 'dotenv';
dotenv.config();
import fetch from 'node-fetch';
import TelegramBot from 'node-telegram-bot-api';
import { EventEmitter } from 'node:events';
import { fetchAuditData, fetchTokenStatistics, formatTokenStatistics, triggerAudit, waitForAuditEndOrError, WAITING_GENERATION_AUDIT_MESSAGE } from '@luckblock/goplus-ai-analyzer-js';
const token = process.env.BOT_TOKEN;
const bot = new TelegramBot(token, {
polling: true
});
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, '🤖 Welcome to the LuckBlock Telegram bot! 🤖\n\n/audit - Full analysis of any erc20 smart contract.\n\n/performance - Track the PnL of any wallet (limited to uniswap v2 during BETA mode)\n\n/block0 - First one in, first one out. The fastest DeFi trading bot, guaranteed.\n\n/register - Register your wallet for air drops, early sniper access and more.');
});
// on /performance or /block0, send coming soon
bot.onText(/\/performance/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Coming soon... 🔒');
});
bot.onText(/\/block0/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Coming soon... 🔒');
});
bot.onText(/\/register/, (msg) => {
const chatId = msg.chat.id;
const [command, ...args] = msg.text.split(' ');
if (!args[0]) {
return bot.sendMessage(chatId, 'Please provide a valid address (e.g. /register 0x1234...)');
}
fetch('https://api.luckblock.io/register/' + args[0], {
method: 'POST'
});
bot.sendMessage(chatId, 'Registered Successfully! ✅');
});
bot.onText(/\/audit/, async (msg, match) => {
const chatId = msg.chat.id;
const [command, ...args] = match.input.split(' ');
const contractAddress = args[0];
if (!contractAddress) {
return bot.sendMessage(chatId, 'Please provide a contract address');
}
const message = await bot.sendMessage(chatId, 'Loading insights...');
const [statistics, initialAuditData] = await Promise.all([
fetchTokenStatistics(contractAddress),
fetchAuditData(contractAddress)
]);
if (!statistics) {
return bot.editMessageText('❌ Oops, something went wrong!', {
message_id: message.message_id,
chat_id: chatId
});
}
const initialAuditIsReady = initialAuditData && initialAuditData.status === 'success';
const statisticsMessage = formatTokenStatistics(statistics, true, initialAuditIsReady ? JSON.parse(initialAuditData?.data) : null);
await bot.editMessageText(statisticsMessage, {
parse_mode: 'MarkdownV2',
message_id: message.message_id,
chat_id: chatId,
disable_web_page_preview: true
});
if (!initialAuditIsReady) {
triggerAudit(contractAddress);
const ee = new EventEmitter();
// subscribe to audit changes
waitForAuditEndOrError(contractAddress, ee);
const auditGenerationMessage = await bot.sendMessage(chatId, `🔍 (audit generation AI) : starting...`);
ee.on('status-update', (status) => {
bot.editMessageText(`🔍 (audit generation AI): ${status}`, {
message_id: auditGenerationMessage.message_id,
chat_id: chatId,
disable_web_page_preview: true
});
});
ee.on('end', (audit) => {
const auditStatisticsMessage = formatTokenStatistics(statistics, true, audit);
bot.deleteMessage(auditGenerationMessage.chat.id, auditGenerationMessage.message_id);
bot.editMessageText(auditStatisticsMessage, {
parse_mode: 'MarkdownV2',
message_id: message.message_id,
chat_id: chatId,
disable_web_page_preview: true
});
});
ee.on('error', (error) => {
const newStatisticsWithoutAudit = statisticsMessage.replace(WAITING_GENERATION_AUDIT_MESSAGE, `[Use our web app](https://app.luckblock.io/audit) to generate the audit report.`);
bot.editMessageText(`❌ Oops, something went wrong! (${error})`, {
message_id: auditGenerationMessage.message_id,
chat_id: chatId,
disable_web_page_preview: true
});
bot.editMessageText(newStatisticsWithoutAudit, {
parse_mode: 'MarkdownV2',
message_id: message.message_id,
chat_id: chatId,
disable_web_page_preview: true
});
});
}
});
console.log(`🤖 luckblock bot is started!`);
function cleanUpServer() {
console.log(`🤖 luckblock bot is stopped!`);
bot.stopPolling({ cancel: true });
process.exit();
}
process.on('uncaughtException', (err) => {
console.error(err);
cleanUpServer();
});
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((eventType) => {
process.on(eventType, cleanUpServer.bind(null, eventType));
});