diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index efde53f70..78d093dcc 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -235,8 +235,8 @@ async def api_exchange_rate_history() -> list[dict]: @api_router.get("/api/v1/rate/{currency}") async def api_check_fiat_rate(currency: str) -> dict[str, float]: - rate = await get_fiat_rate_satoshis(currency) - return {"rate": rate} + rate, price = await get_fiat_rate_satoshis(currency) + return {"rate": rate, "price": price} @api_router.get("/api/v1/currencies") diff --git a/lnbits/utils/exchange_rates.py b/lnbits/utils/exchange_rates.py index e51683223..cc8d46d49 100644 --- a/lnbits/utils/exchange_rates.py +++ b/lnbits/utils/exchange_rates.py @@ -199,7 +199,8 @@ async def btc_rates(currency: str) -> list[tuple[str, float]]: provider: ExchangeRateProvider, ) -> Optional[tuple[str, float]]: if currency.lower() in provider.exclude_to: - raise Exception(f"Provider {provider.name} does not support {currency}.") + logger.warning(f"Provider {provider.name} does not support {currency}.") + return None ticker = provider.convert_ticker(currency) url = provider.api_url.format(**replacements(ticker)) @@ -231,6 +232,7 @@ async def btc_rates(currency: str) -> list[tuple[str, float]]: await fetch_price(provider) for provider in settings.lnbits_exchange_rate_providers ] + return [r for r in results if r is not None] @@ -245,18 +247,20 @@ async def btc_price(currency: str) -> float: return sum(rates_values) / len(rates_values) -async def get_fiat_rate_satoshis(currency: str) -> float: +async def get_fiat_rate_satoshis(currency: str) -> tuple[float, float]: price = await cache.save_result( lambda: btc_price(currency), f"btc-price-{currency}", settings.lnbits_exchange_rate_cache_seconds, ) - return float(100_000_000 / price) + return float(100_000_000 / price), price async def fiat_amount_as_satoshis(amount: float, currency: str) -> int: - return int(amount * (await get_fiat_rate_satoshis(currency))) + rate, _ = await get_fiat_rate_satoshis(currency) + return int(amount * (rate)) async def satoshis_amount_as_fiat(amount: float, currency: str) -> float: - return float(amount / (await get_fiat_rate_satoshis(currency))) + rate, _ = await get_fiat_rate_satoshis(currency) + return float(amount / rate)