parsing tr details succeeded !!!!

This commit is contained in:
Dobromir Popov 2024-10-07 02:14:07 +03:00
parent 14f3674da0
commit d6551660c4

View File

@ -269,7 +269,7 @@ async def get_token_balance_rpc(wallet_address, token_address):
async def get_token_name(mint_address): async def get_token_name(mint_address):
try: try:
token_info = await solana_client.get_token_supply(Pubkey.from_string(mint_address)) token_info = await solana_client.get_account_info_json_parsed(Pubkey.from_string(mint_address))
if token_info.value and 'symbol' in token_info.value: if token_info.value and 'symbol' in token_info.value:
return token_info.value['symbol'] return token_info.value['symbol']
except Exception as e: except Exception as e:
@ -296,7 +296,8 @@ async def get_wallet_balances(wallet_address):
info = parsed_data['info'] info = parsed_data['info']
if isinstance(info, dict) and 'mint' in info and 'tokenAmount' in info: if isinstance(info, dict) and 'mint' in info and 'tokenAmount' in info:
mint = info['mint'] mint = info['mint']
amount = float(info['tokenAmount']['uiAmount']) #amount = float(info['tokenAmount']['amount']) / (10 ** info['tokenAmount']['decimals'])
amount = float(info['tokenAmount']['amount'])/10**info['tokenAmount']['decimals']
if amount > 0: if amount > 0:
token_name = await get_token_name(mint) or 'Unknown' token_name = await get_token_name(mint) or 'Unknown'
balances[mint] = { balances[mint] = {
@ -500,7 +501,7 @@ async def get_transaction_details_rpc(tx_signature, readfromDump=False):
log_messages = result.get("meta", {}).get("logMessages", []) log_messages = result.get("meta", {}).get("logMessages", [])
for log in log_messages: for log in log_messages:
if "order_id" in log: if "order_id" in log:
parsed_result["order_id"] = log.split(":")[1].strip() parsed_result["order_id"] = log.split(":")[2].strip()
break break
# Extract token transfers from innerInstructions # Extract token transfers from innerInstructions
@ -516,15 +517,9 @@ async def get_transaction_details_rpc(tx_signature, readfromDump=False):
if parsed_result["token_in"] is None and amount > 0: if parsed_result["token_in"] is None and amount > 0:
parsed_result["token_in"] = mint parsed_result["token_in"] = mint
parsed_result["amount_in"] = amount parsed_result["amount_in"] = amount
elif parsed_result["token_out"] is None:
parsed_result["token_out"] = mint
parsed_result["amount_out"] = amount
# Calculate USD values if token is USDC # Calculate USD values if token is USDC
if parsed_result["token_in"] == "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v":
parsed_result["amount_in_USD"] = parsed_result["amount_in"]
if parsed_result["token_out"] == "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v":
parsed_result["amount_out_USD"] = parsed_result["amount_out"]
# Calculate percentage swapped # Calculate percentage swapped
if parsed_result["amount_in"] > 0 and parsed_result["amount_out"] > 0: if parsed_result["amount_in"] > 0 and parsed_result["amount_out"] > 0:
@ -555,33 +550,87 @@ async def process_log(log_result):
if log_result['value']['err']: if log_result['value']['err']:
return return
logs = log_result['value']['logs'] logs = log_result['value']['logs']
try: try:
# Detect swap operations in logs # Detect swap operations in logs
swap_operations = ['Program log: Instruction: Swap', 'Program log: Instruction: Swap2'] swap_operations = ['Program log: Instruction: Swap', 'Program log: Instruction: Swap2']
for log_entry in logs: if any(op in logs for op in swap_operations):
if any(op in log_entry for op in swap_operations): # Save the log to a file
try: await save_log(log_result)
tx_signature_str = log_result['value']['signature'] tx_signature_str = log_result['value']['signature']
details = await parse_swap_logs(tx_signature_str, logs)
message_text = (
f"Swap detected:\n"
f"Order ID: {details['order_id']}\n"
f"Token In: {details['token_in']}\n"
f"Token Out: {details['token_out']}\n"
f"Amount In USD: {details['amount_in_USD']}\n"
f"Percentage Swapped: {details['percentage_swapped']:.2f}%"
)
await send_telegram_message(message_text) before_source_balance = 0
await follow_move(details) source_token_change = 0
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
}
i = 0
while i < len(logs):
log_entry = logs[i]
except Exception as e: # Check if we found the 'order_id'
logging.error(f"Error fetching transaction details: {e}") if tr_details["order_id"] is None and "order_id" in log_entry:
return # 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
if tr_details["order_id"] is None or 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:
details = await parse_swap_logs(tx_signature_str, logs)
if before_source_balance > 0 and source_token_change > 0:
tr_details["percentage_swapped"] = (source_token_change / before_source_balance) * 100
if tr_details["order_id"] is not None :
message_text = (
f"Swap detected:\n"
f"Order ID: {tr_details['order_id']}\n"
f"Token In: {tr_details['token_in']}\n"
f"Token Out: {tr_details['token_out']}\n"
f"Amount In USD: {tr_details['amount_in_USD']}\n"
f"Percentage Swapped: {tr_details['percentage_swapped']:.2f}%"
)
await send_telegram_message(message_text)
await follow_move(tr_details)
except Exception as e: except Exception as e:
logging.error(f"Error processing log: {e}") logging.error(f"Error processing log: {e}")
@ -664,6 +713,7 @@ async def follow_move(move):
your_balance = your_balance_info['amount'] your_balance = your_balance_info['amount']
token_name = your_balance_info['name'] token_name = your_balance_info['name']
# move["percentage_swapped"] = (move["amount_out"] / move["amount_in"]) * 100
# Calculate the amount to swap based on the same percentage as the followed move # 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 = your_balance * (move['percentage_swapped'] / 100)
@ -709,13 +759,14 @@ async def follow_move(move):
) )
logging.warning(message) logging.warning(message)
await send_telegram_message(message) await send_telegram_message(message)
# Helper functions (implement these according to your needs) # Helper functions (implement these according to your needs)
async def on_logs(log): async def on_logs(log):
logging.debug(f"Received log: {log}") logging.debug(f"Received log: {log}")
await save_log(log)
await process_log(log) await process_log(log)