Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/ws/sub_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,78 @@ pub struct BookLevel {
pub n: u64,
}

#[cfg(test)]
mod tests {
use super::OrderUpdate;

#[test]
fn deserialize_order_update_with_trigger_fields() {
let payload = r#"{
"order": {
"coin": "ETH",
"side": "B",
"limitPx": "2500.0",
"sz": "1.5",
"oid": 42,
"timestamp": 1710000000000,
"triggerCondition": "PriceAbove",
"isTrigger": true,
"triggerPx": "2450.0",
"isPositionTpsl": true,
"reduceOnly": true,
"orderType": "Stop Market",
"origSz": "1.5",
"tif": "Gtc",
"cloid": "0xabc"
},
"status": "open",
"statusTimestamp": 1710000000010
}"#;

let update: OrderUpdate =
serde_json::from_str(payload).expect("order update should deserialize");

assert!(update.order.is_trigger);
assert_eq!(update.order.trigger_condition, "PriceAbove");
assert_eq!(update.order.trigger_px, "2450.0");
assert!(update.order.is_position_tpsl);
assert!(update.order.reduce_only);
assert_eq!(update.order.order_type, "Stop Market");
assert_eq!(update.order.tif.as_deref(), Some("Gtc"));
}

#[test]
fn deserialize_order_update_with_null_tif() {
let payload = r#"{
"order": {
"coin": "ETH",
"side": "A",
"limitPx": "2500.0",
"sz": "1.5",
"oid": 43,
"timestamp": 1710000000100,
"triggerCondition": "",
"isTrigger": false,
"triggerPx": "0.0",
"isPositionTpsl": false,
"reduceOnly": false,
"orderType": "Limit",
"origSz": "1.5",
"tif": null,
"cloid": null
},
"status": "filled",
"statusTimestamp": 1710000000110
}"#;

let update: OrderUpdate =
serde_json::from_str(payload).expect("order update with null tif should deserialize");

assert_eq!(update.order.tif, None);
assert!(!update.order.is_trigger);
}
}

#[derive(Deserialize, Clone, Debug)]
pub struct L2BookData {
pub coin: String,
Expand Down Expand Up @@ -129,7 +201,14 @@ pub struct BasicOrder {
pub sz: String,
pub oid: u64,
pub timestamp: u64,
pub trigger_condition: String,
pub is_trigger: bool,
pub trigger_px: String,
pub is_position_tpsl: bool,
pub reduce_only: bool,
pub order_type: String,
pub orig_sz: String,
pub tif: Option<String>,
pub cloid: Option<String>,
}

Expand Down