rustywallet-mempool 0.2.0

Mempool.space API client for fee estimation, address info, and transaction tracking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! WebSocket support for real-time mempool data.
//!
//! This module provides WebSocket connectivity for receiving
//! real-time updates from mempool.space.

use std::collections::HashSet;
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tokio::sync::{broadcast, RwLock};

use crate::types::FeeEstimates;

/// WebSocket endpoint URLs.
pub const MAINNET_WS_URL: &str = "wss://mempool.space/api/v1/ws";
/// Testnet WebSocket URL.
pub const TESTNET_WS_URL: &str = "wss://mempool.space/testnet/api/v1/ws";
/// Signet WebSocket URL.
pub const SIGNET_WS_URL: &str = "wss://mempool.space/signet/api/v1/ws";

/// WebSocket event types.
#[derive(Debug, Clone)]
pub enum WsEvent {
    /// New block mined
    Block(BlockEvent),
    /// Mempool update
    MempoolInfo(MempoolInfoEvent),
    /// Fee rate update
    Fees(FeeEstimates),
    /// Address transaction detected
    AddressTx(AddressTxEvent),
    /// Transaction confirmed
    TxConfirmed(TxConfirmedEvent),
    /// Connection status changed
    ConnectionStatus(WsConnectionStatus),
}

/// Block event data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockEvent {
    /// Block height
    pub height: u64,
    /// Block hash
    pub hash: String,
    /// Block timestamp
    pub timestamp: u64,
    /// Number of transactions
    pub tx_count: u32,
    /// Block size in bytes
    pub size: u32,
    /// Block weight
    pub weight: u32,
    /// Total fees in satoshis
    pub total_fees: u64,
    /// Median fee rate
    pub median_fee: f64,
}

/// Mempool info event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MempoolInfoEvent {
    /// Number of transactions in mempool
    pub count: u64,
    /// Total size in virtual bytes
    pub vsize: u64,
    /// Total fees in satoshis
    pub total_fee: u64,
    /// Memory usage in bytes
    pub usage: u64,
}

/// Address transaction event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddressTxEvent {
    /// The address
    pub address: String,
    /// Transaction ID
    pub txid: String,
    /// Value change in satoshis (positive = received, negative = sent)
    pub value: i64,
    /// Whether confirmed
    pub confirmed: bool,
}

/// Transaction confirmed event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxConfirmedEvent {
    /// Transaction ID
    pub txid: String,
    /// Block height
    pub block_height: u64,
    /// Block hash
    pub block_hash: String,
}

/// WebSocket connection status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WsConnectionStatus {
    /// Connected to server
    Connected,
    /// Disconnected from server
    Disconnected,
    /// Reconnecting to server
    Reconnecting,
    /// Connection error
    Error,
}

/// WebSocket subscription configuration.
#[derive(Debug, Clone, Default)]
pub struct WsSubscription {
    /// Subscribe to new blocks
    pub blocks: bool,
    /// Subscribe to mempool info updates
    pub mempool_info: bool,
    /// Subscribe to fee updates
    pub fees: bool,
    /// Addresses to track
    pub addresses: HashSet<String>,
    /// Transactions to track
    pub transactions: HashSet<String>,
}

impl WsSubscription {
    /// Create a new empty subscription.
    pub fn new() -> Self {
        Self::default()
    }

    /// Subscribe to new blocks.
    pub fn with_blocks(mut self) -> Self {
        self.blocks = true;
        self
    }

    /// Subscribe to mempool info.
    pub fn with_mempool_info(mut self) -> Self {
        self.mempool_info = true;
        self
    }

    /// Subscribe to fee updates.
    pub fn with_fees(mut self) -> Self {
        self.fees = true;
        self
    }

    /// Track an address.
    pub fn track_address(mut self, address: impl Into<String>) -> Self {
        self.addresses.insert(address.into());
        self
    }

    /// Track multiple addresses.
    pub fn track_addresses(mut self, addresses: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.addresses.extend(addresses.into_iter().map(|a| a.into()));
        self
    }

    /// Track a transaction.
    pub fn track_transaction(mut self, txid: impl Into<String>) -> Self {
        self.transactions.insert(txid.into());
        self
    }

    /// Check if any subscriptions are active.
    pub fn has_subscriptions(&self) -> bool {
        self.blocks || self.mempool_info || self.fees || 
        !self.addresses.is_empty() || !self.transactions.is_empty()
    }
}

/// WebSocket client state (simulated - actual WebSocket requires additional deps).
pub struct WsClientState {
    /// Current subscription
    pub subscription: WsSubscription,
    /// Connection status
    pub status: WsConnectionStatus,
    /// Event broadcaster
    event_tx: broadcast::Sender<WsEvent>,
}

impl WsClientState {
    /// Create new client state.
    pub fn new() -> Self {
        let (event_tx, _) = broadcast::channel(1000);
        Self {
            subscription: WsSubscription::new(),
            status: WsConnectionStatus::Disconnected,
            event_tx,
        }
    }

    /// Subscribe to events.
    pub fn subscribe(&self) -> broadcast::Receiver<WsEvent> {
        self.event_tx.subscribe()
    }

    /// Broadcast an event.
    pub fn broadcast(&self, event: WsEvent) {
        let _ = self.event_tx.send(event);
    }
}

impl Default for WsClientState {
    fn default() -> Self {
        Self::new()
    }
}

