cbat 0.0.19

The unofficial Rust crate for the Coinbase Advanced Trade API
Documentation
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscribeMessage {
    #[serde(rename = "type")]
    pub message_type: String,
    pub channel: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jwt: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnsubscribeMessage {
    #[serde(rename = "type")]
    pub message_type: String,
    pub channel: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jwt: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionsResponse {
    pub channel: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jwt: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct WebSocketMessage {
    pub channel: String,
    pub client_id: String,
    pub timestamp: String,
    pub sequence_num: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub events: Option<Vec<serde_json::Value>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Channel {
    Heartbeats,
    Candles,
    Status,
    Ticker,
    TickerBatch,
    Level2,
    User,
    MarketTrades,
    FuturesBalanceSummary,
}

impl Channel {
    pub fn as_str(&self) -> &'static str {
        match self {
            Channel::Heartbeats => "heartbeats",
            Channel::Candles => "candles",
            Channel::Status => "status",
            Channel::Ticker => "ticker",
            Channel::TickerBatch => "ticker_batch",
            Channel::Level2 => "level2",
            Channel::User => "user",
            Channel::MarketTrades => "market_trades",
            Channel::FuturesBalanceSummary => "futures_balance_summary",
        }
    }

    pub fn requires_authentication(&self) -> bool {
        matches!(self, Channel::User | Channel::FuturesBalanceSummary)
    }
}

impl From<Channel> for String {
    fn from(channel: Channel) -> Self {
        channel.as_str().to_string()
    }
}