Skip to content

Commit bcf3148

Browse files
committed
nwc: Add Monitor to NostrWalletConnectOptions
1 parent a2efe90 commit bcf3148

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

crates/nwc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
### Added
2929

3030
- Add notification support for real-time payment updates (https://github.com/rust-nostr/nostr/pull/953)
31+
- Add Monitor to NostrWalletConnectOptions (https://github.com/rust-nostr/nostr/pull/989)
3132

3233
## v0.42.0 - 2025/05/20
3334

crates/nwc/examples/nwc.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ async fn main() -> Result<()> {
2020
// Parse URI and compose NWC client
2121
let uri: NostrWalletConnectURI =
2222
NostrWalletConnectURI::from_str(&nwc_uri_string).expect("Failed to parse NWC URI");
23-
let nwc: NWC = NWC::new(uri);
23+
24+
// Create monitor and subscribe to it
25+
let monitor = Monitor::new(100);
26+
let mut monitor_sub = monitor.subscribe();
27+
tokio::spawn(async move {
28+
while let Ok(notification) = monitor_sub.recv().await {
29+
println!("Notification: {notification:?}");
30+
}
31+
});
32+
33+
// Create NWC client with monitor
34+
let nwc: NWC = NWC::with_opts(uri, NostrWalletConnectOptions::default().monitor(monitor));
2435

2536
// Get balance
2637
let balance = nwc.get_balance().await?;

crates/nwc/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ impl NWC {
5353

5454
/// New `NWC` client with custom [`NostrWalletConnectOptions`].
5555
pub fn with_opts(uri: NostrWalletConnectURI, opts: NostrWalletConnectOptions) -> Self {
56+
let pool = match opts.monitor.as_ref() {
57+
Some(monitor) => RelayPool::builder().monitor(monitor.clone()).build(),
58+
None => RelayPool::default(),
59+
};
60+
5661
Self {
5762
uri,
58-
pool: RelayPool::default(),
63+
pool,
5964
opts,
6065
bootstrapped: Arc::new(AtomicBool::new(false)),
6166
notifications_subscribed: Arc::new(AtomicBool::new(false)),

crates/nwc/src/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::time::Duration;
88

9+
use nostr_relay_pool::monitor::Monitor;
910
use nostr_relay_pool::{ConnectionMode, RelayOptions};
1011

1112
/// Default timeout
@@ -16,13 +17,15 @@ pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
1617
pub struct NostrWalletConnectOptions {
1718
pub(super) relay: RelayOptions,
1819
pub(super) timeout: Duration,
20+
pub(super) monitor: Option<Monitor>,
1921
}
2022

2123
impl Default for NostrWalletConnectOptions {
2224
fn default() -> Self {
2325
Self {
2426
relay: RelayOptions::default(),
2527
timeout: DEFAULT_TIMEOUT,
28+
monitor: None,
2629
}
2730
}
2831
}
@@ -48,4 +51,11 @@ impl NostrWalletConnectOptions {
4851
self.timeout = timeout;
4952
self
5053
}
54+
55+
/// Set Relay Pool monitor
56+
#[inline]
57+
pub fn monitor(mut self, monitor: Monitor) -> Self {
58+
self.monitor = Some(monitor);
59+
self
60+
}
5161
}

0 commit comments

Comments
 (0)