Skip to content

Commit

Permalink
handle mint activation and unit activation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed May 29, 2024
1 parent b38f3b3 commit 41d200a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/components/MintSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
lines="1"
v-if="mint.nickname"
@click="
activateMintUrl(mint.url, (verbose = false), (force = true))
activateMintUrl(mint.url, (verbose = false), (force = false))
"
class="cursor-pointer"
style="word-break: break-word"
Expand All @@ -56,7 +56,7 @@
<q-item-label
lines="1"
@click="
activateMintUrl(mint.url, (verbose = false), (force = true))
activateMintUrl(mint.url, (verbose = false), (force = false))
"
class="cursor-pointer"
style="word-break: break-word"
Expand Down
5 changes: 1 addition & 4 deletions src/pages/WalletPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@
<!-- ////////////////// HISTORY LIST ///////////////// -->

<q-tab-panel name="history">
<HistoryTable
:show-token-dialog="showTokenDialog"
:check-token-spendable="checkTokenSpendable"
/>
<HistoryTable :show-token-dialog="showTokenDialog" />
</q-tab-panel>

<!-- ////////////////// INVOICE LIST ///////////////// -->
Expand Down
18 changes: 15 additions & 3 deletions src/stores/mints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ export const useMintsStore = defineStore("mints", {
} else {
if (this.mints.length) {
console.error("No active mint. This should not happen. switching to first one.")
// fallback
// this.activeMintUrl = this.mints[0].url
this.activateMintUrl(this.mints[0].url, false, true)
return new MintClass(this.mints[0]);
}
Expand Down Expand Up @@ -238,14 +236,28 @@ export const useMintsStore = defineStore("mints", {
this.addMintBlocking = false;
}
},
activateMintUrl: async function (url: string, verbose = false, force = false) {
activateMintUrl: async function (url: string, verbose = false, force = false, unit: string | undefined = undefined) {
const mint = this.mints.filter((m) => m.url === url)[0];
if (mint) {
await this.activateMint(mint, verbose, force);
if (unit) {
await this.activateUnit(unit, verbose);
}
} else {
notifyError("Mint not found", "Mint activation failed");
}
},
activateUnit: async function (unit: string, verbose = false) {
const mint = this.mints.find((m) => m.url === this.activeMintUrl);
if (!mint) {
notifyError("No active mint", "Unit activation failed");
return;
}
const mintClass = new MintClass(mint);
if (mintClass.units.includes(unit)) {
this.activeUnit = unit;
}
},
activateMint: async function (mint: Mint, verbose = false, force = false) {
const workers = useWorkersStore();
if (mint.url === this.activeMintUrl && !force) {
Expand Down
28 changes: 10 additions & 18 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Invoice = {
type InvoiceHistory = Invoice & {
date: string;
status: "pending" | "paid";
mint?: string;
mint: string;
unit?: string;
token?: string;
};
Expand Down Expand Up @@ -371,15 +371,10 @@ export const useWalletStore = defineStore("wallet", {
}
let proofs = token.getProofs(tokenJson);

if (token.getMint(tokenJson) != mintStore.activeMintUrl) {
await mintStore.activateMintUrl(token.getMint(tokenJson));
}
const amount = proofs.reduce((s, t) => (s += t.amount), 0);
// activate the mint and the unit
await mintStore.activateMintUrl(token.getMint(tokenJson), false, false, tokenJson.unit);

// set unit to unit in token
if (tokenJson.unit != undefined) {
mintStore.activeUnit = tokenJson.unit
}
const amount = proofs.reduce((s, t) => (s += t.amount), 0);
try {
// redeem
const keysetId = this.getKeyset()
Expand Down Expand Up @@ -725,9 +720,8 @@ export const useWalletStore = defineStore("wallet", {
const proofs = token.getProofs(tokenJson);

// activate the mint
if (token.getMint(tokenJson).length > 0) {
await mintStore.activateMintUrl(token.getMint(tokenJson));
}
const mintInToken = token.getMint(tokenJson);
await mintStore.activateMintUrl(mintInToken);

const spentProofs = await this.checkProofsSpendable(proofs);
if (spentProofs != undefined && spentProofs.length == proofs.length) {
Expand Down Expand Up @@ -777,9 +771,9 @@ export const useWalletStore = defineStore("wallet", {
throw new Error("invoice not found");
}
try {
if (invoice.mint != mintStore.activeMintUrl && invoice.mint != undefined) {
await mintStore.activateMintUrl(invoice.mint, false);
}
// activate the mint
await mintStore.activateMintUrl(invoice.mint, false, false, invoice.unit);

const proofs = await this.mint(invoice.amount, invoice.quote, verbose);
if (!!window.navigator.vibrate) navigator.vibrate(200);
notifySuccess("Received " + uIStore.formatCurrency(invoice.amount, mintStore.activeUnit) + " via Lightning");
Expand All @@ -799,9 +793,7 @@ export const useWalletStore = defineStore("wallet", {
throw new Error("invoice not found");
}
try {
if (invoice.mint != mintStore.activeMintUrl && invoice.mint != undefined) {
await mintStore.activateMintUrl(invoice.mint, false);
}
await mintStore.activateMintUrl(invoice.mint, false, false, invoice.unit);
// this is an outgoing invoice, we first do a getMintQuote to check if the invoice is paid
const mintQuote = await mintStore.activeMint().api.getMeltQuote(quote);
console.log("### mintQuote", mintQuote);
Expand Down

0 comments on commit 41d200a

Please sign in to comment.