diff --git a/Bitkit/Services/CoreService.swift b/Bitkit/Services/CoreService.swift index 4f7e6650..e9f1aaed 100644 --- a/Bitkit/Services/CoreService.swift +++ b/Bitkit/Services/CoreService.swift @@ -216,6 +216,31 @@ class ActivityService { } } + func findActivity(byPaymentId paymentId: String) async throws -> Activity { + guard !paymentId.isEmpty else { + throw AppError(message: "Payment ID is empty", debugMessage: nil) + } + + let activities = try await get(filter: .all, limit: 50) + let activity = activities.first { activity in + switch activity { + case let .lightning(ln): + return ln.id == paymentId + case let .onchain(on): + return on.txId == paymentId + } + } + + guard let activity else { + throw AppError( + message: "Activity not found", + debugMessage: "Could not find activity for payment ID: \(paymentId)" + ) + } + + return activity + } + func update(id: String, activity: Activity) async throws { try await ServiceQueue.background(.core) { try updateActivity(activityId: id, activity: activity) diff --git a/Bitkit/ViewModels/AppViewModel.swift b/Bitkit/ViewModels/AppViewModel.swift index e57ab6d7..9bc123a5 100644 --- a/Bitkit/ViewModels/AppViewModel.swift +++ b/Bitkit/ViewModels/AppViewModel.swift @@ -325,7 +325,17 @@ extension AppViewModel { func handleLdkNodeEvent(_ event: Event) { switch event { case let .paymentReceived(paymentId, paymentHash, amountMsat, customRecords): - sheetViewModel.showSheet(.receivedTx, data: ReceivedTxSheetDetails(type: .lightning, sats: amountMsat / 1000)) + // TODO: Temporary fix while ldk-node bug is not fixed + Task { + do { + let _ = try await coreService.activity.findActivity(byPaymentId: paymentHash) + Logger.debug("Activity already exists for payment \(paymentHash), skipping sheet") + } catch { + await MainActor.run { + sheetViewModel.showSheet(.receivedTx, data: ReceivedTxSheetDetails(type: .lightning, sats: amountMsat / 1000)) + } + } + } case .channelPending(channelId: _, userChannelId: _, formerTemporaryChannelId: _, counterpartyNodeId: _, fundingTxo: _): // Only relevant for channels to external nodes break