diff --git a/package.json b/package.json index f1fc27f..cd0a3c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minibits_wallet", - "version": "0.1.9-beta.32", + "version": "0.1.9-beta.33", "private": true, "scripts": { "android:clean": "cd android && ./gradlew clean", diff --git a/src/models/Mint.ts b/src/models/Mint.ts index 30f2ab4..59b0c7a 100644 --- a/src/models/Mint.ts +++ b/src/models/Mint.ts @@ -54,7 +54,7 @@ export const MintProofsCounterModel = types.model('MintProofsCounter', { self.counter = inFlightTo // temp increase of main counter value self.inFlightTid = inFlightTid - log.trace('[setInFlight]', 'Lock and inflight indexes were set', self) + log.trace('[setInFlight]', 'Lock and inflight indexes were set', {inFlightTid}) }, resetInFlight(inFlightTid: number) { self.inFlightFrom = undefined @@ -66,7 +66,7 @@ export const MintProofsCounterModel = types.model('MintProofsCounter', { increaseProofsCounter(numberOfProofs: number) { if(isNaN(self.counter)) self.counter = 0 self.counter += numberOfProofs - log.trace('[increaseProofsCounter]', 'Increased proofsCounter', {numberOfProofs, counter: self.counter}) + log.info('[increaseProofsCounter]', 'Increased proofsCounter', {numberOfProofs, counter: self.counter}) }, decreaseProofsCounter(numberOfProofs: number) { self.counter -= numberOfProofs @@ -167,7 +167,6 @@ export const MintModel = types self.proofsCounters.push(counter) self.proofsCounters = cast(self.proofsCounters) } - }, removeProofsCounter(counter: MintProofsCounter) { @@ -198,15 +197,20 @@ export const MintModel = types })) .actions(self => ({ createProofsCounter(keyset: CashuMintKeyset) { + const existing = self.getProofsCounter(keyset.id) + + if(!existing) { + const newCounter = MintProofsCounterModel.create({ + keyset: keyset.id, + unit: keyset.unit as MintUnit, + counter: 0, + }) + + self.addProofsCounter(newCounter) + return newCounter + } - const newCounter = MintProofsCounterModel.create({ - keyset: keyset.id, - unit: keyset.unit as MintUnit, - counter: 0, - }) - - self.addProofsCounter(newCounter) - return self.getProofsCounter(keyset.id) + return existing } })) .actions(self => ({ @@ -292,10 +296,10 @@ export const MintModel = types const counter = self.proofsCounters.find(p => p.keyset === keysetId) if(!counter) { const keyset = self.keysets.find(k => k.id === keysetId) - if(keyset) { - return self.createProofsCounter(keyset) + if(!keyset) { + throw new AppError(Err.VALIDATION_ERROR, 'Missing keyset.') } - throw new AppError(Err.VALIDATION_ERROR, 'Missing keyset.') + return self.createProofsCounter(keyset) } return counter diff --git a/src/models/ProofsStore.ts b/src/models/ProofsStore.ts index fc20d64..5108342 100644 --- a/src/models/ProofsStore.ts +++ b/src/models/ProofsStore.ts @@ -140,6 +140,10 @@ export const ProofsStoreModel = types const mintsStore = getRootStore(self).mintsStore const mintInstance = mintsStore.findByUrl(newProofs[0].mintUrl as string) + if(!mintInstance) { + throw new AppError(Err.STORAGE_ERROR, 'Could not find mint', {mintUrl: newProofs[0].mintUrl}) + } + for (const [keysetId, keysetProofs] of proofsByKeysetId.entries()) { for (const proof of keysetProofs) { @@ -165,14 +169,12 @@ export const ProofsStoreModel = types } // Find the corresponding counter for this keysetId - const proofsCounter = mintInstance?.getProofsCounterByKeysetId(keysetId) - if (proofsCounter) { - // Increment the counter by the number of proofs to insert - proofsCounter.increaseProofsCounter(keysetProofs.length) - } - } - - log.debug('[addProofs]', `Added new ${addedProofs.length}${isPending ? ' pending' : ''} proofs to the ProofsStore`) + const proofsCounter = mintInstance.getProofsCounterByKeysetId(keysetId) + // Increment the counter by the number of proofs to insert + proofsCounter.increaseProofsCounter(keysetProofs.length) + } + + log.info('[addProofs]', `Added new ${addedProofs.length}${isPending ? ' pending' : ''} proofs to the ProofsStore`) if (addedProofs.length > 0) { Database.addOrUpdateProofs(addedProofs, isPending) // isSpent = false diff --git a/src/services/wallet/transferTask.ts b/src/services/wallet/transferTask.ts index 4830249..b754f6e 100644 --- a/src/services/wallet/transferTask.ts +++ b/src/services/wallet/transferTask.ts @@ -50,7 +50,8 @@ export const transferTask = async function ( amountToTransfer, unit, meltQuote, - encodedInvoice, + encodedInvoice, + isNwc: nwcEvent ? true : false, createdAt: new Date(), } ] @@ -127,7 +128,8 @@ export const transferTask = async function ( proofs: proofsToPay, mintFeePaid, mintFeeReserve, - isSwapNeeded + isSwapNeeded, + counter } = swapResult proofsToPayAmount = CashuUtils.getProofsAmount(proofsToPay) @@ -135,10 +137,12 @@ export const transferTask = async function ( // TODO in case of swap from inactive keysets, different meltFees might apply than above calculated meltFeeReserve // In such case, we might need to add / substract the fee difference to / from proofsToPay - log.debug('[transfer]', 'Prepared poofsToPay proofs', { + log.info('[transfer]', 'Prepared poofsToPay proofs', { proofsToPayAmount, unit, - transactionId + transactionId, + isSwapNeeded, + counter }) // Update transaction status @@ -148,6 +152,7 @@ export const transferTask = async function ( mintFeePaid, proofsToPayAmount, isSwapNeeded, + counter, createdAt: new Date(), }) @@ -194,8 +199,8 @@ export const transferTask = async function ( lockedProofsCounter.decreaseProofsCounter(countOfInFlightProofs) // release lock - lockedProofsCounter.resetInFlight(transaction.id) - + lockedProofsCounter.resetInFlight(transactionId) + if (state === MeltQuoteState.PAID) { log.debug('[transfer] Invoice PAID', { @@ -277,7 +282,7 @@ export const transferTask = async function ( nwcEvent } as TransactionTaskResult - } else if(state === MeltQuoteState.PENDING) { + } else if(state === MeltQuoteState.PENDING) { log.debug('[transfer] Invoice PENDING', { state, diff --git a/src/services/wallet/utils.ts b/src/services/wallet/utils.ts index 34b2201..f12a14e 100644 --- a/src/services/wallet/utils.ts +++ b/src/services/wallet/utils.ts @@ -29,15 +29,12 @@ const lockAndSetInFlight = async function ( const walletInstance = await walletStore.getWallet(mint.mintUrl, unit, {withSeed: true}) const currentCounter = mint.getProofsCounterByKeysetId!(walletInstance.keys.id) + log.info('[lockAndSetInFlight] Before lock', {transactionId, counter: currentCounter.counter}) + if(!currentCounter) { throw new AppError(Err.VALIDATION_ERROR, 'Missing ProofsCounter.') } - // log.trace('[lockAndSetInFlight] proofsCounter before lock', {currentCounter}) - - if(!retryCount) { - retryCount = 10 - } // deprecated, should not be necessary anymore with serial task queue processing if(currentCounter && currentCounter.inFlightTid && currentCounter.inFlightTid !== transactionId) { @@ -49,8 +46,8 @@ const lockAndSetInFlight = async function ( await delay(1000) - if (retryCount < 50) { - // retry to acquire lock, increment the count of retries up to 50 seconds + if (retryCount < 10) { + // retry to acquire lock, increment the count of retries up to 10 seconds return lockAndSetInFlight( mint, unit, diff --git a/src/services/walletService.ts b/src/services/walletService.ts index b539924..33bbd54 100644 --- a/src/services/walletService.ts +++ b/src/services/walletService.ts @@ -1413,6 +1413,7 @@ const _handleClaimTask = async function (params: { if(result && result.transaction) { const transaction = transactionsStore.findById(result.transaction.id!) const {zapSenderProfile, zapRequest} = claimedToken + let message: string = '' if(transaction) { if (zapSenderProfile) { @@ -1432,10 +1433,12 @@ const _handleClaimTask = async function (params: { return { mintUrl: decoded.token[0].mint, taskFunction: '_handleClaimTask', - message: 'Ecash sent to your lightning address has been received.', + message: result.error ? result.error.message : 'Ecash sent to your lightning address has been received.', + error: result.error || undefined, proofsCount: decoded.token[0].proofs.length, proofsAmount: result.transaction?.amount, } as WalletTaskResult + } catch (e: any) { log.error(e.name, e.message)