gogo2/crypto/sol/modules/log_processor.py
Dobromir Popov 5851af8f80 tick
2024-11-11 22:38:05 +02:00

60 lines
2.0 KiB
Python

import os
import asyncio
from pathlib import Path
from .storage import store_transaction, prisma_client
from .SolanaAPI import SolanaAPI
LOG_DIRECTORY = "./logs"
FILE_MASK = "wh_*.json"
async def process_log_file(file_path):
# Read the file and extract transaction data
with open(file_path, 'r') as file:
data = file.read()
# Assume data is parsed into these variables
wallet_id = "extracted_wallet_id"
transaction_type = "extracted_transaction_type"
sell_currency = "extracted_sell_currency"
sell_amount = 0.0
sell_value = 0.0
buy_currency = "extracted_buy_currency"
buy_amount = 0.0
buy_value = 0.0
solana_signature = "extracted_solana_signature"
details = {}
# Process the webhook data
solana_api = SolanaAPI()
transaction_data = await solana_api.process_wh(data)
# Check if the transaction already exists
existing_transaction = await prisma_client.transaction.find_first(
where={'solana_signature': solana_signature}
)
if not existing_transaction:
# Store the transaction if it doesn't exist
transaction_data = {
'wallet_id': wallet_id,
'type': transaction_type,
'sell_currency': sell_currency,
'sell_amount': sell_amount,
'sell_value': sell_value,
'buy_currency': buy_currency,
'buy_amount': buy_amount,
'buy_value': buy_value,
'solana_signature': solana_signature,
'details': details
}
await store_transaction(transaction_data)
# Rename the file to append '_saved'
new_file_path = file_path.with_name(file_path.stem + "_saved" + file_path.suffix)
os.rename(file_path, new_file_path)
async def watch_for_new_logs():
while True:
for file_path in Path(LOG_DIRECTORY).glob(FILE_MASK):
await process_log_file(file_path)
await asyncio.sleep(10) # Check every 10 seconds