176 lines
6.0 KiB
Python
176 lines
6.0 KiB
Python
# src/ai/agent.py
|
|
from cdp_langchain.utils import CdpAgentkitWrapper
|
|
from cdp_langchain.agent_toolkits import CdpToolkit
|
|
from langchain_openai import ChatOpenAI
|
|
from typing import Dict, Optional
|
|
|
|
class CDPAgentManager:
|
|
"""
|
|
Core CDP integration manager that handles initialization, wallet management,
|
|
and CDP toolkit functionality.
|
|
"""
|
|
|
|
def __init__(self, config: Dict):
|
|
self.config = config
|
|
self.cdp = CdpAgentkitWrapper()
|
|
self.toolkit = CdpToolkit.from_cdp_agentkit_wrapper(self.cdp)
|
|
self.llm = ChatOpenAI(model="gpt-4o-mini")
|
|
|
|
# Initialize wallet management
|
|
self.wallet_manager = CDPWalletManager(self.cdp)
|
|
|
|
# Initialize market analysis components
|
|
self.market_analyzer = CDPMarketAnalyzer(self.toolkit)
|
|
self.token_monitor = CDPTokenMonitor(self.toolkit)
|
|
|
|
# Initialize execution components
|
|
class CDPTransactionManager:
|
|
"""Handles transaction execution using CDP toolkit."""
|
|
|
|
def __init__(self, toolkit: CdpToolkit):
|
|
self.toolkit = toolkit
|
|
|
|
async def execute_transaction(self, transaction_data: Dict) -> Dict:
|
|
"""Execute a transaction using CDP."""
|
|
tools = self.toolkit.get_tools()
|
|
|
|
# Execute transaction
|
|
result = await tools.execute_transaction(transaction_data)
|
|
|
|
return result
|
|
self.transaction_manager = CDPTransactionManager(self.toolkit)
|
|
|
|
async def initialize_user_wallet(self, user_id: int) -> Dict:
|
|
"""
|
|
Initialize CDP wallet for a new user. This creates a dedicated wallet
|
|
that will be used for all user's transactions and analysis.
|
|
"""
|
|
# Create or load wallet data
|
|
wallet_data = await self.wallet_manager.get_user_wallet(user_id)
|
|
if not wallet_data:
|
|
wallet_data = await self.wallet_manager.create_user_wallet(user_id)
|
|
|
|
# Initialize wallet with CDP
|
|
initialized = await self.wallet_manager.initialize_wallet(wallet_data)
|
|
|
|
if initialized:
|
|
return {
|
|
'success': True,
|
|
'wallet_address': wallet_data['address'],
|
|
'network': self.config['network_id'],
|
|
'initialized': True
|
|
}
|
|
|
|
return {'success': False, 'error': 'Wallet initialization failed'}
|
|
|
|
async def analyze_token(self, token_address: str, tier: str = 'basic') -> Dict:
|
|
"""
|
|
Perform token analysis using CDP's toolkit. Analysis depth depends on
|
|
user's subscription tier.
|
|
"""
|
|
try:
|
|
# Basic analysis (available to all tiers)
|
|
basic_analysis = await self.market_analyzer.get_basic_metrics(token_address)
|
|
|
|
if tier == 'basic':
|
|
return basic_analysis
|
|
|
|
# Advanced analysis (pro and premium tiers)
|
|
advanced_analysis = await self.market_analyzer.get_advanced_metrics(
|
|
token_address
|
|
)
|
|
|
|
# AI-enhanced insights (premium tier only)
|
|
if tier == 'premium':
|
|
ai_insights = await self._generate_ai_insights(
|
|
token_address,
|
|
{**basic_analysis, **advanced_analysis}
|
|
)
|
|
|
|
return {
|
|
**basic_analysis,
|
|
**advanced_analysis,
|
|
'ai_insights': ai_insights
|
|
}
|
|
|
|
return {**basic_analysis, **advanced_analysis}
|
|
|
|
except Exception as e:
|
|
self._log_error(f"Token analysis failed: {str(e)}")
|
|
raise
|
|
|
|
class CDPWalletManager:
|
|
"""Manages CDP wallets for users."""
|
|
|
|
def __init__(self, cdp: CdpAgentkitWrapper):
|
|
self.cdp = cdp
|
|
self.active_wallets = {}
|
|
|
|
async def create_user_wallet(self, user_id: int) -> Dict:
|
|
"""Create a new CDP wallet for user."""
|
|
tools = self.cdp.toolkit.get_tools()
|
|
|
|
# Generate wallet using CDP
|
|
wallet = await tools.create_wallet()
|
|
|
|
# Store wallet data
|
|
self.active_wallets[user_id] = {
|
|
'address': wallet['address'],
|
|
'data': wallet['wallet_data']
|
|
}
|
|
|
|
return wallet
|
|
|
|
class CDPMarketAnalyzer:
|
|
"""Handles market analysis using CDP toolkit."""
|
|
|
|
def __init__(self, toolkit: CdpToolkit):
|
|
self.toolkit = toolkit
|
|
|
|
async def get_basic_metrics(self, token_address: str) -> Dict:
|
|
"""Get basic token metrics using CDP."""
|
|
tools = self.toolkit.get_tools()
|
|
|
|
# Get price data
|
|
price = await tools.get_token_price(token_address)
|
|
|
|
# Get basic metrics
|
|
metrics = {
|
|
'current_price': price,
|
|
'market_cap': await tools.get_market_cap(token_address),
|
|
'volume_24h': await tools.get_volume_24h(token_address),
|
|
'holders': await tools.get_holder_count(token_address)
|
|
}
|
|
|
|
return metrics
|
|
|
|
class CDPTokenMonitor:
|
|
"""Monitors token prices and triggers alerts."""
|
|
|
|
def __init__(self, toolkit: CdpToolkit):
|
|
self.toolkit = toolkit
|
|
self.monitored_tokens = {}
|
|
|
|
async def start_monitoring(self, token_address: str, parameters: Dict) -> bool:
|
|
"""Start monitoring a token with specified parameters."""
|
|
tools = self.toolkit.get_tools()
|
|
|
|
try:
|
|
# Set up price monitoring
|
|
monitor = await tools.setup_price_monitor(
|
|
token_address,
|
|
parameters['price_threshold'],
|
|
parameters['update_interval']
|
|
)
|
|
|
|
self.monitored_tokens[token_address] = {
|
|
'monitor': monitor,
|
|
'parameters': parameters,
|
|
'last_update': None
|
|
}
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
self._log_error(f"Failed to start monitoring: {str(e)}")
|
|
return False |