Skip to content

Commit 7f6df27

Browse files
committed
Enhance CronosTransactionAnalyzer initialization and chain ID retrieval
- Updated the CronosTransactionAnalyzer class to accept a dashboard API key for improved chain identification. - Implemented logic to dynamically determine the RPC URL and chain ID based on the provided API key or fallback to defaults. - Added a method to fetch the chain ID from the dashboard API, enhancing flexibility and usability in transaction analysis. - Modified bot.py to initialize the analyzer with the dashboard API key for better integration.
1 parent 10969fb commit 7f6df27

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

bot/cryptocom-ai-telegram-bot/bot.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@
5858
# Custom storage for persistence (optional)
5959
custom_storage = SQLitePlugin(db_path="telegram_agent_state.db")
6060

61-
# Initialize the Cronos transaction analyzer
62-
tx_analyzer = CronosTransactionAnalyzer()
61+
# Initialize the Cronos transaction analyzer with dashboard API key
62+
# This will automatically determine the correct chain and RPC endpoint
63+
tx_analyzer = CronosTransactionAnalyzer(
64+
dashboard_api_key=os.getenv("DASHBOARD_API_KEY")
65+
)
6366

6467
# Global agent instance
6568
agent = None

bot/cryptocom-ai-telegram-bot/cronos_tx_analyzer.py

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,55 @@
55
"""
66

77
import json
8+
import os
89
from typing import Any, Dict, List, Optional
910

1011
import requests
1112
from eth_utils import to_checksum_address
1213
from web3 import Web3
1314

15+
try:
16+
from crypto_com_agent_client.lib.types.chain_helper import (
17+
CHAIN_INFO,
18+
ChainId,
19+
)
20+
from crypto_com_developer_platform_client import Client, Network
21+
AGENT_CLIENT_AVAILABLE = True
22+
except ImportError:
23+
AGENT_CLIENT_AVAILABLE = False
24+
1425

1526
class CronosTransactionAnalyzer:
16-
def __init__(self, rpc_url: str = "https://evm.cronos.org"):
27+
def __init__(self, rpc_url: Optional[str] = None, dashboard_api_key: Optional[str] = None):
1728
"""Initialize the analyzer with Cronos EVM connection"""
18-
self.web3 = Web3(Web3.HTTPProvider(rpc_url))
19-
self.chain_id = 25 # Cronos mainnet
29+
self.dashboard_api_key = dashboard_api_key or os.getenv("DASHBOARD_API_KEY")
30+
31+
# Determine chain from dashboard API key if available
32+
chain_id_from_api = self._get_chain_id_from_dashboard_api()
33+
34+
if rpc_url:
35+
# Use provided RPC URL
36+
self.rpc_url = rpc_url
37+
self.chain_id = chain_id_from_api or 25 # Default to Cronos mainnet
38+
elif AGENT_CLIENT_AVAILABLE and chain_id_from_api:
39+
# Get chain info from dashboard API key using agent client
40+
try:
41+
chain_enum = ChainId(int(chain_id_from_api))
42+
chain_info = CHAIN_INFO[chain_enum]
43+
self.rpc_url = chain_info["rpc"]
44+
self.chain_id = int(chain_id_from_api)
45+
print(f"Using chain {self.chain_id} with RPC: {self.rpc_url}")
46+
except (ValueError, KeyError):
47+
# Fallback to default if chain not supported
48+
self.rpc_url = "https://evm.cronos.org"
49+
self.chain_id = 25
50+
print(f"Unsupported chain {chain_id_from_api}, using fallback")
51+
else:
52+
# Fallback to hardcoded RPC
53+
self.rpc_url = "https://evm.cronos.org"
54+
self.chain_id = 25
55+
56+
self.web3 = Web3(Web3.HTTPProvider(self.rpc_url))
2057

2158
# Known address labels for better descriptions
2259
self.address_labels = {
@@ -72,6 +109,36 @@ def get_address_label(self, address: str) -> str:
72109
checksum_addr, f"0x{address[2:6]}...{address[-4:]}"
73110
)
74111

112+
def _get_chain_id_from_dashboard_api(self) -> Optional[int]:
113+
"""Get chain ID from dashboard API key using developer platform client"""
114+
if not self.dashboard_api_key or not AGENT_CLIENT_AVAILABLE:
115+
return None
116+
117+
try:
118+
# Initialize client with API key
119+
Client.init(api_key=self.dashboard_api_key)
120+
121+
# Get chain ID from developer platform
122+
chain_id_response = Network.chain_id()
123+
124+
if isinstance(chain_id_response, dict):
125+
# Handle nested response format: {'status': 'Success', 'data': {'chainId': '338'}}
126+
if "data" in chain_id_response and isinstance(chain_id_response["data"], dict):
127+
chain_id = chain_id_response["data"].get("chainId")
128+
else:
129+
chain_id = chain_id_response.get("chainId")
130+
131+
if chain_id is not None:
132+
return int(chain_id)
133+
else:
134+
# Direct value response
135+
return int(chain_id_response)
136+
137+
except Exception as e:
138+
print(f"Error getting chain ID from dashboard API: {e}")
139+
140+
return None
141+
75142
def add_address_label(self, address: str, label: str) -> None:
76143
"""Add a new address label"""
77144
checksum_addr = to_checksum_address(address)

0 commit comments

Comments
 (0)