fix(session): price the balance from the warm rate cache only
Some checks failed
lint.yml / fix(session): price the balance from the warm rate cache only (pull_request) Failing after 0s

/session gates the terminal unlocking, and satoshis_amount_as_fiat() on a
cold cache queries external exchanges (~1 s measured on l484). Read the
LNbits btc-price cache directly instead: warm → fiat, miss → null and
the terminal prices the sats itself. No rate lookup ever blocks a tap.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Padreug 2026-09-22 14:23:33 +02:00
commit 8dfd3feba8
2 changed files with 18 additions and 17 deletions

View file

@ -8,7 +8,7 @@ from fastapi import APIRouter, HTTPException, Query, Request
from lnbits.core.crud import get_wallet
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.settings import settings
from lnbits.utils.exchange_rates import satoshis_amount_as_fiat
from lnbits.utils.cache import cache
from lnurl import (
CallbackUrl,
LightningInvoice,
@ -507,8 +507,10 @@ async def api_session(p, c, request: Request, external_id: str):
# Balance + fiat, the way the LNbits wallet page shows them: the wallet's
# own currency first (per-wallet setting, LNbits ≥ 1.6), then the
# instance's default accounting currency; no currency → no fiat, and a
# rate failure never fails the session.
# instance's default accounting currency. The fiat figure comes ONLY from
# LNbits' already-warm rate cache: this response gates the terminal
# unlocking, and a cold lookup queries external exchanges (~1 s). Cache
# miss → `fiat: null`; the terminal prices the sats itself.
wallet = await get_wallet(card.wallet)
balance_msat = int(wallet.balance_msat) if wallet else 0
currency = (getattr(wallet, "currency", None) if wallet else None) or getattr(
@ -516,10 +518,9 @@ async def api_session(p, c, request: Request, external_id: str):
)
fiat = None
if currency:
try:
fiat = await satoshis_amount_as_fiat(balance_msat / 1000, currency)
except Exception:
fiat = None
price = cache.get(f"btc-price-{currency}") # fiat per BTC, as btc_price()
if isinstance(price, (int, float)) and price > 0:
fiat = balance_msat / 1000 / 100_000_000 * float(price)
return {
"authenticated": True,