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"š° {token} Price Information"]
# Current price
current_price = float(ticker.get('last') or 0)
price_text_parts.append(f"\nš Current Price: {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"š 24h Change: {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"š 24h Range: {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"š 24h Volume: {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
)