UNPKG

@getalby/lightning-tools

Version:

Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps

32 lines (30 loc) 1.11 kB
const numSatsInBtc = 100000000; const getFiatBtcRate = async (currency) => { const url = "https://getalby.com/api/rates/" + currency.toLowerCase() + ".json"; const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch rate: ${response.status} ${response.statusText}`); } const data = await response.json(); return data.rate_float / numSatsInBtc; }; const getFiatValue = async ({ satoshi, currency, }) => { const rate = await getFiatBtcRate(currency); return Number(satoshi) * rate; }; const getSatoshiValue = async ({ amount, currency, }) => { const rate = await getFiatBtcRate(currency); return Math.floor(Number(amount) / rate); }; const getFormattedFiatValue = async ({ satoshi, currency, locale, }) => { if (!locale) { locale = "en"; } const fiatValue = await getFiatValue({ satoshi, currency }); return fiatValue.toLocaleString(locale, { style: "currency", currency, }); }; export { getFiatBtcRate, getFiatValue, getFormattedFiatValue, getSatoshiValue }; //# sourceMappingURL=fiat.js.map