use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MakerOrder {
pub order_id: String,
pub maker_address: String,
pub matched_amount: String,
pub fee_rate_bps: Option<String>,
pub asset_id: String,
pub price: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum TradeStatus {
Matched,
Mined,
Confirmed,
Retrying,
Failed,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeMessage {
pub event_type: String,
pub id: String,
pub asset_id: String,
pub market: String,
pub outcome: String,
pub price: String,
pub size: String,
pub side: String,
pub status: TradeStatus,
pub taker_order_id: String,
pub maker_orders: Vec<MakerOrder>,
pub owner: Option<String>,
pub transaction_hash: Option<String>,
pub timestamp: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderEventType {
Placement,
Update,
Cancellation,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderMessage {
pub event_type: String,
pub id: String,
pub asset_id: String,
pub market: String,
pub outcome: String,
pub price: String,
pub side: String,
pub original_size: String,
pub size_matched: String,
#[serde(rename = "type")]
pub order_type: OrderEventType,
pub order_owner: Option<String>,
pub timestamp: String,
}
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum UserMessage {
Trade(TradeMessage),
Order(OrderMessage),
}
impl UserMessage {
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
#[derive(Deserialize)]
struct RawMessage {
event_type: String,
}
let raw: RawMessage = serde_json::from_str(json)?;
match raw.event_type.as_str() {
"trade" => Ok(UserMessage::Trade(serde_json::from_str(json)?)),
"order" => Ok(UserMessage::Order(serde_json::from_str(json)?)),
_ => Err(serde::de::Error::custom(format!(
"Unknown user event type: {}",
raw.event_type
))),
}
}
}