|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import json |
| 8 | +import os |
8 | 9 | from typing import Any, Dict, List, Optional |
9 | 10 |
|
10 | 11 | import requests |
11 | 12 | from eth_utils import to_checksum_address |
12 | 13 | from web3 import Web3 |
13 | 14 |
|
| 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 | + |
14 | 25 |
|
15 | 26 | 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): |
17 | 28 | """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)) |
20 | 57 |
|
21 | 58 | # Known address labels for better descriptions |
22 | 59 | self.address_labels = { |
@@ -72,6 +109,36 @@ def get_address_label(self, address: str) -> str: |
72 | 109 | checksum_addr, f"0x{address[2:6]}...{address[-4:]}" |
73 | 110 | ) |
74 | 111 |
|
| 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 | + |
75 | 142 | def add_address_label(self, address: str, label: str) -> None: |
76 | 143 | """Add a new address label""" |
77 | 144 | checksum_addr = to_checksum_address(address) |
|
0 commit comments