kaspa-sdk 0.2.0

A fully functional implementation SDK for the Kaspa Network, covering most of the functionality that interoperates with the Kaspa Network.
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
434
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::{Mutex, broadcast};
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};

use crate::types::{
    Block, KaspaError, Result, Transaction, UtxosChangedNotification,
    VirtualChainChangedNotification,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum KaspaEvent {
    BlockAdded(BlockAddedEvent),
    VirtualChainChanged(VirtualChainChangedNotification),
    UtxosChanged(UtxosChangedNotification),
    FinalityConflict(FinalityConflictEvent),
    NewTransaction(NewTransactionEvent),
    SyncStateChanged(SyncStateChangedEvent),
    PeerStateChanged(PeerStateChangedEvent),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockAddedEvent {
    pub block: Block,
    pub block_hash: String,
    pub daa_score: u64,
    pub blue_score: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FinalityConflictEvent {
    pub violating_block_hash: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NewTransactionEvent {
    pub transaction: Transaction,
    pub transaction_id: String,
    pub is_orphan: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncStateChangedEvent {
    pub is_synced: bool,
    pub current_block_count: u64,
    pub header_count: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerStateChangedEvent {
    pub peer_count: usize,
    pub connected_peers: Vec<String>,
    pub disconnected_peers: Vec<String>,
}

/// event message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventMessage {
    pub jsonrpc: String,
    pub method: String,
    pub params: EventParams,
}

/// event params
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventParams {
    pub result: serde_json::Value,
}

/// event listener
pub trait EventListener: Send + Sync {
    fn on_event(&self, event: &KaspaEvent);
}

#[derive(Debug, Clone)]
pub struct EventListenerConfig {
    pub ws_url: String,
    pub username: Option<String>,
    pub password: Option<String>,
    pub reconnect_interval: std::time::Duration,
    pub max_reconnect_attempts: usize,
}

impl Default for EventListenerConfig {
    fn default() -> Self {
        Self {
            ws_url: "ws://localhost:16110".to_string(),
            username: None,
            password: None,
            reconnect_interval: std::time::Duration::from_secs(5),
            max_reconnect_attempts: 10,
        }
    }
}

pub struct KaspaEventListener {
    config: EventListenerConfig,
    event_sender: broadcast::Sender<KaspaEvent>,
    event_handlers: Arc<Mutex<Vec<Box<dyn Fn(KaspaEvent) + Send + Sync>>>>,
    subscribed_addresses: Arc<Mutex<HashSet<String>>>,
    is_running: Arc<Mutex<bool>>,
}

impl KaspaEventListener {
    /// create a new event
    ///
    /// # Example
    /// ```
    /// let listener = KaspaEventListener::new();
    /// ```
    pub fn new() -> Self {
        let config = EventListenerConfig::default();
        let (event_sender, _) = broadcast::channel(100);
        Self {
            config,
            event_sender,
            event_handlers: Arc::new(Mutex::new(Vec::new())),
            subscribed_addresses: Arc::new(Mutex::new(HashSet::new())),
            is_running: Arc::new(Mutex::new(false)),
        }
    }

    /// creates a new event listener from config
    ///
    /// # Example
    /// ```
    /// let config = EventListenerConfig {
    ///     ws_url: "ws://.....".to_string(),
    ///     ..Default::default()
    /// };
    /// let listener = KaspaEventListener::from_config(config);
    /// ```
    pub fn from_config(config: EventListenerConfig) -> Self {
        let (event_sender, _) = broadcast::channel(100);
        Self {
            config,
            event_sender,
            event_handlers: Arc::new(Mutex::new(Vec::new())),
            subscribed_addresses: Arc::new(Mutex::new(HashSet::new())),
            is_running: Arc::new(Mutex::new(false)),
        }
    }

    /// subscribe to receive events through broadcast channel
    ///
    /// # Example
    /// ```
    /// let mut receiver = listener.subscribe();
    /// tokio::spawn(async move {
    ///     while let Ok(event) = receiver.recv().await {
    ///     }
    /// });
    /// ```
    pub fn subscribe(&self) -> broadcast::Receiver<KaspaEvent> {
        self.event_sender.subscribe()
    }

    /// add an event handling closure.
    ///
    /// # Example
    /// ```
    /// listener.add_event_handler(Box::new(|event| {
    ///     match event {
    ///         KaspaEvent::BlockAdded(block_event) => {
    ///            // ...
    ///         }
    ///         _ => {}
    ///     }
    /// })).await;
    /// ```
    ///
    pub async fn add_event_handler(&self, handler: Box<dyn Fn(KaspaEvent) + Send + Sync>) {
        let mut handlers = self.event_handlers.lock().await;
        handlers.push(handler);
    }

    /// start listening for events
    ///
    /// # Example
    /// ```
    /// listener.start().await?;
    /// ```
    pub async fn start(&self) -> Result<()> {
        let mut running = self.is_running.lock().await;
        if *running {
            return Err(KaspaError::Custom(
                "EventListener is already running".to_string(),
            ));
        }
        *running = true;
        drop(running);
        let config = self.config.clone();
        let event_sender = self.event_sender.clone();
        let event_handlers = self.event_handlers.clone();
        let subscribed_addresses = self.subscribed_addresses.clone();
        let is_running = self.is_running.clone();
        tokio::spawn(async move {
            Self::run_event_loop(
                config,
                event_sender,
                event_handlers,
                subscribed_addresses,
                is_running,
            )
            .await;
        });
        Ok(())
    }

    /// stop listening for events
    ///
    /// # Example
    /// ```
    /// listener.stop().await;
    /// ```
    pub async fn stop(&self) {
        let mut running = self.is_running.lock().await;
        *running = false;
    }

    /// run event loop
    async fn run_event_loop(
        config: EventListenerConfig,
        event_sender: broadcast::Sender<KaspaEvent>,
        event_handlers: Arc<Mutex<Vec<Box<dyn Fn(KaspaEvent) + Send + Sync>>>>,
        subscribed_addresses: Arc<Mutex<HashSet<String>>>,
        is_running: Arc<Mutex<bool>>,
    ) {
        let mut attempt = 0;
        while *is_running.lock().await && attempt < config.max_reconnect_attempts {
            match connect_async(&config.ws_url).await {
                Ok((ws_stream, _)) => {
                    println!("Connected to Kaspa WebSocket");
                    attempt = 0; // Reset attempt counter on successful connection
                    let (mut write, mut read) = ws_stream.split();
                    // Subscribe to events
                    if let Err(e) =
                        Self::subscribe_to_events(&mut write, &subscribed_addresses).await
                    {
                        println!("Failed to subscribe to events: {}", e);
                        continue;
                    }
                    // Event processing loop
                    while *is_running.lock().await {
                        match read.next().await {
                            Some(Ok(message)) => {
                                if let Err(e) =
                                    Self::handle_message(message, &event_sender, &event_handlers)
                                        .await
                                {
                                    println!("Error processing message: {}", e);
                                }
                            }
                            Some(Err(e)) => {
                                println!("WebSocket error: {}", e);
                                break;
                            }
                            None => break,
                        }
                    }
                }
                Err(e) => {
                    println!(
                        "Failed to connect to WebSocket (attempt {}): {}",
                        attempt + 1,
                        e
                    );
                    attempt += 1;
                    tokio::time::sleep(config.reconnect_interval).await;
                }
            }
        }
    }

    /// subscribe to events
    async fn subscribe_to_events(
        write: &mut futures_util::stream::SplitSink<
            tokio_tungstenite::WebSocketStream<
                tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
            >,
            Message,
        >,
        subscribed_addresses: &Arc<Mutex<HashSet<String>>>,
    ) -> Result<()> {
        // subscribe to the latest trading events
        let tx_subscription = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 4,
            "method": "notifyNewTransaction",
            "params": [{
                "includeTransactionDetails": true
            }]
        });
        write
            .send(Message::Text(tx_subscription.to_string()))
            .await
            .map_err(|e| {
                KaspaError::Custom(format!("Failed to subscribe to new transactions: {}", e))
            })?;
        //sSubscribe to block added events
        let block_subscription = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "notifyBlockAdded",
            "params": [""]
        });
        write
            .send(Message::Text(block_subscription.to_string()))
            .await
            .map_err(|e| KaspaError::Custom(format!("Failed to subscribe to blocks: {}", e)))?;
        // Subscribe to virtual chain changed events
        let vc_subscription = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "notifyVirtualChainChanged",
            "params": [{
                "includeAcceptedTransactionIds": true
            }]
        });
        write
            .send(Message::Text(vc_subscription.to_string()))
            .await
            .map_err(|e| {
                KaspaError::Custom(format!("Failed to subscribe to virtual chain: {}", e))
            })?;
        // Subscribe to UTXO changes for addresses
        let addresses = subscribed_addresses.lock().await;
        if !addresses.is_empty() {
            let utxo_subscription = serde_json::json!({
                "jsonrpc": "2.0",
                "id": 3,
                "method": "notifyUtxosChanged",
                "params": [{
                    "addresses": addresses.iter().collect::<Vec<&String>>()
                }]
            });
            write
                .send(Message::Text(utxo_subscription.to_string()))
                .await
                .map_err(|e| KaspaError::Custom(format!("Failed to subscribe to UTXOs: {}", e)))?;
        }
        Ok(())
    }

    /// handle message
    async fn handle_message(
        message: Message,
        event_sender: &broadcast::Sender<KaspaEvent>,
        event_handlers: &Arc<Mutex<Vec<Box<dyn Fn(KaspaEvent) + Send + Sync>>>>,
    ) -> Result<()> {
        if let Message::Text(text) = message {
            if let Ok(event_message) = serde_json::from_str::<EventMessage>(&text) {
                let event = Self::parse_event(&event_message.method, event_message.params.result)?;
                // Send to broadcast channel
                let _ = event_sender.send(event.clone());
                // Call registered handlers
                let handlers = event_handlers.lock().await;
                for handler in handlers.iter() {
                    handler(event.clone());
                }
            }
        }
        Ok(())
    }

    /// parse event
    fn parse_event(method: &str, params: Value) -> Result<KaspaEvent> {
        match method {
            "blockAdded" => {
                let block: Block =
                    serde_json::from_value(params).map_err(|e| KaspaError::JsonError(e))?;
                Ok(KaspaEvent::BlockAdded(BlockAddedEvent {
                    block: block.clone(),
                    block_hash: "".to_string(), // Would need to extract from block
                    daa_score: 0,
                    blue_score: 0,
                }))
            }
            // virtual chain changed
            "virtualChainChanged" => {
                let notification: VirtualChainChangedNotification =
                    serde_json::from_value(params).map_err(|e| KaspaError::JsonError(e))?;
                Ok(KaspaEvent::VirtualChainChanged(notification))
            }
            // Balance changes
            "utxosChanged" => {
                let notification: UtxosChangedNotification =
                    serde_json::from_value(params).map_err(|e| KaspaError::JsonError(e))?;
                Ok(KaspaEvent::UtxosChanged(notification))
            }
            // new transaction
            "newTransaction" => {
                let tx_event: NewTransactionEvent =
                    serde_json::from_value(params).map_err(|e| KaspaError::JsonError(e))?;
                Ok(KaspaEvent::NewTransaction(tx_event))
            }
            "finalityConflict" => Ok(KaspaEvent::FinalityConflict(FinalityConflictEvent {
                violating_block_hash: "".to_string(),
            })),
            _ => Err(KaspaError::Custom(format!(
                "Unknown event method: {}",
                method
            ))),
        }
    }

    /// subscribe to address
    ///
    /// # Example
    /// ```
    /// listener.subscribe_to_address("kaspa:qpm2...").await?;
    /// ```
    pub async fn subscribe_to_address(&self, address: &str) -> Result<()> {
        let mut addresses = self.subscribed_addresses.lock().await;
        addresses.insert(address.to_string());
        Ok(())
    }

    /// unsubscribe from address
    ///
    /// # Example
    /// ```
    /// listener.unsubscribe_from_address("kaspa:qpm2...").await?;
    /// ```
    pub async fn unsubscribe_from_address(&self, address: &str) -> Result<()> {
        let mut addresses = self.subscribed_addresses.lock().await;
        addresses.remove(address);
        Ok(())
    }
}