From a43a8d7248d4fc284e4bdf156762effdae50b13f Mon Sep 17 00:00:00 2001 From: colmazia Date: Thu, 5 Sep 2024 17:56:48 +0700 Subject: [PATCH] fix from comments --- Cargo.toml | 6 +++--- bothan-binance/src/api/msgs.rs | 12 ++++++------ bothan-bybit/src/api/types/ticker.rs | 4 ++-- bothan-coinbase/src/api/types/channels/ticker.rs | 2 +- bothan-kraken/examples/kraken_basic.rs | 5 +++-- bothan-kraken/src/api/error.rs | 10 ++++++++-- bothan-kraken/src/api/types.rs | 2 +- bothan-kraken/src/api/types/channel.rs | 2 +- bothan-kraken/src/api/types/channel/status.rs | 2 +- bothan-kraken/src/api/types/channel/ticker.rs | 6 +++--- bothan-kraken/src/api/types/message.rs | 2 +- bothan-kraken/src/api/websocket.rs | 13 +++---------- bothan-okx/src/api/error.rs | 10 ++++++++-- bothan-okx/src/api/types.rs | 2 +- bothan-okx/src/api/types/channel.rs | 8 ++++---- bothan-okx/src/api/types/message.rs | 6 +++--- bothan-okx/src/api/websocket.rs | 10 ++-------- bothan-okx/src/worker/asset_worker.rs | 7 ++----- 18 files changed, 53 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 633614db..7c89782e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,13 +8,13 @@ exclude = [".github/"] [workspace] members = [ - "bothan-core", + "bothan-api/server", + "bothan-api/server-cli", "bothan-binance", + "bothan-core", "bothan-coingecko", "bothan-kraken", "bothan-okx", - "bothan-api/server", - "bothan-api/server-cli", ] exclude = ["bothan-api", "bothan-api-proxy"] resolver = "2" diff --git a/bothan-binance/src/api/msgs.rs b/bothan-binance/src/api/msgs.rs index 6fd27d2a..162e0422 100644 --- a/bothan-binance/src/api/msgs.rs +++ b/bothan-binance/src/api/msgs.rs @@ -1,26 +1,26 @@ use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct SuccessResponse { pub result: Option, pub id: u64, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct ErrorResponse { pub code: u16, pub msg: String, pub id: u64, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "e")] pub enum Data { #[serde(rename = "24hrMiniTicker")] MiniTicker(MiniTickerInfo), } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct MiniTickerInfo { #[serde(rename = "E")] pub event_time: i64, @@ -47,13 +47,13 @@ pub struct MiniTickerInfo { pub quote_volume: String, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct StreamResponse { pub stream: String, pub data: Data, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(untagged)] pub enum BinanceResponse { Success(SuccessResponse), diff --git a/bothan-bybit/src/api/types/ticker.rs b/bothan-bybit/src/api/types/ticker.rs index cc91129c..75909a51 100644 --- a/bothan-bybit/src/api/types/ticker.rs +++ b/bothan-bybit/src/api/types/ticker.rs @@ -67,7 +67,7 @@ pub struct SpotTicker { } /// Represents different types of tickers. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(untagged, rename_all = "camelCase")] pub enum Tickers { /// A list of spot tickers. @@ -75,7 +75,7 @@ pub enum Tickers { } /// Represents the response containing tickers. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct TickersResponse { /// The category of the tickers. #[serde(default)] diff --git a/bothan-coinbase/src/api/types/channels/ticker.rs b/bothan-coinbase/src/api/types/channels/ticker.rs index b1c60df5..63337fbc 100644 --- a/bothan-coinbase/src/api/types/channels/ticker.rs +++ b/bothan-coinbase/src/api/types/channels/ticker.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// Represents a ticker with various market data. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Ticker { /// The sequence number of the ticker. pub sequence: usize, diff --git a/bothan-kraken/examples/kraken_basic.rs b/bothan-kraken/examples/kraken_basic.rs index 1e8f66f9..ca699a91 100644 --- a/bothan-kraken/examples/kraken_basic.rs +++ b/bothan-kraken/examples/kraken_basic.rs @@ -31,8 +31,9 @@ async fn main() { sleep(Duration::from_secs(2)).await; loop { - let data = worker.get_asset("BTC/USD").await; - println!("{:?}", data); + let btc_data = worker.get_asset("BTC/USD").await; + let eth_data = worker.get_asset("ETH/USD").await; + println!("{:?}, {:?}", btc_data, eth_data); sleep(Duration::from_secs(5)).await; } } diff --git a/bothan-kraken/src/api/error.rs b/bothan-kraken/src/api/error.rs index e2601947..2bbd77ec 100644 --- a/bothan-kraken/src/api/error.rs +++ b/bothan-kraken/src/api/error.rs @@ -22,5 +22,11 @@ pub enum MessageError { } #[derive(Debug, thiserror::Error)] -#[error(transparent)] -pub struct SendError(#[from] tungstenite::Error); +pub enum SendError { + #[error(transparent)] + Tungstenite(#[from] tungstenite::Error), + + /// Indicates a failure to parse a message. + #[error("failed to parse")] + Parse(#[from] serde_json::Error), +} diff --git a/bothan-kraken/src/api/types.rs b/bothan-kraken/src/api/types.rs index 640adc36..1ec5acaa 100644 --- a/bothan-kraken/src/api/types.rs +++ b/bothan-kraken/src/api/types.rs @@ -12,7 +12,7 @@ pub mod message; pub const DEFAULT_URL: &str = "wss://ws.kraken.com/v2"; /// Represents the different types of responses from the Kraken API. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "snake_case", untagged)] pub enum KrakenResponse { /// A response for public messages. diff --git a/bothan-kraken/src/api/types/channel.rs b/bothan-kraken/src/api/types/channel.rs index e655411c..958eab9c 100644 --- a/bothan-kraken/src/api/types/channel.rs +++ b/bothan-kraken/src/api/types/channel.rs @@ -4,7 +4,7 @@ pub mod status; pub mod ticker; /// Represents the response from various channels. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "channel", content = "data", rename_all = "snake_case")] pub enum ChannelResponse { /// Response for ticker data. diff --git a/bothan-kraken/src/api/types/channel/status.rs b/bothan-kraken/src/api/types/channel/status.rs index 57076b9c..f5679c38 100644 --- a/bothan-kraken/src/api/types/channel/status.rs +++ b/bothan-kraken/src/api/types/channel/status.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// Represents the status information of the Kraken API. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Status { /// The API version. pub api_version: String, diff --git a/bothan-kraken/src/api/types/channel/ticker.rs b/bothan-kraken/src/api/types/channel/ticker.rs index 265bed3d..0ab63c8d 100644 --- a/bothan-kraken/src/api/types/channel/ticker.rs +++ b/bothan-kraken/src/api/types/channel/ticker.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// Represents the event trigger type. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum EventTrigger { #[serde(rename = "bbo")] BBO, @@ -10,7 +10,7 @@ pub enum EventTrigger { } /// Parameters for a ticker request. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct TickerRequestParameters { /// The channel name. pub channel: String, @@ -35,7 +35,7 @@ pub struct TickerSubscriptionResult { } /// Response from a ticker request. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct TickerResponse { /// The symbol of the ticker. pub symbol: String, diff --git a/bothan-kraken/src/api/types/message.rs b/bothan-kraken/src/api/types/message.rs index 30f293bd..9d1084d1 100644 --- a/bothan-kraken/src/api/types/message.rs +++ b/bothan-kraken/src/api/types/message.rs @@ -23,7 +23,7 @@ pub struct PublicMessage { } /// Represents the response to a public message. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct PublicMessageResponse { /// The error message, if any. pub error: Option, diff --git a/bothan-kraken/src/api/websocket.rs b/bothan-kraken/src/api/websocket.rs index ff6a2eeb..e80c977b 100644 --- a/bothan-kraken/src/api/websocket.rs +++ b/bothan-kraken/src/api/websocket.rs @@ -1,6 +1,5 @@ use futures_util::stream::{SplitSink, SplitStream}; use futures_util::{SinkExt, StreamExt}; -use serde_json::json; use tokio::net::TcpStream; use tokio_tungstenite::tungstenite::error::Error as TungsteniteError; use tokio_tungstenite::tungstenite::http::StatusCode; @@ -71,11 +70,8 @@ impl KrakenWebSocketConnection { req_id: None, }; - // Create the subscription payload. - let payload = json!(msg); - - // Send the subscription message. - let message = Message::Text(payload.to_string()); + // Send the unsubscription message. + let message = Message::Text(serde_json::to_string(&msg)?); Ok(self.sender.send(message).await?) } @@ -87,11 +83,8 @@ impl KrakenWebSocketConnection { params: Some(params), req_id: None, }; - // Create the unsubscription payload. - let payload = json!(msg); - // Send the unsubscription message. - let message = Message::Text(payload.to_string()); + let message = Message::Text(serde_json::to_string(&msg)?); Ok(self.sender.send(message).await?) } diff --git a/bothan-okx/src/api/error.rs b/bothan-okx/src/api/error.rs index e2601947..2bbd77ec 100644 --- a/bothan-okx/src/api/error.rs +++ b/bothan-okx/src/api/error.rs @@ -22,5 +22,11 @@ pub enum MessageError { } #[derive(Debug, thiserror::Error)] -#[error(transparent)] -pub struct SendError(#[from] tungstenite::Error); +pub enum SendError { + #[error(transparent)] + Tungstenite(#[from] tungstenite::Error), + + /// Indicates a failure to parse a message. + #[error("failed to parse")] + Parse(#[from] serde_json::Error), +} diff --git a/bothan-okx/src/api/types.rs b/bothan-okx/src/api/types.rs index 21c03d21..d5f33d8a 100644 --- a/bothan-okx/src/api/types.rs +++ b/bothan-okx/src/api/types.rs @@ -9,7 +9,7 @@ pub mod message; pub const DEFAULT_URL: &str = "wss://ws.okx.com:8443/ws/v5/public"; /// Represents the different types of responses from the OKX WebSocket API. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase", untagged)] pub enum OkxResponse { /// A response from a WebSocket message. diff --git a/bothan-okx/src/api/types/channel.rs b/bothan-okx/src/api/types/channel.rs index 3c19f90e..2be49578 100644 --- a/bothan-okx/src/api/types/channel.rs +++ b/bothan-okx/src/api/types/channel.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// Represents a response from a channel subscription. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(untagged)] pub enum ChannelResponse { /// Ticker data response. @@ -9,7 +9,7 @@ pub enum ChannelResponse { } /// Represents push data from a channel. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct PushData { /// The argument for the channel. pub arg: ChannelArgument, @@ -18,7 +18,7 @@ pub struct PushData { } /// Represents the argument for a channel. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ChannelArgument { /// The name of the channel. @@ -28,7 +28,7 @@ pub struct ChannelArgument { } /// Represents ticker data received from the channel. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct TickerData { pub inst_type: String, diff --git a/bothan-okx/src/api/types/message.rs b/bothan-okx/src/api/types/message.rs index 56c50033..ced6fd40 100644 --- a/bothan-okx/src/api/types/message.rs +++ b/bothan-okx/src/api/types/message.rs @@ -9,7 +9,7 @@ pub enum Op { } /// Represents the instrument type. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "UPPERCASE")] pub enum InstrumentType { Spot, @@ -26,7 +26,7 @@ pub struct WebSocketMessage { } /// Represents a response to a WebSocket message. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct WebSocketMessageResponse { /// The event name. @@ -42,7 +42,7 @@ pub struct WebSocketMessageResponse { } /// Represents the arguments for a price request. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct PriceRequestArgument { /// The name of the channel. diff --git a/bothan-okx/src/api/websocket.rs b/bothan-okx/src/api/websocket.rs index ad2cc08e..d101d06b 100644 --- a/bothan-okx/src/api/websocket.rs +++ b/bothan-okx/src/api/websocket.rs @@ -1,6 +1,5 @@ use futures_util::stream::{SplitSink, SplitStream}; use futures_util::{SinkExt, StreamExt}; -use serde_json::json; use tokio::net::TcpStream; use tokio_tungstenite::tungstenite::error::Error as TungsteniteError; use tokio_tungstenite::tungstenite::http::StatusCode; @@ -58,11 +57,8 @@ impl OkxWebSocketConnection { args: Some(ticker_args), }; - // Create the subscription payload. - let payload = json!(msg); - // Send the subscription message. - let message = Message::Text(payload.to_string()); + let message = Message::Text(serde_json::to_string(&msg)?); Ok(self.sender.send(message).await?) } @@ -73,11 +69,9 @@ impl OkxWebSocketConnection { op: Op::Unsubscribe, args: Some(ticker_args), }; - // Create the unsubscription payload. - let payload = json!(msg); // Send the unsubscription message. - let message = Message::Text(payload.to_string()); + let message = Message::Text(serde_json::to_string(&msg)?); Ok(self.sender.send(message).await?) } diff --git a/bothan-okx/src/worker/asset_worker.rs b/bothan-okx/src/worker/asset_worker.rs index b29d219a..5778bd0b 100644 --- a/bothan-okx/src/worker/asset_worker.rs +++ b/bothan-okx/src/worker/asset_worker.rs @@ -131,11 +131,8 @@ async fn handle_reconnect( fn parse_ticker(ticker: TickerData) -> Result { let id = ticker.inst_id.clone(); let price_value = Decimal::from_str_exact(&ticker.last)?; - Ok(AssetInfo::new( - id, - price_value, - chrono::Utc::now().timestamp(), - )) + let timestamp = chrono::Utc::now().timestamp(); + Ok(AssetInfo::new(id, price_value, timestamp)) } async fn store_ticker(store: &WorkerStore, ticker: TickerData) -> Result<(), WorkerError> {