import asyncio import websockets import json from flask import Flask, render_template, request, jsonify from solana.rpc.async_api import AsyncClient from solana.transaction import Signature from solana.rpc.websocket_api import connect from solana.rpc.commitment import Confirmed, Processed from solana.transaction import Transaction from spl.token.client import Token from base64 import b64decode import base58 from solders.rpc.requests import GetTransaction from solders.signature import Signature from solders.pubkey import Pubkey from solders.keypair import Keypair from solders.transaction import VersionedTransaction from solders.transaction import Transaction from solders.message import Message from solders.instruction import Instruction from solders.hash import Hash from solders.instruction import CompiledInstruction from solders import message from jupiter_python_sdk.jupiter import Jupiter, Jupiter_DCA from dexscreener import DexscreenerClient from solana.rpc.types import TokenAccountOpts, TxOpts import datetime import logging from logging.handlers import RotatingFileHandler import base64 import os from dotenv import load_dotenv,set_key import aiohttp from typing import List, Dict import requests import threading import re from typing import List, Dict, Any, Tuple import random from modules.webui import init_app from modules.storage import init_db, store_transaction app = Flask(__name__) from config import ( FOLLOWED_WALLET, YOUR_WALLET, SOLANA_WS_URL, SOLANA_HTTP_URL, DISPLAY_CURRENCY, logger, error_logger ) from modules.SolanaAPI import SolanaAPI, SolanaDEX from modules.utils import telegram_utils # Function to find the latest log file def get_latest_log_file(): log_dir = './logs' try: # files = [f for f in os.listdir(log_dir) if os.path.isfile(os.path.join(log_dir, f))] # filter files mask log_20241005_004103_143116.json files = [f for f in os.listdir(log_dir) if os.path.isfile(os.path.join(log_dir, f)) and f.startswith('log_')] latest_file = max(files, key=lambda x: os.path.getmtime(os.path.join(log_dir, x))) return os.path.join(log_dir, latest_file) except Exception as e: logging.error(f"Error fetching latest log file: {e}") return None # Flask route to retry processing the last log @app.route('/retry-last-log', methods=['GET']) async def retry_last_log(): latest_log_file = get_latest_log_file() if not latest_log_file: return jsonify({"error": "No log files found"}), 404 try: logger.info(f"Processing latest log file: {latest_log_file}") with open(latest_log_file, 'r') as f: log = json.load(f) result = await process_log(log) return jsonify({ "file": latest_log_file, "status": "Log dump processed successfully", "result": result }), 200 except Exception as e: logging.error(f"Error processing log dump: {e}") return jsonify({"error": "Failed to process log"}), 500 # Create the bot with the custom connection pool bot = None TOKENS_INFO = {} try: with open('./logs/token_info.json', 'r') as f: TOKENS_INFO = json.load(f) except Exception as e: logging.error(f"Error loading token info: {str(e)}") # # # # # # # # # # TELEGRAM # # # # # # # # # # if not telegram_utils.bot: try: asyncio.run(telegram_utils.initialize()) except Exception as e: logging.error(f"Error initializing Telegram bot: {str(e)}") # async def telegram_utils.send_telegram_message(message): # try: # await bot.send_message(chat_id=DEVELOPER_CHAT_ID, text=f"[{BOT_NAME}] {message}", parse_mode=ParseMode.HTML) # logging.info(f"Telegram message sent: {message}") # # logging.info(f"Telegram message dummy sent: {message}") # except Exception as e: # logging.error(f"Error sending Telegram message: {str(e)}") # # # # # # # # # # DATABASE # # # # # # # # # # # # # # # # # # # # CRYPTO PUBLIC # # # # # # # # # # # # # # # # # # # # SOLANA BLOCKCHAIN # # # # # # # # # # solana_client = AsyncClient(SOLANA_HTTP_URL) async def get_token_balance_rpc(wallet_address, token_address): url = SOLANA_HTTP_URL headers = {"Content-Type": "application/json"} data = { "jsonrpc": "2.0", "id": 1, "method": "getTokenAccountsByOwner", "params": [ wallet_address, { "mint": token_address }, { "encoding": "jsonParsed" } ] } try: response = requests.post(url, headers=headers, data=json.dumps(data)) response.raise_for_status() # Raises an error for bad responses accounts = response.json() if 'result' in accounts and accounts['result']['value']: first_account = accounts['result']['value'][0]['pubkey'] balance_data = { "jsonrpc": "2.0", "id": 1, "method": "getTokenAccountBalance", "params": [ first_account ] } balance_response = requests.post(url, headers=headers, data=json.dumps(balance_data)) balance_response.raise_for_status() balance = balance_response.json() if 'result' in balance and 'value' in balance['result']: amount = float(balance['result']['value']['uiAmount']) logging.debug(f"Balance for {token_address} in {wallet_address}: {amount}") return amount else: logging.debug(f"No balance found for {token_address} in {wallet_address}") return 0 else: logging.debug(f"No account found for {token_address} in {wallet_address}") return 0 except requests.exceptions.RequestException as e: logging.error(f"Error getting balance for {token_address} in {wallet_address}: {str(e)} \r\n {e}") return 0 # # # solders/solana libs (solana_client) # # # from spl.token._layouts import MINT_LAYOUT from solana.rpc.api import Client, Pubkey from spl.token.async_client import AsyncToken from spl.token.constants import TOKEN_PROGRAM_ID from borsh_construct import String, CStruct METADATA_STRUCT = CStruct( "update_authority" / String, "mint" / String, "name" / String, "symbol" / String, "uri" / String, # ... other fields ... ) import struct def get_token_name_metadata(metadata_account_data): try: # Skip the first 1 + 32 + 32 bytes (1 byte for version, 32 bytes each for update authority and mint) offset = 1 + 32 + 32 # Read the name length (u32) name_length = struct.unpack(" update_authority # stream read less than specified amount, expected 2189641476, found 675 # # Parse metadata # key = data[:4] # update_authority = Pubkey(data[4:36]) # mint = Pubkey(data[36:68]) # name_length = struct.unpack(' 0 and data[0] == 4: # name_len = int.from_bytes(data[1:5], byteorder="little") # name = data[5:5+name_len].decode("utf-8").strip("\x00") # symbol_len = int.from_bytes(data[5+name_len:9+name_len], byteorder="little") # symbol = data[9+name_len:9+name_len+symbol_len].decode("utf-8").strip("\x00") # return {"name": name, "symbol": symbol} except Exception as e: logging.error(f"Error fetching token metadata for {mint_address}: {str(e)}") return None async def get_swap_transaction_details(tx_signature_str): t = await solana_client.get_transaction(Signature.from_string(tx_signature_str), max_supported_transaction_version=0) try: parsed_result = { "order_id": None, "token_in": None, "token_out": None, "amount_in": 0, "amount_out": 0, "amount_in_USD": 0, "amount_out_USD": 0, "percentage_swapped": 0 } instructions = t.value.transaction.transaction.message.instructions # Parse the swap instruction to extract token addresses, amounts, and types for instruction in instructions: if isinstance(instruction, CompiledInstruction): if instruction.program_id == Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"): parsed_info = instruction.parsed.info mint = parsed_info["mint"] amount = float(parsed_info["tokenAmount"]["amount"]) / (10 ** parsed_info["tokenAmount"]["decimals"]) # Determine token in and token out based on balances if parsed_result["token_in"] is None and amount > 0: parsed_result["token_in"] = mint parsed_result["amount_in"] = amount elif parsed_result["token_out"] is None: parsed_result["token_out"] = mint parsed_result["amount_out"] = amount # Calculate percentage swapped if parsed_result["amount_in"] > 0 and parsed_result["amount_out"] > 0: parsed_result["percentage_swapped"] = (parsed_result["amount_out"] / parsed_result["amount_in"]) * 100 return parsed_result except Exception as e: logging.error(f"Error fetching transaction details: {e}") return None # # # # # # # # # # Functionality # # # # # # # # # # def safe_get_property(info, property_name, default='Unknown'): if not isinstance(info, dict): return str(default) value = info.get(property_name, default) return str(value) if value is not None else str(default) async def save_token_info(): with open('./logs/token_info.json', 'w') as f: json.dump(TOKENS_INFO, f, indent=2) async def get_transaction_details_with_retry(transaction_id, retry_delay = 8, max_retries = 16): # wait for the transaction to be confirmed # await async_client.wait_for_confirmation(Signature.from_string(transaction_id)) # qwery every 5 seconds for the transaction details untill not None or 30 seconds for _ in range(max_retries): try: tx_details = await solanaAPI.get_transaction_details_rpc(transaction_id) if tx_details is not None: break except Exception as e: logging.error(f"Error fetching transaction details: {e}") logging.info(f"({_} of {max_retries}) Waiting for transaction details for {transaction_id}") await asyncio.sleep(retry_delay) return tx_details async def save_log(log): try: os.makedirs('./logs', exist_ok=True) timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f") filename = f"./logs/log_{timestamp}.json" with open(filename, 'w') as f: json.dump(log, f, indent=2) except Exception as e: logging.error(f"Error saving RPC log: {e}") PROCESSING_LOG = False async def process_log(log_result): global PROCESSING_LOG tr_details = { "order_id": None, "token_in": None, "token_out": None, "amount_in": 0, "amount_out": 0, "amount_in_USD": 0, "amount_out_USD": 0, "percentage_swapped": 0 } if log_result['value']['err']: return logs = log_result['value']['logs'] try: # Detect swap operations in logs PROCESSING_LOG = True swap_operations = ['Program log: Instruction: Swap', 'Program log: Instruction: Swap2', 'Program log: Instruction: SwapExactAmountIn'] if any(op in logs for op in swap_operations): # Save the log to a file await save_log(log_result) tx_signature_str = log_result['value']['signature'] before_source_balance = 0 source_token_change = 0 i = 0 while i < len(logs): log_entry = logs[i] # Check if we found the 'order_id' if tr_details["order_id"] is None and "order_id" in log_entry: # Extract the order_id tr_details["order_id"] = log_entry.split(":")[-1].strip() tr_details["token_in"] = logs[i + 1].split(":")[-1].strip() tr_details["token_out"] = logs[i + 2].split(":")[-1].strip() # Look for the token change amounts after tokens have been found if "source_token_change" in log_entry: parts = log_entry.split(", ") for part in parts: if "source_token_change" in part: tr_details["amount_in"] = float(part.split(":")[-1].strip()) / 10 ** 6 # Assuming 6 decimals elif "destination_token_change" in part: tr_details["amount_out"] = float(part.split(":")[-1].strip()) / 10 ** 6 # Assuming 6 decimals i += 1 # calculatte percentage swapped by digging before_source_balance, source_token_change and after_source_balance # "Program log: before_source_balance: 19471871, before_destination_balance: 0, amount_in: 19471871, expect_amount_out: 770877527, min_return: 763168752", # "Program log: after_source_balance: 0, after_destination_balance: 770570049", # "Program log: source_token_change: 19471871, destination_token_change: 770570049", if "before_source_balance" in log_entry: parts = log_entry.split(", ") for part in parts: if "before_source_balance" in part: before_source_balance = float(part.split(":")[-1].strip()) / 10 ** 6 if "source_token_change" in log_entry: parts = log_entry.split(", ") for part in parts: if "source_token_change" in part: source_token_change = float(part.split(":")[-1].strip()) / 10 ** 6 # GET DETAILS FROM TRANSACTION IF NOT FOUND IN LOGS try: if tr_details["token_in"] is None or tr_details["token_out"] is None or tr_details["amount_in"] == 0 or tr_details["amount_out"] == 0: logging.warning("Incomplete swap details found in logs. Getting details from transaction") tr_details = await get_transaction_details_info(tx_signature_str, logs) # onlt needed if no details got if before_source_balance > 0 and source_token_change > 0: tr_details["percentage_swapped"] = (source_token_change / before_source_balance) * 100 #dirty fix for percentage > 100 (decimals 9 but expecting 6) if tr_details["percentage_swapped"] > 100: tr_details["percentage_swapped"] = tr_details["percentage_swapped"] / 1000 # update token info: ToDo: check, but already did # all_token_addresses = list(set([tr_details["token_in"], tr_details["token_out"]])) # await get_token_prices(all_token_addresses) try: token_in = TOKENS_INFO[tr_details["token_in"]] token_out = TOKENS_INFO[tr_details["token_out"]] tr_details["symbol_in"] = token_in.get('symbol') tr_details["symbol_out"] = token_out.get('symbol') tr_details['amount_in_USD'] = tr_details['amount_in'] * token_in.get('price', 0) tr_details['amount_out_USD'] = tr_details['amount_out'] * token_out.get('price', 0) except Exception as e: logging.error(f"Error fetching token prices: {e}") message_text = ( f"Swap detected: \n" f"Token In: {tr_details['symbol_in']} ({tr_details['token_in']})\n" f"Token Out: {tr_details['symbol_out']} ({tr_details['token_out']})\n" f"Amount In USD: {tr_details['amount_in_USD']:.2f}\n" f"Percentage Swapped: {tr_details['percentage_swapped']:.2f}%" ) await telegram_utils.send_telegram_message(message_text) await follow_move(tr_details) await save_token_info() except Exception as e: logging.error(f"Error aquiring log details and following: {e}") await telegram_utils.send_telegram_message(f"Not followed! Error following move.") except Exception as e: logging.error(f"Error processing log: {e}") PROCESSING_LOG = False return tr_details # "Program log: Instruction: Swap2", # "Program log: order_id: 13985890735038016", # "Program log: AbrMJWfDVRZ2EWCQ1xSCpoVeVgZNpq1U2AoYG98oRXfn", source # "Program log: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", target # "Program log: before_source_balance: 58730110139, before_destination_balance: 202377778, amount_in: 58730110139, expect_amount_out: 270109505, min_return: 267408410", # "Program log: after_source_balance: 0, after_destination_balance: 472509072", # "Program log: source_token_change: 58730110139, destination_token_change: 270131294", async def get_transaction_details_info(tx_signature_str: str, logs: List[str]) -> Dict[str, Any]: global TOKENS_INFO tr_info = await get_transaction_details_with_retry(tx_signature_str) # Fetch token prices: ToDo: check, but already did # token_prices = await get_token_prices([tr_info['token_in'], tr_info['token_out']]) # # for token, price in token_prices.items(): # # if not token in TOKENS_INFO or not TOKENS_INFO[token].get('symbol'): # # token_name = await get_token_metadata_symbol(token) # # TOKENS_INFO[token] = {'symbol': token_name} # # TOKENS_INFO[token] = {'price': price} # # Calculate USD values # tr_info['amount_in_USD'] = tr_info['amount_in'] * token_prices.get(tr_info['token_in'], 0) # tr_info['amount_out_USD'] = tr_info['amount_out'] * token_prices.get(tr_info['token_out'], 0) # Calculate the percentage of the source balance that was swapped; ToDo: fix decimals for percentage try: tr_info['percentage_swapped'] = (tr_info['amount_in'] / tr_info['before_source_balance']) * 100 if tr_info['before_source_balance'] > 0 else 50 except Exception as e: logging.error(f"Error calculating percentage swapped: {e}") return tr_info def _get_pre_balance(transaction_details: Dict[str, Any], token: str) -> float: pre_balances = transaction_details.get('meta', {}).get('preTokenBalances', []) for balance in pre_balances: if balance['mint'] == token: return float(balance['uiTokenAmount']['amount']) return 0.0 async def follow_move(move): your_balances = await solanaAPI.get_wallet_balances(YOUR_WALLET, doGetTokenName=False) your_balance_info = next((balance for balance in your_balances.values() if balance['address'] == move['token_in']), None) if your_balance_info is not None: # Use the balance print(f"Your balance: {your_balance_info['amount']} {move['symbol_in']}") else: print("No ballance found for {move['symbol_in']}. Skipping move.") await telegram_utils.send_telegram_message(f"No ballance found for {move['symbol_in']}. Skipping move.") return your_balance = your_balance_info['amount'] token_info = TOKENS_INFO.get(move['token_in']) token_name_in = token_info.get('symbol') or await get_token_metadata(move['token_in']) token_name_out = TOKENS_INFO[move['token_out']].get('symbol') or await solanaAPI.get_token_metadata_symbol(move['token_out']) if not your_balance: msg = f"Move not followed:\nNo balance found for token {move['symbol_in']}. Cannot follow move." logging.warning(msg) await telegram_utils.send_telegram_message(msg) return # move["percentage_swapped"] = (move["amount_out"] / move["amount_in"]) * 100 # Calculate the amount to swap based on the same percentage as the followed move amount_to_swap = your_balance * (move['percentage_swapped'] / 100) amount_to_swap = min(amount_to_swap, your_balance) # should not happen if token_name_in == 'USDC': # max 300 amount_to_swap = min(amount_to_swap, 300) # # always get 99% of the amount to swap # amount_to_swap = amount_to_swap * 0.95 decimals = token_info.get('decimals') # Convert to lamports # if decimals is 6, then amount = amount * 1e6; if 9, then amount = amount * 1e9 amount = int(amount_to_swap * 10**decimals) if your_balance < amount_to_swap: # should not happen msg = ( f"Warning:\n" f"Insufficient balance: {your_balance:.6f} {token_name_in}. We want to swap {amount_to_swap:.6f}\n({move['symbol_in']}, decimals {token_info.get('decimals')} amount {amount}).\n This will probably fail. But we will try anyway." ) logging.warning(msg) await telegram_utils.send_telegram_message(msg) try: try: notification = ( f"Initiating move:\n" f"Swapping {move['percentage_swapped']:.2f}% ({amount_to_swap:.2f}) {token_name_in} for {token_name_out}" ) # logging.info(notification) # error_logger.info(notification) # await telegram_utils.send_telegram_message(notification) except Exception as e: logging.error(f"Error sending notification: {e}") for retry in range(3): try: private_key = Keypair.from_bytes(base58.b58decode(pk)) async_client = AsyncClient(SOLANA_WS_URL) jupiter = Jupiter(async_client, private_key) transaction_data = await jupiter.swap( input_mint=move['token_in'], output_mint=move['token_out'], amount=amount, slippage_bps=100, # Increased to 1% ) logging.info(f"Initiating move. Transaction data:\n {transaction_data}") error_logger.info(f"Initiating move. Transaction data:\n {transaction_data}") raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data)) signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message)) signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature]) opts = TxOpts(skip_preflight=False, preflight_commitment=Processed) # send the transaction result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts) transaction_id = json.loads(result.to_json())['result'] print(f"Follow Transaction Sent: https://solscan.io/tx/{transaction_id}") # append to notification notification += f"\n\nTransaction: {transaction_id}" await telegram_utils.send_telegram_message(f"Follow Transaction Sent: {transaction_id}") tx_details = await get_transaction_details_with_retry(transaction_id) if tx_details is not None: break else: logging.warning(f"Failed to get transaction details for {transaction_id}. Probably transaction failed. Retrying again...") await asyncio.sleep(3) except Exception as e: error_message = f"Move Failed:\n{str(e)}" logging.error(error_message) # log the errors to /logs/errors.log error_logger.error(error_message) error_logger.exception(e) await telegram_utils.send_telegram_message(error_message) amount = amount * 0.75 await get_wallet_balances(YOUR_WALLET, doGetTokenName=False) try: if tx_details is None: logging.info(f"Failed to get transaction details for {transaction_id}") notification = ( f"Move Followed, failed to get transaction details.\n" f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['token_in']}) " f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n" f"\n\nTransaction: {transaction_id}" ) else: notification = ( f"Move Followed:\n" f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['symbol_in']}) " f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n" f"for {tx_details['amount_out']:.2f} {token_name_out}" # f"Amount In USD: {tr_details['amount_in_USD']}\n" f"\n\nTransaction: {transaction_id}" ) logging.info(notification) await telegram_utils.send_telegram_message(notification) except Exception as e: logging.error(f"Error sending notification: {e}") except Exception as e: error_message = f"Swap Follow Error:\n{str(e)}" logging.error(error_message) # log the errors to /logs/errors.log error_logger.error(error_message) error_logger.exception(e) \ # if error_message contains 'Program log: Error: insufficient funds' if 'insufficient funds' in error_message: await telegram_utils.send_telegram_message("Insufficient funds. Cannot follow move. Please check your balance.") else: await telegram_utils.send_telegram_message(error_message) # Helper functions pk = os.getenv("PK") async def check_PK(): global pk if not pk: try: script_dir = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(script_dir, 'secret.pk'), 'r') as f: pk = f.read().strip() if pk: logging.info("Private key loaded successfully from file.") else: logging.warning("Private key file is empty.") except FileNotFoundError: logging.warning("Private key file not found.") except Exception as e: logging.error(f"Error reading private key file: {str(e)}") if not pk: logging.error("Private key not found in environment variables. Will not be able to sign transactions.") # send TG warning message await telegram_utils.send_telegram_message("Warning: Private key not found in environment variables. Will not be able to sign transactions.") solanaAPI = SolanaAPI(process_transaction_callback=process_log) async def main(): global solanaAPI, bot, PROCESSING_LOG await telegram_utils.send_telegram_message("Solana Agent Started. Connecting to mainnet...") await check_PK() # new: restart wallet_watch_loop every hour await solanaAPI.wallet_watch_loop() # while True: # wallet_watch_task = asyncio.create_task(solanaAPI.wallet_watch_loop()) # try: # # Wait for an hour or until the task completes, whichever comes first # await asyncio.wait_for(wallet_watch_task, timeout=3600) # except asyncio.TimeoutError: # # If an hour has passed, cancel the task if not PROCESSING # if PROCESSING_LOG: # logging.info("wallet_watch_loop is processing logs. Will not restart.") # await telegram_utils.send_telegram_message("wallet_watch_loop is processing logs. Will not restart.") # else: # wallet_watch_task.cancel() # try: # await wallet_watch_task # except asyncio.CancelledError: # logging.info("wallet_watch_loop was cancelled after running for an hour") # except Exception as e: # logging.error(f"Error in wallet_watch_loop: {str(e)}") # await telegram_utils.send_telegram_message(f"Error in wallet_watch_loop: {str(e)}") # logging.info("Restarting wallet_watch_loop") # await telegram_utils.send_telegram_message("Restarting wallet_watch_loop") async def run_flask(): # loop = asyncio.get_running_loop() # await loop.run_in_executor(None, lambda: app.run(debug=False, port=3001, use_reloader=False)) app = init_app() loop = asyncio.get_running_loop() await loop.run_in_executor(None, lambda: app.run(debug=False, port=3001, use_reloader=False)) async def run_all(): await asyncio.gather( init_db(), main(), run_flask() ) if __name__ == '__main__': asyncio.run(run_all())