revert: Keep using price API for price lookups

The price API requires full contract addresses (0x...-bsc format).
Improved error handling and formatting for price responses.
This commit is contained in:
shokollm
2026-04-14 00:12:46 +00:00
parent 62bcd6e099
commit f4f6168f68

View File

@@ -985,27 +985,24 @@ class ConversationalAgent:
} }
def _execute_price(self, token_ids: str) -> Dict[str, Any]: def _execute_price(self, token_ids: str) -> Dict[str, Any]:
"""Execute price lookup for the given token IDs or symbols.""" """Execute price lookup for the given token IDs."""
try: try:
tokens_list = token_ids.replace(",", " ").split() tokens_list = token_ids.replace(",", " ").split()
if not tokens_list: if not tokens_list:
return { return {
"response": "No token provided. Please provide a token name, symbol, or address like 'PEPE', 'TRUMP', or '0x...'", "response": "No token IDs provided. Please provide token IDs like 'PEPE-bsc TRUMP-bsc'",
"thinking": None, "thinking": None,
"strategy_updated": False, "strategy_updated": False,
"strategy_needs_confirmation": False, "strategy_needs_confirmation": False,
"success": True, "success": True,
} }
# Use search to find tokens and get their prices
# Search for the first token
search_keyword = tokens_list[0]
code, output = self._call_ave_script( code, output = self._call_ave_script(
"search", "price",
["--keyword", search_keyword.strip(), "--chain", "bsc", "--limit", "5"], ["--tokens"] + tokens_list,
) )
if self._is_error_output(code, output): if self._is_error_output(code, output):
return { return {
"response": f"Failed to search for token: {output}", "response": f"Failed to get prices: {output}",
"thinking": None, "thinking": None,
"strategy_updated": False, "strategy_updated": False,
"strategy_needs_confirmation": False, "strategy_needs_confirmation": False,
@@ -1013,21 +1010,15 @@ class ConversationalAgent:
} }
try: try:
data = json.loads(output) data = json.loads(output)
# Handle both dict with 'tokens' key and direct list prices = data.get("data", {})
data_field = data.get("data", []) if not isinstance(prices, dict):
if isinstance(data_field, list): prices = {}
tokens = data_field if prices:
else:
tokens = data_field.get("tokens", [])
if tokens:
price_text = "💰 **Token Prices:**\n" price_text = "💰 **Token Prices:**\n"
for t in tokens[:5]: for token_id, price_data in prices.items():
addr = t.get("token", "") price = price_data.get("price", "N/A") if isinstance(price_data, dict) else "N/A"
symbol = t.get("symbol", "") change_24h = price_data.get("token_price_change_24h", "N/A") if isinstance(price_data, dict) else "N/A"
name = t.get("name", "") mc = price_data.get("market_cap", "N/A") if isinstance(price_data, dict) else "N/A"
price = t.get("current_price_usd") or t.get("price", "N/A")
change_24h = t.get("token_price_change_24h", "N/A") or t.get("price_change_24h", "N/A")
mc = t.get("market_cap", "N/A")
try: try:
price_str = f"${float(price):,.6f}" if price and price != "N/A" else price price_str = f"${float(price):,.6f}" if price and price != "N/A" else price
except (ValueError, TypeError): except (ValueError, TypeError):
@@ -1036,7 +1027,7 @@ class ConversationalAgent:
mc_str = f"${float(mc):,.0f}" if mc and mc != "N/A" else mc mc_str = f"${float(mc):,.0f}" if mc and mc != "N/A" else mc
except (ValueError, TypeError): except (ValueError, TypeError):
mc_str = str(mc) if mc else "N/A" mc_str = str(mc) if mc else "N/A"
price_text += f"- **{symbol}** ({name}): {price_str} (MC: {mc_str})\n" price_text += f"- **{token_id}**: {price_str} (MC: {mc_str})\n"
return { return {
"response": price_text, "response": price_text,
"thinking": None, "thinking": None,