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.

This commit is contained in:
padreug 2025-06-19 17:33:24 +02:00
parent 5f21a27f0e
commit 5bbcee2afa
2 changed files with 30 additions and 3 deletions

View file

@ -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]: async def get_payments_by_lamassu_transaction(lamassu_transaction_id: str) -> List[DcaPayment]:
return await db.fetchall( return await db.fetchall(
"SELECT * FROM myextension.dca_payments WHERE lamassu_transaction_id = :transaction_id", "SELECT * FROM myextension.dca_payments WHERE lamassu_transaction_id = :transaction_id",

View file

@ -34,7 +34,8 @@ from .crud import (
get_active_lamassu_config, get_active_lamassu_config,
update_config_test_result, update_config_test_result,
update_poll_start_time, update_poll_start_time,
update_poll_success_time update_poll_success_time,
update_dca_payment_status
) )
from .models import CreateDcaPaymentData, LamassuTransaction, DcaClient from .models import CreateDcaPaymentData, LamassuTransaction, DcaClient
@ -601,8 +602,12 @@ class LamassuTransactionProcessor:
# Send Bitcoin to client's wallet # Send Bitcoin to client's wallet
success = await self.send_dca_payment(client, distribution, transaction_id) success = await self.send_dca_payment(client, distribution, transaction_id)
if success: 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") logger.info(f"DCA payment sent to client {client_id[:8]}...: {distribution['sats_amount']} sats")
else: 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]}...") logger.error(f"Failed to send DCA payment to client {client_id[:8]}...")
except Exception as e: except Exception as e:
@ -693,10 +698,16 @@ class LamassuTransactionProcessor:
crypto_atoms = transaction["crypto_amount"] # Full amount including commission crypto_atoms = transaction["crypto_amount"] # Full amount including commission
transaction_id = transaction["transaction_id"] 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 # Credit the source wallet with the full crypto_atoms amount
await update_wallet_balance( await update_wallet_balance(
wallet_id=admin_config.source_wallet_id, wallet=source_wallet,
amount=crypto_atoms * 1000 # Convert sats to millisats amount=crypto_atoms # Function expects sats, not millisats
) )
logger.info(f"Credited source wallet with {crypto_atoms} sats from transaction {transaction_id}") 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}") logger.error(f"Error crediting source wallet for transaction {transaction.get('transaction_id', 'unknown')}: {e}")
return False 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: async def send_commission_payment(self, transaction: Dict[str, Any], commission_amount_sats: int) -> bool:
"""Send commission to the configured commission wallet""" """Send commission to the configured commission wallet"""
try: try: