parse transaction wip

This commit is contained in:
Dobromir Popov 2024-10-05 19:15:00 +03:00
parent 1eeaec6e84
commit 42c290fbc6

View File

@ -619,41 +619,49 @@ async def process_log(log_result):
except Exception as e: except Exception as e:
logging.error(f"Error processing log: {e}") logging.error(f"Error processing log: {e}")
def parse_swap_logs(logs): def parse_swap_logs(logs):
swap_details = { total_amount_in = 0
"source_token_address": "", total_amount_out = 0
"destination_token_address": "", source_token_address = ""
"amount_in": 0, destination_token_address = ""
"amount_out": 0
}
for log in logs: for log in logs:
if "SwapEvent" in log: if "SwapEvent" in log:
# Extract amounts from SwapEvent parts = log.split("{ ")[1].strip(" }").split(", ")
parts = log.split("amount_in: ")[1].split(", amount_out: ") event_details = {}
swap_details["amount_in"] = int(parts[0]) for part in parts:
swap_details["amount_out"] = int(parts[1].split(" ")[0]) key, value = part.split(": ")
event_details[key.strip()] = value.strip()
# Aggregate amounts
total_amount_in += int(event_details.get("amount_in", 0))
total_amount_out += int(event_details.get("amount_out", 0))
if "source_token_change:" in log: if "source_token_change:" in log:
# Extract source and destination token changes # Extract final source and destination token addresses
changes = log.split(", ") changes = log.split(", ")
for change in changes: for change in changes.Trim('Program log:'):
key, value = change.split(": ") key, value = change.split(": ")
if key == "source_token_change": if key == "source_token_change":
swap_details["amount_in"] = int(value) total_amount_in = int(value)
elif key == "destination_token_change": elif key == "destination_token_change":
swap_details["amount_out"] = int(value) total_amount_out = int(value)
if "Program log:" in log and len(log.split()) == 2: if "Program log:" in log and len(log.split()) == 2:
# Extract token addresses (assuming they are logged as single entries) # Extract token addresses
token_address = log.split(": ")[1] token_address = log.split(": ")[1]
if not swap_details["source_token_address"]: if not source_token_address:
swap_details["source_token_address"] = token_address source_token_address = token_address
elif not swap_details["destination_token_address"]: elif not destination_token_address:
swap_details["destination_token_address"] = token_address destination_token_address = token_address
return swap_details
return {
"source_token_address": source_token_address,
"destination_token_address": destination_token_address,
"total_amount_in": total_amount_in,
"total_amount_out": total_amount_out,
}
async def on_logs(log): async def on_logs(log):
logging.debug(f"Received log: {log}") logging.debug(f"Received log: {log}")