Skip to content

Commit

Permalink
[FIX] Convert USD amount to SATs before requesting a lnurl invoice (#207
Browse files Browse the repository at this point in the history
)

* * 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
  • Loading branch information
lollerfirst authored Aug 16, 2024
1 parent a039e5f commit 6c8dfee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/components/BalanceView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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 () {
Expand All @@ -221,8 +216,8 @@ export default defineComponent({
return this.activeUnit;
},
toggleHideBalance() {
this.hideBalance = !this.hideBalance
}
this.hideBalance = !this.hideBalance;
},
},
});
</script>
26 changes: 25 additions & 1 deletion src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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];
Expand All @@ -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");
Expand All @@ -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}`
);
Expand All @@ -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);
}
},
Expand Down

0 comments on commit 6c8dfee

Please sign in to comment.