From 5bbcee2afab597d41ef156f40f8c8487c4c46b9b Mon Sep 17 00:00:00 2001 From: padreug Date: Thu, 19 Jun 2025 17:33:24 +0200 Subject: [PATCH] Add DCA payment status update functionality: implement update_dca_payment_status method in CRUD operations and integrate it into the LamassuTransactionProcessor for updating payment statuses to 'confirmed' or 'failed' based on transaction outcomes. Enhance error handling and logging for better traceability. --- crud.py | 8 ++++++++ transaction_processor.py | 25 ++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crud.py b/crud.py index 43e4030..cc3384e 100644 --- a/crud.py +++ b/crud.py @@ -238,6 +238,14 @@ async def get_all_payments() -> List[DcaPayment]: ) +async def update_dca_payment_status(payment_id: str, status: str) -> None: + """Update the status of a DCA payment""" + await db.execute( + "UPDATE myextension.dca_payments SET status = :status WHERE id = :id", + {"status": status, "id": payment_id} + ) + + async def get_payments_by_lamassu_transaction(lamassu_transaction_id: str) -> List[DcaPayment]: return await db.fetchall( "SELECT * FROM myextension.dca_payments WHERE lamassu_transaction_id = :transaction_id", diff --git a/transaction_processor.py b/transaction_processor.py index 6c071e8..6836aea 100644 --- a/transaction_processor.py +++ b/transaction_processor.py @@ -34,7 +34,8 @@ from .crud import ( get_active_lamassu_config, update_config_test_result, update_poll_start_time, - update_poll_success_time + update_poll_success_time, + update_dca_payment_status ) from .models import CreateDcaPaymentData, LamassuTransaction, DcaClient @@ -601,8 +602,12 @@ class LamassuTransactionProcessor: # Send Bitcoin to client's wallet success = await self.send_dca_payment(client, distribution, transaction_id) if success: + # Update payment status to confirmed after successful payment + await self.update_payment_status(dca_payment.id, "confirmed") logger.info(f"DCA payment sent to client {client_id[:8]}...: {distribution['sats_amount']} sats") else: + # Update payment status to failed if payment failed + await self.update_payment_status(dca_payment.id, "failed") logger.error(f"Failed to send DCA payment to client {client_id[:8]}...") except Exception as e: @@ -693,10 +698,16 @@ class LamassuTransactionProcessor: crypto_atoms = transaction["crypto_amount"] # Full amount including commission transaction_id = transaction["transaction_id"] + # Get the source wallet object + source_wallet = await get_wallet(admin_config.source_wallet_id) + if not source_wallet: + logger.error(f"Source wallet {admin_config.source_wallet_id} not found") + return False + # Credit the source wallet with the full crypto_atoms amount await update_wallet_balance( - wallet_id=admin_config.source_wallet_id, - amount=crypto_atoms * 1000 # Convert sats to millisats + wallet=source_wallet, + amount=crypto_atoms # Function expects sats, not millisats ) logger.info(f"Credited source wallet with {crypto_atoms} sats from transaction {transaction_id}") @@ -706,6 +717,14 @@ class LamassuTransactionProcessor: logger.error(f"Error crediting source wallet for transaction {transaction.get('transaction_id', 'unknown')}: {e}") return False + async def update_payment_status(self, payment_id: str, status: str) -> None: + """Update the status of a DCA payment""" + try: + await update_dca_payment_status(payment_id, status) + logger.info(f"Updated payment {payment_id[:8]}... status to {status}") + except Exception as e: + logger.error(f"Error updating payment status for {payment_id}: {e}") + async def send_commission_payment(self, transaction: Dict[str, Any], commission_amount_sats: int) -> bool: """Send commission to the configured commission wallet""" try: