mirror of
https://github.com/lnbits/lnbits.git
synced 2025-12-12 21:51:36 +01:00
feat: return the price with the fiat rate (#2823)
* feat: return the price with the fiat rate * fix: not supported currencies
This commit is contained in:
@@ -235,8 +235,8 @@ async def api_exchange_rate_history() -> list[dict]:
|
|||||||
|
|
||||||
@api_router.get("/api/v1/rate/{currency}")
|
@api_router.get("/api/v1/rate/{currency}")
|
||||||
async def api_check_fiat_rate(currency: str) -> dict[str, float]:
|
async def api_check_fiat_rate(currency: str) -> dict[str, float]:
|
||||||
rate = await get_fiat_rate_satoshis(currency)
|
rate, price = await get_fiat_rate_satoshis(currency)
|
||||||
return {"rate": rate}
|
return {"rate": rate, "price": price}
|
||||||
|
|
||||||
|
|
||||||
@api_router.get("/api/v1/currencies")
|
@api_router.get("/api/v1/currencies")
|
||||||
|
|||||||
@@ -199,7 +199,8 @@ async def btc_rates(currency: str) -> list[tuple[str, float]]:
|
|||||||
provider: ExchangeRateProvider,
|
provider: ExchangeRateProvider,
|
||||||
) -> Optional[tuple[str, float]]:
|
) -> Optional[tuple[str, float]]:
|
||||||
if currency.lower() in provider.exclude_to:
|
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)
|
ticker = provider.convert_ticker(currency)
|
||||||
url = provider.api_url.format(**replacements(ticker))
|
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)
|
await fetch_price(provider)
|
||||||
for provider in settings.lnbits_exchange_rate_providers
|
for provider in settings.lnbits_exchange_rate_providers
|
||||||
]
|
]
|
||||||
|
|
||||||
return [r for r in results if r is not None]
|
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)
|
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(
|
price = await cache.save_result(
|
||||||
lambda: btc_price(currency),
|
lambda: btc_price(currency),
|
||||||
f"btc-price-{currency}",
|
f"btc-price-{currency}",
|
||||||
settings.lnbits_exchange_rate_cache_seconds,
|
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:
|
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:
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user