From dc0eb8e4d6ed75f2440af52db26f59dee3a33fd4 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 10 Oct 2024 09:18:43 +0300 Subject: [PATCH] stoing symbol in transactinos, relaxed retries, removed redundant solana API calls --- crypto/sol/app.py | 49 +++++++++++++++++++++--------------------- crypto/sol/compose.yml | 1 + 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/crypto/sol/app.py b/crypto/sol/app.py index 237ce93..539236e 100644 --- a/crypto/sol/app.py +++ b/crypto/sol/app.py @@ -668,10 +668,12 @@ async def get_transaction_details_rpc(tx_signature, readfromDump=False): if parsed_result["token_in"] is None: parsed_result["token_in"] = transfer['mint'] + parsed_result["symbol_in"] = TOKENS_INFO[transfer['mint']]['symbol'] parsed_result["amount_in"] = transfer['amount']/10**TOKENS_INFO[transfer['mint']]['decimals'] parsed_result["amount_in_USD"] = parsed_result["amount_in"] * TOKENS_INFO[transfer['mint']].get('price', current_price[transfer['mint']]) elif parsed_result["token_out"] is None: parsed_result["token_out"] = transfer['mint'] + parsed_result["symbol_out"] = TOKENS_INFO[transfer['mint']]['symbol'] parsed_result["amount_out"] = transfer['amount']/10**TOKENS_INFO[transfer['mint']]['decimals'] parsed_result["amount_out_USD"] = parsed_result["amount_out"] * TOKENS_INFO[transfer['mint']]['price'] @@ -811,7 +813,7 @@ 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 = 5, max_retries = 12): +async def get_transaction_details_with_retry(transaction_id, retry_delay = 7, max_retries = 11): # 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 @@ -919,9 +921,9 @@ async def process_log(log_result): tr_details["percentage_swapped"] = tr_details["percentage_swapped"] / 1000 - # update token info - all_token_addresses = list(set([tr_details["token_in"], tr_details["token_out"]])) - await get_token_prices(all_token_addresses) + # 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"]] @@ -968,17 +970,17 @@ async def get_transaction_details_info(tx_signature_str: str, logs: List[str]) - tr_info = await get_transaction_details_with_retry(tx_signature_str) - # Fetch token prices - 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} + # 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 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: @@ -1051,6 +1053,7 @@ async def follow_move(move): 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)) @@ -1062,7 +1065,7 @@ async def follow_move(move): transaction_id = json.loads(result.to_json())['result'] print(f"Follow Transaction Sent: https://solscan.io/tx/{transaction_id}") - send_telegram_message(f"Follow Transaction Sent: {transaction_id}") + await 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: @@ -1076,7 +1079,7 @@ async def follow_move(move): if tx_details is None: logging.info(f"Failed to get transaction details for {transaction_id}") notification = ( - f"Move Followed:\n" + 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}" @@ -1103,13 +1106,6 @@ async def follow_move(move): error_logger.error(error_message) error_logger.exception(e) # await send_telegram_message(error_message) - # else: - # msg = ( - # f"Move Not Followed:\n" - # f"Insufficient balance to swap {amount_to_swap:.6f} {token_name_in} ({move['token_in']})" - # ) - # logging.warning(msg) - # await send_telegram_message(msg) # Helper functions (implement these according to your needs) @@ -1152,10 +1148,11 @@ async def subscribe_to_wallet(): await websocket.send(json.dumps(request)) logger.info("Subscription request sent") - + conn_active = False while True: try: response = await websocket.recv() + conn_active = True response_data = json.loads(response) logger.debug(f"Received response: {response_data}") if 'result' in response_data: @@ -1175,7 +1172,9 @@ async def subscribe_to_wallet(): except websockets.exceptions.ConnectionClosedError as e: logger.error(f"Connection closed unexpectedly: {e}") - break + if conn_active: + conn_active = False + await send_telegram_message("Connection to Solana network was closed. Not listening for transactions right now. Attempting to reconnect...") except json.JSONDecodeError as e: logger.error(f"Failed to decode JSON: {e}") except Exception as e: diff --git a/crypto/sol/compose.yml b/crypto/sol/compose.yml index 65425da..64fe3ad 100644 --- a/crypto/sol/compose.yml +++ b/crypto/sol/compose.yml @@ -14,3 +14,4 @@ services: sh -c "pip install -r requirements.txt && python app.py" restart: unless-stopped + \ No newline at end of file