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

Implement invoice and swap blocking states #168

Merged
merged 1 commit into from
May 23, 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
65 changes: 48 additions & 17 deletions src/components/MintSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@
<q-item-section>
<q-item-label overline>Multimint Swaps</q-item-label>
<q-item-label caption
>Swap funds from one mint to another via Lightning. Note: Leave
room for potential Lightning fees.</q-item-label
>
>Swap funds from one mint to another via Lightning. Note: This is
an experimental feature and should be used carefully. Leave room
for potential Lightning fees. If the incoming payment does not
succeed, check the incoming pending invoice manually by clicking
the refresh button.
</q-item-label>
</q-item-section>
</q-item>
<q-item>
Expand Down Expand Up @@ -306,12 +309,20 @@
)
"
:disable="
!swapData.from_url || !swapData.to_url || !(swapData.amount > 0)
!swapData.from_url ||
!swapData.to_url ||
!(swapData.amount > 0) ||
swapData.from_url == swapData.to_url
"
:loading="swapBlocking"
>
<q-icon size="xs" name="swap_horiz" class="q-pr-xs" />
Swap</q-btn
>
Swap
<template v-slot:loading>
<q-spinner-hourglass size="xs" />
Swap
</template>
</q-btn>
</q-item>
</q-list>
</div>
Expand Down Expand Up @@ -481,6 +492,7 @@ import { useSettingsStore } from "src/stores/settings";
import { useNostrStore } from "src/stores/nostr";
import { useP2PKStore } from "src/stores/p2pk";
import { useWorkersStore } from "src/stores/workers";
import { notifyError, notifyWarning } from "src/js/notify";

export default defineComponent({
name: "MintSettings",
Expand Down Expand Up @@ -516,6 +528,7 @@ export default defineComponent({
show: false,
},
showEditMintDialog: false,
swapBlocking: false,
};
},
computed: {
Expand All @@ -532,6 +545,7 @@ export default defineComponent({
]),
...mapState(useNostrStore, ["pubkey", "mintRecommendations"]),
...mapState(useWalletStore, ["mnemonic"]),
...mapState(useWorkersStore, ["invoiceWorkerRunning"]),
...mapWritableState(useMintsStore, [
"addMintData",
"showAddMintDialog",
Expand All @@ -548,7 +562,14 @@ export default defineComponent({
}
},
},
watch: {},
watch: {
// if swapBlocking is true and invoiceWorkerRunning changes to false, then swapBlocking should be set to false
invoiceWorkerRunning: function (val) {
if (this.swapBlocking && !val) {
this.swapBlocking = false;
}
},
},
methods: {
...mapActions(useNostrStore, [
"init",
Expand Down Expand Up @@ -618,18 +639,28 @@ export default defineComponent({
},
//
mintSwap: async function (from_url, to_url, amount) {
// get invoice
await this.activateMintUrl(to_url);
let invoice = await this.requestMint(amount);
if (this.swapBlocking) {
notifyWarning("Swap in progress");
return;
}
this.swapBlocking = true;
try {
// get invoice
await this.activateMintUrl(to_url);
let invoice = await this.requestMint(amount);

// pay invoice
await this.activateMintUrl(from_url);
await this.decodeRequest(invoice.request);
await this.melt();
// pay invoice
await this.activateMintUrl(from_url);
await this.decodeRequest(invoice.request);
await this.melt();

// settle invoice on other side
await this.activateMintUrl(to_url);
await this.invoiceCheckWorker();
// settle invoice on other side
await this.activateMintUrl(to_url);
await this.invoiceCheckWorker();
} catch (e) {
console.error("Error swapping", e);
notifyError("Error swapping");
}
},
enable_terminal: function () {
// enable debug terminal
Expand Down
2 changes: 1 addition & 1 deletion src/components/PayInvoiceDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
:label="!payInvoiceData.blocking ? 'Pay' : 'Processing...'"
><q-spinner-hourglass
v-if="payInvoiceData.blocking"
color="white"
color="primary"
size="1em"
/></q-btn>
<q-btn v-close-popup flat color="grey" class="q-ml-auto"
Expand Down
16 changes: 8 additions & 8 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ export const useWalletStore = defineStore("wallet", {
if (this.payInvoiceData.blocking) {
throw new Error("already processing an invoice.");
}
this.payInvoiceData.blocking = true;
console.log("#### pay lightning");
if (this.payInvoiceData.invoice == null) {
throw new Error("no invoice provided.");
Expand All @@ -558,12 +557,13 @@ export const useWalletStore = defineStore("wallet", {
"amount with fees",
amount
);
const { keepProofs, sendProofs } = await this.splitToSend(
mintStore.activeMint().unitProofs(mintStore.activeUnit),
amount
);
proofsStore.setReserved(sendProofs, true);
this.payInvoiceData.blocking = true;
try {
const { keepProofs, sendProofs } = await this.splitToSend(
mintStore.activeMint().unitProofs(mintStore.activeUnit),
amount
);
proofsStore.setReserved(sendProofs, true);
// NUT-08 blank outputs for change
let n_outputs = Math.max(Math.ceil(Math.log2(quote.fee_reserve)), 1);
const keysetId = this.getKeyset();
Expand Down Expand Up @@ -614,14 +614,14 @@ export const useWalletStore = defineStore("wallet", {

this.payInvoiceData.invoice = { sat: 0, memo: "", bolt11: "" };
this.payInvoiceData.show = false;
this.payInvoiceData.blocking = false;
return data;
} catch (error: any) {
this.payInvoiceData.blocking = false;
proofsStore.setReserved(sendProofs, false);
console.error(error);
notifyApiError(error);
throw error;
} finally {
this.payInvoiceData.blocking = false;
}
},
// /check
Expand Down
Loading