ethereal_rust_sdk 0.1.28

Trading client for Ethereal exchange
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use bytes::Bytes;
use log::{debug, error, info, warn};
use serde::de::DeserializeOwned;
use std::{
    future::Future,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
    time::Duration,
};

use dashmap::DashMap;
use futures_util::sink::SinkExt;
use futures_util::StreamExt;
use thiserror::Error;
use tokio::{
    net::TcpStream,
    sync::{
        mpsc::{self, UnboundedSender},
        oneshot, watch, Mutex,
    },
    task::JoinHandle,
    time::{interval, sleep, Instant, MissedTickBehavior},
};
use yawc::{Frame, MaybeTlsStream, OpCode, Options, WebSocket};

use crate::{
    // channels::public_channels,
    channels::Channels,
    enums::Environment,
    routing::extract_event,
    subscriptions::Subscriptions,
    types::ResponseSender, // utils::{get_server_url, get_typed_callback},
};

const PING_INTERVAL: Duration = Duration::from_secs(5);
const READ_TIMEOUT: Duration = Duration::from_secs(7);

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ConnectionState {
    Disconnected,
    Connected,
    Reconnecting,
    Exited,
}
pub enum InternalCommand {
    Send(Frame),
    Close,
}

#[derive(Error, Debug)]
pub enum ClientError {
    #[error("WebSocket error: {0}")]
    WebsocketError(#[from] yawc::WebSocketError),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Subscription error: {0}")]
    SubscriptionError(String),
    #[error("Transport error: {0}")]
    Transport(#[from] Box<dyn std::error::Error + Send + Sync>),
    #[error("Deserialization error: {0}")]
    Deserialization(#[from] serde_json::Error),
}
type SubscriptionMap = Arc<DashMap<String, (UnboundedSender<bytes::Bytes>, Vec<bytes::Bytes>)>>;

pub struct WsClient {
    write_tx: UnboundedSender<InternalCommand>,
    state_rx: watch::Receiver<ConnectionState>,
    pub environment: Environment,
    supervisor_handle: Arc<Mutex<JoinHandle<()>>>,
    subs: SubscriptionMap,
    pending_requests: Arc<DashMap<u64, ResponseSender>>,
    shutdown_tx: watch::Sender<bool>,
    next_id: Arc<AtomicU64>,
    subscription_tasks: Arc<Mutex<Vec<JoinHandle<()>>>>,
    current_connection_state: Arc<Mutex<ConnectionState>>,
}

#[inline(always)]
pub fn deserialise_to_type<T>(s: &Bytes) -> Result<T, serde_json::Error>
where
    T: DeserializeOwned,
{
    match serde_json::from_slice::<T>(s) {
        Ok(val) => Ok(val),
        Err(e) => {
            error!("Deserialization error: {e:?}");
            error!("Raw response: {}", String::from_utf8_lossy(s));
            Err(e)
        }
    }
}

impl WsClient {
    pub fn new(environment: Environment) -> Self {
        let (state_tx, state_rx) = watch::channel::<ConnectionState>(ConnectionState::Disconnected);
        let pending_requests = Arc::new(DashMap::new());
        let subs: SubscriptionMap = Arc::new(DashMap::new());

        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel::<InternalCommand>();
        let (shutdown_tx, shutdown_rx) = watch::channel(false);

        let supervisor_handle = tokio::spawn(connection_supervisor(
            environment.get_server_url().to_string(),
            cmd_rx,
            shutdown_rx,
            pending_requests.clone(),
            subs.clone(),
            state_tx.clone(),
        ));
        let next_id = Arc::new(AtomicU64::new(1));

        let subscription_tasks = Arc::new(Mutex::new(Vec::new()));

        Self {
            write_tx: cmd_tx.clone(),
            state_rx,
            environment,
            supervisor_handle: Arc::new(Mutex::new(supervisor_handle)),
            subs,
            pending_requests,
            shutdown_tx,
            next_id,
            subscription_tasks,
            current_connection_state: Arc::new(Mutex::new(ConnectionState::Disconnected)),
        }
    }

    pub fn subscriptions(&self) -> Subscriptions<'_> {
        Subscriptions { client: self }
    }
    pub async fn subscribe_channels<P, F, Fut>(
        &self,
        event: Channels,
        payloads: Vec<Bytes>,
        mut callback: F,
    ) -> Result<(), ClientError>
    where
        P: DeserializeOwned + Send + 'static,
        F: FnMut(P) -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let (tx, mut rx) = mpsc::unbounded_channel::<Bytes>();

        self.subs.insert(event.as_string(), (tx, payloads.clone()));
        debug!("Subscribing to public channel: {event:?}");

        let handle = tokio::spawn(async move {
            while let Some(msg) = rx.recv().await {
                let parsed: P = match deserialise_to_type(&msg) {
                    Ok(m) => m,
                    Err(e) => {
                        warn!("Failed to parse channel message: {e}; raw: {msg:?}");
                        continue;
                    }
                };
                callback(parsed).await;
            }
        });

        for payload in payloads {
            let res = self.send_rpc_nowait(payload).await;
            match res {
                Ok(_) => debug!("Subscription message sent for channel: {event:?}"),
                Err(e) => {
                    error!("Failed to send subscription message for channel {event:?}: {e}");
                    return Err(e);
                }
            }
        }
        info!("Subscription result: ok! Channel: {event:?}");
        self.subscription_tasks.lock().await.push(handle);
        Ok(())
    }

    pub async fn send_rpc_nowait(&self, msg: Bytes) -> Result<(), ClientError> {
        self.write_tx
            .send(InternalCommand::Send(Frame::text(msg)))
            .map_err(|e| ClientError::Transport(Box::new(e)))
    }
    pub async fn send_rpc<T>(&self, msg: Bytes) -> Result<T, ClientError>
    where
        T: serde::de::DeserializeOwned + std::fmt::Debug,
    {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);

        let (tx, rx) = oneshot::channel::<Bytes>();
        self.pending_requests.insert(id, tx);

        if let Err(e) = self.write_tx.send(InternalCommand::Send(Frame::text(msg))) {
            self.pending_requests.remove(&id);
            return Err(ClientError::Transport(Box::new(e)));
        }

        let response = rx.await;
        let resp = match response {
            Ok(r) => r,
            Err(e) => {
                return Err(ClientError::Transport(Box::new(e)));
            }
        };

        let envelope: T = deserialise_to_type(&resp)?;
        Ok(envelope)
    }
    pub async fn run_till_event(&self) -> ConnectionState {
        let mut rx = self.state_rx.clone();

        loop {
            if rx.changed().await.is_ok() {
                let state = *rx.borrow_and_update();
                let mut current_state = self.current_connection_state.lock().await;
                if state != *current_state {
                    info!("Connection state changed to: {:?}", state);
                    *current_state = state;
                    return state;
                }
            } else {
                return ConnectionState::Exited;
            }
        }
    }

