Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Convert USD amount to SATs before requesting a lnurl invoice #207

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading