forked from hummingbot/condor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
534 lines (442 loc) · 17.5 KB
/
main.py
File metadata and controls
534 lines (442 loc) · 17.5 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
import logging
import importlib
import sys
import os
import asyncio
from pathlib import Path
from telegram import Update, BotCommand, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
Application,
CommandHandler,
CallbackQueryHandler,
ContextTypes,
PicklePersistence,
)
from handlers.portfolio import portfolio_command, get_portfolio_callback_handler
from handlers.bots import bots_command, bots_callback_handler
from handlers.cex import trade_command, cex_callback_handler
from handlers.dex import swap_command, lp_command, dex_callback_handler
from handlers.config import config_command, get_config_callback_handler, get_modify_value_handler
from handlers.routines import routines_command, routines_callback_handler
from handlers import clear_all_input_states
from utils.auth import restricted
from utils.config import TELEGRAM_TOKEN
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def _get_start_menu_keyboard() -> InlineKeyboardMarkup:
"""Build the start menu inline keyboard."""
keyboard = [
[
InlineKeyboardButton("📊 Portfolio", callback_data="start:portfolio"),
InlineKeyboardButton("🤖 Bots", callback_data="start:bots"),
],
[
InlineKeyboardButton("💱 Swap", callback_data="start:swap"),
InlineKeyboardButton("📊 Trade", callback_data="start:trade"),
InlineKeyboardButton("💧 LP", callback_data="start:lp"),
],
[
InlineKeyboardButton("🔌 Servers", callback_data="start:config_servers"),
InlineKeyboardButton("🔑 Keys", callback_data="start:config_keys"),
InlineKeyboardButton("🌐 Gateway", callback_data="start:config_gateway"),
],
[
InlineKeyboardButton("❓ Help", callback_data="start:help"),
],
]
return InlineKeyboardMarkup(keyboard)
def _get_help_keyboard() -> InlineKeyboardMarkup:
"""Build the help menu inline keyboard."""
keyboard = [
[
InlineKeyboardButton("📊 Portfolio", callback_data="help:portfolio"),
InlineKeyboardButton("🤖 Bots", callback_data="help:bots"),
],
[
InlineKeyboardButton("💱 Swap", callback_data="help:swap"),
InlineKeyboardButton("📊 Trade", callback_data="help:trade"),
InlineKeyboardButton("💧 LP", callback_data="help:lp"),
],
[
InlineKeyboardButton("⚙️ Config", callback_data="help:config"),
],
[
InlineKeyboardButton("🔙 Back to Menu", callback_data="help:back"),
],
]
return InlineKeyboardMarkup(keyboard)
HELP_TEXTS = {
"main": r"""
❓ *Help \- Command Guide*
Select a command below to learn more about its features and usage:
📊 *Portfolio* \- View holdings and performance
🤖 *Bots* \- Monitor trading bot status
💱 *Swap* \- Quick token swaps via DEX
📊 *Trade* \- Order book trading \(CEX/CLOB\)
💧 *LP* \- Liquidity pool management
⚙️ *Config* \- System configuration
""",
"portfolio": r"""
📊 *Portfolio Command*
View your complete portfolio summary across all connected accounts\.
*Features:*
• Real\-time balance overview by account
• PnL tracking with historical charts
• Holdings breakdown by asset
• Multi\-connector aggregation
*Usage:*
• Tap the button or type `/portfolio`
• Use ⚙️ Settings to adjust the time period \(1d, 3d, 7d, 30d\)
• View performance graphs and detailed breakdowns
*Tips:*
• Connect multiple accounts via Config to see aggregated portfolio
• PnL is calculated based on your configured time window
""",
"bots": r"""
🤖 *Bots Command*
Monitor the status of all your active trading bots\.
*Features:*
• View all running bot instances
• Check bot health and uptime
• See active strategies per bot
• Monitor trading activity
*Usage:*
• Tap the button or type `/bots`
• View the status of each connected bot
• Check which strategies are currently active
*Tips:*
• Ensure your API servers are properly configured in Config
• Bots must be running on connected Hummingbot instances
""",
"trade": r"""
📊 *Trade Command*
Trade on Central Limit Order Book exchanges \(Spot \& Perpetual\)\.
*Features:*
• Place market and limit orders
• Set leverage for perpetual trading
• View and manage open orders
• Monitor positions with PnL
• Quick account switching
*Usage:*
• Tap the button or type `/trade`
• Select an account and connector
• Use the menu to place orders or view positions
*Order Types:*
• 📝 *Place Order* \- Submit new orders
• ⚙️ *Set Leverage* \- Adjust perpetual leverage
• 🔍 *Orders Details* \- View/cancel open orders
• 📊 *Positions Details* \- Monitor active positions
*Tips:*
• Always verify the selected account before trading
• Use limit orders for better price control
""",
"swap": r"""
💱 *Swap Command*
Quick token swaps on Decentralized Exchanges via Gateway\.
*Features:*
• Token swaps with real\-time quotes
• Multiple DEX router support
• Slippage configuration
• Swap history with status tracking
*Usage:*
• Tap the button or type `/swap`
• Select network and token pair
• Get quote and execute
*Operations:*
• 💰 *Quote* \- Get swap price estimates
• ✅ *Execute* \- Execute token swaps
• 🔍 *History* \- View past swaps
*Tips:*
• Always check quotes before executing swaps
• Gateway must be running for DEX operations
""",
"lp": r"""
💧 *LP Command*
Manage liquidity positions on CLMM pools\.
*Features:*
• View LP positions with PnL
• Collect fees from positions
• Add/close positions
• Pool explorer with GeckoTerminal
• OHLCV charts and pool analytics
*Usage:*
• Tap the button or type `/lp`
• View your positions or explore pools
• Manage fees and positions
*Operations:*
• 📍 *Positions* \- View and manage LP positions
• 📋 *Pools* \- Browse available pools
• 🦎 *Explorer* \- GeckoTerminal pool discovery
• 📊 *Charts* \- View pool OHLCV data
*Tips:*
• Monitor V/TVL ratio for pool activity
• Check APR and fee tiers before adding liquidity
""",
"config": r"""
⚙️ *Config Command*
Configure your trading infrastructure and credentials\.
*Sections:*
🔌 *API Servers*
• Add/remove Hummingbot instances
• Configure connection endpoints
• Test server connectivity
🔑 *API Keys*
• Manage exchange credentials
• Add new exchange API keys
• Securely store credentials
🌐 *Gateway*
• Configure Gateway container
• Set up DEX chain connections
• Manage wallet credentials
*Usage:*
• Tap the button or type `/config`
• Select the section you want to configure
• Follow the prompts to add or modify settings
*Tips:*
• Keep your API keys secure
• Test connections after adding new servers
• Gateway is required for DEX trading
""",
}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Start the conversation and display the main menu."""
from utils.config import AUTHORIZED_USERS
chat_id = update.effective_chat.id
user_id = update.effective_user.id
username = update.effective_user.username or "No username"
# Check if user is authorized
if user_id not in AUTHORIZED_USERS:
reply_text = rf"""
🔒 *Access Restricted*
You are not authorized to use this bot\.
🆔 *Your Chat Info*:
📱 Chat ID: `{chat_id}`
👤 User ID: `{user_id}`
Share this information with the bot administrator to request access\.
"""
await update.message.reply_text(reply_text, parse_mode="MarkdownV2")
return
# Clear all pending input states to prevent interference
clear_all_input_states(context)
reply_text = rf"""
🚀 *Welcome to Condor\!* 🦅
Manage your trading bots efficiently and monitor their performance\.
🆔 *Your Chat Info*:
📱 Chat ID: `{chat_id}`
👤 User ID: `{user_id}`
🏷️ Username: `@{username}`
Select a command below to get started:
"""
keyboard = _get_start_menu_keyboard()
await update.message.reply_text(reply_text, parse_mode="MarkdownV2", reply_markup=keyboard)
@restricted
async def start_callback_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle callbacks from the start menu."""
query = update.callback_query
await query.answer()
data = query.data
action = data.split(":")[1] if ":" in data else data
# Handle navigation to commands
if data.startswith("start:"):
if action == "portfolio":
await portfolio_command(update, context)
elif action == "bots":
await bots_command(update, context)
elif action == "trade":
await trade_command(update, context)
elif action == "swap":
await swap_command(update, context)
elif action == "lp":
await lp_command(update, context)
elif action == "config_servers":
from handlers.config.servers import show_api_servers
from handlers import clear_all_input_states
clear_all_input_states(context)
await show_api_servers(query, context)
elif action == "config_keys":
from handlers.config.api_keys import show_api_keys
from handlers import clear_all_input_states
clear_all_input_states(context)
await show_api_keys(query, context)
elif action == "config_gateway":
from handlers.config.gateway import show_gateway_menu
from handlers import clear_all_input_states
clear_all_input_states(context)
context.user_data.pop("dex_state", None)
context.user_data.pop("cex_state", None)
await show_gateway_menu(query, context)
elif action == "help":
await query.edit_message_text(
HELP_TEXTS["main"],
parse_mode="MarkdownV2",
reply_markup=_get_help_keyboard()
)
# Handle help submenu
elif data.startswith("help:"):
if action == "back":
# Go back to main start menu
chat_id = update.effective_chat.id
user_id = update.effective_user.id
username = update.effective_user.username or "No username"
reply_text = rf"""
🚀 *Welcome to Condor\!* 🦅
Manage your trading bots efficiently and monitor their performance\.
🆔 *Your Chat Info*:
📱 Chat ID: `{chat_id}`
👤 User ID: `{user_id}`
🏷️ Username: `@{username}`
Select a command below to get started:
"""
await query.edit_message_text(
reply_text,
parse_mode="MarkdownV2",
reply_markup=_get_start_menu_keyboard()
)
elif action in HELP_TEXTS:
# Show specific help with back button
keyboard = [[InlineKeyboardButton("🔙 Back to Help", callback_data="start:help")]]
await query.edit_message_text(
HELP_TEXTS[action],
parse_mode="MarkdownV2",
reply_markup=InlineKeyboardMarkup(keyboard)
)
def reload_handlers():
"""Reload all handler modules."""
modules_to_reload = [
'handlers.portfolio',
'handlers.bots',
'handlers.bots.menu',
'handlers.bots.controllers',
'handlers.bots._shared',
'handlers.cex',
'handlers.cex.menu',
'handlers.cex.trade',
'handlers.cex.orders',
'handlers.cex.positions',
'handlers.cex._shared',
'handlers.dex',
'handlers.dex.menu',
'handlers.dex.swap_quote',
'handlers.dex.swap_execute',
'handlers.dex.swap_history',
'handlers.dex.pools',
'handlers.dex._shared',
'handlers.config',
'handlers.config.servers',
'handlers.config.api_keys',
'handlers.config.gateway',
'handlers.config.user_preferences',
'handlers.routines',
'routines.base',
'utils.auth',
'utils.telegram_formatters',
]
for module_name in modules_to_reload:
if module_name in sys.modules:
importlib.reload(sys.modules[module_name])
logger.info(f"Reloaded module: {module_name}")
def register_handlers(application: Application) -> None:
"""Register all command handlers."""
# Import fresh versions after reload
from handlers.portfolio import portfolio_command, get_portfolio_callback_handler
from handlers.bots import bots_command, bots_callback_handler
from handlers.cex import trade_command, cex_callback_handler
from handlers.dex import swap_command, lp_command, dex_callback_handler
from handlers.config import config_command, get_config_callback_handler, get_modify_value_handler
from handlers.routines import routines_command, routines_callback_handler
# Clear existing handlers
application.handlers.clear()
# Add command handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("portfolio", portfolio_command))
application.add_handler(CommandHandler("bots", bots_command))
application.add_handler(CommandHandler("swap", swap_command))
application.add_handler(CommandHandler("trade", trade_command))
application.add_handler(CommandHandler("lp", lp_command))
application.add_handler(CommandHandler("config", config_command))
application.add_handler(CommandHandler("routines", routines_command))
# Add callback query handler for start menu navigation
application.add_handler(CallbackQueryHandler(start_callback_handler, pattern="^(start:|help:)"))
# Add callback query handlers for trading operations
application.add_handler(CallbackQueryHandler(cex_callback_handler, pattern="^cex:"))
application.add_handler(CallbackQueryHandler(dex_callback_handler, pattern="^dex:"))
application.add_handler(CallbackQueryHandler(bots_callback_handler, pattern="^bots:"))
application.add_handler(CallbackQueryHandler(routines_callback_handler, pattern="^routines:"))
# Add callback query handler for portfolio settings
application.add_handler(get_portfolio_callback_handler())
# Add callback query handler for config menu
application.add_handler(get_config_callback_handler())
# Add UNIFIED message handler for ALL text input
# This single handler routes to: CLOB trading, DEX trading, and Config flows
# based on context state. This avoids issues with multiple MessageHandlers
# competing for the same filter.
application.add_handler(get_modify_value_handler())
logger.info("Handlers registered successfully")
async def post_init(application: Application) -> None:
"""Register bot commands after initialization."""
commands = [
BotCommand("start", "Welcome message and quick commands overview"),
BotCommand("portfolio", "View detailed portfolio breakdown by account and connector"),
BotCommand("bots", "Check status of all active trading bots"),
BotCommand("swap", "Quick token swaps via DEX routers"),
BotCommand("trade", "Order book trading (CEX/CLOB) with limit orders"),
BotCommand("lp", "Liquidity pool management and explorer"),
BotCommand("config", "Configure API servers and credentials"),
BotCommand("routines", "Run configurable Python scripts"),
]
await application.bot.set_my_commands(commands)
# Start file watcher
asyncio.create_task(watch_and_reload(application))
async def watch_and_reload(application: Application) -> None:
"""Watch for file changes and reload handlers automatically."""
try:
from watchfiles import awatch
except ImportError:
logger.warning("watchfiles not installed. Auto-reload disabled. Install with: pip install watchfiles")
return
handlers_path = Path(__file__).parent / "handlers"
routines_path = Path(__file__).parent / "routines"
logger.info(f"👀 Watching for changes in: {handlers_path}, {routines_path}")
async for changes in awatch(handlers_path, routines_path):
logger.info(f"📝 Detected changes: {changes}")
try:
reload_handlers()
register_handlers(application)
logger.info("✅ Auto-reloaded handlers successfully")
except Exception as e:
logger.error(f"❌ Auto-reload failed: {e}", exc_info=True)
def get_persistence() -> PicklePersistence:
"""
Build a persistence object that works both locally and in Docker.
- Uses an env var override if provided.
- Defaults to <project_root>/condor_bot_data.pickle.
- Ensures the parent directory exists, but does NOT create the file.
"""
base_dir = Path(__file__).parent
default_path = base_dir / "condor_bot_data.pickle"
persistence_path = Path(os.getenv("CONDOR_PERSISTENCE_FILE", default_path))
# Make sure the directory exists; the file will be created by PTB
persistence_path.parent.mkdir(parents=True, exist_ok=True)
return PicklePersistence(filepath=persistence_path)
def main() -> None:
"""Run the bot."""
# Setup persistence to save user data, chat data, and bot data
# This will save trading context, last used parameters, etc.
persistence = get_persistence()
# Create the Application with persistence enabled
application = (
Application.builder()
.token(TELEGRAM_TOKEN)
.persistence(persistence)
.post_init(post_init)
.build()
)
# Register all handlers
register_handlers(application)
# Run the bot
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()