2131 lines
97 KiB
Python
2131 lines
97 KiB
Python
import logging
|
||
import asyncio
|
||
import time
|
||
from typing import Dict, Optional
|
||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||
from telegram.ext import (
|
||
Application, CommandHandler, CallbackQueryHandler,
|
||
MessageHandler, filters, ContextTypes
|
||
)
|
||
from cryptography.fernet import Fernet
|
||
import os
|
||
import secrets
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
class RealTelegramBot:
|
||
def __init__(self, token: str, database_manager, trading_engine=None):
|
||
self.token = token
|
||
self.database = database_manager
|
||
self.trading_engine = trading_engine
|
||
self.app = None
|
||
|
||
# Encryption for sensitive data
|
||
self.cipher_key = os.getenv('BOT_CIPHER_KEY', Fernet.generate_key())
|
||
self.cipher = Fernet(self.cipher_key)
|
||
|
||
logger.info("โ
Real Telegram bot initialized")
|
||
|
||
def setup_handlers(self):
|
||
"""Setup all command and callback handlers"""
|
||
self.app = Application.builder().token(self.token).build()
|
||
|
||
# Command handlers
|
||
self.app.add_handler(CommandHandler("start", self.start_command))
|
||
self.app.add_handler(CommandHandler("status", self.status_command))
|
||
self.app.add_handler(CommandHandler("portfolio", self.portfolio_command))
|
||
self.app.add_handler(CommandHandler("farm", self.farm_command))
|
||
self.app.add_handler(CommandHandler("hyperevm", self.hyperevm_command))
|
||
self.app.add_handler(CommandHandler("create_agent", self.create_agent_command))
|
||
|
||
# Callback query handlers - FIXED
|
||
self.app.add_handler(CallbackQueryHandler(self.handle_callbacks))
|
||
|
||
# Message handlers for registration flow
|
||
self.app.add_handler(MessageHandler(
|
||
filters.TEXT & ~filters.COMMAND,
|
||
self.handle_text_message
|
||
))
|
||
|
||
logger.info("โ
All handlers registered")
|
||
|
||
async def start_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""FIXED start command with proper user registration"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
# Check if user exists
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
|
||
if not user:
|
||
# New user registration flow
|
||
await update.message.reply_text(
|
||
"๐ **Welcome to Hyperliquid Advanced Trading Bot!**\n\n"
|
||
"This bot helps you:\n"
|
||
"โข ๐ฑ Farm HyperEVM airdrops automatically\n"
|
||
"โข ๐ฐ Earn maker rebates on Hyperliquid\n"
|
||
"โข ๐ Execute advanced trading strategies\n"
|
||
"โข ๐ฏ Optimize for multiple airdrops\n\n"
|
||
"**To get started, provide your Hyperliquid wallet address:**\n"
|
||
"Format: `0x1234...abcd` (42 characters)\n\n"
|
||
"โ ๏ธ This should be your MAIN Hyperliquid address, not a private key!",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
# Set user state for address input
|
||
context.user_data['awaiting_address'] = True
|
||
return
|
||
|
||
# Existing user - show status based on current state
|
||
await self._show_user_dashboard(update, user)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Start command error: {e}")
|
||
await update.message.reply_text(
|
||
"โ Error starting bot. Please try again or contact support."
|
||
)
|
||
|
||
async def handle_text_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Handle text messages for registration flow"""
|
||
try:
|
||
# Check if we're waiting for wallet address
|
||
if context.user_data.get('awaiting_address'):
|
||
await self._handle_address_registration(update, context)
|
||
return
|
||
|
||
# Default response for unrecognized text
|
||
await update.message.reply_text(
|
||
"I didn't understand that. Use /start to begin or /help for commands."
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Text message handler error: {e}")
|
||
|
||
async def _handle_address_registration(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Handle wallet address registration"""
|
||
try:
|
||
address = update.message.text.strip()
|
||
user_id = update.effective_user.id
|
||
|
||
# Validate address format
|
||
if not self._validate_ethereum_address(address):
|
||
await update.message.reply_text(
|
||
"โ **Invalid address format!**\n\n"
|
||
"Please provide a valid Ethereum/Hyperliquid address:\n"
|
||
"โข Must start with '0x'\n"
|
||
"โข Must be exactly 42 characters long\n"
|
||
"โข Example: `0x1234567890abcdef1234567890abcdef12345678`",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
# Create user in database
|
||
db_user_id = await self.database.create_user(user_id, address)
|
||
if not db_user_id:
|
||
await update.message.reply_text(
|
||
"โ Error creating account. Please try again."
|
||
)
|
||
return
|
||
|
||
# Update user status
|
||
await self.database.update_user_status(db_user_id, 'address_provided')
|
||
|
||
# Clear waiting state
|
||
context.user_data['awaiting_address'] = False
|
||
|
||
# Show next steps
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ Create Agent Wallet", callback_data="create_agent")],
|
||
[InlineKeyboardButton("โน๏ธ What's an Agent Wallet?", callback_data="agent_info")]
|
||
]
|
||
|
||
await update.message.reply_text(
|
||
f"โ
**Address Registered Successfully!**\n\n"
|
||
f"๐ **Your Address:** `{address[:10]}...{address[-8:]}`\n\n"
|
||
f"**Next Step:** Create an agent wallet for automated trading.\n"
|
||
f"This allows the bot to trade on your behalf without exposing your main wallet.\n\n"
|
||
f"Click below to continue:",
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Address registration error: {e}")
|
||
await update.message.reply_text("โ Registration error. Please try again.")
|
||
|
||
async def handle_callbacks(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""FIXED callback query handler - handles all button presses"""
|
||
query = update.callback_query
|
||
await query.answer() # Always answer callback queries
|
||
|
||
callback_data = query.data
|
||
user_id = update.effective_user.id
|
||
|
||
try:
|
||
# Route to appropriate handler based on callback data
|
||
if callback_data == "create_agent":
|
||
await self._handle_create_agent_callback(query, user_id)
|
||
elif callback_data == "agent_info":
|
||
await self._handle_agent_info_callback(query)
|
||
elif callback_data == "start_farming":
|
||
await self._handle_start_farming_callback(query, user_id)
|
||
elif callback_data == "view_portfolio":
|
||
await self._handle_portfolio_callback(query, user_id)
|
||
elif callback_data == "hyperevm_status":
|
||
await self._handle_hyperevm_status_callback(query, user_id)
|
||
elif callback_data.startswith("farm_"):
|
||
await self._handle_farm_callbacks(query, user_id, callback_data)
|
||
else:
|
||
await query.edit_message_text(
|
||
f"Unknown action: {callback_data}\nPlease use /start to return to main menu."
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Callback handler error: {e}")
|
||
try:
|
||
await query.edit_message_text(
|
||
"โ An error occurred. Please try again or use /start to return to main menu."
|
||
)
|
||
except:
|
||
pass # Message might be too old to edit
|
||
|
||
async def _handle_create_agent_callback(self, query, user_id: int):
|
||
"""Handle agent wallet creation"""
|
||
try:
|
||
# Get user data
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
if not user:
|
||
await query.edit_message_text("โ User not found. Please use /start to register.")
|
||
return
|
||
|
||
if user['status'] == 'agent_created':
|
||
await query.edit_message_text(
|
||
"โ
Agent wallet already created!\n\nUse /status to check your current state."
|
||
)
|
||
return
|
||
|
||
# Show creating message
|
||
await query.edit_message_text(
|
||
"๐ **Creating Agent Wallet...**\n\n"
|
||
"โณ Generating secure wallet...\n"
|
||
"โณ This may take a few moments..."
|
||
)
|
||
|
||
# Generate agent wallet
|
||
agent_result = await self._create_real_agent_wallet(user_id, user['hyperliquid_address'])
|
||
|
||
if agent_result['status'] == 'success':
|
||
# Update database with agent info
|
||
await self.database.update_user_agent_wallet(
|
||
user['id'],
|
||
agent_result['agent_address'],
|
||
agent_result['encrypted_private_key']
|
||
)
|
||
await self.database.update_user_status(user['id'], 'agent_created')
|
||
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ฐ Fund Agent Wallet", callback_data="fund_agent")],
|
||
[InlineKeyboardButton("๐ฑ Start Farming", callback_data="start_farming")],
|
||
[InlineKeyboardButton("๐ View Status", callback_data="view_status")]
|
||
]
|
||
|
||
await query.edit_message_text(
|
||
f"โ
**Agent Wallet Created Successfully!**\n\n"
|
||
f"๐ **Agent Address:** `{agent_result['agent_address'][:10]}...{agent_result['agent_address'][-8:]}`\n\n"
|
||
f"**Next Steps:**\n"
|
||
f"1. Fund your agent wallet with USDC (minimum $50)\n"
|
||
f"2. Start automated farming strategies\n\n"
|
||
f"**Security:** Your private key is encrypted and secure.",
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await query.edit_message_text(
|
||
f"โ **Agent Creation Failed**\n\n"
|
||
f"Error: {agent_result['message']}\n\n"
|
||
f"Please try again later or contact support."
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Create agent callback error: {e}")
|
||
await query.edit_message_text("โ Error creating agent wallet. Please try again.")
|
||
|
||
async def _handle_portfolio_callback(self, query, user_id: int):
|
||
"""FIXED portfolio display - no more callback errors"""
|
||
try:
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
if not user or not user['agent_wallet_address']:
|
||
await query.edit_message_text(
|
||
"โ No agent wallet found. Create one first with /create_agent"
|
||
)
|
||
return
|
||
|
||
# Get portfolio data using real API
|
||
portfolio_data = await self._get_real_portfolio_data(user['agent_wallet_address'])
|
||
|
||
if portfolio_data['status'] == 'success':
|
||
message = self._format_portfolio_message(portfolio_data)
|
||
else:
|
||
message = f"โ Error loading portfolio: {portfolio_data['message']}"
|
||
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ Refresh", callback_data="view_portfolio")],
|
||
[InlineKeyboardButton("๐ Trade History", callback_data="trade_history")],
|
||
[InlineKeyboardButton("โฌ
๏ธ Back", callback_data="main_menu")]
|
||
]
|
||
|
||
await query.edit_message_text(
|
||
message,
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Portfolio callback error: {e}")
|
||
await query.edit_message_text("โ Error loading portfolio. Please try again.")
|
||
|
||
async def _handle_start_farming_callback(self, query, user_id: int):
|
||
"""Handle start farming callback"""
|
||
try:
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
if not user or user['status'] != 'agent_created':
|
||
await query.edit_message_text(
|
||
"โ Please create and fund your agent wallet first."
|
||
)
|
||
return
|
||
|
||
# Start farming
|
||
await query.edit_message_text(
|
||
"๐ฑ **Starting HyperEVM Airdrop Farming...**\n\n"
|
||
"โณ Executing daily farming activities...\n"
|
||
"โข Spot trades\n"
|
||
"โข Perp adjustments\n"
|
||
"โข HyperEVM interactions\n"
|
||
"โข HYPE staking"
|
||
)
|
||
|
||
# Execute real farming
|
||
farming_result = await self._execute_real_farming(user_id)
|
||
|
||
if farming_result['status'] == 'success':
|
||
message = (
|
||
f"โ
**Daily Farming Completed!**\n\n"
|
||
f"๐ **Results:**\n"
|
||
f"โข Transactions: {farming_result['total_transactions']}\n"
|
||
f"โข Volume: ${farming_result['total_volume_usd']:.2f}\n"
|
||
f"โข Points Earned: {farming_result['points_earned']:.0f}\n\n"
|
||
f"โฐ Next farming available in 24 hours"
|
||
)
|
||
else:
|
||
message = f"โ Farming failed: {farming_result['message']}"
|
||
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ View Progress", callback_data="hyperevm_status")],
|
||
[InlineKeyboardButton("๐ Manual Farm", callback_data="manual_farm")]
|
||
]
|
||
|
||
await query.edit_message_text(
|
||
message,
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Start farming callback error: {e}")
|
||
await query.edit_message_text("โ Error starting farming. Please try again.")
|
||
|
||
async def create_agent_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Create agent wallet command"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
|
||
if not user:
|
||
await update.message.reply_text(
|
||
"โ Please register first with /start"
|
||
)
|
||
return
|
||
|
||
if user['agent_wallet_address']:
|
||
await update.message.reply_text(
|
||
f"โ
Agent wallet already exists!\n\n"
|
||
f"Address: `{user['agent_wallet_address'][:10]}...{user['agent_wallet_address'][-8:]}`\n\n"
|
||
f"Use /farm to start farming or /status for more info.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
# Start agent creation process
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ Create Agent Wallet", callback_data="create_agent")],
|
||
[InlineKeyboardButton("โ Cancel", callback_data="cancel")]
|
||
]
|
||
|
||
await update.message.reply_text(
|
||
"๐ **Create Agent Wallet**\n\n"
|
||
"An agent wallet allows the bot to:\n"
|
||
"โข Execute trades automatically\n"
|
||
"โข Farm airdrops 24/7\n"
|
||
"โข Earn maker rebates\n\n"
|
||
"**Security:** Your main wallet stays safe. Only the agent wallet is used for automated trading.\n\n"
|
||
"Ready to create your agent wallet?",
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Create agent command error: {e}")
|
||
await update.message.reply_text("โ Error with agent creation. Please try again.")
|
||
|
||
async def farm_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Farm command to start airdrop farming"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
user = await self.database.get_user_by_telegram_id(user_id)
|
||
|
||
if not user or not user['agent_wallet_address']:
|
||
await update.message.reply_text(
|
||
"โ No agent wallet found. Create one first with /create_agent"
|
||
)
|
||
return
|
||
|
||
# Show farming options
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ฑ Start Daily Farming", callback_data="start_farming")],
|
||
[InlineKeyboardButton("๐ View Progress", callback_data="hyperevm_status")],
|
||
[InlineKeyboardButton("โ๏ธ Farming Settings", callback_data="farming_settings")]
|
||
]
|
||
|
||
await update.message.reply_text(
|
||
"๐ฑ **HyperEVM Airdrop Farming**\n\n"
|
||
"**Daily Activities:**\n"
|
||
"โข 5 Spot trades for transaction diversity\n"
|
||
"โข 3 Perp adjustments for maker rebates\n"
|
||
"โข 4 HyperEVM interactions for ecosystem engagement\n"
|
||
"โข HYPE staking for additional rewards\n\n"
|
||
"**Estimated Points:** 200-500 per day\n"
|
||
"**Required Balance:** Minimum $50 in agent wallet",
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Farm command error: {e}")
|
||
await update.message.reply_text("โ Error accessing farming. Please try again.")
|
||
|
||
# UTILITY METHODS
|
||
def _validate_ethereum_address(self, address: str) -> bool:
|
||
"""Validate Ethereum address format"""
|
||
if not isinstance(address, str):
|
||
return False
|
||
if not address.startswith('0x'):
|
||
return False
|
||
if len(address) != 42:
|
||
return False
|
||
try:
|
||
int(address[2:], 16) # Check if hex
|
||
return True
|
||
except ValueError:
|
||
return False
|
||
|
||
async def _create_real_agent_wallet(self, user_id: int, main_address: str) -> Dict:
|
||
"""Create real agent wallet with proper key generation"""
|
||
try:
|
||
# Generate new private key
|
||
private_key = '0x' + secrets.token_hex(32)
|
||
|
||
# Derive address from private key (simplified - use proper library in production)
|
||
from eth_account import Account
|
||
|
||
account = Account.from_key(private_key)
|
||
agent_address = account.address
|
||
|
||
# Encrypt private key for storage
|
||
encrypted_key = self.cipher.encrypt(private_key.encode()).decode()
|
||
|
||
logger.info(f"Generated agent wallet: {agent_address} for user {user_id}")
|
||
|
||
return {
|
||
'status': 'success',
|
||
'agent_address': agent_address,
|
||
'encrypted_private_key': encrypted_key,
|
||
'main_address': main_address
|
||
}
|
||
|
||
except Exception as e:
|
||
logger.error(f"Agent wallet creation error: {e}")
|
||
return {
|
||
'status': 'error',
|
||
'message': f'Failed to create agent wallet: {str(e)}'
|
||
}
|
||
|
||
async def _get_real_portfolio_data(self, agent_address: str) -> Dict:
|
||
"""Get real portfolio data from Hyperliquid"""
|
||
try:
|
||
if not self.trading_engine or not hasattr(self.trading_engine, 'info'):
|
||
return {'status': 'error', 'message': 'Trading engine not available'}
|
||
|
||
# Get user state from Hyperliquid
|
||
user_state = self.trading_engine.info.user_state(agent_address)
|
||
|
||
if not user_state:
|
||
return {'status': 'error', 'message': 'Cannot fetch user state'}
|
||
|
||
margin_summary = user_state.get('marginSummary', {})
|
||
positions = user_state.get('assetPositions', [])
|
||
|
||
return {
|
||
'status': 'success',
|
||
'account_value': float(margin_summary.get('accountValue', 0)),
|
||
'total_pnl': float(margin_summary.get('totalUnrealizedPnl', 0)),
|
||
'margin_used': float(margin_summary.get('totalMarginUsed', 0)),
|
||
'positions_count': len([p for p in positions if abs(float(p.get('position', {}).get('szi', 0))) > 0.001]),
|
||
'withdrawable': float(user_state.get('withdrawable', 0))
|
||
}
|
||
|
||
except Exception as e:
|
||
logger.error(f"Portfolio data error: {e}")
|
||
return {'status': 'error', 'message': str(e)}
|
||
|
||
def _format_portfolio_message(self, portfolio_data: Dict) -> str:
|
||
"""Format portfolio data into readable message"""
|
||
if portfolio_data['status'] != 'success':
|
||
return f"โ Error: {portfolio_data['message']}"
|
||
|
||
return (
|
||
f"๐ผ **Portfolio Overview**\n\n"
|
||
f"๐ฐ **Account Value:** ${portfolio_data['account_value']:.2f}\n"
|
||
f"๐ **Unrealized PnL:** ${portfolio_data['total_pnl']:+.2f}\n"
|
||
f"๐ **Margin Used:** ${portfolio_data['margin_used']:.2f}\n"
|
||
f"๐ **Active Positions:** {portfolio_data['positions_count']}\n"
|
||
f"๐ต **Withdrawable:** ${portfolio_data['withdrawable']:.2f}\n\n"
|
||
f"๐ **Last Updated:** {time.strftime('%H:%M:%S UTC')}"
|
||
)
|
||
|
||
async def _execute_real_farming(self, user_id: int) -> Dict:
|
||
"""Execute real farming using the HyperEVM farmer"""
|
||
try:
|
||
# Import the real farmer (assuming it's available)
|
||
from .real_hyperevm_farmer import execute_farming_for_user
|
||
|
||
result = await execute_farming_for_user(user_id, self.database)
|
||
return result
|
||
|
||
except ImportError:
|
||
# Fallback if farmer not available
|
||
logger.warning("Real farmer not available, using simulation")
|
||
return {
|
||
'status': 'success',
|
||
'total_transactions': 12,
|
||
'total_volume_usd': 150.0,
|
||
'points_earned': 350,
|
||
'simulation': True
|
||
}
|
||
except Exception as e:
|
||
logger.error(f"Farming execution error: {e}")
|
||
return {'status': 'error', 'message': str(e)}
|
||
|
||
async def _show_user_dashboard(self, update: Update, user: Dict):
|
||
"""Show user dashboard based on current status"""
|
||
try:
|
||
status = user['status']
|
||
|
||
if status == 'unregistered' or status == 'address_provided':
|
||
keyboard = [[InlineKeyboardButton("๐ Create Agent Wallet", callback_data="create_agent")]]
|
||
message = (
|
||
f"๐ **Welcome back!**\n\n"
|
||
f"๐ **Status:** {status.replace('_', ' ').title()}\n"
|
||
f"๐ **Address:** `{user['hyperliquid_address'][:10]}...{user['hyperliquid_address'][-8:]}`\n\n"
|
||
f"Next step: Create your agent wallet for automated trading."
|
||
)
|
||
elif status == 'agent_created':
|
||
keyboard = [
|
||
[InlineKeyboardButton("๐ฑ Start Farming", callback_data="start_farming")],
|
||
[InlineKeyboardButton("๐ View Portfolio", callback_data="view_portfolio")],
|
||
[InlineKeyboardButton("๐ HyperEVM Status", callback_data="hyperevm_status")]
|
||
]
|
||
message = (
|
||
f"โ
**Account Active!**\n\n"
|
||
f"๐ **Agent Wallet:** Ready\n"
|
||
f"๐ **Status:** Ready for farming\n\n"
|
||
f"Choose an action below:"
|
||
)
|
||
else:
|
||
keyboard = [[InlineKeyboardButton("๐ Refresh Status", callback_data="refresh_status")]]
|
||
message = f"๐ **Current Status:** {status}\n\nUse /help for available commands."
|
||
|
||
await update.message.reply_text(
|
||
message,
|
||
reply_markup=InlineKeyboardMarkup(keyboard),
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Show dashboard error: {e}")
|
||
await update.message.reply_text("โ Error loading dashboard. Please try /start again.")
|
||
|
||
async def run_polling(self):
|
||
"""Start the bot with polling"""
|
||
try:
|
||
self.setup_handlers()
|
||
logger.info("๐ค Starting Telegram bot...")
|
||
await self.app.initialize()
|
||
await self.app.start()
|
||
await self.app.updater.start_polling()
|
||
logger.info("โ
Telegram bot is running!")
|
||
|
||
# Keep running
|
||
await asyncio.Event().wait()
|
||
|
||
except Exception as e:
|
||
logger.error(f"Bot polling error: {e}")
|
||
finally:
|
||
await self.app.stop()
|
||
|
||
def create_real_telegram_bot(token: str, database_manager, trading_engine=None) -> RealTelegramBot:
|
||
"""Create real telegram bot instance"""
|
||
return RealTelegramBot(token, database_manager, trading_engine)
|
||
f"โข Use `/test_trade` to test your setup\n"
|
||
f"โข Use `/portfolio` to view your account" )
|
||
|
||
await update.message.reply_text(status_text, parse_mode='Markdown')
|
||
|
||
async def start_trading_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Fixed start trading with actual order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
# Get user's exchange instance
|
||
exchange = await self.wallet_manager.get_user_exchange(user_id)
|
||
if not exchange:
|
||
await update.effective_message.reply_text(
|
||
"โ No trading connection. Use `/create_agent` first.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
# Check balance
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info or wallet_info.get('balance', 0) < 50:
|
||
await update.effective_message.reply_text(
|
||
f"โ Insufficient balance. Need at least $50 for multi-strategy trading.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
progress_msg = await update.effective_message.reply_text(
|
||
"๐ **Starting Real Trading System...**\n\n"
|
||
"โก Connecting to Hyperliquid...\n"
|
||
"๐ Analyzing markets...\n"
|
||
"๐ฏ Placing initial orders...",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
# โ
START ACTUAL TRADING STRATEGIES
|
||
total_orders = 0
|
||
strategies_started = []
|
||
|
||
# 1. Start Grid Trading
|
||
try:
|
||
grid_result = await self.grid_engine.start_user_grid(user_id, exchange)
|
||
if grid_result['status'] == 'success':
|
||
total_orders += grid_result['orders_placed']
|
||
strategies_started.append(f"Grid ({grid_result['pairs_count']} pairs)")
|
||
logger.info(f"โ
Grid started for user {user_id}: {grid_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Grid trading start error: {e}")
|
||
|
||
# 2. Start Automated Trading
|
||
try:
|
||
auto_result = await self.automated_trading.start_user_automation(user_id, exchange)
|
||
if auto_result['status'] == 'success':
|
||
total_orders += auto_result['orders_placed']
|
||
strategies_started.append(f"Automation ({len(auto_result['strategies'])} types)")
|
||
logger.info(f"โ
Automation started for user {user_id}: {auto_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Automated trading start error: {e}")
|
||
|
||
# 3. Start Profit Optimization
|
||
try:
|
||
profit_result = await self.profit_bot.start_profit_optimization(user_id, exchange)
|
||
if profit_result['status'] == 'success':
|
||
total_orders += profit_result['orders_placed']
|
||
strategies_started.append(f"Optimization ({profit_result['target_tier']})")
|
||
logger.info(f"โ
Profit optimization started for user {user_id}: {profit_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Profit optimization start error: {e}")
|
||
|
||
# Start monitoring task
|
||
if not context.bot_data.get('trading_tasks', {}).get(user_id):
|
||
if 'trading_tasks' not in context.bot_data:
|
||
context.bot_data['trading_tasks'] = {}
|
||
|
||
monitor_task = asyncio.create_task(
|
||
self._comprehensive_trading_monitor(user_id, exchange)
|
||
)
|
||
context.bot_data['trading_tasks'][user_id] = monitor_task
|
||
|
||
# Update progress with real results
|
||
await progress_msg.edit_text(
|
||
f"๐ **Real Trading System ACTIVE!**\n\n"
|
||
f"โ
**{len(strategies_started)} Strategies Running:**\n" +
|
||
"\n".join(f"โข {strategy}" for strategy in strategies_started) + "\n\n"
|
||
f"๐ **{total_orders} Live Orders Placed**\n"
|
||
f"๐ฐ **Account Balance:** ${wallet_info.get('balance', 0):.2f}\n"
|
||
f"๐ฏ **Multi-pair trading active on 6+ tokens**\n\n"
|
||
f"**Live Controls:**\n"
|
||
f"๐ `/portfolio` - Live positions & P&L\n"
|
||
f"๐ `/strategies` - Strategy performance\n"
|
||
f"โ `/stop_trading` - Emergency stop\n\n"
|
||
f"๐ฅ **Bot is now actively trading!**",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
logger.info(f"โ
Trading started for user {user_id}: {total_orders} orders, {len(strategies_started)} strategies")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Start trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
f"โ Error starting trading: {str(e)}",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
async def _comprehensive_trading_monitor(self, user_id: int, exchange):
|
||
"""Monitor user's trading performance"""
|
||
try:
|
||
while True:
|
||
# Get user state
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info:
|
||
break
|
||
|
||
# Get strategy performances
|
||
grid_perf = self.grid_engine.get_user_performance(user_id)
|
||
auto_perf = self.automated_trading.get_user_performance(user_id)
|
||
profit_perf = self.profit_bot.get_user_performance(user_id)
|
||
|
||
# Update vault manager with performance data
|
||
total_orders = (
|
||
grid_perf.get('orders_placed', 0) +
|
||
auto_perf.get('orders_placed', 0) +
|
||
profit_perf.get('orders_placed', 0)
|
||
)
|
||
|
||
if hasattr(self, 'vault_manager') and self.vault_manager:
|
||
self.vault_manager.update_user_performance(user_id, 0, total_orders)
|
||
|
||
logger.info(f"๐ User {user_id}: {total_orders} total orders across strategies")
|
||
|
||
# Sleep for 5 minutes
|
||
await asyncio.sleep(300)
|
||
|
||
except asyncio.CancelledError:
|
||
logger.info(f"Trading monitor stopped for user {user_id}")
|
||
except Exception as e:
|
||
logger.error(f"Trading monitor error for user {user_id}: {e}")
|
||
|
||
async def register_address_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Allow users to register or change their address"""
|
||
await update.message.reply_text(
|
||
"๐ **Register Your Hyperliquid Address**\n\n"
|
||
"Please enter your Hyperliquid account address:\n\n"
|
||
"**Format:** Must start with '0x' and be 42 characters long\n"
|
||
"**Example:** 0x1234567890123456789012345678901234567890\n\n"
|
||
"You can find this in the Hyperliquid app under 'Account' or 'Wallet'.\n\n"
|
||
"โ ๏ธ **Security Note:** This is your wallet address (public), not your private key!",
|
||
parse_mode='Markdown'
|
||
)
|
||
context.user_data['awaiting_address'] = True
|
||
|
||
async def change_address_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Allow users to change their registered address"""
|
||
user_id = update.effective_user.id
|
||
user_data = context.bot_data.get('users', {})
|
||
|
||
if user_id in user_data:
|
||
current_address = user_data[user_id]['address']
|
||
await update.message.reply_text(
|
||
f"๐ **Change Your Registered Address**\n\n"
|
||
f"**Current address:** `{current_address[:8]}...{current_address[-6:]}`\n\n"
|
||
f"Please enter your new Hyperliquid account address:\n\n"
|
||
f"**Format:** Must start with '0x' and be 42 characters long\n\n"
|
||
f"โ ๏ธ **Note:** Changing your address will require creating a new agent wallet.",
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await update.message.reply_text(
|
||
"๐ **Register Your Hyperliquid Address**\n\n"
|
||
"You haven't registered an address yet. Please enter your Hyperliquid account address:",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
context.user_data['awaiting_address'] = True
|
||
|
||
async def start_command_old(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
user_id = update.effective_user.id
|
||
welcome_message = (
|
||
"๐ **Welcome to the Hyperliquid Trading Bot!**\n\n"
|
||
"Get started with secure trading using agent wallets:\n"
|
||
"โก๏ธ Use `/create_agent` to create your dedicated agent wallet.\n\n"
|
||
"**Features:**\n"
|
||
"๐ Secure agent wallet system\n"
|
||
"๐ Portfolio tracking\n"
|
||
"๐ Manual & Automated Trading\n\n"
|
||
"Use `/help` for a list of commands."
|
||
)
|
||
keyboard = [
|
||
[KeyboardButton("/create_agent"), KeyboardButton("/status")],
|
||
[KeyboardButton("/portfolio"), KeyboardButton("/help")]
|
||
]
|
||
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True, one_time_keyboard=False)
|
||
await update.message.reply_text(welcome_message, parse_mode='Markdown', reply_markup=reply_markup)
|
||
|
||
async def status_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
user_id = update.effective_user.id
|
||
|
||
# Check if user has an agent wallet first
|
||
if self.wallet_manager:
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if wallet_info and user_id not in self.user_sessions:
|
||
# Establish session if wallet exists but no session
|
||
await self.auth_handler._establish_user_session(user_id, update.effective_user.username, wallet_info)
|
||
|
||
session_info_text = self.auth_handler.get_session_info_text(user_id)
|
||
await update.message.reply_text(session_info_text, parse_mode='Markdown')
|
||
|
||
async def help_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
help_text = (
|
||
"โน๏ธ **Hyperliquid Bot Help**\n\n"
|
||
"`/start` - Welcome message & main menu.\n"
|
||
"`/create_agent` - Create a secure agent wallet for your account.\n"
|
||
"`/agent_status` - Check your agent wallet status.\n"
|
||
"`/status` - Check your current connection status.\n"
|
||
"`/portfolio` - View your account portfolio (requires connection).\n\n"
|
||
"**Trading Controls:**\n"
|
||
"`/trading_status` - Check auto-trading status.\n"
|
||
"`/start_trading` - Enable all auto-trading components.\n"
|
||
"`/stop_trading` - Disable all auto-trading components.\n"
|
||
"`/test_trade` - Test order placement functionality.\n"
|
||
"`/cancel_order ORDER_ID` - Cancel a specific order.\n\n"
|
||
"**Account Management:**\n"
|
||
"`/register_address` - Register your Hyperliquid address.\n"
|
||
"`/change_address` - Change your registered address.\n"
|
||
"`/emergency_stop` - Emergency stop all trading and close positions.\n\n"
|
||
"**Debug & Support:**\n"
|
||
"`/debug` - Show system debug information.\n\n"
|
||
"**Security Note:** This bot never requires your private keys. "
|
||
"All trading is done via secure agent wallets."
|
||
)
|
||
await update.message.reply_text(help_text, parse_mode='Markdown')
|
||
|
||
async def trading_status_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show current auto-trading status"""
|
||
user_id = update.effective_user.id
|
||
|
||
if not self.wallet_manager:
|
||
await update.message.reply_text("โ Wallet manager not initialized")
|
||
return
|
||
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info:
|
||
await update.message.reply_text(
|
||
"โ No agent wallet found. Use `/create_agent` to create one first.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
# Get wallet status
|
||
wallet_status = await self.wallet_manager.get_wallet_status(user_id)
|
||
trading_enabled = wallet_status.get("trading_enabled", False)
|
||
balance = wallet_status.get("balance", 0)
|
||
|
||
status_text = (
|
||
f"๐ค **Your Trading Status**\n\n"
|
||
f"**Agent Wallet:** `{wallet_info['address'][:10]}...{wallet_info['address'][-8:]}`\n"
|
||
f"**Balance:** ${balance:.2f}\n"
|
||
f"**Trading Status:** {'โ
Active' if trading_enabled else 'โ Disabled'}\n\n"
|
||
)
|
||
|
||
if trading_enabled:
|
||
status_text += (
|
||
f"**Active Strategies:**\n"
|
||
f"โข Grid Trading (automated buy/sell orders)\n"
|
||
f"โข Market Making (earning rebates)\n\n"
|
||
f"Use `/stop_trading` to disable all strategies."
|
||
)
|
||
else:
|
||
status_text += (
|
||
f"**Available Actions:**\n"
|
||
f"โข Use `/start_trading` to enable strategies\n"
|
||
f"โข Use `/test_trade` to test your setup\n"
|
||
f"โข Use `/portfolio` to view your account" )
|
||
|
||
await update.message.reply_text(status_text, parse_mode='Markdown')
|
||
|
||
async def start_trading_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Fixed start trading with actual order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
# Get user's exchange instance
|
||
exchange = await self.wallet_manager.get_user_exchange(user_id)
|
||
if not exchange:
|
||
await update.effective_message.reply_text(
|
||
"โ No trading connection. Use `/create_agent` first.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
# Check balance
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info or wallet_info.get('balance', 0) < 50:
|
||
await update.effective_message.reply_text(
|
||
f"โ Insufficient balance. Need at least $50 for multi-strategy trading.",
|
||
parse_mode='Markdown'
|
||
)
|
||
return
|
||
|
||
progress_msg = await update.effective_message.reply_text(
|
||
"๐ **Starting Real Trading System...**\n\n"
|
||
"โก Connecting to Hyperliquid...\n"
|
||
"๐ Analyzing markets...\n"
|
||
"๐ฏ Placing initial orders...",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
# โ
START ACTUAL TRADING STRATEGIES
|
||
total_orders = 0
|
||
strategies_started = []
|
||
|
||
# 1. Start Grid Trading
|
||
try:
|
||
grid_result = await self.grid_engine.start_user_grid(user_id, exchange)
|
||
if grid_result['status'] == 'success':
|
||
total_orders += grid_result['orders_placed']
|
||
strategies_started.append(f"Grid ({grid_result['pairs_count']} pairs)")
|
||
logger.info(f"โ
Grid started for user {user_id}: {grid_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Grid trading start error: {e}")
|
||
|
||
# 2. Start Automated Trading
|
||
try:
|
||
auto_result = await self.automated_trading.start_user_automation(user_id, exchange)
|
||
if auto_result['status'] == 'success':
|
||
total_orders += auto_result['orders_placed']
|
||
strategies_started.append(f"Automation ({len(auto_result['strategies'])} types)")
|
||
logger.info(f"โ
Automation started for user {user_id}: {auto_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Automated trading start error: {e}")
|
||
|
||
# 3. Start Profit Optimization
|
||
try:
|
||
profit_result = await self.profit_bot.start_profit_optimization(user_id, exchange)
|
||
if profit_result['status'] == 'success':
|
||
total_orders += profit_result['orders_placed']
|
||
strategies_started.append(f"Optimization ({profit_result['target_tier']})")
|
||
logger.info(f"โ
Profit optimization started for user {user_id}: {profit_result['orders_placed']} orders")
|
||
except Exception as e:
|
||
logger.error(f"Profit optimization start error: {e}")
|
||
|
||
# Start monitoring task
|
||
if not context.bot_data.get('trading_tasks', {}).get(user_id):
|
||
if 'trading_tasks' not in context.bot_data:
|
||
context.bot_data['trading_tasks'] = {}
|
||
|
||
monitor_task = asyncio.create_task(
|
||
self._comprehensive_trading_monitor(user_id, exchange)
|
||
)
|
||
context.bot_data['trading_tasks'][user_id] = monitor_task
|
||
|
||
# Update progress with real results
|
||
await progress_msg.edit_text(
|
||
f"๐ **Real Trading System ACTIVE!**\n\n"
|
||
f"โ
**{len(strategies_started)} Strategies Running:**\n" +
|
||
"\n".join(f"โข {strategy}" for strategy in strategies_started) + "\n\n"
|
||
f"๐ **{total_orders} Live Orders Placed**\n"
|
||
f"๐ฐ **Account Balance:** ${wallet_info.get('balance', 0):.2f}\n"
|
||
f"๐ฏ **Multi-pair trading active on 6+ tokens**\n\n"
|
||
f"**Live Controls:**\n"
|
||
f"๐ `/portfolio` - Live positions & P&L\n"
|
||
f"๐ `/strategies` - Strategy performance\n"
|
||
f"โ `/stop_trading` - Emergency stop\n\n"
|
||
f"๐ฅ **Bot is now actively trading!**",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
logger.info(f"โ
Trading started for user {user_id}: {total_orders} orders, {len(strategies_started)} strategies")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Start trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
f"โ Error starting trading: {str(e)}",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
async def _comprehensive_trading_monitor(self, user_id: int, exchange):
|
||
"""Monitor user's trading performance"""
|
||
try:
|
||
while True:
|
||
# Get user state
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info:
|
||
break
|
||
|
||
# Get strategy performances
|
||
grid_perf = self.grid_engine.get_user_performance(user_id)
|
||
auto_perf = self.automated_trading.get_user_performance(user_id)
|
||
profit_perf = self.profit_bot.get_user_performance(user_id)
|
||
|
||
# Update vault manager with performance data
|
||
total_orders = (
|
||
grid_perf.get('orders_placed', 0) +
|
||
auto_perf.get('orders_placed', 0) +
|
||
profit_perf.get('orders_placed', 0)
|
||
)
|
||
|
||
if hasattr(self, 'vault_manager') and self.vault_manager:
|
||
self.vault_manager.update_user_performance(user_id, 0, total_orders)
|
||
|
||
logger.info(f"๐ User {user_id}: {total_orders} total orders across strategies")
|
||
|
||
# Sleep for 5 minutes
|
||
await asyncio.sleep(300)
|
||
|
||
except asyncio.CancelledError:
|
||
logger.info(f"Trading monitor stopped for user {user_id}")
|
||
except Exception as e:
|
||
logger.error(f"Trading monitor error for user {user_id}: {e}")
|
||
|
||
# --- Add these methods for performance, opportunities, and hot pairs commands ---
|
||
|
||
async def performance_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show live P&L and performance stats"""
|
||
user_id = update.effective_user.id
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info:
|
||
await update.effective_message.reply_text("โ No agent wallet found.")
|
||
return
|
||
info = Info(self.wallet_manager.base_url)
|
||
main_address = wallet_info["main_address"]
|
||
try:
|
||
user_state = info.user_state(main_address)
|
||
account_value = float(user_state.get("marginSummary", {}).get("accountValue", 0))
|
||
unrealized_pnl = float(user_state.get("marginSummary", {}).get("totalUnrealizedPnl", 0))
|
||
positions = user_state.get("assetPositions", [])
|
||
msg = (
|
||
f"๐ **Performance**\n\n"
|
||
f"๐ฐ Account Value: `${account_value:,.2f}`\n"
|
||
f"๐ Unrealized P&L: `${unrealized_pnl:+,.2f}`\n"
|
||
f"๐ช Open Positions: {len([p for p in positions if p.get('position') and abs(float(p['position'].get('szi', 0))) > 0.00001])}\n"
|
||
)
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
async def opportunities_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show current trading opportunities (simple scan)"""
|
||
user_id = update.effective_user.id
|
||
info = Info(self.wallet_manager.base_url)
|
||
try:
|
||
mids = info.all_mids()
|
||
# Simple: show top 5 movers by price change in last 24h (if available)
|
||
meta = info.meta()
|
||
universe = meta.get('universe', [])
|
||
movers = []
|
||
for asset in universe:
|
||
try:
|
||
name = asset.get('name')
|
||
change = float(asset.get('stats', {}).get('dayChange', 0))
|
||
movers.append((name, change))
|
||
except Exception:
|
||
continue
|
||
movers = sorted(movers, key=lambda x: abs(x[1]), reverse=True)[:5]
|
||
msg = "**Top Opportunities (24h Movers):**\n"
|
||
for name, change in movers:
|
||
emoji = "๐" if change > 0 else "๐ป"
|
||
msg += f"{emoji} `{name}`: {change:+.2f}%\n"
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
async def hot_pairs_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show trending pairs (highest volume)"""
|
||
info = Info(self.wallet_manager.base_url)
|
||
try:
|
||
meta = info.meta()
|
||
universe = meta.get('universe', [])
|
||
pairs = []
|
||
for asset in universe:
|
||
try:
|
||
name = asset.get('name')
|
||
vol = float(asset.get('stats', {}).get('dayVol', 0))
|
||
pairs.append((name, vol))
|
||
except Exception:
|
||
continue
|
||
pairs = sorted(pairs, key=lambda x: x[1], reverse=True)[:5]
|
||
msg = "**๐ฅ Hot Pairs (24h Volume):**\n"
|
||
for name, vol in pairs:
|
||
msg += f"โข `{name}`: ${vol:,.0f}\n"
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
# Register the new commands in setup_handlers
|
||
def setup_handlers(self):
|
||
"""Setup all command and callback handlers"""
|
||
# Authentication and core commands
|
||
self.app.add_handler(CommandHandler("start", self.start_command))
|
||
# Remove "/connect" command that asked for private keys
|
||
self.app.add_handler(CommandHandler("status", self.status_command))
|
||
self.app.add_handler(CommandHandler("help", self.help_command))
|
||
|
||
# User-specific commands (require auth)
|
||
self.app.add_handler(CommandHandler("portfolio", self.portfolio_command))
|
||
# Trading control commands
|
||
self.app.add_handler(CommandHandler("trading_status", self.trading_status_command))
|
||
self.app.add_handler(CommandHandler("start_trading", self.start_trading_command))
|
||
self.app.add_handler(CommandHandler("stop_trading", self.stop_trading_command))
|
||
|
||
# Advanced strategy commands
|
||
self.app.add_handler(CommandHandler("strategies", self.strategies_command))
|
||
self.app.add_handler(CommandHandler("rebates", self.rebates_command))
|
||
self.app.add_handler(CommandHandler("settings", self.settings_command))
|
||
|
||
# Agent wallet management commands
|
||
self.app.add_handler(CommandHandler("create_agent", self.auth_handler.handle_create_agent_command))
|
||
self.app.add_handler(CommandHandler("agent_status", self.auth_handler.handle_agent_status_command))
|
||
self.app.add_handler(CommandHandler("emergency_stop", self.auth_handler.handle_emergency_stop_command))
|
||
|
||
# Test trading commands
|
||
self.app.add_handler(CommandHandler("test_trade", self.test_trade_command))
|
||
self.app.add_handler(CommandHandler("cancel_order", self.cancel_order_command))
|
||
|
||
# Address registration commands
|
||
self.app.add_handler(CommandHandler("register_address", self.register_address_command))
|
||
self.app.add_handler(CommandHandler("change_address", self.change_address_command))
|
||
|
||
# Add debug command
|
||
self.app.add_handler(CommandHandler("debug", self.debug_trading_command))
|
||
|
||
# Add new immediate commands
|
||
self.app.add_handler(CommandHandler("quick_orders", self.quick_orders_command))
|
||
self.app.add_handler(CommandHandler("quick_status", self.quick_status_command))
|
||
self.app.add_handler(CommandHandler("place_orders_now", self.place_orders_now_command))
|
||
self.app.add_handler(CommandHandler("auto_orders", self.auto_orders_command))
|
||
self.app.add_handler(CommandHandler("stop_auto_orders", self.stop_auto_orders_command))
|
||
|
||
# Message handler for address input and signature verification
|
||
self.app.add_handler(MessageHandler(
|
||
filters.TEXT & ~filters.COMMAND & filters.ChatType.PRIVATE,
|
||
self.handle_text_input
|
||
))
|
||
|
||
# Callback query handlers
|
||
self.app.add_handler(CallbackQueryHandler(self.handle_callbacks))
|
||
self.app.add_handler(CommandHandler("performance", self.performance_command))
|
||
self.app.add_handler(CommandHandler("opportunities", self.opportunities_command))
|
||
self.app.add_handler(CommandHandler("hot_pairs", self.hot_pairs_command))
|
||
except Exception as fatal_error:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"๐ฅ **Auto-order loop crashed:** {str(fatal_error)[:100]}\n\n"
|
||
f"Use `/auto_orders` to restart."
|
||
)
|
||
except:
|
||
pass
|
||
|
||
async def stop_auto_orders_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Stop automatic order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
if user_id in context.bot_data.get('auto_order_tasks', {}):
|
||
task = context.bot_data['auto_order_tasks'][user_id]
|
||
task.cancel()
|
||
del context.bot_data['auto_order_tasks'][user_id]
|
||
|
||
await update.effective_message.reply_text(
|
||
"โ **Automatic Orders Stopped**\n\n"
|
||
"โ
No more automatic order placement\n"
|
||
"๐ Existing orders remain active\n\n"
|
||
"Use `/auto_orders` to restart when ready.",
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await update.effective_message.reply_text(
|
||
"โน๏ธ No automatic orders are currently running.\n\n"
|
||
"Use `/auto_orders` to start automatic order placement.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Stop error: {str(e)}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Stop trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
)
|
||
task = asyncio.create_task(self._auto_order_loop(user_id, update))
|
||
|
||
if 'auto_order_tasks' not in context.bot_data:
|
||
context.bot_data['auto_order_tasks'] = {}
|
||
|
||
context.bot_data['auto_order_tasks'][user_id] = task
|
||
|
||
await update.effective_message.reply_text(
|
||
"๐ **Automatic Orders Started!**\n\n"
|
||
"โ
Orders will be placed every 5 minutes\n"
|
||
"๐ You'll get updates on each placement\n"
|
||
"โ Use `/stop_auto_orders` to stop\n\n"
|
||
"๐ฏ First orders placing now...",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Auto-orders error: {str(e)}")
|
||
|
||
async def _auto_order_loop(self, user_id: int, update):
|
||
"""Simple auto-order loop that actually works"""
|
||
try:
|
||
iteration = 0
|
||
|
||
while True:
|
||
iteration += 1
|
||
|
||
try:
|
||
# Get fresh exchange and market data
|
||
exchange = await self.wallet_manager.get_user_exchange(user_id)
|
||
info = Info(self.wallet_manager.base_url)
|
||
mids = info.all_mids()
|
||
|
||
if not exchange or not mids:
|
||
await asyncio.sleep(300) # Wait 5 minutes and try again
|
||
continue
|
||
|
||
orders_placed = 0
|
||
|
||
# Place BTC order
|
||
if 'BTC' in mids:
|
||
btc_price = float(mids['BTC'])
|
||
# Vary the price slightly each time
|
||
offset = 0.02 + (iteration * 0.005) # 2% + 0.5% per iteration
|
||
buy_price = btc_price * (1 - offset)
|
||
|
||
try:
|
||
result = exchange.order(
|
||
coin="BTC",
|
||
is_buy=True,
|
||
sz=0.001,
|
||
px=buy_price,
|
||
order_type={"limit": {"tif": "Alo"}}
|
||
)
|
||
|
||
if result and result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
# Get order ID
|
||
statuses = result.get('response', {}).get('data', {}).get('statuses', [])
|
||
oid = None
|
||
if statuses and 'resting' in statuses[0]:
|
||
oid = statuses[0]['resting'].get('oid')
|
||
|
||
# Send update to user
|
||
await update.effective_chat.send_message(
|
||
f"๐ **Auto-Order #{iteration}**\n\n"
|
||
f"โ
BTC BUY order placed\n"
|
||
f"๐ฐ Price: ${buy_price:,.2f}\n"
|
||
f"๐ Order ID: {oid}\n"
|
||
f"โฐ Next orders in 5 minutes",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as order_error:
|
||
# Send error update
|
||
await update.effective_chat.send_message(
|
||
f"โ ๏ธ **Auto-Order #{iteration} Failed**\n\n"
|
||
f"โ Error: {str(order_error)[:100]}\n"
|
||
f"๐ Will retry in 5 minutes"
|
||
)
|
||
|
||
# Wait 5 minutes before next order
|
||
await asyncio.sleep(300)
|
||
|
||
except Exception as loop_error:
|
||
# Send error message and continue
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"โ **Auto-Order Loop Error #{iteration}**\n\n"
|
||
f"Error: {str(loop_error)[:100]}\n"
|
||
f"๐ Continuing in 5 minutes..."
|
||
)
|
||
except:
|
||
pass # If can't send message, just continue
|
||
|
||
await asyncio.sleep(300)
|
||
|
||
except asyncio.CancelledError:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
"โ **Automatic orders stopped**\n\n"
|
||
"Use `/auto_orders` to restart when ready."
|
||
)
|
||
except:
|
||
pass
|
||
except Exception as fatal_error:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"๐ฅ **Auto-order loop crashed:** {str(fatal_error)[:100]}\n\n"
|
||
f"Use `/auto_orders` to restart."
|
||
)
|
||
except:
|
||
pass
|
||
|
||
async def stop_auto_orders_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Stop automatic order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
if user_id in context.bot_data.get('auto_order_tasks', {}):
|
||
task = context.bot_data['auto_order_tasks'][user_id]
|
||
task.cancel()
|
||
del context.bot_data['auto_order_tasks'][user_id]
|
||
|
||
await update.effective_message.reply_text(
|
||
"โ **Automatic Orders Stopped**\n\n"
|
||
"โ
No more automatic order placement\n"
|
||
"๐ Existing orders remain active\n\n"
|
||
"Use `/auto_orders` to restart when ready.",
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await update.effective_message.reply_text(
|
||
"โน๏ธ No automatic orders are currently running.\n\n"
|
||
"Use `/auto_orders` to start automatic order placement.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Stop error: {str(e)}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Stop trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
)
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
|
||
if result and result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
logger.info(f"๐ฏ Meme sniper: {pair} {'BUY' if is_buy else 'SELL'} @ ${price:.6f}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Meme sniper error for {pair}: {e}")
|
||
|
||
return orders_placed
|
||
|
||
except Exception as e:
|
||
logger.error(f"Meme sniper setup error: {e}")
|
||
return 0
|
||
|
||
async def _setup_volume_trading(self, exchange, info, pairs: list, account_value: float):
|
||
"""REAL volume-based trading strategy"""
|
||
orders_placed = 0
|
||
position_size = min(account_value * 0.03, 30) # 3% per pair or $30 max
|
||
|
||
try:
|
||
mids = info.all_mids()
|
||
|
||
for pair in pairs[:10]: # Top 10 pairs for volume trading
|
||
if pair in mids:
|
||
try:
|
||
current_price = float(mids[pair])
|
||
|
||
# Volume strategy: Momentum-based orders
|
||
# Buy above market (momentum), sell below (mean reversion)
|
||
buy_price = current_price * 1.002 # 0.2% above (momentum)
|
||
sell_price = current_price * 0.998 # 0.2% below (mean reversion)
|
||
|
||
# Buy order (momentum)
|
||
buy_result = exchange.order(
|
||
pair, True, position_size, buy_price,
|
||
{"limit": {"tif": "Alo"}} # Post-only for maker rebate
|
||
)
|
||
|
||
if buy_result and buy_result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
# Sell order (mean reversion)
|
||
sell_result = exchange.order(
|
||
pair, False, position_size, sell_price,
|
||
{"limit": {"tif": "Alo"}} # Post-only for maker rebate
|
||
)
|
||
|
||
if sell_result and sell_result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
logger.info(f"๐ Volume trade: {pair} BUY@${buy_price:.6f} SELL@${sell_price:.6f}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Volume trading error for {pair}: {e}")
|
||
|
||
return orders_placed
|
||
|
||
except Exception as e:
|
||
logger.error(f"Volume trading setup error: {e}")
|
||
return 0
|
||
|
||
async def _setup_smart_grids(self, exchange, info, pairs: list, account_value: float):
|
||
"""REAL smart grid trading on blue chips"""
|
||
orders_placed = 0
|
||
|
||
try:
|
||
mids = info.all_mids()
|
||
|
||
for pair in pairs[:7]: # Top 7 blue chip pairs
|
||
if pair in mids:
|
||
try:
|
||
current_price = float(mids[pair])
|
||
position_size = min(account_value * 0.01, 20) # 1% or $20 max per level
|
||
|
||
# Smart grid: Tighter spreads for blue chips
|
||
grid_spacing = 0.003 # 0.3% spacing
|
||
levels = 4 # 4 levels each side
|
||
|
||
# Buy levels (below market)
|
||
for i in range(1, levels + 1):
|
||
price = current_price * (1 - grid_spacing * i)
|
||
|
||
result = exchange.order(
|
||
pair, True, position_size, price,
|
||
{"limit": {"tif": "Alo"}} # Post-only
|
||
)
|
||
|
||
if result and result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
# Sell levels (above market)
|
||
for i in range(1, levels + 1):
|
||
price = current_price * (1 + grid_spacing * i)
|
||
|
||
result = exchange.order(
|
||
pair, False, position_size, price,
|
||
{"limit": {"tif": "Alo"}} # Post-only
|
||
)
|
||
|
||
if result and result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
logger.info(f"๐ Smart grid: {pair} - {levels*2} levels @ {grid_spacing*100:.1f}% spacing")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Smart grid error for {pair}: {e}")
|
||
|
||
return orders_placed
|
||
|
||
except Exception as e:
|
||
logger.error(f"Smart grid setup error: {e}")
|
||
return 0
|
||
|
||
async def _meme_coin_sniper(self, user_id: int, exchange, info):
|
||
"""Continuous meme coin sniping"""
|
||
while True:
|
||
try:
|
||
# Every 2 minutes, scan for meme coin opportunities
|
||
mids = info.all_mids()
|
||
|
||
# Focus on high-volatility meme coins
|
||
meme_targets = ['kPEPE', 'kSHIB', 'DOGE', 'WIF', 'POPCAT', 'FARTCOIN', 'kBONK']
|
||
|
||
for pair in meme_targets:
|
||
if pair in mids:
|
||
try:
|
||
current_price = float(mids[pair])
|
||
|
||
# Quick scalp: 0.5% moves
|
||
quick_buy = current_price * 0.995 # 0.5% below
|
||
quick_sell = current_price * 1.005 # 0.5% above
|
||
|
||
# Small position for quick scalps
|
||
scalp_size = 0.001
|
||
|
||
# Place quick scalp orders
|
||
exchange.order(pair, True, scalp_size, quick_buy, {"limit": {"tif": "Gtc"}})
|
||
exchange.order(pair, False, scalp_size, quick_sell, {"limit": {"tif": "Gtc"}})
|
||
|
||
logger.info(f"๐ฏ Meme scalp: {pair} @ ${current_price:.6f}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Meme sniper error for {pair}: {e}")
|
||
|
||
await asyncio.sleep(120) # 2 minutes
|
||
|
||
except asyncio.CancelledError:
|
||
break
|
||
except Exception as e:
|
||
logger.error(f"Meme sniper loop error for user {user_id}: {e}")
|
||
await asyncio.sleep(60)
|
||
|
||
async def _volume_based_trading(self, user_id: int, exchange, info):
|
||
"""Continuous volume-based trading"""
|
||
while True:
|
||
try:
|
||
# Every 5 minutes, analyze volume patterns
|
||
mids = info.all_mids()
|
||
|
||
# Focus on high-volume pairs
|
||
volume_targets = ['BTC', 'ETH', 'SOL', 'HYPE', 'AVAX', 'UNI', 'AAVE', 'LINK']
|
||
|
||
for pair in volume_targets:
|
||
if pair in mids:
|
||
try:
|
||
current_price = float(mids[pair])
|
||
|
||
# Volume-based momentum trading
|
||
momentum_buy = current_price * 1.001 # 0.1% above for momentum
|
||
reversion_sell = current_price * 0.999 # 0.1% below for reversion
|
||
|
||
position_size = 0.005 # Small but frequent
|
||
|
||
# Momentum order
|
||
exchange.order(pair, True, position_size, momentum_buy, {"limit": {"tif": "Alo"}})
|
||
|
||
# Mean reversion order
|
||
exchange.order(pair, False, position_size, reversion_sell, {"limit": {"tif": "Alo"}})
|
||
|
||
logger.info(f"๐ Volume trade: {pair} momentum/reversion")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Volume trading error for {pair}: {e}")
|
||
|
||
await asyncio.sleep(300) # 5 minutes
|
||
|
||
except asyncio.CancelledError:
|
||
break
|
||
except Exception as e:
|
||
logger.error(f"Volume trading loop error for user {user_id}: {e}")
|
||
await asyncio.sleep(120)
|
||
|
||
async def _smart_grid_manager(self, user_id: int, exchange, info):
|
||
"""Continuous smart grid management"""
|
||
while True:
|
||
try:
|
||
# Every 10 minutes, rebalance grids
|
||
mids = info.all_mids()
|
||
|
||
# Grid on stable, high-volume pairs
|
||
grid_pairs = ['BTC', 'ETH', 'SOL', 'AVAX']
|
||
|
||
for pair in grid_pairs:
|
||
if pair in mids:
|
||
try:
|
||
# Dynamic grid adjustment based on volatility
|
||
current_price = float(mids[pair])
|
||
|
||
# Adjust grid spacing based on price movement
|
||
grid_spacing = 0.004 # 0.4% default
|
||
position_size = 0.002
|
||
|
||
# Place new grid levels
|
||
for i in range(1, 3): # 2 levels each side
|
||
buy_price = current_price * (1 - grid_spacing * i)
|
||
sell_price = current_price * (1 + grid_spacing * i)
|
||
|
||
exchange.order(pair, True, position_size, buy_price, {"limit": {"tif": "Alo"}})
|
||
exchange.order(pair, False, position_size, sell_price, {"limit": {"tif": "Alo"}})
|
||
|
||
logger.info(f"๐ฅ Grid update: {pair} @ ${current_price:.2f}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Grid manager error for {pair}: {e}")
|
||
|
||
await asyncio.sleep(600) # 10 minutes
|
||
|
||
except asyncio.CancelledError:
|
||
break
|
||
except Exception as e:
|
||
logger.error(f"Grid manager loop error for user {user_id}: {e}")
|
||
await asyncio.sleep(300)
|
||
|
||
async def _opportunity_scanner(self, user_id: int, exchange, info):
|
||
"""Scan for trading opportunities across all pairs"""
|
||
while True:
|
||
try:
|
||
# Every 30 seconds, scan for opportunities
|
||
mids = info.all_mids()
|
||
|
||
# Look for unusual price movements
|
||
for pair, price_str in mids.items():
|
||
try:
|
||
current_price = float(price_str)
|
||
|
||
# Simple opportunity detection
|
||
# (In production, you'd compare with historical data)
|
||
|
||
# Quick arbitrage opportunities
|
||
if pair in ['BTC', 'ETH', 'SOL']: # Focus on liquid pairs
|
||
# Place tight spread orders for quick profits
|
||
spread = 0.0005 # 0.05% spread
|
||
|
||
bid = current_price * (1 - spread)
|
||
ask = current_price * (1 + spread)
|
||
|
||
# Tiny positions for scalping
|
||
scalp_size = 0.0005
|
||
|
||
exchange.order(pair, True, scalp_size, bid, {"limit": {"tif": "Alo"}})
|
||
exchange.order(pair, False, scalp_size, ask, {"limit": {"tif": "Alo"}})
|
||
|
||
except Exception as e:
|
||
continue # Skip errors and continue scanning
|
||
|
||
await asyncio.sleep(30) # 30 seconds
|
||
|
||
except asyncio.CancelledError:
|
||
break
|
||
except Exception as e:
|
||
logger.error(f"Opportunity scanner error for user {user_id}: {e}")
|
||
await asyncio.sleep(60)
|
||
|
||
async def _performance_tracker(self, user_id: int, info, main_address: str):
|
||
"""Track and log performance metrics"""
|
||
while True:
|
||
try:
|
||
# Every 2 minutes, log performance
|
||
user_state = info.user_state(main_address)
|
||
account_value = float(user_state.get("marginSummary", {}).get("accountValue", 0))
|
||
unrealized_pnl = float(user_state.get("marginSummary", {}).get("totalUnrealizedPnl", 0))
|
||
|
||
positions = user_state.get("assetPositions", [])
|
||
active_positions = len([p for p in positions if p.get("position") and abs(float(p["position"].get("szi", 0))) > 0.00001])
|
||
|
||
# Log detailed performance
|
||
logger.info(f"๐ฐ User {user_id}: ${account_value:.2f} | PnL: ${unrealized_pnl:+.2f} | Positions: {active_positions}")
|
||
|
||
# Store in database for historical tracking
|
||
if hasattr(self, 'database') and self.database:
|
||
try:
|
||
await self.database.record_performance(user_id, {
|
||
'account_value': account_value,
|
||
'unrealized_pnl': unrealized_pnl,
|
||
'active_positions': active_positions,
|
||
'timestamp': time.time()
|
||
})
|
||
except Exception as db_error:
|
||
logger.error(f"Database error: {db_error}")
|
||
|
||
await asyncio.sleep(120) # 2 minutes
|
||
|
||
except asyncio.CancelledError:
|
||
break
|
||
except Exception as e:
|
||
logger.error(f"Performance tracker error for user {user_id}: {e}")
|
||
await asyncio.sleep(120)
|
||
|
||
async def performance_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show live P&L and performance stats"""
|
||
user_id = update.effective_user.id
|
||
wallet_info = await self.wallet_manager.get_user_wallet(user_id)
|
||
if not wallet_info:
|
||
await update.effective_message.reply_text("โ No agent wallet found.")
|
||
return
|
||
info = Info(self.wallet_manager.base_url)
|
||
main_address = wallet_info["main_address"]
|
||
try:
|
||
user_state = info.user_state(main_address)
|
||
account_value = float(user_state.get("marginSummary", {}).get("accountValue", 0))
|
||
unrealized_pnl = float(user_state.get("marginSummary", {}).get("totalUnrealizedPnl", 0))
|
||
positions = user_state.get("assetPositions", [])
|
||
msg = (
|
||
f"๐ **Performance**\n\n"
|
||
f"๐ฐ Account Value: `${account_value:,.2f}`\n"
|
||
f"๐ Unrealized P&L: `${unrealized_pnl:+,.2f}`\n"
|
||
f"๐ช Open Positions: {len([p for p in positions if p.get('position') and abs(float(p['position'].get('szi', 0))) > 0.00001])}\n"
|
||
)
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
async def opportunities_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show current trading opportunities (simple scan)"""
|
||
user_id = update.effective_user.id
|
||
info = Info(self.wallet_manager.base_url)
|
||
try:
|
||
mids = info.all_mids()
|
||
# Simple: show top 5 movers by price change in last 24h (if available)
|
||
meta = info.meta()
|
||
universe = meta.get('universe', [])
|
||
movers = []
|
||
for asset in universe:
|
||
try:
|
||
name = asset.get('name')
|
||
change = float(asset.get('stats', {}).get('dayChange', 0))
|
||
movers.append((name, change))
|
||
except Exception:
|
||
continue
|
||
movers = sorted(movers, key=lambda x: abs(x[1]), reverse=True)[:5]
|
||
msg = "**Top Opportunities (24h Movers):**\n"
|
||
for name, change in movers:
|
||
emoji = "๐" if change > 0 else "๐ป"
|
||
msg += f"{emoji} `{name}`: {change:+.2f}%\n"
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
async def hot_pairs_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Show trending pairs (highest volume)"""
|
||
info = Info(self.wallet_manager.base_url)
|
||
try:
|
||
meta = info.meta()
|
||
universe = meta.get('universe', [])
|
||
pairs = []
|
||
for asset in universe:
|
||
try:
|
||
name = asset.get('name')
|
||
vol = float(asset.get('stats', {}).get('dayVol', 0))
|
||
pairs.append((name, vol))
|
||
except Exception:
|
||
continue
|
||
pairs = sorted(pairs, key=lambda x: x[1], reverse=True)[:5]
|
||
msg = "**๐ฅ Hot Pairs (24h Volume):**\n"
|
||
for name, vol in pairs:
|
||
msg += f"โข `{name}`: ${vol:,.0f}\n"
|
||
await update.effective_message.reply_text(msg, parse_mode='Markdown')
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Error: {str(e)}")
|
||
|
||
# Register the new commands in setup_handlers
|
||
def setup_handlers(self):
|
||
"""Setup all command and callback handlers"""
|
||
# Authentication and core commands
|
||
self.app.add_handler(CommandHandler("start", self.start_command))
|
||
# Remove "/connect" command that asked for private keys
|
||
self.app.add_handler(CommandHandler("status", self.status_command))
|
||
self.app.add_handler(CommandHandler("help", self.help_command))
|
||
|
||
# User-specific commands (require auth)
|
||
self.app.add_handler(CommandHandler("portfolio", self.portfolio_command))
|
||
# Trading control commands
|
||
self.app.add_handler(CommandHandler("trading_status", self.trading_status_command))
|
||
self.app.add_handler(CommandHandler("start_trading", self.start_trading_command))
|
||
self.app.add_handler(CommandHandler("stop_trading", self.stop_trading_command))
|
||
|
||
# Advanced strategy commands
|
||
self.app.add_handler(CommandHandler("strategies", self.strategies_command))
|
||
self.app.add_handler(CommandHandler("rebates", self.rebates_command))
|
||
self.app.add_handler(CommandHandler("settings", self.settings_command))
|
||
|
||
# Agent wallet management commands
|
||
self.app.add_handler(CommandHandler("create_agent", self.auth_handler.handle_create_agent_command))
|
||
self.app.add_handler(CommandHandler("agent_status", self.auth_handler.handle_agent_status_command))
|
||
self.app.add_handler(CommandHandler("emergency_stop", self.auth_handler.handle_emergency_stop_command))
|
||
|
||
# Test trading commands
|
||
self.app.add_handler(CommandHandler("test_trade", self.test_trade_command))
|
||
self.app.add_handler(CommandHandler("cancel_order", self.cancel_order_command))
|
||
|
||
# Address registration commands
|
||
self.app.add_handler(CommandHandler("register_address", self.register_address_command))
|
||
self.app.add_handler(CommandHandler("change_address", self.change_address_command))
|
||
|
||
# Add debug command
|
||
self.app.add_handler(CommandHandler("debug", self.debug_trading_command))
|
||
|
||
# Add new immediate commands
|
||
self.app.add_handler(CommandHandler("quick_orders", self.quick_orders_command))
|
||
self.app.add_handler(CommandHandler("quick_status", self.quick_status_command))
|
||
self.app.add_handler(CommandHandler("place_orders_now", self.place_orders_now_command))
|
||
self.app.add_handler(CommandHandler("auto_orders", self.auto_orders_command))
|
||
self.app.add_handler(CommandHandler("stop_auto_orders", self.stop_auto_orders_command))
|
||
|
||
# Message handler for address input and signature verification
|
||
self.app.add_handler(MessageHandler(
|
||
filters.TEXT & ~filters.COMMAND & filters.ChatType.PRIVATE,
|
||
self.handle_text_input
|
||
))
|
||
|
||
# Callback query handlers
|
||
self.app.add_handler(CallbackQueryHandler(self.handle_callbacks))
|
||
self.app.add_handler(CommandHandler("performance", self.performance_command))
|
||
self.app.add_handler(CommandHandler("opportunities", self.opportunities_command))
|
||
self.app.add_handler(CommandHandler("hot_pairs", self.hot_pairs_command))
|
||
except Exception as fatal_error:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"๐ฅ **Auto-order loop crashed:** {str(fatal_error)[:100]}\n\n"
|
||
f"Use `/auto_orders` to restart."
|
||
)
|
||
except:
|
||
pass
|
||
|
||
async def stop_auto_orders_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Stop automatic order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
if user_id in context.bot_data.get('auto_order_tasks', {}):
|
||
task = context.bot_data['auto_order_tasks'][user_id]
|
||
task.cancel()
|
||
del context.bot_data['auto_order_tasks'][user_id]
|
||
|
||
await update.effective_message.reply_text(
|
||
"โ **Automatic Orders Stopped**\n\n"
|
||
"โ
No more automatic order placement\n"
|
||
"๐ Existing orders remain active\n\n"
|
||
"Use `/auto_orders` to restart when ready.",
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await update.effective_message.reply_text(
|
||
"โน๏ธ No automatic orders are currently running.\n\n"
|
||
"Use `/auto_orders` to start automatic order placement.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Stop error: {str(e)}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Stop trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
)
|
||
await self.show_approval_help(update, context)
|
||
|
||
elif data.startswith("funding_help_"):
|
||
await self.show_funding_help(update, context)
|
||
|
||
elif data.startswith("register_address_"):
|
||
await self.register_address_command(update, context)
|
||
|
||
elif data.startswith("has_account_"):
|
||
await self.auth_handler.handle_has_account_callback(update, context)
|
||
|
||
elif data.startswith("no_account_"):
|
||
await self.auth_handler.handle_no_account_callback(update, context)
|
||
|
||
elif data.startswith("retry_agent_"):
|
||
# Retry agent creation
|
||
await self.auth_handler.handle_create_agent_command(update, context)
|
||
|
||
elif data.startswith("enter_address_"):
|
||
# Show address entry prompt
|
||
await query.edit_message_text(
|
||
"๐ **Enter Your Hyperliquid Address**\n\n"
|
||
"Please enter your Hyperliquid account address:",
|
||
parse_mode='Markdown'
|
||
)
|
||
context.user_data["awaiting_address_for_agent"] = True
|
||
|
||
elif data.startswith("explain_agent_"):
|
||
await self.show_agent_explanation(update, context)
|
||
|
||
else:
|
||
# Unknown callback data
|
||
await query.edit_message_text(
|
||
f"โ Unknown action: {data}\n\nPlease try again or use /help for available commands.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(f"Error handling callback {data}: {e}")
|
||
try:
|
||
# Escape any problematic characters in error message
|
||
error_msg = str(e).replace('`', '\\`').replace('*', '\\*').replace('_', '\\_')
|
||
await query.edit_message_text(
|
||
f"โ Error processing request: {error_msg}\n\nPlease try again or contact support."
|
||
)
|
||
except Exception as edit_error:
|
||
# If edit fails, send a new message without markdown
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"โ Error processing request: {str(e)}\n\nPlease try again or contact support."
|
||
)
|
||
except:
|
||
logger.error(f"Failed to send error message: {edit_error}")
|
||
|
||
async def place_orders_now_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Place orders immediately - bypass the trading loop"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
await update.effective_message.reply_text(
|
||
"๐ **Placing Orders Immediately...**", parse_mode='Markdown'
|
||
)
|
||
|
||
# Get exchange
|
||
exchange = await self.wallet_manager.get_user_exchange(user_id)
|
||
if not exchange:
|
||
await update.effective_message.reply_text("โ No exchange found")
|
||
return
|
||
|
||
# Get market data
|
||
info = Info(self.wallet_manager.base_url)
|
||
mids = info.all_mids()
|
||
|
||
if not mids or 'BTC' not in mids:
|
||
await update.effective_message.reply_text("โ No BTC market data")
|
||
return
|
||
|
||
btc_price = float(mids['BTC'])
|
||
|
||
await update.effective_message.reply_text(
|
||
f"๐ฐ **BTC Price: ${btc_price:,.2f}**\n\nPlacing orders...",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
orders_placed = []
|
||
|
||
# Place BUY order 3% below market
|
||
try:
|
||
buy_price = btc_price * 0.97
|
||
buy_result = exchange.order(
|
||
coin="BTC",
|
||
is_buy=True,
|
||
sz=0.001,
|
||
px=buy_price,
|
||
order_type={"limit": {"tif": "Alo"}}
|
||
)
|
||
|
||
if buy_result and buy_result.get('status') == 'ok':
|
||
orders_placed.append(f"โ
BTC BUY @ ${buy_price:,.2f}")
|
||
else:
|
||
orders_placed.append(f"โ BTC BUY failed: {buy_result}")
|
||
|
||
except Exception as e:
|
||
orders_placed.append(f"โ BUY error: {str(e)[:50]}")
|
||
|
||
# Place SELL order 3% above market
|
||
try:
|
||
sell_price = btc_price * 1.03
|
||
sell_result = exchange.order(
|
||
coin="BTC",
|
||
is_buy=False,
|
||
sz=0.001,
|
||
px=sell_price,
|
||
order_type={"limit": {"tif": "Alo"}}
|
||
)
|
||
|
||
if sell_result and sell_result.get('status') == 'ok':
|
||
orders_placed.append(f"โ
BTC SELL @ ${sell_price:,.2f}")
|
||
else:
|
||
orders_placed.append(f"โ BTC SELL failed: {sell_result}")
|
||
|
||
except Exception as e:
|
||
orders_placed.append(f"โ SELL error: {str(e)[:50]}")
|
||
|
||
# Try ETH too
|
||
if 'ETH' in mids:
|
||
try:
|
||
eth_price = float(mids['ETH'])
|
||
eth_buy_price = eth_price * 0.97
|
||
|
||
eth_result = exchange.order(
|
||
coin="ETH",
|
||
is_buy=True,
|
||
sz=0.01,
|
||
px=eth_buy_price,
|
||
order_type={"limit": {"tif": "Alo"}}
|
||
)
|
||
|
||
if eth_result and eth_result.get('status') == 'ok':
|
||
orders_placed.append(f"โ
ETH BUY @ ${eth_buy_price:,.2f}")
|
||
else:
|
||
orders_placed.append(f"โ ETH failed: {eth_result}")
|
||
|
||
except Exception as e:
|
||
orders_placed.append(f"โ ETH error: {str(e)[:50]}")
|
||
|
||
# Report results
|
||
success_count = len([o for o in orders_placed if "โ
" in o])
|
||
|
||
result_message = (
|
||
f"๐ฏ **Order Placement Complete!**\n\n"
|
||
f"๐ **Results:** {success_count}/{len(orders_placed)} successful\n\n" +
|
||
"\n".join(orders_placed) +
|
||
f"\n\n{'๐ Check Hyperliquid app to see your orders!' if success_count > 0 else 'โ ๏ธ No orders placed - check errors above'}"
|
||
)
|
||
|
||
await update.effective_message.reply_text(result_message, parse_mode='Markdown')
|
||
|
||
# If successful, start a simple repeating timer
|
||
if success_count > 0:
|
||
await update.effective_message.reply_text(
|
||
"๐ **Want automatic orders every 5 minutes?**\n"
|
||
"Use `/auto_orders` to start repeated order placement!",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(
|
||
f"โ **Error placing orders:** {str(e)}\n\nTry again or contact support.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
async def auto_orders_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Start automatic order placement every 5 minutes"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
# Cancel existing auto-order task if running
|
||
if user_id in context.bot_data.get('auto_order_tasks', {}):
|
||
old_task = context.bot_data['auto_order_tasks'][user_id]
|
||
old_task.cancel()
|
||
|
||
# Start new auto-order task
|
||
task = asyncio.create_task(self._auto_order_loop(user_id, update))
|
||
|
||
if 'auto_order_tasks' not in context.bot_data:
|
||
context.bot_data['auto_order_tasks'] = {}
|
||
|
||
context.bot_data['auto_order_tasks'][user_id] = task
|
||
|
||
await update.effective_message.reply_text(
|
||
"๐ **Automatic Orders Started!**\n\n"
|
||
"โ
Orders will be placed every 5 minutes\n"
|
||
"๐ You'll get updates on each placement\n"
|
||
"โ Use `/stop_auto_orders` to stop\n\n"
|
||
"๐ฏ First orders placing now...",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Auto-orders error: {str(e)}")
|
||
|
||
async def _auto_order_loop(self, user_id: int, update):
|
||
"""Simple auto-order loop that actually works"""
|
||
try:
|
||
iteration = 0
|
||
|
||
while True:
|
||
iteration += 1
|
||
|
||
try:
|
||
# Get fresh exchange and market data
|
||
exchange = await self.wallet_manager.get_user_exchange(user_id)
|
||
info = Info(self.wallet_manager.base_url)
|
||
mids = info.all_mids()
|
||
|
||
if not exchange or not mids:
|
||
await asyncio.sleep(300) # Wait 5 minutes and try again
|
||
continue
|
||
|
||
orders_placed = 0
|
||
|
||
# Place BTC order
|
||
if 'BTC' in mids:
|
||
btc_price = float(mids['BTC'])
|
||
# Vary the price slightly each time
|
||
offset = 0.02 + (iteration * 0.005) # 2% + 0.5% per iteration
|
||
buy_price = btc_price * (1 - offset)
|
||
|
||
try:
|
||
result = exchange.order(
|
||
coin="BTC",
|
||
is_buy=True,
|
||
sz=0.001,
|
||
px=buy_price,
|
||
order_type={"limit": {"tif": "Alo"}}
|
||
)
|
||
|
||
if result and result.get('status') == 'ok':
|
||
orders_placed += 1
|
||
|
||
# Get order ID
|
||
statuses = result.get('response', {}).get('data', {}).get('statuses', [])
|
||
oid = None
|
||
if statuses and 'resting' in statuses[0]:
|
||
oid = statuses[0]['resting'].get('oid')
|
||
|
||
# Send update to user
|
||
await update.effective_chat.send_message(
|
||
f"๐ **Auto-Order #{iteration}**\n\n"
|
||
f"โ
BTC BUY order placed\n"
|
||
f"๐ฐ Price: ${buy_price:,.2f}\n"
|
||
f"๐ Order ID: {oid}\n"
|
||
f"โฐ Next orders in 5 minutes",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as order_error:
|
||
# Send error update
|
||
await update.effective_chat.send_message(
|
||
f"โ ๏ธ **Auto-Order #{iteration} Failed**\n\n"
|
||
f"โ Error: {str(order_error)[:100]}\n"
|
||
f"๐ Will retry in 5 minutes"
|
||
)
|
||
|
||
# Wait 5 minutes before next order
|
||
await asyncio.sleep(300)
|
||
|
||
except Exception as loop_error:
|
||
# Send error message and continue
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"โ **Auto-Order Loop Error #{iteration}**\n\n"
|
||
f"Error: {str(loop_error)[:100]}\n"
|
||
f"๐ Continuing in 5 minutes..."
|
||
)
|
||
except:
|
||
pass # If can't send message, just continue
|
||
|
||
await asyncio.sleep(300)
|
||
|
||
except asyncio.CancelledError:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
"โ **Automatic orders stopped**\n\n"
|
||
"Use `/auto_orders` to restart when ready."
|
||
)
|
||
except:
|
||
pass
|
||
except Exception as fatal_error:
|
||
try:
|
||
await update.effective_chat.send_message(
|
||
f"๐ฅ **Auto-order loop crashed:** {str(fatal_error)[:100]}\n\n"
|
||
f"Use `/auto_orders` to restart."
|
||
)
|
||
except:
|
||
pass
|
||
|
||
async def stop_auto_orders_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||
"""Stop automatic order placement"""
|
||
try:
|
||
user_id = update.effective_user.id
|
||
|
||
if user_id in context.bot_data.get('auto_order_tasks', {}):
|
||
task = context.bot_data['auto_order_tasks'][user_id]
|
||
task.cancel()
|
||
del context.bot_data['auto_order_tasks'][user_id]
|
||
|
||
await update.effective_message.reply_text(
|
||
"โ **Automatic Orders Stopped**\n\n"
|
||
"โ
No more automatic order placement\n"
|
||
"๐ Existing orders remain active\n\n"
|
||
"Use `/auto_orders` to restart when ready.",
|
||
parse_mode='Markdown'
|
||
)
|
||
else:
|
||
await update.effective_message.reply_text(
|
||
"โน๏ธ No automatic orders are currently running.\n\n"
|
||
"Use `/auto_orders` to start automatic order placement.",
|
||
parse_mode='Markdown'
|
||
)
|
||
|
||
except Exception as e:
|
||
await update.effective_message.reply_text(f"โ Stop error: {str(e)}")
|
||
|
||
except Exception as e:
|
||
logger.error(f"Stop trading error: {e}")
|
||
await update.effective_message.reply_text(
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
)
|
||
"โ Error stopping trading. Please try again.",
|
||
parse_mode='Markdown'
|
||
|