Skip to content

Commit

Permalink
dexie works
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Dec 30, 2024
1 parent 1fff18b commit 0531650
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 54 deletions.
7 changes: 4 additions & 3 deletions src/stores/mints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class MintClass {
unitProofs(unit: string) {
const unitKeysets = this.unitKeysets(unit);
return this.proofs.filter((p) =>
unitKeysets.map((k) => k.id).includes(p.id)
unitKeysets.map((k) => k.id).includes(p.id) && !p.reserved
);
}

Expand Down Expand Up @@ -134,7 +134,7 @@ export const useMintsStore = defineStore("mints", {
}

const keysetIds = unitKeysets.map((k) => k.id);
activeProofs.value = proofs.value.filter((p) => keysetIds.includes(p.id));
activeProofs.value = proofs.value.filter((p) => keysetIds.includes(p.id)).filter((p) => !p.reserved);
};

// Watch for changes in activeMintUrl and activeUnit
Expand Down Expand Up @@ -164,6 +164,7 @@ export const useMintsStore = defineStore("mints", {
.filter((k) => k.unit === activeUnit);
const balance = this.proofs
.filter((p) => allUnitKeysets.map((k) => k.id).includes(p.id))
.filter((p) => !p.reserved)
.reduce((sum, p) => sum + p.amount, 0);
return balance;
},
Expand Down Expand Up @@ -238,7 +239,7 @@ export const useMintsStore = defineStore("mints", {
mintUnitProofs(mint: Mint, unit: string): WalletProof[] {
const unitKeysets = mint.keysets.filter((k) => k.unit === unit);
return this.proofs.filter((p) =>
unitKeysets.map((k) => k.id).includes(p.id)
unitKeysets.map((k) => k.id).includes(p.id) && !p.reserved
);
},
toggleUnit: function () {
Expand Down
20 changes: 10 additions & 10 deletions src/stores/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ export const useProofsStore = defineStore("proofs", {
},
async addProofs(proofs: Proof[], quote?: string) {
const walletProofs = this.proofsToWalletProofs(proofs);
const proofsTable = cashuDb.proofs;
walletProofs.forEach((p) => {
proofsTable.add(p);
}
);
await cashuDb.transaction('rw', cashuDb.proofs, async () => {
walletProofs.forEach(async (p) => {
await cashuDb.proofs.add(p);
});
});
},
async removeProofs(proofs: Proof[]) {
const walletProofs = this.proofsToWalletProofs(proofs);
const proofsTable = cashuDb.proofs;
walletProofs.forEach((p) => {
proofsTable.delete(p.secret);
}
);
await cashuDb.transaction('rw', cashuDb.proofs, async () => {
walletProofs.forEach(async (p) => {
await cashuDb.proofs.delete(p.secret);
});
});
},
async getProofsForQuote(quote: string): Promise<WalletProof[]> {
return await cashuDb.proofs.where('quote').equals(quote).toArray();
Expand Down
59 changes: 18 additions & 41 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ export const useWalletStore = defineStore("wallet", {
throw new Error("could not split proofs.");
}

// if (invalidate) {
// await proofsStore.removeProofs(sendProofs);
// }
await proofsStore.setReserved(sendProofs, true);
if (invalidate) {
await proofsStore.removeProofs(sendProofs);
}
return { keepProofs, sendProofs };
} catch (error: any) {
await proofsStore.setReserved(proofsToSend, false);
Expand Down Expand Up @@ -745,29 +745,9 @@ export const useWalletStore = defineStore("wallet", {
) {
const uIStore = useUiStore();
const proofsStore = useProofsStore();
const mintStore = useMintsStore();
const tokenStore = useTokensStore();

console.log("#### pay lightning");
// if (this.payInvoiceData.invoice == null) {
// throw new Error("no invoice provided.");
// }
// const invoice = this.payInvoiceData.invoice.bolt11;
// throw an error if the invoice is already in invoiceHistory
// if (
// this.invoiceHistory.find(
// (i) => i.bolt11 === quote.request && i.amount < 0 && i.status === "paid"
// )
// ) {
// notifyError("Invoice already paid.");
// throw new Error("invoice already paid.");
// }

// const quote = this.payInvoiceData.meltQuote.response;
// if (quote == null) {
// throw new Error("no quote found.");
// }

console.log("#### melt()");
const amount = quote.amount + quote.fee_reserve;
let countChangeOutputs = 0;
const keysetId = this.getKeyset(mintWallet.mint.mintUrl, mintWallet.unit);
Expand All @@ -790,7 +770,8 @@ export const useWalletStore = defineStore("wallet", {

await uIStore.lockMutex();
try {
await this.addOutgoingPendingInvoiceToHistory(quote, sendProofs);
await this.addOutgoingPendingInvoiceToHistory(quote);
await proofsStore.setReserved(sendProofs, true, quote.quote);

// NUT-08 blank outputs for change
const counter = this.keysetCounter(keysetId);
Expand All @@ -817,17 +798,6 @@ export const useWalletStore = defineStore("wallet", {
if (data.quote.state != MeltQuoteState.PAID) {
throw new Error("Invoice not paid.");
}
let amount_paid = amount - proofsStore.sumProofs(data.change);
useUiStore().vibrate();

notifySuccess(
"Paid " +
uIStore.formatCurrency(amount_paid, mintWallet.unit) +
" via Lightning"
);
console.log("#### pay lightning: token paid");
// delete spent tokens from db
await proofsStore.removeProofs(sendProofs);

// NUT-08 get change
if (data.change != null) {
Expand All @@ -838,6 +808,17 @@ export const useWalletStore = defineStore("wallet", {
await proofsStore.addProofs(changeProofs);
}

// delete spent tokens from db
await proofsStore.removeProofs(sendProofs);

let amount_paid = amount - proofsStore.sumProofs(data.change);
useUiStore().vibrate();
notifySuccess(
"Paid " +
uIStore.formatCurrency(amount_paid, mintWallet.unit) +
" via Lightning"
);
console.log("#### pay lightning: token paid");
tokenStore.addPaidToken({
amount: -amount_paid,
token: proofsStore.serializeProofs(sendProofs),
Expand Down Expand Up @@ -940,7 +921,7 @@ export const useWalletStore = defineStore("wallet", {
});
}
}
// return unspent proofs
// return spent proofs
return spentProofs;
} catch (error: any) {
console.error(error);
Expand Down Expand Up @@ -1275,7 +1256,6 @@ export const useWalletStore = defineStore("wallet", {
true
);
if (spentProofs != undefined && spentProofs.length == proofs.length) {
await proofsStore.removeProofs(proofs);
useUiStore().vibrate();
notifySuccess(
"Sent " +
Expand All @@ -1299,10 +1279,7 @@ export const useWalletStore = defineStore("wallet", {
////////////// UI HELPERS //////////////
addOutgoingPendingInvoiceToHistory: async function (
quote: MeltQuoteResponse,
sendProofs: Proof[]
) {
const proofsStore = useProofsStore();
await proofsStore.setReserved(sendProofs, true, quote.quote);
const mintStore = useMintsStore();
this.invoiceHistory.push({
amount: -(quote.amount + quote.fee_reserve),
Expand Down

0 comments on commit 0531650

Please sign in to comment.