please fix the spacing import asyncio import json import time import base64 import…
- User
please fix the spacing import asyncio import json import time import base64 import traceback from pathlib import Path from solana.rpc.async_api import AsyncClient from solana.publickey import PublicKey from solana.transaction import Transaction from solana.keypair import Keypair from solana.rpc.commitment import Commitment from spl.token.instructions import ( get_associated_token_address, create_associated_token_account, ) from solana_rust_bot import TradingCore, TradeParams from solana_logger import logger from balance_manager import BalanceManager from config_loader import config import websockets import torch import os
SOLANA_VERSION = "solana-client/1.18.23 (src:aeb3a2e1; feat:4215500110, client:SolanaLabs)" WEBSOCKET_CONFIG = { "ping_interval": 0.5, "ping_timeout": 5, "compression": None, "max_size": 223, "close_timeout": 1.0, "max_queue": 216, "read_limit": 2**16, }
class QuantumTrader: def init(self): logger.system("🚀 Initializing Quantum Trader")
# Load configurations first self.network_config = config.get_network_config() self.trade_config = config.get_trade_config() self.perf_config = config.get_performance_config() # Initialize components self._setup_cuda() self._init_core() # Setup state management self.execution_lock = asyncio.Lock() self.in_trade = False self.ata_cache = {} self.ws = None self.reconnect_delay = 1.0 self.max_reconnect_delay = 30.0 logger.system("✨ Quantum Trader Initialized!") def _setup_cuda(self): """Configure CUDA for optimal performance.""" try: torch.cuda.init() torch.backends.cuda.matmul.allow_tf32 = self.perf_config["allow_tf32"] torch.backends.cudnn.benchmark = True torch.cuda.set_device(0) torch.cuda.empty_cache() memory_fraction = self.perf_config["gpu_memory_fraction"] torch.cuda.set_per_process_memory_fraction(memory_fraction) self.stream = torch.cuda.Stream(priority=self.perf_config["stream_priority"]) logger.system(f"💻 CUDA Initialized | Memory: {memory_fraction * 100}%") except Exception as e: logger.error(f"CUDA initialization failed: {e}") raise def _init_core(self): """Initialize core trading components.""" try: # Load keypair self.keypair_bytes = config.get_keypair_bytes() self.keypair = Keypair.from_secret_key(self.keypair_bytes) # Initialize trading core self.trading_core = TradingCore( rpc_url=self.network_config["rpc_url"], keypair_bytes=self.keypair_bytes ) # Initialize balance manager and client self.balance_manager = BalanceManager() self.client = AsyncClient( self.network_config["rpc_url"], commitment=Commitment("confirmed") ) logger.info(f"Trading core initialized for wallet: {str(self.keypair.public_key)}") except Exception as e: logger.error(f"Core initialization error: {e}") raise async def _create_and_get_ata(self, token_mint: str) -> str: """Create ATA if needed and return address.""" try: # Check cache first if token_mint in self.ata_cache: return self.ata_cache[token_mint] wallet_pubkey = self.keypair.public_key token_pubkey = PublicKey(token_mint) ata = get_associated_token_address(wallet_pubkey, token_pubkey) # Check if account exists account_info = await self.client.get_account_info(ata) if account_info.value is not None: self.ata_cache[token_mint] = str(ata) return str(ata) # Explicitly specify associated token program ID create_ata_ix = create_associated_token_account( payer=wallet_pubkey, owner=wallet_pubkey, mint=token_pubkey, associated_token_program_id=PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL") ) # Build transaction recent_blockhash = (await self.client.get_latest_blockhash()).value.blockhash tx = Transaction( recent_blockhash=recent_blockhash, fee_payer=wallet_pubkey, ) tx.add(create_ata_ix) # Sign and send tx.sign(self.keypair) tx_bytes = tx.serialize() # Use skip preflight since we check simulation sig = await self.client.send_raw_transaction( tx_bytes, opts={ "skip_preflight": False, "preflight_commitment": "confirmed" } ) # Wait for confirmation await self.client.confirm_transaction(sig.value, commitment="confirmed") logger.info(f"Created ATA {str(ata)} for token {token_mint}") self.ata_cache[token_mint] = str(ata) return str(ata) except Exception as e: logger.error(f"ATA creation failed: {str(e)}") return None def decode_raydium_data(self, raw_data: bytes) -> dict: """Decode Raydium instruction data.""" try: if len(raw_data) < 9: return None # Extract instruction type and amount instruction_type = raw_data[0] amount = int.from_bytes(raw_data[1:9], "little") / 1e9 # Validate size bounds if amount < self.trade_config["min_victim_size"] or amount > self.trade_config["max_victim_size"]: return None if instruction_type not in [0x03, 0x04]: return None # Extract token mint token_mint = None for offset in [9, 17]: if len(raw_data) >= offset + 32: try: from base58 import b58encode token_bytes = raw_data[offset:offset + 32] potential_mint = b58encode(token_bytes).decode("utf-8") if len(potential_mint) in [32, 44]: token_mint = potential_mint break except: continue if not token_mint: return None # Calculate profit with CUDA with torch.cuda.stream(self.stream): position_size = self.trade_config["front_run_amount"] impact_multiplier = 1.2 if amount >= 20.0 else 1.0 victim_impact = amount * 0.0015 * impact_multiplier our_impact = position_size * 0.002 gas_cost = 0.002 net_profit = (position_size * (victim_impact - our_impact)) - gas_cost if net_profit < self.trade_config["min_profit"]: return None return { "victim_data": raw_data, "amount": amount, "token_mint": token_mint, "net_profit": float(net_profit), "instruction_type": instruction_type, } except Exception as e: logger.error(f"Decode error: {e}") return None async def execute_trade(self, trade_data: dict) -> bool: """Execute trade with proper ATA handling.""" if self.in_trade or self.balance_manager.is_trading_paused: return False async with self.execution_lock: try: self.in_trade = True # Create ATA first user_token = await self._create_and_get_ata(trade_data["token_mint"]) if not user_token: return False # Account setup with proper ATA market_accounts = config.get_market_accounts() trade_accounts = { "signer": config.get("wallet"), "user": config.get("wallet"), "user_token": str(user_token), "user_wsol": config.get("wsol_token_account"), "raydium_program": config.get("raydium"), **market_accounts } # Verify accounts for key, value in trade_accounts.items(): if not value or value.lower() == "none": logger.error(f"Missing required account: {key}") return False position_size = self.trade_config["front_run_amount"] logger.trade( f""" - Assistant
- User
- Assistant