price.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import logging
  2. from telegram import Update
  3. from telegram.ext import ContextTypes
  4. from .base import InfoCommandsBase
  5. from src.utils.token_display_formatter import get_formatter
  6. from src.config.config import Config
  7. logger = logging.getLogger(__name__)
  8. class PriceCommands(InfoCommandsBase):
  9. """Handles all price-related commands."""
  10. async def price_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  11. """Handle the /price command to show price information."""
  12. if not self._is_authorized(update):
  13. return
  14. # Determine token first to use in the loading message
  15. token = context.args[0].upper() if context.args else Config.DEFAULT_TRADING_TOKEN
  16. # Send a loading message first
  17. sent_message = await self._reply(update, f"⏳ Fetching price for {token}...")
  18. if not sent_message:
  19. logger.error("Failed to send initial message in price_command.")
  20. return
  21. try:
  22. # Construct the full market symbol
  23. symbol = f"{token}/USDC:USDC"
  24. market_data = await self.trading_engine.get_market_data(symbol)
  25. if not market_data or not market_data.get('ticker'):
  26. await context.bot.edit_message_text(
  27. chat_id=sent_message.chat.id,
  28. message_id=sent_message.message_id,
  29. text=f"❌ Could not fetch price data for {symbol}"
  30. )
  31. return
  32. ticker = market_data['ticker']
  33. formatter = get_formatter()
  34. # Format price information
  35. price_text_parts = [f"💰 <b>{token} Price Information</b>"]
  36. # Current price
  37. current_price = float(ticker.get('last') or 0)
  38. price_text_parts.append(f"\n📊 <b>Current Price:</b> {await formatter.format_price_with_symbol(current_price, token)}")
  39. # 24h change
  40. change_24h = float(ticker.get('percentage') or 0)
  41. change_emoji = "🟢" if change_24h >= 0 else "🔴"
  42. price_text_parts.append(f"📈 <b>24h Change:</b> {change_emoji} {change_24h:+.2f}%")
  43. # 24h high/low
  44. high_24h = float(ticker.get('high') or 0)
  45. low_24h = float(ticker.get('low') or 0)
  46. 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)}")
  47. # Volume
  48. volume_24h = float(ticker.get('quoteVolume') or 0)
  49. price_text_parts.append(f"💎 <b>24h Volume:</b> {await formatter.format_price_with_symbol(volume_24h, token)}")
  50. final_message = "\n".join(price_text_parts)
  51. await context.bot.edit_message_text(
  52. chat_id=sent_message.chat.id,
  53. message_id=sent_message.message_id,
  54. text=final_message,
  55. parse_mode='HTML'
  56. )
  57. except Exception as e:
  58. error_message = f"❌ Error processing price command: {str(e)}"
  59. logger.error(f"Error in price command: {e}", exc_info=True)
  60. if sent_message:
  61. await context.bot.edit_message_text(
  62. chat_id=sent_message.chat.id,
  63. message_id=sent_message.message_id,
  64. text=error_message
  65. )