Forráskód Böngészése

新交易所htx的rest 部分完成

hl 1 éve
szülő
commit
b5f0c1d3c3
2 módosított fájl, 91 hozzáadás és 237 törlés
  1. 32 232
      exchanges/src/htx_swap_rest.rs
  2. 59 5
      exchanges/tests/htx_swap_test.rs

+ 32 - 232
exchanges/src/htx_swap_rest.rs

@@ -84,10 +84,8 @@ impl HtxSwapRest {
     }
 
     //查询合约账户(【全仓】获取用户账户信息)
-    pub async fn get_account(&mut self) -> ResponseData {
-        let params = serde_json::json!({
-            "valuation_asset":"USDT"
-         });
+    pub async fn get_account(&mut self,params: Value) -> ResponseData {
+
         let data = self.request("POST".to_string(),
                                 "/linear-swap-api/v1".to_string(),
                                 format!("/swap_balance_valuation"),
@@ -155,236 +153,38 @@ impl HtxSwapRest {
         ).await
     }
 
+    //撤销单个订单 (【全仓】撤销订单)
+    pub async fn cancel_order(&mut self, params: Value) -> ResponseData {
+        let data = self.request("POST".to_string(),
+                                "/linear-swap-api/v1".to_string(),
+                                format!("/swap_cross_cancel"),
+                                true,
+                                params,
+        ).await;
+        data
+    }
 
-    // //下单:side-下单方向,pos_side-持仓方向
-    // pub async fn order(&mut self,
-    //                    settle: String,
-    //                    pos_side: String,
-    //                    side: String,
-    //                    contract: String,
-    //                    size: i64,
-    //                    price: String,
-    //                    text: String,
-    // ) -> ResponseData
-    // {
-    //     if side != "buy" && side != "sell" {
-    //         ResponseData::error(self.label.clone(), format!("未知下单方向!{}", side));
-    //     }
-    //     if pos_side != "long" && pos_side != "short" {
-    //         ResponseData::error(self.label.clone(), format!("未知持仓方向!{}", side));
-    //     }
-    //     let mut param = serde_json::json!({
-    //         "contract":contract, //合约标识
-    //         "size":size,
-    //         "price":price,
-    //         "text":text,
-    //     });
-    //     if price == "0" {
-    //         param.as_object_mut().unwrap().insert("tif".to_string(), serde_json::json!("ioc"));
-    //     }
-    //     if size == 0 {   //数量为0则平仓
-    //         param.as_object_mut().unwrap().insert("close".to_string(), serde_json::json!(true));
-    //     }
-    //     match format!("{}_{}", pos_side, side).as_str() {
-    //         "long_buy" => {//开多
-    //             param.as_object_mut().unwrap().insert("reduce_only".to_string(), serde_json::json!(false));
-    //         }
-    //         "long_sell" => {//平多
-    //             param.as_object_mut().unwrap().insert("reduce_only".to_string(), serde_json::json!(true));
-    //         }
-    //         "short_buy" => {//平空
-    //             param.as_object_mut().unwrap().insert("reduce_only".to_string(), serde_json::json!(true));
-    //         }
-    //         "short_sell" => {//开空
-    //             param.as_object_mut().unwrap().insert("reduce_only".to_string(), serde_json::json!(false));
-    //         }
-    //         _ => {} // 处理未知请求类型
-    //     };
-    //     // trace!("----param{}", param.to_string());
-    //     let data = self.swap_order(settle, param).await;
-    //     data
-    // }
-    // //合约交易下单
-    // pub async fn swap_order(&mut self, settle: String, params: serde_json::Value) -> ResponseData {
-    //     let data = self.request("POST".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/orders", settle),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    // // 提交一个自动订单
-    // pub async fn place_price_order(&mut self, settle: String, params: Value) -> ResponseData {
-    //     self.request("POST".to_string(),
-    //                  "/api/v4".to_string(),
-    //                  format!("/futures/{}/price_orders", settle),
-    //                  true,
-    //                  params.to_string()).await
-    // }
-    // // 撤销自动订单
-    // pub async fn cancel_price_order(&mut self, settle: String, order_id: String) -> ResponseData {
-    //     self.request("DELETE".to_string(),
-    //                  "/api/v4".to_string(),
-    //                  format!("/futures/{}/price_orders/{}", settle, order_id),
-    //                  true,
-    //                  "{}".to_string(),
-    //     ).await
-    // }
-    // //设置持仓模式
-    // pub async fn setting_dual_mode(&mut self, settle: String, dual_mode: bool) -> ResponseData {
-    //     let params = serde_json::json!({
-    //             "dual_mode":dual_mode,
-    //          });
-    //     let data = self.request("POST".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/dual_mode", settle),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    // //更新双仓模式下的杠杆
-    // pub async fn setting_dual_leverage(&mut self, settle: String, symbol: String, leverage: String) -> ResponseData {
-    //     let params = serde_json::json!({
-    //             "leverage":leverage,
-    //          });
-    //     let data = self.request("POST".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/dual_comp/positions/{}/leverage", settle, symbol),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    // //交易账户互转
-    // pub async fn wallet_transfers(&mut self, currency: String, from: String, to: String, amount: String, settle: String) -> ResponseData {
-    //     let params = serde_json::json!({
-    //             "currency":currency,
-    //             "from":from,
-    //             "to":to,
-    //             "amount":amount,
-    //             "settle":settle,
-    //          });
-    //     let data = self.request("POST".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/wallet/transfers"),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    // //撤销单个订单
-    // pub async fn cancel_order(&mut self, settle: String, order_id: String) -> ResponseData {
-    //     let params = serde_json::json!({
-    //          });
-    //
-    //     let data = self.request("DELETE".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/orders/{}", settle, order_id),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    // //撤销所有挂单
-    // pub async fn cancel_order_all(&mut self) -> ResponseData {
-    //     let params = serde_json::json!({
-    //          });
-    //     let data = self.request("POST".to_string(),
-    //                             "/api/v5".to_string(),
-    //                             format!("/sprd/mass-cancel"),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    //
-    // //弃用
-    // pub async fn swap_bazaar_order(&mut self, text: String, origin_side: String, settle: String, contract: String, size: i64) -> ResponseData {
-    //     let mut reduce_only = false;
-    //     let mut param = serde_json::json!({
-    //         "text":text,
-    //         "contract":contract,
-    //         "price":"0",
-    //         "size":size,
-    //     });
-    //
-    //     let req = match origin_side.as_str() {
-    //         "kd" => {
-    //             reduce_only = false;
-    //             true
-    //         }
-    //         "pd" => {
-    //             reduce_only = true;
-    //             true
-    //         }
-    //         "kk" => {
-    //             reduce_only = false;
-    //             true
-    //         }
-    //         "pk" => {
-    //             reduce_only = true;
-    //             true
-    //         }
-    //         _ => { false } // 处理未知请求类型
-    //     };
-    //     if req {
-    //         param.as_object_mut().unwrap().insert("reduce_only".to_string(), serde_json::json!(reduce_only));
-    //     }
-    //
-    //     let data = self.swap_order(settle, param).await;
-    //     data
-    // }
-    //
-    // //批量取消状态为 open 的订单
-    // pub async fn cancel_orders(&mut self, settle: String, contract: String) -> ResponseData {
-    //     let params = serde_json::json!({
-    //         "contract":contract
-    //          });
-    //
-    //     let data = self.request("DELETE".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/orders", settle),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    //
-    // //查询个人成交记录
-    // pub async fn my_trades(&mut self, settle: String, contract: String, limit: i64) -> ResponseData {
-    //     let mut params = serde_json::json!({
-    //         "contract":contract,
-    //         "limit":1000
-    //     });
-    //     if limit > 0 {
-    //         params["limit"] = serde_json::json!(limit);
-    //     }
-    //
-    //     let data = self.request("GET".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/my_trades", settle),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
-    //
-    // //查询合约账户变更历史
-    // pub async fn account_book(&mut self, settle: String) -> ResponseData {
-    //     let params = serde_json::json!({
-    //             "limit":200
-    //          });
-    //     let data = self.request("GET".to_string(),
-    //                             "/api/v4".to_string(),
-    //                             format!("/futures/{}/account_book", settle),
-    //                             true,
-    //                             params.to_string(),
-    //     ).await;
-    //     data
-    // }
+    //设置持仓模式 (【全仓】切换持仓模式)
+    pub async fn setting_dual_mode(&mut self, params: Value) -> ResponseData {
+        let data = self.request("POST".to_string(),
+                                "/linear-swap-api/v1".to_string(),
+                                format!("/swap_cross_switch_position_mode"),
+                                true,
+                                params,
+        ).await;
+        data
+    }
 
+    //设置杠杆(【全仓】切换杠杆)
+    pub async fn setting_dual_leverage(&mut self, params: Value) -> ResponseData {
+        let data = self.request("POST".to_string(),
+                                "/linear-swap-api/v1".to_string(),
+                                format!("/swap_cross_switch_lever_rate"),
+                                true,
+                                params,
+        ).await;
+        data
+    }
 
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/

+ 59 - 5
exchanges/tests/htx_swap_test.rs

@@ -16,8 +16,8 @@ use exchanges::htx_swap_ws::{HtxSwapLogin, HtxSwapSubscribeType, HtxSwapWs, HtxS
 use exchanges::proxy;
 use exchanges::response_base::ResponseData;
 
-const ACCESS_KEY: &str = "";
-const SECRET_KEY: &str = "";
+const ACCESS_KEY: &str = "58edb6bb-qz5c4v5b6n-24498508-c5919";
+const SECRET_KEY: &str = "ccd21182-75a310ca-719696f8-f93b6";
 
 
 //ws-订阅公共频道信息
@@ -84,7 +84,7 @@ async fn ws_custom_subscribe() {
         secret: SECRET_KEY.to_string(),
     };
     let t1 = tokio::spawn(async move {
-        let mut ws = get_ws(Option::from(param),HtxSwapWsType::Private);
+        let mut ws = get_ws(Option::from(param), HtxSwapWsType::Private);
         ws.set_symbols(vec!["BTC_USDT".to_string()]);
         ws.set_subscribe(vec![
             // HtxSwapSubscribeType::PuFuturesDepth,
@@ -106,7 +106,7 @@ async fn ws_custom_subscribe() {
     return;
 }
 
-fn get_ws(btree_map: Option<HtxSwapLogin>,ws_type:HtxSwapWsType) -> HtxSwapWs {
+fn get_ws(btree_map: Option<HtxSwapLogin>, ws_type: HtxSwapWsType) -> HtxSwapWs {
     let htx_ws = HtxSwapWs::new(btree_map, ws_type);
     htx_ws
 }
@@ -131,7 +131,10 @@ async fn rest_get_account_test() {
     // proxy_handle();
 
     let mut ret = get_rest();
-    let req_data = ret.get_account().await;
+    let req_data = ret.get_account(json!({
+            "valuation_asset":"USDT"
+         })
+    ).await;
     trace!("htx--查询合约账户--{:?}", req_data);
     trace!("htx--查询合约账户--{}", req_data.data);
 }
@@ -211,6 +214,57 @@ async fn rest_cancel_price_order_test() {
     trace!("htx--全部撤单--{}", req_data.data);
 }
 
+/*撤单*/
+#[tokio::test]
+async fn rest_cancel_order_test() {
+    global::log_utils::init_log_with_trace();
+    // proxy_handle();
+
+    let mut ret = get_rest();
+
+    let req_data = ret.cancel_order(json!({
+        "order_id":"123"
+    })).await;
+    trace!("htx--撤单--{:?}", req_data);
+    trace!("htx--撤单--{}", req_data.data);
+}
+
+
+/*设置持仓模式*/
+#[tokio::test]
+async fn rest_setting_dual_mode_test() {
+    global::log_utils::init_log_with_trace();
+    // proxy_handle();
+
+    let mut ret = get_rest();
+
+    let req_data = ret.setting_dual_mode(json!({
+        "margin_account":"USDT",
+        "position_mode":"dual_side",
+    })).await;
+    trace!("htx--设置持仓模式--{:?}", req_data);
+    trace!("htx--设置持仓模式--{}", req_data.data);
+}
+
+
+/*设置杠杆*/
+#[tokio::test]
+async fn rest_setting_dual_leverage_test() {
+    global::log_utils::init_log_with_trace();
+    // proxy_handle();
+
+    let mut ret = get_rest();
+
+    let req_data = ret.setting_dual_leverage(json!({
+        "pair":"BTC-USDT",
+        "contract_type":"swap",
+        "lever_rate":1,
+    })).await;
+    trace!("htx--设置持仓模式--{:?}", req_data);
+    trace!("htx--设置持仓模式--{}", req_data.data);
+}
+
+
 fn get_rest() -> HtxSwapRest {
     let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
     btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());