log processor

This commit is contained in:
Dobromir Popov
2024-11-06 11:08:59 +02:00
parent fb09daf983
commit 3dd7e315c7
6 changed files with 94 additions and 8 deletions

View File

@ -767,6 +767,16 @@ class SolanaAPI:
slippage_bps=300, # Increased to 3%
)
logging.info(f"Initiating move. Transaction data:\n {transaction_data}")
fee = async_client.get_fee_for_message(transaction_data.message)
# priority_fee = 0
# if PRIORITY:
# priority_fee = 100 * PRIORITY # defalt if we can't get current rate
# try:
# priority_fee = await calculate_priority_fee(async_client, PRIORITY)
# except:
# logging.warning(f"Failed to get priority fee. Using default value: {priority_fee}")
# error_logger.info(f"Initiating move. Transaction data:\n {transaction_data}")
raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
# message = raw_transaction.message
@ -774,8 +784,12 @@ class SolanaAPI:
# signature = private_key.sign_message( bytes(message) )
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)
opts = TxOpts(
skip_preflight=False,
preflight_commitment=Processed,
# max_retries=10,
# priority_fee =priority_fee
)
# send the transaction
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
@ -842,6 +856,30 @@ class SolanaAPI:
logging.error(f"Error following move: {e}")
async def calculate_priority_fee(async_client, priority_level=5):
recent_fees = await async_client.get_recent_prioritization_fees()
if not recent_fees:
return 1000 # fallback value in microlamports
# Calculate average and max fees
fees = [fee.prioritization_fee for fee in recent_fees]
avg_fee = sum(fees) / len(fees)
max_fee = max(fees)
# Calculate base fee (weighted average between mean and max)
base_fee = (2 * avg_fee + max_fee) / 3 # You can adjust this weighting
# Calculate scaling factor (priority_level / 5)
# priority 5 = 1x base_fee
# priority 10 = 2x base_fee
# priority 1 = 0.2x base_fee
scaling_factor = priority_level / 5
final_fee = int(base_fee * scaling_factor)
# Set minimum fee to avoid too low values
return max(final_fee, 100) # minimum 100 microlamports
class SolanaDEX:
def __init__(self, DISPLAY_CURRENCY: str):

View File

@ -0,0 +1,36 @@
import os
import asyncio
from pathlib import Path
from .storage import store_transaction
LOG_DIRECTORY = "path/to/log/directory"
FILE_MASK = "*.log"
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 = {}
# Store the transaction
await store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details)
# 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

View File

@ -14,6 +14,9 @@ async def init_db():
await prisma_client.connect()
async def store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details=None):
"""
Store a transaction record in the database.
"""
await prisma_client.transaction.create(
data={
'wallet_id': wallet_id,