ccxt_exchanges/okx/
ws_exchange_impl.rs

1//! WsExchange trait implementation for OKX
2//!
3//! This module implements the unified `WsExchange` trait from `ccxt-core` for OKX,
4//! providing real-time WebSocket data streaming capabilities.
5
6use async_trait::async_trait;
7use ccxt_core::{
8    error::{Error, Result},
9    types::{Balance, Ohlcv, Order, OrderBook, Ticker, Timeframe, Trade},
10    ws_client::WsConnectionState,
11    ws_exchange::{MessageStream, WsExchange},
12};
13
14use super::Okx;
15
16#[async_trait]
17impl WsExchange for Okx {
18    // ==================== Connection Management ====================
19
20    async fn ws_connect(&self) -> Result<()> {
21        let ws = self.create_ws();
22        ws.connect().await?;
23        Ok(())
24    }
25
26    async fn ws_disconnect(&self) -> Result<()> {
27        let ws = self.create_ws();
28        ws.disconnect().await
29    }
30
31    fn ws_is_connected(&self) -> bool {
32        // Without persistent state tracking, return false
33        // In a full implementation, we'd track the active connection
34        false
35    }
36
37    fn ws_state(&self) -> WsConnectionState {
38        // Return disconnected since we don't have persistent state
39        WsConnectionState::Disconnected
40    }
41
42    // ==================== Public Data Streams ====================
43
44    async fn watch_ticker(&self, symbol: &str) -> Result<MessageStream<Ticker>> {
45        let ws = self.create_ws();
46
47        // Convert unified symbol (BTC/USDT) to OKX format (BTC-USDT)
48        let okx_symbol = symbol.replace('/', "-");
49
50        // Try to get market info if available (optional)
51        let market = self.base().market(symbol).await.ok().map(|m| (*m).clone());
52
53        ws.watch_ticker(&okx_symbol, market).await
54    }
55
56    async fn watch_tickers(&self, _symbols: &[String]) -> Result<MessageStream<Vec<Ticker>>> {
57        // TODO: Implement multi-symbol ticker watching
58        Err(Error::not_implemented(
59            "watch_tickers not yet implemented for OKX",
60        ))
61    }
62
63    async fn watch_order_book(
64        &self,
65        symbol: &str,
66        limit: Option<u32>,
67    ) -> Result<MessageStream<OrderBook>> {
68        let ws = self.create_ws();
69
70        // Convert unified symbol (BTC/USDT) to OKX format (BTC-USDT)
71        let okx_symbol = symbol.replace('/', "-");
72
73        ws.watch_order_book(&okx_symbol, limit).await
74    }
75
76    async fn watch_trades(&self, symbol: &str) -> Result<MessageStream<Vec<Trade>>> {
77        let ws = self.create_ws();
78
79        // Convert unified symbol (BTC/USDT) to OKX format (BTC-USDT)
80        let okx_symbol = symbol.replace('/', "-");
81
82        // Try to get market info if available (optional)
83        let market = self.base().market(symbol).await.ok().map(|m| (*m).clone());
84
85        ws.watch_trades(&okx_symbol, market).await
86    }
87
88    async fn watch_ohlcv(
89        &self,
90        _symbol: &str,
91        _timeframe: Timeframe,
92    ) -> Result<MessageStream<Ohlcv>> {
93        Err(Error::not_implemented(
94            "watch_ohlcv not yet implemented for OKX",
95        ))
96    }
97
98    // ==================== Private Data Streams ====================
99
100    async fn watch_balance(&self) -> Result<MessageStream<Balance>> {
101        Err(Error::not_implemented(
102            "watch_balance not yet implemented for OKX",
103        ))
104    }
105
106    async fn watch_orders(&self, _symbol: Option<&str>) -> Result<MessageStream<Order>> {
107        Err(Error::not_implemented(
108            "watch_orders not yet implemented for OKX",
109        ))
110    }
111
112    async fn watch_my_trades(&self, _symbol: Option<&str>) -> Result<MessageStream<Trade>> {
113        Err(Error::not_implemented(
114            "watch_my_trades not yet implemented for OKX",
115        ))
116    }
117
118    // ==================== Subscription Management ====================
119
120    async fn subscribe(&self, _channel: &str, _symbol: Option<&str>) -> Result<()> {
121        Err(Error::not_implemented(
122            "subscribe not yet implemented for OKX",
123        ))
124    }
125
126    async fn unsubscribe(&self, _channel: &str, _symbol: Option<&str>) -> Result<()> {
127        Err(Error::not_implemented(
128            "unsubscribe not yet implemented for OKX",
129        ))
130    }
131
132    fn subscriptions(&self) -> Vec<String> {
133        Vec::new()
134    }
135}