Refactor transaction fetching logic in LamassuTransactionProcessor: update query to retrieve all transactions since the threshold, filter out already processed transactions using local database, and enhance logging to provide detailed insights on new transactions.

This commit is contained in:
padreug 2025-06-18 16:07:33 +02:00
parent a107f825af
commit a4c5d11d99

View file

@ -443,9 +443,8 @@ class LamassuTransactionProcessor:
# Format as UTC for database query # Format as UTC for database query
time_threshold_str = time_threshold.strftime('%Y-%m-%d %H:%M:%S UTC') time_threshold_str = time_threshold.strftime('%Y-%m-%d %H:%M:%S UTC')
# Query for successful cash-out transactions (people selling BTC for fiat) # First, get all transactions since the threshold from Lamassu database
# These are the transactions that trigger DCA distributions lamassu_query = f"""
query = f"""
SELECT SELECT
co.id as transaction_id, co.id as transaction_id,
co.fiat as fiat_amount, co.fiat as fiat_amount,
@ -459,18 +458,28 @@ class LamassuTransactionProcessor:
FROM cash_out_txs co FROM cash_out_txs co
WHERE co.created > '{time_threshold_str}' WHERE co.created > '{time_threshold_str}'
AND co.status = 'confirmed' AND co.status = 'confirmed'
AND co.id NOT IN (
SELECT DISTINCT lamassu_transaction_id
FROM myextension.dca_payments
WHERE lamassu_transaction_id IS NOT NULL
)
ORDER BY co.created DESC ORDER BY co.created DESC
""" """
transactions = await self.execute_ssh_query(db_config, query) all_transactions = await self.execute_ssh_query(db_config, lamassu_query)
logger.info(f"Found {len(transactions)} new transactions to process since {time_threshold}") # Then filter out already processed transactions using our local database
return transactions from .crud import get_all_payments
processed_payments = await get_all_payments()
processed_transaction_ids = {
payment.lamassu_transaction_id
for payment in processed_payments
if payment.lamassu_transaction_id
}
# Filter out already processed transactions
new_transactions = [
tx for tx in all_transactions
if tx['transaction_id'] not in processed_transaction_ids
]
logger.info(f"Found {len(all_transactions)} total transactions since {time_threshold}, {len(new_transactions)} are new")
return new_transactions
except Exception as e: except Exception as e:
logger.error(f"Error fetching transactions from Lamassu database: {e}") logger.error(f"Error fetching transactions from Lamassu database: {e}")