bybit_rust_api/rest/pre_upgrade/
pre_upgrade_client.rs

1use crate::rest::client::{RestClient, SecType, ServerResponse};
2use crate::rest::BybitResult as Result;
3use serde_json::json;
4
5#[derive(Clone)]
6pub struct PreUpgradeClient {
7    client: RestClient,
8}
9
10impl PreUpgradeClient {
11    pub fn new(client: RestClient) -> Self {
12        PreUpgradeClient { client }
13    }
14
15    /// Get pre-upgrade order history
16    /// Get orders placed before the system upgrade
17    pub async fn get_order_history(
18        &self,
19        category: &str,
20        symbol: Option<&str>,
21        base_coin: Option<&str>,
22        order_id: Option<&str>,
23        order_link_id: Option<&str>,
24        order_filter: Option<&str>,
25        order_status: Option<&str>,
26        start_time: Option<i64>,
27        end_time: Option<i64>,
28        limit: Option<i32>,
29        cursor: Option<&str>,
30    ) -> Result<ServerResponse<serde_json::Value>> {
31        let endpoint = "v5/pre-upgrade/order/history";
32        let mut params = json!({
33            "category": category
34        });
35
36        if let Some(symbol) = symbol {
37            params["symbol"] = json!(symbol);
38        }
39        if let Some(base_coin) = base_coin {
40            params["baseCoin"] = json!(base_coin);
41        }
42        if let Some(order_id) = order_id {
43            params["orderId"] = json!(order_id);
44        }
45        if let Some(order_link_id) = order_link_id {
46            params["orderLinkId"] = json!(order_link_id);
47        }
48        if let Some(order_filter) = order_filter {
49            params["orderFilter"] = json!(order_filter);
50        }
51        if let Some(order_status) = order_status {
52            params["orderStatus"] = json!(order_status);
53        }
54        if let Some(start_time) = start_time {
55            params["startTime"] = json!(start_time);
56        }
57        if let Some(end_time) = end_time {
58            params["endTime"] = json!(end_time);
59        }
60        if let Some(limit) = limit {
61            params["limit"] = json!(limit);
62        }
63        if let Some(cursor) = cursor {
64            params["cursor"] = json!(cursor);
65        }
66
67        let response = self.client.get(endpoint, params, SecType::Signed).await?;
68        Ok(response)
69    }
70
71    /// Get pre-upgrade trade history
72    /// Get trade execution history before the system upgrade
73    pub async fn get_trade_history(
74        &self,
75        category: &str,
76        symbol: Option<&str>,
77        order_id: Option<&str>,
78        order_link_id: Option<&str>,
79        base_coin: Option<&str>,
80        start_time: Option<i64>,
81        end_time: Option<i64>,
82        exec_type: Option<&str>,
83        limit: Option<i32>,
84        cursor: Option<&str>,
85    ) -> Result<ServerResponse<serde_json::Value>> {
86        let endpoint = "v5/pre-upgrade/execution/list";
87        let mut params = json!({
88            "category": category
89        });
90
91        if let Some(symbol) = symbol {
92            params["symbol"] = json!(symbol);
93        }
94        if let Some(order_id) = order_id {
95            params["orderId"] = json!(order_id);
96        }
97        if let Some(order_link_id) = order_link_id {
98            params["orderLinkId"] = json!(order_link_id);
99        }
100        if let Some(base_coin) = base_coin {
101            params["baseCoin"] = json!(base_coin);
102        }
103        if let Some(start_time) = start_time {
104            params["startTime"] = json!(start_time);
105        }
106        if let Some(end_time) = end_time {
107            params["endTime"] = json!(end_time);
108        }
109        if let Some(exec_type) = exec_type {
110            params["execType"] = json!(exec_type);
111        }
112        if let Some(limit) = limit {
113            params["limit"] = json!(limit);
114        }
115        if let Some(cursor) = cursor {
116            params["cursor"] = json!(cursor);
117        }
118
119        let response = self.client.get(endpoint, params, SecType::Signed).await?;
120        Ok(response)
121    }
122
123    /// Get pre-upgrade transaction log
124    /// Get account transaction log before the system upgrade
125    pub async fn get_transaction_log(
126        &self,
127        category: Option<&str>,
128        base_coin: Option<&str>,
129        coin: Option<&str>,
130        start_time: Option<i64>,
131        end_time: Option<i64>,
132        limit: Option<i32>,
133        cursor: Option<&str>,
134    ) -> Result<ServerResponse<serde_json::Value>> {
135        let endpoint = "v5/pre-upgrade/account/transaction-log";
136        let mut params = json!({});
137
138        if let Some(category) = category {
139            params["category"] = json!(category);
140        }
141        if let Some(base_coin) = base_coin {
142            params["baseCoin"] = json!(base_coin);
143        }
144        if let Some(coin) = coin {
145            params["coin"] = json!(coin);
146        }
147        if let Some(start_time) = start_time {
148            params["startTime"] = json!(start_time);
149        }
150        if let Some(end_time) = end_time {
151            params["endTime"] = json!(end_time);
152        }
153        if let Some(limit) = limit {
154            params["limit"] = json!(limit);
155        }
156        if let Some(cursor) = cursor {
157            params["cursor"] = json!(cursor);
158        }
159
160        let response = self.client.get(endpoint, params, SecType::Signed).await?;
161        Ok(response)
162    }
163
164    /// Get pre-upgrade closed PnL
165    /// Get closed profit and loss records before the system upgrade
166    pub async fn get_closed_pnl(
167        &self,
168        category: &str,
169        symbol: Option<&str>,
170        start_time: Option<i64>,
171        end_time: Option<i64>,
172        limit: Option<i32>,
173        cursor: Option<&str>,
174    ) -> Result<ServerResponse<serde_json::Value>> {
175        let endpoint = "v5/pre-upgrade/position/closed-pnl";
176        let mut params = json!({
177            "category": category
178        });
179
180        if let Some(symbol) = symbol {
181            params["symbol"] = json!(symbol);
182        }
183        if let Some(start_time) = start_time {
184            params["startTime"] = json!(start_time);
185        }
186        if let Some(end_time) = end_time {
187            params["endTime"] = json!(end_time);
188        }
189        if let Some(limit) = limit {
190            params["limit"] = json!(limit);
191        }
192        if let Some(cursor) = cursor {
193            params["cursor"] = json!(cursor);
194        }
195
196        let response = self.client.get(endpoint, params, SecType::Signed).await?;
197        Ok(response)
198    }
199
200    /// Get pre-upgrade option delivery record
201    /// Get option delivery records before the system upgrade
202    pub async fn get_option_delivery_record(
203        &self,
204        category: &str,
205        symbol: Option<&str>,
206        expired_date: Option<&str>,
207        limit: Option<i32>,
208        cursor: Option<&str>,
209    ) -> Result<ServerResponse<serde_json::Value>> {
210        let endpoint = "v5/pre-upgrade/asset/delivery-record";
211        let mut params = json!({
212            "category": category
213        });
214
215        if let Some(symbol) = symbol {
216            params["symbol"] = json!(symbol);
217        }
218        if let Some(expired_date) = expired_date {
219            params["expiredDate"] = json!(expired_date);
220        }
221        if let Some(limit) = limit {
222            params["limit"] = json!(limit);
223        }
224        if let Some(cursor) = cursor {
225            params["cursor"] = json!(cursor);
226        }
227
228        let response = self.client.get(endpoint, params, SecType::Signed).await?;
229        Ok(response)
230    }
231
232    /// Get pre-upgrade USDC session settlement
233    /// Get USDC session settlement records before the system upgrade
234    pub async fn get_usdc_session_settlement(
235        &self,
236        category: &str,
237        symbol: Option<&str>,
238        limit: Option<i32>,
239        cursor: Option<&str>,
240    ) -> Result<ServerResponse<serde_json::Value>> {
241        let endpoint = "v5/pre-upgrade/asset/settlement-record";
242        let mut params = json!({
243            "category": category
244        });
245
246        if let Some(symbol) = symbol {
247            params["symbol"] = json!(symbol);
248        }
249        if let Some(limit) = limit {
250            params["limit"] = json!(limit);
251        }
252        if let Some(cursor) = cursor {
253            params["cursor"] = json!(cursor);
254        }
255
256        let response = self.client.get(endpoint, params, SecType::Signed).await?;
257        Ok(response)
258    }
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264    use crate::rest::ApiKeyPair;
265
266    fn create_test_client() -> PreUpgradeClient {
267        let api_key_pair = ApiKeyPair::new(
268            "test_key".to_string(),
269            "test_secret".to_string(),
270            "".to_string(),
271        );
272        let rest_client =
273            RestClient::new(api_key_pair, "https://api-testnet.bybit.com".to_string());
274        PreUpgradeClient::new(rest_client)
275    }
276
277    #[test]
278    fn test_client_creation() {
279        let client = create_test_client();
280        // Test that client was created successfully
281        assert_eq!(
282            std::mem::size_of_val(&client),
283            std::mem::size_of::<PreUpgradeClient>()
284        );
285    }
286
287    #[tokio::test]
288    async fn test_get_order_history_required_params() {
289        let client = create_test_client();
290        let result = client
291            .get_order_history(
292                "linear",
293                Some("BTCUSDT"),
294                None,
295                None,
296                None,
297                None,
298                None,
299                None,
300                None,
301                Some(10),
302                None,
303            )
304            .await;
305        // Should not panic with valid category parameter
306        assert!(result.is_err() || result.is_ok());
307    }
308
309    #[tokio::test]
310    async fn test_get_trade_history_required_params() {
311        let client = create_test_client();
312        let result = client
313            .get_trade_history(
314                "linear",
315                Some("BTCUSDT"),
316                None,
317                None,
318                None,
319                None,
320                None,
321                None,
322                Some(20),
323                None,
324            )
325            .await;
326        // Should not panic with valid category parameter
327        assert!(result.is_err() || result.is_ok());
328    }
329
330    #[tokio::test]
331    async fn test_get_closed_pnl_required_params() {
332        let client = create_test_client();
333        let result = client
334            .get_closed_pnl(
335                "linear",
336                Some("BTCUSDT"),
337                Some(1672531200000), // 2023-01-01
338                None,
339                Some(10),
340                None,
341            )
342            .await;
343        // Should not panic with valid category parameter
344        assert!(result.is_err() || result.is_ok());
345    }
346}