Răsfoiți Sursa

修改kucoin 的订阅

875428575@qq.com 2 ani în urmă
părinte
comite
ab203b6063
1 a modificat fișierele cu 48 adăugiri și 24 ștergeri
  1. 48 24
      exchanges/src/kucoin_swap_ws.rs

+ 48 - 24
exchanges/src/kucoin_swap_ws.rs

@@ -38,8 +38,8 @@ pub enum KucoinSubscribeType {
     PuContractMarketExecution,
     PuContractMarkettickerV2,
 
-    PrContractAccountWallet(String),
-    PrContractPosition(String),
+    PrContractAccountWallet,
+    PrContractPosition,
     PrContractMarketTradeOrdersSys,
     PrContractMarketTradeOrders,
 }
@@ -134,6 +134,8 @@ impl KucoinSwapWs {
             }
         };
 
+        trace!("kucoin-rest 获取ws连接地址:{:?}",res_data);
+
         if res_data.code == "200" {
             let mut ws_url = "".to_string();
             let mut ws_token = "".to_string();
@@ -179,43 +181,64 @@ impl KucoinSwapWs {
     /*******************************************************************************************************/
     //订阅枚举解析
     pub fn enum_to_string(symbol: String, subscribe_type: KucoinSubscribeType) -> serde_json::Value {
+        let timestamp =  chrono::Utc::now().timestamp_millis();
         match subscribe_type {
             KucoinSubscribeType::PuContractMarketLevel2Depth50 => {
                 serde_json::json!({
                      "topic": format!("/contractMarket/level2Depth50:{}", symbol),
                      "type": "subscribe",
+                     "response": true
                 })
             }
             KucoinSubscribeType::PuContractMarketExecution => {
                 serde_json::json!({
                      "topic": format!("/contractMarket/execution:{}", symbol),
                      "type": "subscribe",
+                     "response": true
                 })
             }
             KucoinSubscribeType::PuContractMarkettickerV2 => {
                 serde_json::json!({
                      "topic": format!("/contractMarket/tickerV2:{}", symbol),
                      "type": "subscribe",
+                     "response": true
                 })
             }
-
-            KucoinSubscribeType::PrContractAccountWallet(user_id) => {
+            // "type":"subscribe",
+            // "topic":"/market/ticker:BTC-USDT,ETH-USDT",
+            // "privateChannel":false,
+            // "response":true
+            KucoinSubscribeType::PrContractAccountWallet => {
                 serde_json::json!({
-                     "topic": format!("/contractAccount/wallet"),
-                     "subject": "orderMargin.change",
+                    "type": "subscribe",
+                    "topic": "/contractAccount/wallet",
+                    "privateChannel":true,
+                    "response":true,
                 })
             }
-            KucoinSubscribeType::PrContractPosition(user_id) => {
-                format!("/contract/position:{}", symbol);
-                serde_json::json!({})
+            KucoinSubscribeType::PrContractPosition => {
+                serde_json::json!({
+                    "type": "subscribe",
+                    "topic": format!("/contract/position:{}", symbol),
+                    "privateChannel":true,
+                    "response":true,
+                })
             }
             KucoinSubscribeType::PrContractMarketTradeOrdersSys => {
-                format!("/contractMarket/tradeOrders");
-                serde_json::json!({})
+                serde_json::json!({
+                    "type": "subscribe",
+                    "topic": format!("/contractMarket/tradeOrders"),
+                    "privateChannel":true,
+                    "response":true,
+                })
             }
             KucoinSubscribeType::PrContractMarketTradeOrders => {
-                format!("/contractMarket/tradeOrders:{}", symbol);
-                serde_json::json!({})
+                serde_json::json!({
+                    "type": "subscribe",
+                    "topic": format!("/contractMarket/tradeOrders:{}", symbol),
+                    "privateChannel":true,
+                    "response":true,
+                })
             }
         }
     }
@@ -455,17 +478,18 @@ impl KucoinSwapWs {
     {
         let mut res_data = ResponseData::new(lable, "200".to_string(), "success".to_string(), "".to_string());
         let json_value: serde_json::Value = serde_json::from_str(&text).unwrap();
-        if json_value.get("id").is_some() && json_value.get("type").is_some() {
-            //订阅 相应
-            if json_value["type"].as_str() == Option::from("welcome") {
-                //链接成功
-                res_data.code = "-200".to_string();
-                res_data.message = "链接成功,主动发起订阅".to_string();
-                trace!("链接成功,主动发起订阅:");
-            } else if json_value["type"].as_str() == Option::from("ack") {
-                res_data.code = "-201".to_string();
-                res_data.message = "订阅成功".to_string();
-            }
+        //订阅 相应
+        if json_value["type"].as_str() == Option::from("welcome") {
+            //链接成功
+            res_data.code = "-200".to_string();
+            res_data.message = "链接成功,主动发起订阅".to_string();
+            trace!("链接成功,主动发起订阅:");
+        } else if json_value["type"].as_str() == Option::from("ack") {
+            res_data.code = "-201".to_string();
+            res_data.message = "订阅成功".to_string();
+        } else if json_value["type"].as_str() == Option::from("error") {
+            res_data.code = format!("{}", json_value["code"]);
+            res_data.message = format!("{}", json_value["data"].as_str().unwrap());
         } else if json_value.get("topic").is_some() {
             res_data.channel = format!("{}", json_value["topic"].as_str().unwrap());
             res_data.data = json_value["data"].to_string();