1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import logging
- from telegram import Update
- from telegram.ext import ContextTypes
- from .base import InfoCommandsBase
- from src.utils.token_display_formatter import get_formatter
- from src.config.config import Config
- logger = logging.getLogger(__name__)
- class PriceCommands(InfoCommandsBase):
- """Handles all price-related commands."""
- async def price_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
- """Handle the /price command to show price information."""
- if not self._is_authorized(update):
- return
- # Determine token first to use in the loading message
- token = context.args[0].upper() if context.args else Config.DEFAULT_TRADING_TOKEN
- # Send a loading message first
- sent_message = await self._reply(update, f"⏳ Fetching price for {token}...")
- if not sent_message:
- logger.error("Failed to send initial message in price_command.")
- return
- try:
- # Construct the full market symbol
- symbol = f"{token}/USDC:USDC"
- market_data = await self.trading_engine.get_market_data(symbol)
-
- if not market_data or not market_data.get('ticker'):
- await context.bot.edit_message_text(
- chat_id=sent_message.chat.id,
- message_id=sent_message.message_id,
- text=f"❌ Could not fetch price data for {symbol}"
- )
- return
- ticker = market_data['ticker']
- formatter = get_formatter()
- # Format price information
- price_text_parts = [f"💰 <b>{token} Price Information</b>"]
- # Current price
- current_price = float(ticker.get('last') or 0)
- price_text_parts.append(f"\n📊 <b>Current Price:</b> {await formatter.format_price_with_symbol(current_price, token)}")
- # 24h change
- change_24h = float(ticker.get('percentage') or 0)
- change_emoji = "🟢" if change_24h >= 0 else "🔴"
- price_text_parts.append(f"📈 <b>24h Change:</b> {change_emoji} {change_24h:+.2f}%")
- # 24h high/low
- high_24h = float(ticker.get('high') or 0)
- low_24h = float(ticker.get('low') or 0)
- price_text_parts.append(f"📊 <b>24h Range:</b> {await formatter.format_price_with_symbol(low_24h, token)} - {await formatter.format_price_with_symbol(high_24h, token)}")
- # Volume
- volume_24h = float(ticker.get('quoteVolume') or 0)
- price_text_parts.append(f"💎 <b>24h Volume:</b> {await formatter.format_price_with_symbol(volume_24h, token)}")
- final_message = "\n".join(price_text_parts)
- await context.bot.edit_message_text(
- chat_id=sent_message.chat.id,
- message_id=sent_message.message_id,
- text=final_message,
- parse_mode='HTML'
- )
- except Exception as e:
- error_message = f"❌ Error processing price command: {str(e)}"
- logger.error(f"Error in price command: {e}", exc_info=True)
- if sent_message:
- await context.bot.edit_message_text(
- chat_id=sent_message.chat.id,
- message_id=sent_message.message_id,
- text=error_message
- )
|