2024-12-04 23:10:02 +03:00

188 lines
6.9 KiB
Python

from typing import Dict, List, Optional, Tuple
from decimal import Decimal
from datetime import datetime, timedelta
import asyncio
from cdp_langchain.utils import CdpAgentkitWrapper
from cdp_langchain.agent_toolkits import CdpToolkit
class TokenSniper:
"""
High-performance token sniping system that enables fast entry and exit execution
using CDP's toolkit while implementing robust safety checks and monitoring.
"""
def __init__(self, config: Dict):
self.config = config
self.cdp = CdpAgentkitWrapper()
self.toolkit = CdpToolkit.from_cdp_agentkit_wrapper(self.cdp)
# Execution parameters
self.max_entry_slippage = Decimal(str(config.get('max_entry_slippage', '0.10')))
self.max_position_size = Decimal(str(config.get('max_position_size', '1.0')))
self.min_liquidity = Decimal(str(config.get('min_liquidity', '50000')))
# Auto-exit parameters
self.profit_target = Decimal(str(config.get('profit_target', '0.5')))
self.stop_loss = Decimal(str(config.get('stop_loss', '0.2')))
self.trailing_stop = Decimal(str(config.get('trailing_stop', '0.1')))
# State tracking
self.active_positions: Dict[str, Dict] = {}
self.pending_transactions: Dict[str, Dict] = {}
self.execution_history: List[Dict] = []
async def execute_entry(self, token_address: str, entry_params: Dict) -> Dict:
"""
Execute fast entry into a token position.
Args:
token_address: Token address to enter
entry_params: Entry execution parameters
Returns:
Entry execution results including transaction details
"""
try:
# Validate entry conditions
await self._validate_entry_conditions(token_address, entry_params)
tools = self.toolkit.get_tools()
# Prepare entry transaction
tx_params = await self._prepare_entry_transaction(
token_address,
entry_params
)
# Execute via CDP with high priority
tx = await tools.execute_transaction(
tx_params,
priority='high'
)
if tx['success']:
position = await self._initialize_position(
token_address,
tx
)
await self._start_position_monitoring(position)
return {
'status': 'completed' if tx['success'] else 'failed',
'transaction': tx,
'position': position if tx['success'] else None,
'metrics': await self._calculate_entry_metrics(tx),
'timestamp': datetime.utcnow()
}
except Exception as e:
self._log_error("Entry execution failed", e)
raise
async def execute_exit(self, position_id: str, exit_params: Dict) -> Dict:
"""
Execute fast exit from a token position.
Args:
position_id: Position identifier to exit
exit_params: Exit execution parameters
Returns:
Exit execution results including transaction details
"""
position = self.active_positions.get(position_id)
if not position:
raise ValueError("Position not found")
try:
tools = self.toolkit.get_tools()
# Prepare exit transaction
tx_params = await self._prepare_exit_transaction(
position,
exit_params
)
# Execute via CDP with high priority
tx = await tools.execute_transaction(
tx_params,
priority='high'
)
if tx['success']:
await self._close_position(position_id, tx)
return {
'status': 'completed' if tx['success'] else 'failed',
'transaction': tx,
'position': position,
'profit_loss': self._calculate_profit_loss(position, tx),
'metrics': await self._calculate_exit_metrics(tx),
'timestamp': datetime.utcnow()
}
except Exception as e:
self._log_error("Exit execution failed", e)
raise
async def _validate_entry_conditions(self, token_address: str, entry_params: Dict) -> None:
"""Validate conditions for entry execution."""
tools = self.toolkit.get_tools()
# Check liquidity
liquidity = await tools.get_token_liquidity(token_address)
if Decimal(str(liquidity)) < self.min_liquidity:
raise ValueError("Insufficient liquidity for entry")
# Validate position size
if Decimal(str(entry_params['amount'])) > self.max_position_size:
raise ValueError("Position size exceeds maximum allowed")
# Check contract security
contract_check = await self._verify_contract_security(token_address)
if not contract_check['is_safe']:
raise ValueError(f"Contract security check failed: {contract_check['reason']}")
# Validate gas parameters
await self._validate_gas_params(entry_params.get('gas_params', {}))
async def _prepare_entry_transaction(self, token_address: str, entry_params: Dict) -> Dict:
"""Prepare transaction parameters for entry execution."""
tools = self.toolkit.get_tools()
# Get optimal route
route = await tools.find_optimal_route(
token_address,
entry_params['amount']
)
# Calculate execution parameters
execution_price = await tools.get_execution_price(
route,
entry_params['amount']
)
# Set slippage tolerance
max_slippage = min(
self.max_entry_slippage,
Decimal(str(entry_params.get('max_slippage', self.max_entry_slippage)))
)
return {
'token_address': token_address,
'route': route,
'amount': entry_params['amount'],
'execution_price': execution_price,
'slippage_tolerance': max_slippage,
'gas_params': self._prepare_gas_params(entry_params.get('gas_params', {})),
'deadline': int(datetime.utcnow().timestamp() + 60) # 1 minute deadline
}
def _log_error(self, message: str, error: Exception) -> None:
"""Log error with details."""
error_details = {
'message': message,
'error': str(error),
'timestamp': datetime.utcnow().isoformat()
}
print(f"Token Sniper Error: {error_details}") # Replace with proper logging