mirror of
https://github.com/lnbits/lnbits.git
synced 2025-12-11 05:01:38 +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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user