Skip to content

Commit

Permalink
make waiting for zap receipt a subscription since there might be dela…
Browse files Browse the repository at this point in the history
…y in the zapper to publish the receipt
  • Loading branch information
frnandu committed Dec 22, 2024
1 parent d3c777c commit 3cb8452
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/ndk/example/zaps/zap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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']!;
Expand Down
13 changes: 8 additions & 5 deletions packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ class Zaps {
: Filter(kinds: [ZapReceipt.KIND], pTags: [pubKey!])

Check warning on line 78 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L77-L78

Added lines #L77 - L78 were not covered by tests
]);
// 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");

Check warning on line 84 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L81-L84

Added lines #L81 - L84 were not covered by tests
});

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) {
Expand All @@ -89,13 +95,10 @@ class Zaps {
} else {
Logger.log.w("Zap Receipt invalid: $receipt");

Check warning on line 96 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L96

Added line #L96 was not covered by tests
}
timeout.cancel();
_requests.closeSubscription(
zapResponse.zapReceiptResponse!.requestId);

Check warning on line 100 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L98-L100

Added lines #L98 - L100 were not covered by tests
}
}).onError((error) {
_requests.closeSubscription(
zapResponse.zapReceiptResponse!.requestId);
Logger.log.w("timed out waiting for zap receipt for invoice $invoice");
});
} else {
zapResponse.emitReceipt(null);

Check warning on line 104 in packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/usecases/zaps/zaps.dart#L104

Added line #L104 was not covered by tests
Expand Down

0 comments on commit 3cb8452

Please sign in to comment.