Skip to content

Commit

Permalink
fix from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
colmazia committed Sep 5, 2024
1 parent 7637e5e commit a43a8d7
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 56 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions bothan-binance/src/api/msgs.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
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,
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions bothan-bybit/src/api/types/ticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ 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.
Spot(Vec<SpotTicker>),
}

/// 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)]
Expand Down
2 changes: 1 addition & 1 deletion bothan-coinbase/src/api/types/channels/ticker.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 3 additions & 2 deletions bothan-kraken/examples/kraken_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 8 additions & 2 deletions bothan-kraken/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
2 changes: 1 addition & 1 deletion bothan-kraken/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bothan-kraken/src/api/types/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bothan-kraken/src/api/types/channel/status.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 3 additions & 3 deletions bothan-kraken/src/api/types/channel/ticker.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion bothan-kraken/src/api/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct PublicMessage<T> {
}

/// 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<String>,
Expand Down
13 changes: 3 additions & 10 deletions bothan-kraken/src/api/websocket.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?)
}

Expand All @@ -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?)
}

Expand Down
10 changes: 8 additions & 2 deletions bothan-okx/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
2 changes: 1 addition & 1 deletion bothan-okx/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions bothan-okx/src/api/types/channel.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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.
Ticker(PushData<Vec<TickerData>>),
}

/// Represents push data from a channel.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PushData<T> {
/// The argument for the channel.
pub arg: ChannelArgument,
Expand All @@ -18,7 +18,7 @@ pub struct PushData<T> {
}

/// 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.
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions bothan-okx/src/api/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +26,7 @@ pub struct WebSocketMessage<T> {
}

/// 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.
Expand All @@ -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.
Expand Down
10 changes: 2 additions & 8 deletions bothan-okx/src/api/websocket.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?)
}

Expand All @@ -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?)
}

Expand Down
7 changes: 2 additions & 5 deletions bothan-okx/src/worker/asset_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,8 @@ async fn handle_reconnect(
fn parse_ticker(ticker: TickerData) -> Result<AssetInfo, WorkerError> {
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> {
Expand Down

0 comments on commit a43a8d7

Please sign in to comment.