-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
136 lines (118 loc) · 3.49 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
import { Telegraf } from "telegraf";
import { config } from "dotenv";
import { message } from "telegraf/filters";
import Dbservice from "./db/index.js";
import { getPools } from "./utils.js";
import fs from "fs";
config();
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN, {
handlerTimeout: Infinity,
});
let chain = "";
let version = "v2"; // Default version can be set to "v2" or "v3"
// Generate keyboard for the main menu
const getKeyboardCommands = () => {
const commandList = {
inline_keyboard: [
[
{ text: "Start query", callback_data: "query" },
// { text: "Fetch tokens", callback_data: "fetch" },
],
],
};
return commandList;
};
// Generate keyboard for chain selection
const getChainKeyboardCommands = () => {
const commandList = {
inline_keyboard: [
[
{ text: "BSC", callback_data: "chain:bsc" },
{ text: "Polygon", callback_data: "chain:polygon" },
{ text: "Base", callback_data: "chain:base" },
],
],
};
return commandList;
};
// Start command - welcomes the user
bot.start((ctx) => {
const commandList = getKeyboardCommands();
const options = {
reply_markup: commandList,
};
ctx.reply("Welcome to TokenFinder!", options);
});
// Action for 'Start query' - prompting user to choose a chain
bot.action("query", async (ctx) => {
const commandList = getChainKeyboardCommands();
const options = {
reply_markup: commandList,
};
ctx.reply("Choose your chain and version", options);
});
// Action for 'Fetch tokens' - prompting user to choose a chain
bot.action("fetch", async (ctx) => {
const commandList = getChainKeyboardCommands();
const options = {
reply_markup: commandList,
};
ctx.reply("Choose your chain", options);
});
// Handle chain selection for querying pools
bot.action(/^chain:(bsc|polygon|base)$/, async (ctx) => {
const query = ctx.update.callback_query.data;
chain = query.split(":")[1];
const commandList = {
inline_keyboard: [
[
{ text: "V2", callback_data: "version:v2" },
{ text: "V3", callback_data: "version:v3" },
],
],
};
const options = {
reply_markup: commandList,
};
ctx.reply(
`You selected ${chain.toUpperCase()}, now choose the version`,
options
);
});
// Handle version selection
bot.action(/^version:(v2|v3)$/, async (ctx) => {
version = ctx.callbackQuery.data.split(":")[1];
const message =
`You selected version ${version}. Now, please input the start and end date in this format \n` +
`Example: startDate=2024-01-01, endDate=2024-05-05`;
return ctx.reply(message);
});
// Handle date input and start the token pool query
bot.on(message("text"), async (ctx) => {
const date = ctx.message.text
.trim()
.split(",")
.reduce((obj, param) => {
const [key, value] = param.trim().split("=");
obj[key] = value;
return obj;
}, {});
if (date.startDate && date.endDate) {
await getPools(date.startDate, date.endDate, chain, version, ctx);
} else {
await ctx.reply(
"Please ensure your dates are in the correct format: `startDate=YYYY-MM-DD, endDate=YYYY-MM-DD`."
);
}
});
// Utility sleep function to wait for a few seconds
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Launch the bot and connect to the database
bot.launch();
Dbservice.connect();
console.log("Running TokenFinder");
// Graceful shutdown
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));