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

Multinut #310

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
146 changes: 146 additions & 0 deletions src/components/MultinutPicker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<template>
<div style="max-width: 800px; margin: 0 auto">
<!-- Mints Selection -->
<div class="q-px-xs text-left" on-left>
<q-list padding>
<q-item>
<q-item-section>
<q-item-label overline class="text-weight-bold">
Mutlinut payment
</q-item-label>
<q-item-label caption>
Select one or multiple mints to execute a payment from.
</q-item-label>
</q-item-section>
</q-item>
</q-list>
</div>

<!-- List of mints with checkboxes -->
<div class="q-pb-md q-px-xs text-left" on-left>
<q-list padding>
<div v-for="mint in mints" :key="mint.url">
<q-item clickable class="q-pb-xs">
<q-item-section avatar>
<q-checkbox
v-model="selectedMints"
:val="mint.url"
:color="selectedMints.includes(mint.url) ? 'primary' : 'grey'"
class="cursor-pointer"
/>
</q-item-section>
<q-item-section>
<q-item-label
lines="1"
class="cursor-pointer"
style="word-break: break-word"
>
{{ mint.nickname || mint.url }}
</q-item-label>
<q-item-label>
<q-badge
v-for="unit in mintClass(mint).units"
:key="unit"
:color="selectedMints.includes(mint.url) ? 'primary' : 'grey'"
:label="
formatCurrency(mintClass(mint).unitBalance(unit), unit)
"
class="q-mx-xs q-mb-xs"
/>
</q-item-label>
</q-item-section>
</q-item>
<q-separator spaced />
</div>
<!-- Total Selected Balance -->
<q-item>
<q-item-section>
<q-item-label overline class="text-weight-bold">
Total Selected Balance
</q-item-label>
<q-item-label caption>
{{ formatCurrency(totalSelectedBalance, activeUnit) }}
</q-item-label>
</q-item-section>
</q-item>
</q-list>
<!-- button to quote all selected mints -->
<q-btn
unelevated
rounded
color="primary"
:disabled="selectedMints.length === 0"
@click="quoteSelectedMints"
label="Quote"
:loading="quoteButtonLoading"
class="q-px-lg"
/>
</div>
</div>
</template>

<script>
import { defineComponent } from "vue";
import { mapActions, mapState, mapWritableState } from "pinia";
import { useMintsStore, MintClass } from "src/stores/mints";
import { useWalletStore } from "src/stores/wallet";

export default defineComponent({
name: "MultinutView",
mixins: [windowMixin],
data() {
return {
selectedMints: [],
};
},
computed: {
...mapWritableState(useWalletStore, ["payInvoiceData"]),
...mapState(useMintsStore, ["mints", "activeUnit"]),
totalSelectedBalance() {
return this.selectedMints.reduce((total, mintUrl) => {
const mint = this.mints.find((m) => m.url === mintUrl);
if (mint) {
const mintInstance = this.mintClass(mint);
return total + mintInstance.unitBalance(this.activeUnit);
}
return total;
}, 0);
},
},
methods: {
...mapActions(useWalletStore, ["meltQuote", "melt"]),
...mapActions(useMintsStore, ["activateMintUrl"]),
mintClass(mint) {
return new MintClass(mint);
},
quoteSelectedMints: async function () {
const totalQuoteAmount = this.payInvoiceData.meltQuote.response.amount;
const nSelectedMints = this.selectedMints.length;
const amountPerMint = totalQuoteAmount / nSelectedMints;
let quotesArray = [];
for (const mintUrl of this.selectedMints) {
console.log(`Quoting mint: ${mintUrl}`);
const mintWallet = useWalletStore().mintWallet(
mintUrl,
useMintsStore().activeUnit
);
// await this.activateMintUrl(mintUrl);
this.payInvoiceData.input.amount = amountPerMint;
const quote = await this.meltQuote(
mintWallet,
this.payInvoiceData.input.request,
amountPerMint
);
console.log(quote);
quotesArray.push(quote);
}
// for (let i = 0; i < nSelectedMints; i++) {
// console.log(`Paying mint: ${this.selectedMints[i]}`);
// await this.activateMintUrl(this.selectedMints[i]);
// this.payInvoiceData.meltQuote.response = quotesArray[i];
// await this.melt();
// }
},
},
});
</script>
20 changes: 6 additions & 14 deletions src/components/PayInvoiceDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<div class="col-12">
<ChooseMint />
</div>
<div class="col-12">
<MultinutPicker />
</div>
<div v-if="enoughActiveBalance" class="row q-mt-lg">
<q-btn
unelevated
Expand Down Expand Up @@ -138,7 +141,7 @@
autofocus
v-model.number="payInvoiceData.input.amount"
type="number"
:label="'Amount (' + tickerShort + ') *'"
:label="'MPP Amount (' + tickerShort + ') *'"
:min="payInvoiceData.lnurlpay.minSendable / 1000"
:max="payInvoiceData.lnurlpay.maxSendable / 1000"
:readonly="
Expand Down Expand Up @@ -231,19 +234,6 @@
>
</div>
</q-form>
<!-- <div v-else>
<q-responsive :ratio="1">
<qrcode-stream
@decode="decodeQR"
class="rounded-borders"
></qrcode-stream>
</q-responsive>
<div class="row q-mt-lg">
<q-btn @click="closeCamera" flat color="grey" class="q-ml-auto">
Close
</q-btn>
</div>
</div> -->
</div>
</q-card>
</q-dialog>
Expand All @@ -262,6 +252,7 @@ import ToggleUnit from "components/ToggleUnit.vue";

