fix issue with cashu

This commit is contained in:
Danny Morabito 2025-07-10 18:49:30 +02:00
parent 5089dc9365
commit b194ace128
Signed by: dannym
GPG key ID: 7CC8056A5A04557E
2 changed files with 20 additions and 14 deletions

View file

@ -34,10 +34,11 @@ export default class PortalBtcWalletCashu {
} }
async redeemCashuQuotes() { async redeemCashuQuotes() {
const attempt = this.npubCash.tryRedeemUnredeemedCashuQuotes( const { proofs, quotes } = await this.npubCash
await this.cashuStore.getLastRedeemedCashuQuoteTimestamp(), .tryRedeemUnredeemedCashuQuotes(
); await this.cashuStore.getLastRedeemedCashuQuoteTimestamp(),
for await (const quote of attempt) { );
for (const quote of quotes) {
this.cashuTxns.push({ this.cashuTxns.push({
txId: `cashu-quote-${quote.quoteId}`, txId: `cashu-quote-${quote.quoteId}`,
paymentType: "receive", paymentType: "receive",
@ -46,12 +47,10 @@ export default class PortalBtcWalletCashu {
status: "complete", status: "complete",
}); });
} }
const proofs = await attempt.next(); this.proofs.push(...proofs);
if (!proofs.done || typeof proofs.value === "undefined") return [];
this.proofs.push(...proofs.value);
await this.persistState(); await this.persistState();
this.onBalanceUpdated?.(); this.onBalanceUpdated?.();
return proofs.value; return proofs;
} }
async persistState() { async persistState() {

View file

@ -89,13 +89,20 @@ export default class NpubCash {
); );
} }
async *tryRedeemUnredeemedCashuQuotes( async tryRedeemUnredeemedCashuQuotes(
latestRedeemedCashuQuoteTimestamp: number, latestRedeemedCashuQuoteTimestamp: number,
) { ) {
const proofs: Proof[] = [];
const quotes: NpubCashQuote[] = await this.getAllPaidQuotes( const quotes: NpubCashQuote[] = await this.getAllPaidQuotes(
latestRedeemedCashuQuoteTimestamp, latestRedeemedCashuQuoteTimestamp,
); );
const ret = {
proofs: [] as Proof[],
quotes: [] as {
quoteId: string;
amountSat: number;
timestamp: number;
}[],
};
for (const quote of quotes) { for (const quote of quotes) {
const mint = new CashuMint(quote.mintUrl); const mint = new CashuMint(quote.mintUrl);
const wallet = new CashuWallet(mint); const wallet = new CashuWallet(mint);
@ -107,18 +114,18 @@ export default class NpubCash {
quote.amount, quote.amount,
quote.quoteId, quote.quoteId,
); );
proofs.push(...newProofs); ret.proofs.push(...newProofs);
const amountReceived = newProofs.reduce( const amountReceived = newProofs.reduce(
(sum, p) => sum + p.amount, (sum, p) => sum + p.amount,
0, 0,
); );
yield { ret.quotes.push({
quoteId: quote.quoteId, quoteId: quote.quoteId,
amountSat: amountReceived, amountSat: amountReceived,
timestamp: quote.paidAt, timestamp: quote.paidAt,
}; });
} }
} }
return proofs; return ret;
} }
} }