#![allow(deprecated)]
use serde::{Deserialize, Serialize};
use crate::RequestId;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct OrderParams {
pub tx: String,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "method", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ClientMessage {
#[serde(alias = "subscribe")]
Subscribe {
id: Option<RequestId>,
#[serde(default)]
params: Vec<String>,
},
#[serde(alias = "unsubscribe")]
Unsubscribe {
id: Option<RequestId>,
#[serde(default)]
params: Vec<String>,
},
#[serde(alias = "list_subscriptions")]
ListSubscriptions { id: Option<RequestId> },
#[serde(alias = "ping")]
Ping { id: Option<RequestId> },
#[serde(alias = "submit")]
Submit { id: Option<RequestId>, params: OrderParams },
#[deprecated(
since = "0.3.0",
note = "use `ClientMessage::Submit`; the action is decoded from the signed tx"
)]
#[serde(alias = "order.place", rename = "ORDER.PLACE")]
OrderPlace { id: Option<RequestId>, params: OrderParams },
#[deprecated(
since = "0.3.0",
note = "use `ClientMessage::Submit`; the action is decoded from the signed tx"
)]
#[serde(alias = "order.cancel", rename = "ORDER.CANCEL")]
OrderCancel { id: Option<RequestId>, params: OrderParams },
#[deprecated(
since = "0.3.0",
note = "use `ClientMessage::Submit`; the action is decoded from the signed tx"
)]
#[serde(alias = "order.amend", alias = "order.modify", rename = "ORDER.AMEND")]
OrderAmend { id: Option<RequestId>, params: OrderParams },
#[deprecated(
since = "0.3.0",
note = "use `ClientMessage::Submit`; the action is decoded from the signed tx"
)]
#[serde(alias = "order.cancelAll", rename = "ORDER.CANCEL_ALL")]
OrderCancelAll { id: Option<RequestId>, params: OrderParams },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn submit_serializes_with_screaming_method() {
let msg = ClientMessage::Submit {
id: Some(RequestId::from(7u64)),
params: OrderParams { tx: "deadbeef".to_string() },
};
let json = serde_json::to_string(&msg).unwrap();
assert_eq!(json, r#"{"method":"SUBMIT","id":7,"params":{"tx":"deadbeef"}}"#);
}
#[test]
fn submit_accepts_lowercase_alias() {
let json = r#"{"method":"submit","id":7,"params":{"tx":"deadbeef"}}"#;
let msg: ClientMessage = serde_json::from_str(json).unwrap();
assert!(matches!(msg, ClientMessage::Submit { .. }));
}
#[test]
fn deprecated_order_place_still_round_trips() {
let json = r#"{"method":"ORDER.PLACE","id":1,"params":{"tx":"abc"}}"#;
let msg: ClientMessage = serde_json::from_str(json).unwrap();
assert!(matches!(msg, ClientMessage::OrderPlace { .. }));
}
}