Calculates user balance from journal entries
Refactors user balance calculation to directly parse journal entries, enhancing accuracy and efficiency. This change eliminates reliance on direct database queries and provides a more reliable mechanism for determining user balances. Adds logging for debugging purposes. Also extracts and uses fiat metadata from invoice/payment extras.
This commit is contained in:
parent
5c1c7b1b05
commit
8396331d5a
3 changed files with 71 additions and 74 deletions
10
views_api.py
10
views_api.py
|
|
@ -3,6 +3,7 @@ from decimal import Decimal
|
|||
from http import HTTPStatus
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from loguru import logger
|
||||
from lnbits.core.models import User, WalletTypeInfo
|
||||
from lnbits.decorators import (
|
||||
check_super_user,
|
||||
|
|
@ -1234,6 +1235,8 @@ async def api_generate_payment_invoice(
|
|||
# Calculate proportional fiat amount for this invoice
|
||||
invoice_extra = {"tag": "castle", "user_id": target_user_id}
|
||||
|
||||
logger.info(f"User balance for invoice generation - sats: {user_balance.balance}, fiat_balances: {user_balance.fiat_balances}")
|
||||
|
||||
if user_balance.fiat_balances:
|
||||
# Simple single-currency solution: use the first (and should be only) currency
|
||||
currencies = list(user_balance.fiat_balances.keys())
|
||||
|
|
@ -1267,6 +1270,8 @@ async def api_generate_payment_invoice(
|
|||
"btc_rate": btc_rate,
|
||||
})
|
||||
|
||||
logger.info(f"Invoice extra metadata: {invoice_extra}")
|
||||
|
||||
# Create invoice on castle wallet
|
||||
invoice_data = CreateInvoice(
|
||||
out=False,
|
||||
|
|
@ -1366,12 +1371,15 @@ async def api_record_payment(
|
|||
fiat_currency = None
|
||||
fiat_amount = None
|
||||
if payment.extra and isinstance(payment.extra, dict):
|
||||
logger.info(f"Payment.extra contents: {payment.extra}")
|
||||
fiat_currency = payment.extra.get("fiat_currency")
|
||||
fiat_amount_str = payment.extra.get("fiat_amount")
|
||||
if fiat_amount_str:
|
||||
from decimal import Decimal
|
||||
fiat_amount = Decimal(str(fiat_amount_str))
|
||||
|
||||
logger.info(f"Extracted fiat metadata - currency: {fiat_currency}, amount: {fiat_amount}")
|
||||
|
||||
# Get user's receivable account (what user owes)
|
||||
user_receivable = await get_or_create_user_account(
|
||||
target_user_id, AccountType.ASSET, "Accounts Receivable"
|
||||
|
|
@ -1399,6 +1407,8 @@ async def api_record_payment(
|
|||
reference=data.payment_hash
|
||||
)
|
||||
|
||||
logger.info(f"Formatted payment entry: {entry}")
|
||||
|
||||
# Submit to Fava
|
||||
result = await fava.add_entry(entry)
|
||||
logger.info(f"Payment entry submitted to Fava: {result.get('data', 'Unknown')}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue