From 5f21a27f0e69ea9d338c54809d55f14583066010 Mon Sep 17 00:00:00 2001 From: padreug Date: Thu, 19 Jun 2025 17:33:16 +0200 Subject: [PATCH] Add test transaction endpoint and UI integration: implement API for simulating transactions with mock data, including commission and discount calculations. Enhance frontend to trigger test transactions and display results in a dialog, improving user experience and testing capabilities. --- static/js/index.js | 52 +++++++++++++++++++++++++++ templates/myextension/index.html | 10 ++++++ views_api.py | 60 ++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/static/js/index.js b/static/js/index.js index 2620c7c..3c333e3 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -60,6 +60,7 @@ window.app = Vue.createApp({ lastPollTime: null, testingConnection: false, runningManualPoll: false, + runningTestTransaction: false, lamassuConfig: null, // Config dialog @@ -515,6 +516,57 @@ window.app = Vue.createApp({ this.runningManualPoll = false } }, + + async testTransaction() { + this.runningTestTransaction = true + try { + const {data} = await LNbits.api.request( + 'POST', + '/myextension/api/v1/dca/test-transaction', + this.g.user.wallets[0].adminkey + ) + + // Show detailed results in a dialog + const details = data.transaction_details + + let dialogContent = `Test Transaction Results

` + dialogContent += `Transaction ID: ${details.transaction_id}
` + dialogContent += `Total Amount: ${details.total_amount_sats} sats
` + dialogContent += `Base Amount: ${details.base_amount_sats} sats
` + dialogContent += `Commission: ${details.commission_amount_sats} sats (${details.commission_percentage}%)
` + if (details.discount > 0) { + dialogContent += `Discount: ${details.discount}%
` + dialogContent += `Effective Commission: ${details.effective_commission}%
` + } + dialogContent += `
Check your wallets to see the distributions!` + + this.$q.dialog({ + title: 'Test Transaction Completed', + message: dialogContent, + html: true, + ok: { + color: 'positive', + label: 'Great!' + } + }) + + // Also show a brief notification + this.$q.notify({ + type: 'positive', + message: `Test transaction processed: ${details.total_amount_sats} sats distributed`, + timeout: 5000 + }) + + // Refresh data + await this.getDeposits() + await this.getLamassuConfig() + + } catch (error) { + LNbits.utils.notifyApiError(error) + } finally { + this.runningTestTransaction = false + } + }, // Legacy Methods (keep for backward compatibility) async closeFormDialog() { diff --git a/templates/myextension/index.html b/templates/myextension/index.html index a7ad3b8..259a355 100644 --- a/templates/myextension/index.html +++ b/templates/myextension/index.html @@ -383,6 +383,16 @@ > Manual Poll + + Test Transaction + diff --git a/views_api.py b/views_api.py index 336c59a..b471db1 100644 --- a/views_api.py +++ b/views_api.py @@ -390,6 +390,66 @@ async def api_manual_poll( ) +@myextension_api_router.post("/api/v1/dca/test-transaction") +async def api_test_transaction( + wallet: WalletTypeInfo = Depends(require_admin_key), + crypto_atoms: int = 103, + commission_percentage: float = 0.03, + discount: float = 0.0, +) -> dict: + """Test transaction processing with simulated Lamassu transaction data""" + try: + from .transaction_processor import transaction_processor + import uuid + from datetime import datetime, timezone + + # Create a mock transaction that mimics Lamassu database structure + mock_transaction = { + "transaction_id": str(uuid.uuid4())[:8], # Short ID for testing + "crypto_amount": crypto_atoms, # Total sats including commission + "fiat_amount": 100, # Mock fiat amount (100 centavos = 1 GTQ) + "commission_percentage": commission_percentage, # Already as decimal + "discount": discount, + "transaction_time": datetime.now(timezone.utc), + "crypto_code": "BTC", + "fiat_code": "GTQ", + "device_id": "test_device", + "status": "confirmed" + } + + # Process the mock transaction through the complete DCA flow + await transaction_processor.process_transaction(mock_transaction) + + # Calculate commission for response + if commission_percentage > 0: + effective_commission = commission_percentage * (100 - discount) / 100 + base_crypto_atoms = int(crypto_atoms / (1 + effective_commission)) + commission_amount_sats = crypto_atoms - base_crypto_atoms + else: + base_crypto_atoms = crypto_atoms + commission_amount_sats = 0 + + return { + "success": True, + "message": "Test transaction processed successfully", + "transaction_details": { + "transaction_id": mock_transaction["transaction_id"], + "total_amount_sats": crypto_atoms, + "base_amount_sats": base_crypto_atoms, + "commission_amount_sats": commission_amount_sats, + "commission_percentage": commission_percentage * 100, # Show as percentage + "effective_commission": effective_commission * 100 if commission_percentage > 0 else 0, + "discount": discount + } + } + + except Exception as e: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Error processing test transaction: {str(e)}" + ) + + # Lamassu Configuration Endpoints @myextension_api_router.get("/api/v1/dca/config")