UNPKG

@getalby/lightning-tools

Version:

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

37 lines (34 loc) 1.23 kB
'use strict'; 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, }); }; exports.getFiatBtcRate = getFiatBtcRate; exports.getFiatValue = getFiatValue; exports.getFormattedFiatValue = getFormattedFiatValue; exports.getSatoshiValue = getSatoshiValue; //# sourceMappingURL=fiat.cjs.map