diff --git a/cashu.ts b/cashu.ts index d126581..eb916ee 100644 --- a/cashu.ts +++ b/cashu.ts @@ -34,10 +34,11 @@ export default class PortalBtcWalletCashu { } async redeemCashuQuotes() { - const attempt = this.npubCash.tryRedeemUnredeemedCashuQuotes( - await this.cashuStore.getLastRedeemedCashuQuoteTimestamp(), - ); - for await (const quote of attempt) { + const { proofs, quotes } = await this.npubCash + .tryRedeemUnredeemedCashuQuotes( + await this.cashuStore.getLastRedeemedCashuQuoteTimestamp(), + ); + for (const quote of quotes) { this.cashuTxns.push({ txId: `cashu-quote-${quote.quoteId}`, paymentType: "receive", @@ -46,12 +47,10 @@ export default class PortalBtcWalletCashu { status: "complete", }); } - const proofs = await attempt.next(); - if (!proofs.done || typeof proofs.value === "undefined") return []; - this.proofs.push(...proofs.value); + this.proofs.push(...proofs); await this.persistState(); this.onBalanceUpdated?.(); - return proofs.value; + return proofs; } async persistState() { diff --git a/npubCash.ts b/npubCash.ts index 3f4a96f..ad70485 100644 --- a/npubCash.ts +++ b/npubCash.ts @@ -89,13 +89,20 @@ export default class NpubCash { ); } - async *tryRedeemUnredeemedCashuQuotes( + async tryRedeemUnredeemedCashuQuotes( latestRedeemedCashuQuoteTimestamp: number, ) { - const proofs: Proof[] = []; const quotes: NpubCashQuote[] = await this.getAllPaidQuotes( latestRedeemedCashuQuoteTimestamp, ); + const ret = { + proofs: [] as Proof[], + quotes: [] as { + quoteId: string; + amountSat: number; + timestamp: number; + }[], + }; for (const quote of quotes) { const mint = new CashuMint(quote.mintUrl); const wallet = new CashuWallet(mint); @@ -107,18 +114,18 @@ export default class NpubCash { quote.amount, quote.quoteId, ); - proofs.push(...newProofs); + ret.proofs.push(...newProofs); const amountReceived = newProofs.reduce( (sum, p) => sum + p.amount, 0, ); - yield { + ret.quotes.push({ quoteId: quote.quoteId, amountSat: amountReceived, timestamp: quote.paidAt, - }; + }); } } - return proofs; + return ret; } }