diff --git a/src/modules/wallet/services/WalletWebSocketService.ts b/src/modules/wallet/services/WalletWebSocketService.ts index 5b332d0..0969a33 100644 --- a/src/modules/wallet/services/WalletWebSocketService.ts +++ b/src/modules/wallet/services/WalletWebSocketService.ts @@ -231,21 +231,41 @@ export class WalletWebSocketService extends BaseService { this.handlePaymentNotification(data.payment) } - // Handle wallet balance update. - // `wallet_balance` is authoritative and already POST-payment for BOTH - // directions: LNbits re-fetches the wallet AFTER the payment settles - // before emitting the notification (core services/payments.py - // `_send_payment_notification_in_background` → "fetch balance again" → - // services/notifications.py `send_ws_payment_notification` → - // `wallet.balance`). Use it as-is. The previous code subtracted the - // outgoing amount on top, double-deducting (100 → send 10 → showed 80, - // refresh corrected to 90). + // Handle wallet balance update if (data.wallet_balance !== undefined) { - console.log('WalletWebSocketService: Updating balance to', data.wallet_balance, 'sats', { + console.log('WalletWebSocketService: Processing balance update', { + newBalance: data.wallet_balance, hasPayment: !!data.payment, paymentAmount: data.payment?.amount }) - this.updateWalletBalance(data.wallet_balance) + + let finalBalance = data.wallet_balance + + // For outgoing payments, LNbits sends pre-payment balance, so we need to adjust + // For incoming payments, LNbits sends post-payment balance, so use as-is + if (data.payment && data.payment.amount < 0) { + // Outgoing payment - subtract the payment amount from the balance + const paymentSats = Math.abs(data.payment.amount) / 1000 + finalBalance = data.wallet_balance - paymentSats + console.log('WalletWebSocketService: Adjusting balance for outgoing payment', { + originalBalance: data.wallet_balance, + paymentSats: paymentSats, + finalBalance: finalBalance + }) + } else if (data.payment && data.payment.amount > 0) { + // Incoming payment - use balance as-is (already post-payment) + console.log('WalletWebSocketService: Using balance as-is for incoming payment', { + balance: data.wallet_balance + }) + } else { + // No payment in message - use balance as-is + console.log('WalletWebSocketService: Using balance as-is (no payment)', { + balance: data.wallet_balance + }) + } + + console.log('WalletWebSocketService: Updating balance to', finalBalance, 'sats') + this.updateWalletBalance(finalBalance) } } catch (error) {