binance_client/http_api_v3/data/order/post/
request.rs1use chrono::prelude::*;
6use rust_decimal::Decimal;
7
8use crate::http_api_v3::data::order::post::response::r#type::Type as ResponseType;
9use crate::http_api_v3::data::order_side::OrderSide;
10use crate::http_api_v3::data::order_time_in_force::OrderTimeInForce;
11use crate::http_api_v3::data::order_type::OrderType;
12
13pub struct Query {
17 pub symbol: String,
19 pub side: OrderSide,
21 pub r#type: OrderType,
23 pub time_in_force: Option<OrderTimeInForce>,
25 pub quantity: Option<Decimal>,
27 pub quote_order_qty: Option<Decimal>,
29 pub price: Option<Decimal>,
31 pub new_client_order_id: Option<String>,
33 pub stop_price: Option<Decimal>,
35 pub iceberg_qty: Option<Decimal>,
37 pub new_order_resp_type: Option<ResponseType>,
40 pub recv_window: Option<i64>,
42 pub timestamp: i64,
44}
45
46impl Query {
47 const QUERY_INITIAL_CAPACITY: usize = 255;
49
50 pub fn market(
54 symbol: &str,
55 side: OrderSide,
56 quantity: Decimal,
57 use_base_quantity: bool,
58 ) -> Self {
59 let (quantity, quote_order_qty) = if use_base_quantity {
60 (None, Some(quantity))
61 } else {
62 (Some(quantity), None)
63 };
64
65 Self {
66 symbol: symbol.to_owned(),
67 side,
68 r#type: OrderType::Market,
69 time_in_force: None,
70 quantity,
71 quote_order_qty,
72 price: None,
73 new_client_order_id: None,
74 stop_price: None,
75 iceberg_qty: None,
76 new_order_resp_type: Some(ResponseType::Full),
77 recv_window: None,
78 timestamp: Utc::now().timestamp_millis(),
79 }
80 }
81
82 pub fn limit(symbol: &str, side: OrderSide, quantity: Decimal, price: Decimal) -> Self {
86 Self {
87 symbol: symbol.to_owned(),
88 side,
89 r#type: OrderType::Limit,
90 time_in_force: Some(OrderTimeInForce::GoodTilCanceled),
91 quantity: Some(quantity),
92 quote_order_qty: None,
93 price: Some(price),
94 new_client_order_id: None,
95 stop_price: None,
96 iceberg_qty: None,
97 new_order_resp_type: Some(ResponseType::Ack),
98 recv_window: None,
99 timestamp: Utc::now().timestamp_millis(),
100 }
101 }
102}
103
104impl ToString for Query {
105 fn to_string(&self) -> String {
106 let mut params = String::with_capacity(Self::QUERY_INITIAL_CAPACITY);
107 params += &format!("symbol={}", self.symbol.to_owned());
108 params += &format!("&side={}", self.side);
109 params += &format!("&type={}", self.r#type);
110 if let Some(time_in_force) = self.time_in_force {
111 params += &format!("&timeInForce={}", time_in_force);
112 }
113 if let Some(quantity) = self.quantity {
114 params += &format!("&quantity={}", quantity);
115 }
116 if let Some(quote_order_qty) = self.quote_order_qty {
117 params += &format!(""eOrderQty={}", quote_order_qty);
118 }
119 if let Some(price) = self.price {
120 params += &format!("&price={}", price);
121 }
122 if let Some(ref new_client_order_id) = self.new_client_order_id {
123 params += &format!("&newClientOrderId={}", new_client_order_id.to_owned());
124 }
125 if let Some(stop_price) = self.stop_price {
126 params += &format!("&stopPrice={}", stop_price);
127 }
128 if let Some(iceberg_qty) = self.iceberg_qty {
129 params += &format!("&icebergQty={}", iceberg_qty);
130 }
131 if let Some(new_order_resp_type) = self.new_order_resp_type {
132 params += &format!("&newOrderRespType={}", new_order_resp_type);
133 }
134 if let Some(recv_window) = self.recv_window {
135 params += &format!("&recvWindow={}", recv_window);
136 }
137 params += &format!("×tamp={}", self.timestamp);
138 params
139 }
140}