2024-12-01 23:53:12 +03:00

226 lines
7.9 KiB
Python

"""
Base CDP agent implementation providing core functionality for blockchain interactions.
"""
from typing import Dict, List, Optional, Union
from dataclasses import dataclass
import asyncio
import logging
from datetime import datetime
from cdp_langchain.agent_toolkits import CdpToolkit
from cdp_langchain.utils import CdpAgentkitWrapper
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
@dataclass
class AgentConfig:
"""Configuration for CDP agent"""
network_id: str
api_key_name: str
api_key_private: str
model_name: str = "gpt-4o-mini"
max_position_size: float = 1.0
gas_limit: int = 2000000
slippage_tolerance: float = 0.5
retry_attempts: int = 3
monitoring_interval: int = 10
@dataclass
class AgentState:
"""Current state of the CDP agent"""
wallet_address: str
balance: float
active_positions: Dict[str, float]
pending_transactions: List[str]
last_action_timestamp: float
is_active: bool
class BaseCDPAgent:
"""
Base CDP agent implementation providing core functionality
for blockchain interactions and trading operations.
"""
def __init__(self, config: AgentConfig, logger: Optional[logging.Logger] = None):
"""Initialize the CDP agent with configuration"""
self.config = config
self.logger = logger or logging.getLogger(__name__)
# Initialize CDP components
self.llm = self._initialize_llm()
self.cdp = self._initialize_cdp()
self.toolkit = self._initialize_toolkit()
self.memory = MemorySaver()
# Initialize state
self.state = None
self._last_error = None
self.logger.info("CDP agent initialized successfully")
def _initialize_llm(self) -> ChatOpenAI:
"""Initialize language model"""
try:
return ChatOpenAI(
model=self.config.model_name,
temperature=0.2
)
except Exception as e:
self.logger.error(f"LLM initialization failed: {str(e)}")
raise
def _initialize_cdp(self) -> CdpAgentkitWrapper:
"""Initialize CDP wrapper"""
try:
values = {
"network_id": self.config.network_id,
"api_key_name": self.config.api_key_name,
"api_key_private": self.config.api_key_private
}
return CdpAgentkitWrapper(**values)
except Exception as e:
self.logger.error(f"CDP initialization failed: {str(e)}")
raise
def _initialize_toolkit(self) -> CdpToolkit:
"""Initialize CDP toolkit"""
try:
return CdpToolkit.from_cdp_agentkit_wrapper(self.cdp)
except Exception as e:
self.logger.error(f"Toolkit initialization failed: {str(e)}")
raise
async def initialize(self):
"""Initialize agent state and wallet"""
try:
# Initialize wallet
wallet_data = await self._initialize_wallet()
# Initialize state
self.state = AgentState(
wallet_address=wallet_data['address'],
balance=await self._get_balance(),
active_positions={},
pending_transactions=[],
last_action_timestamp=datetime.now().timestamp(),
is_active=True
)
self.logger.info(f"Agent initialized with wallet: {self.state.wallet_address}")
return self.state
except Exception as e:
self.logger.error(f"Agent initialization failed: {str(e)}")
raise
async def _initialize_wallet(self) -> Dict:
"""Initialize CDP wallet"""
try:
# Export existing wallet or create new one
wallet_data = self.cdp.export_wallet()
return wallet_data
except Exception as e:
self.logger.error(f"Wallet initialization failed: {str(e)}")
raise
async def _get_balance(self) -> float:
"""Get current wallet balance"""
try:
tools = self.toolkit.get_tools()
balance = await tools.get_balance()
return float(balance)
except Exception as e:
self.logger.error(f"Balance check failed: {str(e)}")
raise
async def execute_transaction(self, transaction: Dict) -> str:
"""Execute transaction with retry logic"""
for attempt in range(self.config.retry_attempts):
try:
tools = self.toolkit.get_tools()
# Validate transaction
if not await self._validate_transaction(transaction):
raise ValueError("Transaction validation failed")
# Execute transaction
tx_hash = await tools.execute_transaction(transaction)
# Update state
self.state.pending_transactions.append(tx_hash)
self.state.last_action_timestamp = datetime.now().timestamp()
self.logger.info(f"Transaction executed: {tx_hash}")
return tx_hash
except Exception as e:
self.logger.warning(f"Transaction attempt {attempt + 1} failed: {str(e)}")
if attempt == self.config.retry_attempts - 1:
raise
await asyncio.sleep(1)
async def _validate_transaction(self, transaction: Dict) -> bool:
"""Validate transaction parameters"""
try:
# Check value limits
if transaction.get('value', 0) > self.config.max_position_size:
return False
# Check sufficient balance
if transaction.get('value', 0) > self.state.balance:
return False
# Additional validation could be added here
return True
except Exception as e:
self.logger.error(f"Transaction validation failed: {str(e)}")
return False
async def monitor_transactions(self):
"""Monitor pending transactions"""
try:
tools = self.toolkit.get_tools()
for tx_hash in self.state.pending_transactions[:]:
status = await tools.get_transaction_status(tx_hash)
if status in ['confirmed', 'failed']:
self.state.pending_transactions.remove(tx_hash)
if status == 'confirmed':
await self._handle_confirmed_transaction(tx_hash)
else:
await self._handle_failed_transaction(tx_hash)
except Exception as e:
self.logger.error(f"Transaction monitoring failed: {str(e)}")
raise
async def _handle_confirmed_transaction(self, tx_hash: str):
"""Handle confirmed transaction"""
try:
# Update balance
self.state.balance = await self._get_balance()
self.logger.info(f"Transaction confirmed: {tx_hash}")
except Exception as e:
self.logger.error(f"Failed to handle confirmed transaction: {str(e)}")
async def _handle_failed_transaction(self, tx_hash: str):
"""Handle failed transaction"""
try:
self.logger.warning(f"Transaction failed: {tx_hash}")
# Additional error handling could be added here
except Exception as e:
self.logger.error(f"Failed to handle failed transaction: {str(e)}")
async def cleanup(self):
"""Cleanup agent resources"""
try:
self.state.is_active = False
# Additional cleanup could be added here
self.logger.info("Agent cleanup completed")
except Exception as e:
self.logger.error(f"Cleanup failed: {str(e)}")