From 3cb8452c9fa6b761e124447733a8e8e77e52011c Mon Sep 17 00:00:00 2001 From: Fmar Date: Sun, 22 Dec 2024 18:35:03 +0100 Subject: [PATCH] make waiting for zap receipt a subscription since there might be delay in the zapper to publish the receipt --- packages/ndk/example/zaps/zap.dart | 2 +- .../ndk/lib/domain_layer/usecases/zaps/zaps.dart | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/ndk/example/zaps/zap.dart b/packages/ndk/example/zaps/zap.dart index f305cc58..7e12b465 100644 --- a/packages/ndk/example/zaps/zap.dart +++ b/packages/ndk/example/zaps/zap.dart @@ -15,7 +15,7 @@ void main() async { final ndk = Ndk(NdkConfig( eventVerifier: Bip340EventVerifier(), cache: MemCacheManager(), - logLevel: Logger.logLevels.info)); + logLevel: Logger.logLevels.trace)); // You need an NWC_URI env var or to replace with your NWC uri connection final nwcUri = Platform.environment['NWC_URI']!; diff --git a/packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart b/packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart index 155188f6..f5849c3a 100644 --- a/packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart +++ b/packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart @@ -78,7 +78,13 @@ class Zaps { : Filter(kinds: [ZapReceipt.KIND], pTags: [pubKey!]) ]); // TODO make timeout waiting for receipt parameterizable somehow - zapResponse.zapReceiptResponse!.stream.timeout(Duration(seconds: 30)).listen((event) { + final timeout = Timer(Duration(seconds: 30), () { + _requests.closeSubscription( + zapResponse.zapReceiptResponse!.requestId); + Logger.log.w("timed out waiting for zap receipt for invoice $invoice"); + }); + + zapResponse.zapReceiptResponse!.stream.listen((event) { String? bolt11 = event.getFirstTag("bolt11"); String? preimage = event.getFirstTag("preimage"); if (bolt11!=null && bolt11 == invoice || preimage!=null && preimage==payResponse.preimage) { @@ -89,13 +95,10 @@ class Zaps { } else { Logger.log.w("Zap Receipt invalid: $receipt"); } + timeout.cancel(); _requests.closeSubscription( zapResponse.zapReceiptResponse!.requestId); } - }).onError((error) { - _requests.closeSubscription( - zapResponse.zapReceiptResponse!.requestId); - Logger.log.w("timed out waiting for zap receipt for invoice $invoice"); }); } else { zapResponse.emitReceipt(null);