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:
Vlad Stan
2024-12-13 14:42:39 +02:00
committed by GitHub
parent 524a4c9213
commit 36dbdaa159
2 changed files with 11 additions and 7 deletions

View File

@@ -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)