// import * as bolt11Decoder from "light-bolt11-decoder";
import * as _ from "underscore";
import MultinutPicker from "./MultinutPicker.vue";
import { Scan as ScanIcon } from "lucide-vue-next";

export default defineComponent({
Expand All @@ -270,6 +261,7 @@ export default defineComponent({
components: {
ChooseMint,
ToggleUnit,
MultinutPicker,
ScanIcon,
},
props: {},
Expand Down
59 changes: 29 additions & 30 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,10 @@ export const useWalletStore = defineStore("wallet", {
lnurlauth: {},
input: {
request: "",
amount: null,
amount: undefined,
comment: "",
quote: "",
} as {
request: string;
amount: number | null;
comment: string;
quote: string;
},
} as { request: string, amount: number | undefined, comment: string, quote: string },
},
};
},
Expand Down Expand Up @@ -706,23 +701,29 @@ export const useWalletStore = defineStore("wallet", {
const data = await this.meltQuote(mintWallet, payload.request);
mintStore.assertMintError(data);
this.payInvoiceData.meltQuote.response = data;
this.payInvoiceData.blocking = false;
return data;
} catch (error: any) {
this.payInvoiceData.blocking = false;
this.payInvoiceData.meltQuote.error = error;
console.error(error);
notifyApiError(error);
throw error;
} finally {
}
},
meltQuote: async function (
wallet: CashuWallet,
request: string
): Promise<MeltQuoteResponse> {
meltQuote: async function (wallet: CashuWallet, request: string, mpp_amount: number | undefined = undefined): Promise<MeltQuoteResponse> {
// const payload: MeltQuotePayload = {
// unit: mintStore.activeUnit,
// request: this.payInvoiceData.input.request,
// };
// this.payInvoiceData.meltQuote.payload = payload;
// const data = await mintStore.activeMint().api.createMeltQuote(payload);
// const data = await this.wallet.createMeltQuote(this.payInvoiceData.input.request, { mpp_amount: this.payInvoiceData.input.amount });
const mintStore = useMintsStore();
const data = await wallet.createMeltQuote(request);
let options = {};
if (mpp_amount) {
options = { mpp: { amount: mpp_amount } };
}
const data = await wallet.createMeltQuote(request, options);
mintStore.assertMintError(data);
return data;
},
Expand Down Expand Up @@ -835,8 +836,8 @@ export const useWalletStore = defineStore("wallet", {

notifySuccess(
"Paid " +
uIStore.formatCurrency(amount_paid, mintWallet.unit) +
" via Lightning"
uIStore.formatCurrency(amount_paid, mintWallet.unit) +
" via Lightning"
);
console.log("#### pay lightning: token paid");
// delete spent tokens from db
Expand Down Expand Up @@ -953,8 +954,6 @@ export const useWalletStore = defineStore("wallet", {
});
}
}
// return unspent proofs
return spentProofs;
} catch (error: any) {
console.error(error);
notifyApiError(error);
Expand Down Expand Up @@ -1098,10 +1097,10 @@ export const useWalletStore = defineStore("wallet", {
const proofStore = useProofsStore();
notifySuccess(
"Sent " +
uIStore.formatCurrency(
proofStore.sumProofs(spentProofs),
historyToken.unit
)
uIStore.formatCurrency(
proofStore.sumProofs(spentProofs),
historyToken.unit
)
);
} else {
console.log("### token not paid yet");
Expand Down Expand Up @@ -1183,8 +1182,8 @@ export const useWalletStore = defineStore("wallet", {
useUiStore().vibrate();
notifySuccess(
"Received " +
uIStore.formatCurrency(invoice.amount, invoice.unit) +
" via Lightning"
uIStore.formatCurrency(invoice.amount, invoice.unit) +
" via Lightning"
);
unsub();
return proofs;
Expand Down Expand Up @@ -1241,8 +1240,8 @@ export const useWalletStore = defineStore("wallet", {
useUiStore().vibrate();
notifySuccess(
"Received " +
uIStore.formatCurrency(invoice.amount, invoice.unit) +
" via Lightning"
uIStore.formatCurrency(invoice.amount, invoice.unit) +
" via Lightning"
);
return proofs;
} catch (error) {
Expand Down Expand Up @@ -1292,10 +1291,10 @@ export const useWalletStore = defineStore("wallet", {
useUiStore().vibrate();
notifySuccess(
"Sent " +
uIStore.formatCurrency(
useProofsStore().sumProofs(proofs),
invoice.unit
)
uIStore.formatCurrency(
useProofsStore().sumProofs(proofs),
invoice.unit
)
);
}
// set invoice in history to paid
Expand Down
Loading