/// WebSocket client for real-time mempool data.
///
/// Note: This is a high-level API. Actual WebSocket connectivity
/// requires the `tokio-tungstenite` crate which can be added as
/// an optional dependency.
pub struct MempoolWsClient {
    ws_url: String,
    state: Arc<RwLock<WsClientState>>,
}

impl MempoolWsClient {
    /// Create a new WebSocket client for mainnet.
    pub fn new() -> Self {
        Self::with_url(MAINNET_WS_URL)
    }

    /// Create a new WebSocket client for testnet.
    pub fn testnet() -> Self {
        Self::with_url(TESTNET_WS_URL)
    }

    /// Create a new WebSocket client for signet.
    pub fn signet() -> Self {
        Self::with_url(SIGNET_WS_URL)
    }

    /// Create a new WebSocket client with custom URL.
    pub fn with_url(url: &str) -> Self {
        Self {
            ws_url: url.to_string(),
            state: Arc::new(RwLock::new(WsClientState::new())),
        }
    }

    /// Get the WebSocket URL.
    pub fn url(&self) -> &str {
        &self.ws_url
    }

    /// Subscribe to events.
    pub async fn subscribe(&self) -> broadcast::Receiver<WsEvent> {
        self.state.read().await.subscribe()
    }

    /// Get current connection status.
    pub async fn status(&self) -> WsConnectionStatus {
        self.state.read().await.status
    }

    /// Update subscription configuration.
    pub async fn set_subscription(&self, subscription: WsSubscription) {
        let mut state = self.state.write().await;
        state.subscription = subscription;
    }

    /// Get current subscription.
    pub async fn get_subscription(&self) -> WsSubscription {
        self.state.read().await.subscription.clone()
    }

    /// Track an address for transactions.
    pub async fn track_address(&self, address: impl Into<String>) {
        let mut state = self.state.write().await;
        state.subscription.addresses.insert(address.into());
    }

    /// Untrack an address.
    pub async fn untrack_address(&self, address: &str) {
        let mut state = self.state.write().await;
        state.subscription.addresses.remove(address);
    }

    /// Track a transaction for confirmation.
    pub async fn track_transaction(&self, txid: impl Into<String>) {
        let mut state = self.state.write().await;
        state.subscription.transactions.insert(txid.into());
    }

    /// Untrack a transaction.
    pub async fn untrack_transaction(&self, txid: &str) {
        let mut state = self.state.write().await;
        state.subscription.transactions.remove(txid);
    }

    /// Simulate receiving a block event (for testing).
    #[cfg(test)]
    pub async fn simulate_block(&self, event: BlockEvent) {
        let state = self.state.read().await;
        state.broadcast(WsEvent::Block(event));
    }

    /// Simulate receiving a fee update (for testing).
    #[cfg(test)]
    pub async fn simulate_fees(&self, fees: FeeEstimates) {
        let state = self.state.read().await;
        state.broadcast(WsEvent::Fees(fees));
    }
}

impl Default for MempoolWsClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for WebSocket subscriptions.
pub struct WsSubscriptionBuilder {
    subscription: WsSubscription,
}

impl WsSubscriptionBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            subscription: WsSubscription::new(),
        }
    }

    /// Subscribe to new blocks.
    pub fn blocks(mut self) -> Self {
        self.subscription.blocks = true;
        self
    }

    /// Subscribe to mempool info.
    pub fn mempool_info(mut self) -> Self {
        self.subscription.mempool_info = true;
        self
    }

    /// Subscribe to fee updates.
    pub fn fees(mut self) -> Self {
        self.subscription.fees = true;
        self
    }

    /// Track an address.
    pub fn address(mut self, address: impl Into<String>) -> Self {
        self.subscription.addresses.insert(address.into());
        self
    }

    /// Track a transaction.
    pub fn transaction(mut self, txid: impl Into<String>) -> Self {
        self.subscription.transactions.insert(txid.into());
        self
    }

    /// Build the subscription.
    pub fn build(self) -> WsSubscription {
        self.subscription
    }
}

impl Default for WsSubscriptionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ws_subscription() {
        let sub = WsSubscription::new()
            .with_blocks()
            .with_fees()
            .track_address("addr1");
        
        assert!(sub.blocks);
        assert!(sub.fees);
        assert!(!sub.mempool_info);
        assert!(sub.addresses.contains("addr1"));
        assert!(sub.has_subscriptions());
    }

    #[test]
    fn test_ws_subscription_builder() {
        let sub = WsSubscriptionBuilder::new()
            .blocks()
            .fees()
            .address("addr1")
            .transaction("txid1")
            .build();
        
        assert!(sub.blocks);
        assert!(sub.fees);
        assert!(sub.addresses.contains("addr1"));
        assert!(sub.transactions.contains("txid1"));
    }

    #[test]
    fn test_ws_connection_status() {
        assert_eq!(WsConnectionStatus::Connected, WsConnectionStatus::Connected);
        assert_ne!(WsConnectionStatus::Connected, WsConnectionStatus::Disconnected);
    }

    #[test]
    fn test_block_event() {
        let event = BlockEvent {
            height: 800000,
            hash: "abc123".to_string(),
            timestamp: 1234567890,
            tx_count: 1000,
            size: 1000000,
            weight: 4000000,
            total_fees: 50000000,
            median_fee: 10.5,
        };
        
        assert_eq!(event.height, 800000);
        assert_eq!(event.tx_count, 1000);
    }

    #[tokio::test]
    async fn test_ws_client() {
        let client = MempoolWsClient::new();
        assert_eq!(client.url(), MAINNET_WS_URL);
        assert_eq!(client.status().await, WsConnectionStatus::Disconnected);
    }
}