    pub async fn shutdown(&self, reason: &'static str) -> Result<(), ClientError> {
        debug!("Shutdown requested: {reason}");
        self.subs.clear();
        let _ = self.shutdown_tx.send(true);
        let _ = self.write_tx.send(InternalCommand::Close);
        // we join the supervisor task to ensure it has fully exited before we return from shutdown
        let supervisor_handle = self.supervisor_handle.lock().await;
        supervisor_handle.abort();
        for task in self.subscription_tasks.lock().await.drain(..) {
            task.abort();
        }
        Ok(())
    }

    pub async fn resubscribe_all(&self) -> Result<(), ClientError> {
        debug!("Resubscribing to all channels");
        for entry in self.subs.iter() {
            let channel = entry.key();
            let (_sender, payloads) = entry.value();
            debug!("Resubscribing to channel: {channel} with payloads: {payloads:?}");
            for payload in payloads {
                let res = self.send_rpc_nowait(payload.to_owned()).await;
                match res {
                    Ok(_) => debug!("Resubscription message sent for channel: {channel:?}"),
                    Err(e) => {
                        error!(
                            "Failed to send resubscription message for channel {channel:?}: {e}"
                        );
                        return Err(e);
                    }
                }
            }
        }
        Ok(())
    }
    pub async fn wait_for_connection(&self) {
        let mut rx = self.state_rx.clone();

        // If already connected, return immediately
        if *rx.borrow_and_update() == ConnectionState::Connected {
            let mut current_state = self.current_connection_state.lock().await;
            *current_state = ConnectionState::Connected;
            return;
        }
        while rx.changed().await.is_ok() {
            if *rx.borrow_and_update() == ConnectionState::Connected {
                let mut current_state = self.current_connection_state.lock().await;
                *current_state = ConnectionState::Connected;
                return;
            }
        }
    }
}

