1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// US-001: Trading endpoints stub — implemented in US-004, US-005
// US-004: place_order endpoint
// US-005: cancel_order, get_open_orders, get_position_risk endpoints
use crate::futures::models::trading::{
CancelOrderParams, CancelOrderResponse, ModifyOrderParams, OrderResponse, PlaceOrderParams,
PositionResponse,
};
use crate::rest::client::RestClient;
use crate::rest::error::AsterDexError;
use crate::rest::response::ApiResponse;
impl RestClient {
/// Place a new futures order.
///
/// # Errors
/// - `ApiError { code: -1102 }` — required parameter missing (e.g., price for LIMIT)
/// - `ApiError { code: -1121 }` — invalid symbol
/// - `RateLimited` — HTTP 429
/// - `IpBanned` — HTTP 418
pub async fn place_order(
&self,
params: PlaceOrderParams,
) -> Result<ApiResponse<OrderResponse>, AsterDexError> {
// Convert bool → &'static str once, outside the Vec, so we don't need
// String conversions per call.
let reduce_only_s = params.reduce_only.map(bool_as_str);
let price_protect_s = params.price_protect.map(bool_as_str);
let mut pairs: Vec<(&str, &str)> = Vec::with_capacity(12);
pairs.push(("symbol", ¶ms.symbol));
pairs.push(("side", ¶ms.side));
pairs.push(("type", ¶ms.order_type));
pairs.push(("quantity", ¶ms.quantity));
if let Some(p) = params.price.as_deref() {
pairs.push(("price", p));
}
if let Some(tif) = params.time_in_force.as_deref() {
pairs.push(("timeInForce", tif));
}
if let Some(s) = reduce_only_s {
pairs.push(("reduceOnly", s));
}
if let Some(id) = params.new_client_order_id.as_deref() {
pairs.push(("newClientOrderId", id));
}
if let Some(sp) = params.stop_price.as_deref() {
pairs.push(("stopPrice", sp));
}
if let Some(ap) = params.activation_price.as_deref() {
pairs.push(("activationPrice", ap));
}
if let Some(cr) = params.callback_rate.as_deref() {
pairs.push(("callbackRate", cr));
}
if let Some(wt) = params.working_type.as_deref() {
pairs.push(("workingType", wt));
}
if let Some(s) = price_protect_s {
pairs.push(("priceProtect", s));
}
if let Some(ort) = params.new_order_resp_type.as_deref() {
pairs.push(("newOrderRespType", ort));
}
self.signed_post("/fapi/v3/order", &pairs).await
}
/// Cancel an open order.
///
/// One of `params.order_id` or `params.orig_client_order_id` must be set.
///
/// # Errors
/// - `ApiError { code: -2011 }` — order not found
/// - `ApiError { code: -1102 }` — neither orderId nor origClientOrderId provided
pub async fn cancel_order(
&self,
params: CancelOrderParams,
) -> Result<ApiResponse<CancelOrderResponse>, AsterDexError> {
// `order_id` is numeric → one allocation; bind outside the Vec so the
// string lives long enough.
let order_id_str = params.order_id.map(|id| id.to_string());
let mut pairs: Vec<(&str, &str)> = Vec::with_capacity(3);
pairs.push(("symbol", ¶ms.symbol));
if let Some(s) = order_id_str.as_deref() {
pairs.push(("orderId", s));
}
if let Some(cid) = params.orig_client_order_id.as_deref() {
pairs.push(("origClientOrderId", cid));
}
self.signed_delete("/fapi/v3/order", &pairs).await
}
/// Modify an existing open LIMIT order in-place (price and/or quantity).
///
/// Calls `PUT /fapi/v3/order` directly. The original `order_id` is preserved —
/// this is a true amendment, not a cancel + re-place. At least one of
/// `params.quantity` or `params.price` must be set. Either `params.order_id`
/// or `params.orig_client_order_id` must be provided to identify the order.
///
/// Only LIMIT orders can be modified. If the new qty/price fails exchange
/// filters the modification is rejected and the original order remains unchanged.
///
/// # Errors
/// - `ApiError { code: -2011 }` — order not found
/// - `ApiError { code: -1102 }` — required parameter missing
pub async fn modify_order(
&self,
params: ModifyOrderParams,
) -> Result<ApiResponse<OrderResponse>, AsterDexError> {
let order_id_str = params.order_id.map(|id| id.to_string());
let mut pairs: Vec<(&str, &str)> = Vec::with_capacity(5);
pairs.push(("symbol", ¶ms.symbol));
if let Some(s) = order_id_str.as_deref() {
pairs.push(("orderId", s));
}
if let Some(cid) = params.orig_client_order_id.as_deref() {
pairs.push(("origClientOrderId", cid));
}
if let Some(q) = params.quantity.as_deref() {
pairs.push(("quantity", q));
}
if let Some(p) = params.price.as_deref() {
pairs.push(("price", p));
}
self.signed_put("/fapi/v3/order", &pairs).await
}
/// Get all open orders. Pass `None` for all symbols or `Some("BTCUSDT")` for one symbol.
pub async fn get_open_orders(
&self,
symbol: Option<&str>,
) -> Result<ApiResponse<Vec<OrderResponse>>, AsterDexError> {
let mut kv: Vec<(&str, &str)> = vec![];
if let Some(s) = symbol {
kv.push(("symbol", s));
}
self.signed_get("/fapi/v3/openOrders", &kv).await
}
/// Get position risk information. Pass `None` for all symbols or `Some("BTCUSDT")` for one symbol.
pub async fn get_position_risk(
&self,
symbol: Option<&str>,
) -> Result<ApiResponse<Vec<PositionResponse>>, AsterDexError> {
let mut kv: Vec<(&str, &str)> = vec![];
if let Some(s) = symbol {
kv.push(("symbol", s));
}
self.signed_get("/fapi/v3/positionRisk", &kv).await
}
}
/// Zero-alloc bool → form-value string.
#[inline]
fn bool_as_str(b: bool) -> &'static str {
if b { "true" } else { "false" }
}