From 6c8dfeeb80ef6c343ac238d277e3d3a5538c673c Mon Sep 17 00:00:00 2001 From: lollerfirst <43107113+lollerfirst@users.noreply.github.com> Date: Fri, 16 Aug 2024 02:19:33 +0200 Subject: [PATCH] [FIX] Convert USD amount to SATs before requesting a lnurl invoice (#207) * * Added `fetchBitcoinPriceUSD` to wallet.ts, which fetches the price from Coinbase * Modified `lnurlPayFirst`: avoid potentially query the host 2 times. * Modified `lnurlPaySecond`: if `mintStore.activeUnit` is `usd` then calculate the equivalent in satoshis and use that for the invoice. * one unique definition of --- src/components/BalanceView.vue | 17 ++++++----------- src/stores/wallet.ts | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/components/BalanceView.vue b/src/components/BalanceView.vue index 62dde710..e325bf02 100644 --- a/src/components/BalanceView.vue +++ b/src/components/BalanceView.vue @@ -119,13 +119,6 @@ import ToggleUnit from "components/ToggleUnit.vue"; import axios from "axios"; -async function fetchBitcoinPrice() { - var { data } = await axios.get( - "https://api.coinbase.com/v2/exchange-rates?currency=BTC" - ); - return data.data.rates.USD; -} - export default defineComponent({ name: "BalanceView", mixins: [windowMixin], @@ -206,12 +199,14 @@ export default defineComponent({ ...mapActions(useWalletStore, [ "checkPendingInvoices", "checkPendingTokens", + "fetchBitcoinPriceUSD", ]), async fetchPrice() { try { - this.bitcoinPrice = await fetchBitcoinPrice(); + this.bitcoinPrice = await this.fetchBitcoinPriceUSD(); + console.log(`bitcoinPrice: ${this.bitcoinPrice}`); } catch (e) { - console.warn("Could not get Bitcoin price."); + console.warn(`Could not get Bitcoin price. ${e}`); } }, toggleUnit: function () { @@ -221,8 +216,8 @@ export default defineComponent({ return this.activeUnit; }, toggleHideBalance() { - this.hideBalance = !this.hideBalance - } + this.hideBalance = !this.hideBalance; + }, }, }); diff --git a/src/stores/wallet.ts b/src/stores/wallet.ts index 5d646054..649d6b9d 100644 --- a/src/stores/wallet.ts +++ b/src/stores/wallet.ts @@ -1026,11 +1026,18 @@ export const useWalletStore = defineStore("wallet", { mintStore.addMintData = { url: req, nickname: "" } } }, + fetchBitcoinPriceUSD: async function () { + var { data } = await axios.get( + "https://api.coinbase.com/v2/exchange-rates?currency=BTC" + ); + return data.data.rates.USD; + }, lnurlPayFirst: async function (address: string) { var host; if (address.split("@").length == 2) { let [user, lnaddresshost] = address.split("@"); host = `https://${lnaddresshost}/.well-known/lnurlp/${user}`; + var { data } = await axios.get(host); // Moved it here: we don't want 2 potential calls } else if (address.toLowerCase().slice(0, 6) === "lnurl1") { let host = Buffer.from( bech32.fromWords(bech32.decode(address, 20000).words) @@ -1042,7 +1049,6 @@ export const useWalletStore = defineStore("wallet", { notifyError("Invalid LNURL", "LNURL Error"); return; } - var { data } = await axios.get(host); if (data.tag == "payRequest") { this.payInvoiceData.lnurlpay = data; this.payInvoiceData.lnurlpay.domain = host.split("https://")[1].split("/")[0]; @@ -1057,6 +1063,7 @@ export const useWalletStore = defineStore("wallet", { } }, lnurlPaySecond: async function () { + const mintStore = useMintsStore(); let amount = this.payInvoiceData.input.amount; if (this.payInvoiceData.lnurlpay == null) { notifyError("No LNURL data", "LNURL Error"); @@ -1067,6 +1074,22 @@ export const useWalletStore = defineStore("wallet", { this.payInvoiceData.lnurlpay.minSendable <= amount * 1000 && this.payInvoiceData.lnurlpay.maxSendable >= amount * 1000 ) { + if (mintStore.activeUnit == "usd") { + try { + var priceUsd = await this.fetchBitcoinPriceUSD(); + } catch (e) { + notifyError( + "Couldn't get Bitcoin price", + "fetchBitcoinPriceUSD Error" + ); + return; + } + + const satPrice = 1 / (priceUsd / 1e8); + const usdAmount = amount; + amount = Math.floor(usdAmount * satPrice); + console.log(`converted amount: ${amount}`); + } var { data } = await axios.get( `${this.payInvoiceData.lnurlpay.callback}?amount=${amount * 1000}` ); @@ -1076,6 +1099,7 @@ export const useWalletStore = defineStore("wallet", { return; } console.log(data.pr); + console.log(`callback: ${this.payInvoiceData.lnurlpay.callback}`); await this.decodeRequest(data.pr); } },