async fn connection_supervisor(
    url: String,
    mut cmd_rx: mpsc::UnboundedReceiver<InternalCommand>,
    mut shutdown_rx: watch::Receiver<bool>,
    pending_requests: Arc<DashMap<u64, ResponseSender>>,
    subscriptions: SubscriptionMap,
    connection_state_tx: watch::Sender<ConnectionState>,
) {
    let mut attempts = 0;
    loop {
        info!("Connection supervisor started for {url}");

        if *shutdown_rx.borrow() {
            info!("Supervisor sees shutdown for {url}");
            break;
        }
        let client = WebSocket::connect(url.parse().unwrap())
            .with_options(Options::default().with_high_compression())
            .await;

        match client {
            Ok(ws_stream) => {
                info!("Connected to {url}");
                attempts = 0;
                connection_state_tx.send(ConnectionState::Connected).ok();
                let result = run_single_connection(
                    ws_stream,
                    &mut cmd_rx,
                    &mut shutdown_rx,
                    &pending_requests,
                    &subscriptions,
                )
                .await;
                info!("Connection to {url} ended with result: {result:?}");

                if result.is_ok() {
                    info!("Connection exited normally for {url}");
                    connection_state_tx.send(ConnectionState::Exited).ok();
                    break;
                }
                if let Err(e) = result {
                    error!("Connection error on {url}: {e}");
                    connection_state_tx.send(ConnectionState::Disconnected).ok();
                }
            }
            Err(e) => {
                attempts += 1;
                error!("Failed to connect to {url}: {e} - attempt {attempts}");
                if *shutdown_rx.borrow() || cmd_rx.is_closed() {
                    break;
                }
                tokio::time::sleep(std::time::Duration::from_secs(3u64.pow(attempts))).await;
                connection_state_tx.send(ConnectionState::Disconnected).ok();
            }
        }
    }
    info!("Connection supervisor exited for {url}");
}

async fn run_single_connection(
    mut ws: WebSocket<MaybeTlsStream<TcpStream>>,
    cmd_rx: &mut mpsc::UnboundedReceiver<InternalCommand>,
    shutdown_rx: &mut watch::Receiver<bool>,
    pending_requests: &Arc<DashMap<u64, ResponseSender>>,
    subscriptions: &SubscriptionMap,
) -> Result<(), ClientError> {
    // Set up ping interval
    let mut ping_interval = interval(PING_INTERVAL);
    ping_interval.set_missed_tick_behavior(MissedTickBehavior::Delay);

    let read_deadline = sleep(READ_TIMEOUT);
    tokio::pin!(read_deadline);

    loop {
        tokio::select! {
            _ = ping_interval.tick() => {
                if let Err(e) = ws.send(Frame::ping(Vec::new())).await {
                    warn!("Failed to send ping for connection {e}");
                    return Err(ClientError::WebsocketError(e));
                }
                debug!("Ping sent successfully");
            }

            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("Shutdown requested.");
                    let _ = ws.close().await;
                    return Ok(());
                }
            }

            maybe_cmd = cmd_rx.recv() => {
                match maybe_cmd {
                    Some(InternalCommand::Send(msg)) => {
                        ws.send(msg).await?;
                    }
                    Some(InternalCommand::Close) => {
                        info!("Close command received");
                        let _ = ws.close().await;
                        return Ok(());
                    }
                    None => {
                        info!("Command channel closed.");
                        let _ = ws.close().await;
                        return Ok(());
                    }
                }
            }

            msg = ws.next() => {
                read_deadline.as_mut().reset(Instant::now() + READ_TIMEOUT);
                match msg {
                    None => {
                        warn!("WebSocket stream ended.");
                        return Ok(());
                    },
                    Some(frame) => {
                        let (opcode, _is_fin, body) = frame.into_parts();
                        match opcode {
                            OpCode::Text => {
                                // info!("Received text frame");
                                handle_incoming(
                                    &body,
                                    pending_requests,
                                    subscriptions
                                ).await;
                            },
                            OpCode::Pong => {
                                debug!("Received pong frame");
                            },
                            OpCode::Close => {
                                info!("Received close frame from server");
                                return Err(ClientError::WebsocketError(yawc::WebSocketError::ConnectionClosed));
                            },
                            _ => {
                                warn!("Received unsupported non-text frame, opcode: {opcode:?}");
                                continue;
                            }

                        }
                    }
                }
            }

        _ = &mut read_deadline => {
            warn!("WebSocket read timeout. No messages received within {READ_TIMEOUT:?}");
            return Err(ClientError::Io(std::io::Error::new(std::io::ErrorKind::TimedOut, "WebSocket read timeout")));
        }
        }
    }
}

#[inline(always)]
pub async fn handle_incoming(
    bytes: &Bytes,
    _pending_requests: &Arc<DashMap<u64, ResponseSender>>,
    subscriptions: &SubscriptionMap,
) {
    // // ---- fast path: channel_name ----
    if let Some(channel) = extract_event(bytes) {
        for routes in [subscriptions] {
            if let Some(subscription) = routes.get(channel) {
                let (sender, _payloads) = subscription.value();
                if sender.send(bytes.to_owned()).is_err() {
                    routes.remove(channel);
                }
                return;
            }
        }
        warn!("No subscription handler for channel: {channel}");
        return;
    }
    warn!("Received unhandled message: {bytes:?}");
}