From 5049e5df78b034b0cc03be06bc8ad24d935cbeca Mon Sep 17 00:00:00 2001 From: minibits-cash Date: Tue, 18 Nov 2025 15:47:59 +0100 Subject: [PATCH] Melt counter fixes and model removal fixes --- dist/BUNDLE_ID | 2 +- src/models/Mint.ts | 7 +++++-- src/models/ProofsStore.ts | 2 ++ src/models/Transaction.ts | 7 ++++++- src/models/WalletStore.ts | 2 +- src/services/walletService.ts | 25 ++++++++++++++++--------- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/dist/BUNDLE_ID b/dist/BUNDLE_ID index bcef554d..5f314d46 100644 --- a/dist/BUNDLE_ID +++ b/dist/BUNDLE_ID @@ -1 +1 @@ -019a65ce-c4d4-743b-82e5-36596526cf2b \ No newline at end of file +019a7dcc-68e1-7ed1-83b7-69f47c8a166b \ No newline at end of file diff --git a/src/models/Mint.ts b/src/models/Mint.ts index 7ca26c79..1cbe02e2 100644 --- a/src/models/Mint.ts +++ b/src/models/Mint.ts @@ -57,8 +57,11 @@ export const MintProofsCounterModel = types.model('MintProofsCounter', { } }, removeInFlightRequest(transactionId: number) { - // Only work with the array if this node is alive - if (!isAlive(self)) return + + if (!isAlive(self)) { + log.error('[removeInFlightRequest]', 'ProofsCounter is not alive, aborting removal', { keyset: self.keyset }) + return + } // Create a shallow copy to avoid modifying during iteration const idx = self.inFlightRequests.findIndex(r => r.transactionId === transactionId) diff --git a/src/models/ProofsStore.ts b/src/models/ProofsStore.ts index 4d12e11e..1a446df0 100644 --- a/src/models/ProofsStore.ts +++ b/src/models/ProofsStore.ts @@ -154,6 +154,8 @@ export const ProofsStoreModel = types // Update counters for (const [keysetId, proofs] of proofsByKeyset) { + if(isPending) continue // do not update counters for move to pending proofs + const counter = mintInstance.getProofsCounterByKeysetId(keysetId) counter?.increaseProofsCounter(proofs.length) } diff --git a/src/models/Transaction.ts b/src/models/Transaction.ts index 411422e2..5d1bb3d4 100644 --- a/src/models/Transaction.ts +++ b/src/models/Transaction.ts @@ -1,4 +1,4 @@ -import { Instance, SnapshotIn, SnapshotOut, types } from 'mobx-state-tree' +import { Instance, isAlive, SnapshotIn, SnapshotOut, types } from 'mobx-state-tree' import { log } from '../services/logService' import { MintUnit } from '../services/wallet/currency' import { Database } from '../services' @@ -73,6 +73,11 @@ export const TransactionModel = types // Update multiple fields in database with a single query const updatedTransaction = Database.updateTransaction(self.id, fields) + if (!isAlive(self)) { + log.error('[update]', 'Transaction instance is not alive, aborting state update', { id: self.id }) + return + } + // Update the model to keep store in sync Object.keys(updatedTransaction).forEach(key => { ;(self as any)[key] = (updatedTransaction as any)[key] diff --git a/src/models/WalletStore.ts b/src/models/WalletStore.ts index 1abb7e3b..4aaae690 100644 --- a/src/models/WalletStore.ts +++ b/src/models/WalletStore.ts @@ -827,7 +827,7 @@ export const WalletStoreModel = types currentCounter.increaseProofsCounter(countChangeOutputs) } - log.debug('[WalletStore.payLightningMelt] counter before melt', {localCounter: currentCounter.counter, counterValueForMelt}) + log.trace('[WalletStore.payLightningMelt] Counter state before melt', {localCounter: currentCounter.counter, counterValueForMelt}) const meltParams: MeltParams = options?.inFlightRequest?.request || { meltQuote, diff --git a/src/services/walletService.ts b/src/services/walletService.ts index 83161b85..4790afc4 100644 --- a/src/services/walletService.ts +++ b/src/services/walletService.ts @@ -1531,17 +1531,24 @@ const handlePendingQueue = async function (): Promise { if (isBefore(transfer.expiresAt as Date, new Date())) { log.debug('[handlePendingQueue]', `Transfer invoice expired: ${transfer.paymentId} ${transfer.quote}`) - const transactionData = JSON.parse(transfer.data) - transactionData.push({ + const update = { status: TransactionStatus.EXPIRED, - message: 'Invoice expired', + message: '[handlePendingQueue] related invoice expired', createdAt: new Date(), - }) - - transfer.update({ - status: TransactionStatus.EXPIRED, - data: JSON.stringify(transactionData) - }) + } + + try { + const transactionData = JSON.parse(transfer.data) + transactionData.push(update) + + transfer.update({ + status: TransactionStatus.EXPIRED, + data: JSON.stringify(transactionData) + }) + } catch (e) { + transfer.update(update) + } + } } }