Kaynağa Gözat

优化代码

875428575@qq.com 2 yıl önce
ebeveyn
işleme
f9cf82e7e4

+ 1 - 1
exchanges/src/binance_swap_ws.rs

@@ -3,7 +3,7 @@ use std::{io};
 use std::io::{Write};
 use std::net::{IpAddr, Ipv4Addr, SocketAddr};
 use tokio::sync::mpsc::Sender;
-use tracing::{info, trace};
+use tracing::{ trace};
 use crate::{proxy};
 use tungstenite::client::{AutoStream, connect_with_proxy, ProxyAutoStream};
 use tungstenite::{connect, Message, WebSocket};

+ 5 - 0
exchanges/src/binance_usdt_swap_rest.rs

@@ -11,6 +11,7 @@ pub struct BinanceUsdtSwapRest {
     pub label: String,
     login_param: BTreeMap<String, String>,
     rest: RestTool,
+    response_time: i64,
 }
 
 impl BinanceUsdtSwapRest {
@@ -34,6 +35,7 @@ impl BinanceUsdtSwapRest {
             label,
             login_param,
             rest: RestTool::new(base_url.to_string()),
+            response_time: 0,
         }
     }
 
@@ -63,6 +65,9 @@ impl BinanceUsdtSwapRest {
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
+    fn get_response_time(&self) -> i64 {
+        self.response_time
+    }
     //组装heander
     fn get_headers(&self, is_login: bool) -> HeaderMap {
         let mut headers = HeaderMap::new();

+ 12 - 6
exchanges/src/gate_swap_rest.rs

@@ -17,6 +17,8 @@ pub struct GateSwapRest {
     /*******参数*/
     //登陆所需参数
     login_param: BTreeMap<String, String>,
+    response_time: i64,
+
 }
 
 impl GateSwapRest {
@@ -42,6 +44,7 @@ impl GateSwapRest {
             base_url: base_url.to_string(),
             client: Client::new(),
             login_param,
+            response_time: 0,
         }
     }
 
@@ -169,10 +172,10 @@ impl GateSwapRest {
                        text: String,
     ) -> ResponseData {
         if side != "buy" && side != "sell" {
-            ResponseData::error(self.label.clone(),format!("未知下单方向!{}", side));
+            ResponseData::error(self.label.clone(), format!("未知下单方向!{}", side));
         }
         if pos_side != "long" && pos_side != "short" {
-            ResponseData::error(self.label.clone(),format!("未知持仓方向!{}", side));
+            ResponseData::error(self.label.clone(), format!("未知持仓方向!{}", side));
         }
         let mut param = serde_json::json!({
             "contract":contract, //合约标识
@@ -327,6 +330,9 @@ impl GateSwapRest {
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
+    fn get_response_time(&self) -> i64 {
+        self.response_time
+    }
     //调用请求
     async fn request(&self,
                      requesst_type: String,
@@ -370,7 +376,7 @@ impl GateSwapRest {
         //是否需要登陆-- 组装sing
         if is_login {
             if !is_login_param {
-                let e = ResponseData::error(self.label.clone(),"登陆参数错误!".to_string());
+                let e = ResponseData::error(self.label.clone(), "登陆参数错误!".to_string());
                 return e;
             } else {//需要登陆-且登陆参数齐全
                 //组装sing
@@ -465,11 +471,11 @@ impl GateSwapRest {
             // 读取响应的内容
             let body = response.text().await?;
             // trace!("ok-----{}", body);
-            res_data = ResponseData::new(self.label.clone(),"200".to_string(), "success".to_string(), body);
+            res_data = ResponseData::new(self.label.clone(), "200".to_string(), "success".to_string(), body);
         } else {
             let body = response.text().await?;
             // trace!("error-----{}", body);
-            res_data = ResponseData::error(self.label.clone(),body.to_string())
+            res_data = ResponseData::error(self.label.clone(), body.to_string())
         }
         Ok(res_data)
     }
@@ -485,7 +491,7 @@ impl GateSwapRest {
                 }
             }
             Err(err) => {
-                let error = ResponseData::error("".to_string(),format!("json 解析失败:{}", err));
+                let error = ResponseData::error("".to_string(), format!("json 解析失败:{}", err));
                 error
             }
         }

+ 9 - 2
exchanges/src/kucoin_swap_rest.rs

@@ -16,6 +16,7 @@ pub struct KucoinSwapRest {
     //是否需要登陆
     //登陆所需参数
     login_param: BTreeMap<String, String>,
+    response_time:i64,
 
 }
 
@@ -42,6 +43,7 @@ impl KucoinSwapRest {
             base_url,
             client: Client::new(),
             login_param,
+            response_time:0,
         }
     }
 
@@ -112,10 +114,12 @@ impl KucoinSwapRest {
     }
     //查看订单列表
     pub async fn get_orders(&self, status: String, symbol: String) -> ResponseData {
-        let params = serde_json::json!({
-            "status":status,
+        let mut params = serde_json::json!({
             "symbol":symbol
          });
+        if status.len() > 0 {
+            params.as_object_mut().unwrap().insert("status".parse().unwrap(), status.parse().unwrap());
+        }
         let data = self.request("GET".to_string(),
                                 "/api/v1".to_string(),
                                 format!("/orders"),
@@ -257,6 +261,9 @@ impl KucoinSwapRest {
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
+    fn get_response_time(&self) -> i64 {
+        self.response_time
+    }
     //调用请求
     pub async fn request(&self,
                          method: String,

+ 27 - 27
exchanges/src/kuconin_swap_ws.rs → exchanges/src/kucoin_swap_ws.rs

@@ -15,14 +15,14 @@ use crate::proxy::ParsingDetail;
 use crate::response_base::ResponseData;
 
 
-pub enum KuconinWsType {
+pub enum KucoinWsType {
     Public,
     Private,
 }
 
 #[derive(Debug)]
 #[derive(Clone)]
-pub struct KuconinWsParam {
+pub struct KucoinWsParam {
     pub token: String,
     pub ws_url: String,
     pub ws_ping_interval: i64,
@@ -30,7 +30,7 @@ pub struct KuconinWsParam {
 }
 
 #[derive(Clone)]                        //订阅枚举
-pub enum KuconinSubscribeType {
+pub enum KucoinSubscribeType {
     PuContractMarketLevel2Depth50,
     PuContractMarketExecution,
 
@@ -41,7 +41,7 @@ pub enum KuconinSubscribeType {
 }
 
 #[derive(Clone)]
-pub struct KuconinSwapWs {
+pub struct KucoinSwapWs {
     pub label: String,
     request_url: String,
     //实际ws 链接地址
@@ -49,31 +49,31 @@ pub struct KuconinSwapWs {
     //代理信息
     // login_param: BTreeMap<String, String>,
     //登陆数据
-    ws_param: KuconinWsParam,
+    ws_param: KucoinWsParam,
     //kuconis特殊参数
     symbol_s: Vec<String>,
     //订阅币对
-    subscribe_types: Vec<KuconinSubscribeType>,
+    subscribe_types: Vec<KucoinSubscribeType>,
     //订阅信息
     sender: Sender<ResponseData>,     //数据通道
 }
 
-impl KuconinSwapWs {
+impl KucoinSwapWs {
     /*******************************************************************************************************/
     /*****************************************获取一个对象****************************************************/
     /*******************************************************************************************************/
     pub async fn new(is_colo: bool,
                      login_param: BTreeMap<String, String>,
-                     ws_type: KuconinWsType,
+                     ws_type: KucoinWsType,
                      sender: Sender<ResponseData>,
-    ) -> KuconinSwapWs {
-        return KuconinSwapWs::new_label("default-KuconinSwapWs".to_string(), is_colo, login_param, ws_type, sender).await;
+    ) -> KucoinSwapWs {
+        return KucoinSwapWs::new_label("default-KucoinSwapWs".to_string(), is_colo, login_param, ws_type, sender).await;
     }
     pub async fn new_label(label: String, is_colo: bool,
                            login_param: BTreeMap<String, String>,
-                           ws_type: KuconinWsType,
+                           ws_type: KucoinWsType,
                            sender: Sender<ResponseData>,
-    ) -> KuconinSwapWs
+    ) -> KucoinSwapWs
     {
         if is_colo {
             trace!("不支持高速通道")
@@ -83,13 +83,13 @@ impl KuconinSwapWs {
         let parsing_detail = proxy::ParsingDetail::parsing_environment_variables();
 
         /*******公共频道-私有频道数据组装*/
-        let mut ws_param = KuconinWsParam {
+        let mut ws_param = KucoinWsParam {
             token: "".to_string(),
             ws_url: "".to_string(),
             ws_ping_interval: 0,
             ws_ping_timeout: 0,
         };
-        let res_data = KuconinSwapWs::get_rul_token(ws_type, login_param.clone()).await;
+        let res_data = KucoinSwapWs::get_rul_token(ws_type, login_param.clone()).await;
         match res_data {
             Ok(param) => {
                 ws_param = param
@@ -99,7 +99,7 @@ impl KuconinSwapWs {
             }
         }
         /*****返回结构体*******/
-        KuconinSwapWs {
+        KucoinSwapWs {
             label,
             request_url: "".to_string(),
             proxy: parsing_detail,
@@ -115,17 +115,17 @@ impl KuconinSwapWs {
     /*****************************************订阅函数********************************************************/
     /*******************************************************************************************************/
     //手动添加订阅信息
-    pub fn set_subscribe(&mut self, subscribe_types: Vec<KuconinSubscribeType>) {
+    pub fn set_subscribe(&mut self, subscribe_types: Vec<KucoinSubscribeType>) {
         self.subscribe_types.extend(subscribe_types);
     }
     //根据当前类型获取对应的频道 地址 与 token
-    async fn get_rul_token(ws_type: KuconinWsType, login_param: BTreeMap<String, String>) -> Result<KuconinWsParam, reqwest::Error> {
+    async fn get_rul_token(ws_type: KucoinWsType, login_param: BTreeMap<String, String>) -> Result<KucoinWsParam, reqwest::Error> {
         let kucoin_exc = KucoinSwapRest::new(false, login_param.clone());
         let res_data = match ws_type {
-            KuconinWsType::Public => {
+            KucoinWsType::Public => {
                 kucoin_exc.get_public_token().await
             }
-            KuconinWsType::Private => {
+            KucoinWsType::Private => {
                 kucoin_exc.get_private_token().await
             }
         };
@@ -157,7 +157,7 @@ impl KuconinSwapWs {
             }
 
 
-            Ok(KuconinWsParam { ws_url, token: ws_token, ws_ping_interval, ws_ping_timeout })
+            Ok(KucoinWsParam { ws_url, token: ws_token, ws_ping_interval, ws_ping_timeout })
         } else {
             panic!("公共/私有-频道获取失败:{:?}", res_data)
         }
@@ -174,24 +174,24 @@ impl KuconinSwapWs {
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
     //订阅枚举解析
-    pub fn enum_to_string(symbol: String, subscribe_type: KuconinSubscribeType) -> String {
+    pub fn enum_to_string(symbol: String, subscribe_type: KucoinSubscribeType) -> String {
         match subscribe_type {
-            KuconinSubscribeType::PuContractMarketLevel2Depth50 => {
+            KucoinSubscribeType::PuContractMarketLevel2Depth50 => {
                 format!("/contractMarket/level2Depth50:{}", symbol)
             }
-            KuconinSubscribeType::PuContractMarketExecution => {
+            KucoinSubscribeType::PuContractMarketExecution => {
                 format!("/contractMarket/execution:{}", symbol)
             }
-            KuconinSubscribeType::PrContractAccountWallet => {
+            KucoinSubscribeType::PrContractAccountWallet => {
                 format!("/contractAccount/wallet")
             }
-            KuconinSubscribeType::PrContractPosition => {
+            KucoinSubscribeType::PrContractPosition => {
                 format!("/contract/position:{}", symbol)
             }
-            KuconinSubscribeType::PrContractMarketTradeOrdersSys => {
+            KucoinSubscribeType::PrContractMarketTradeOrdersSys => {
                 format!("/contractMarket/tradeOrders")
             }
-            KuconinSubscribeType::PrContractMarketTradeOrders => {
+            KucoinSubscribeType::PrContractMarketTradeOrders => {
                 format!("/contractMarket/tradeOrders:{}", symbol)
             }
         }

+ 2 - 2
exchanges/src/response_base.rs

@@ -12,7 +12,7 @@ pub struct ResponseData {
 
 impl ResponseData {
     pub fn new(lable: String, code: String, message: String, data: String) -> ResponseData {
-        ResponseData { lable, code, message, data, channel: "".to_string() }
+        ResponseData { lable, code, message, data, channel: "".to_string()  }
     }
     pub fn error(lable: String, message: String) -> ResponseData {
         ResponseData {
@@ -20,7 +20,7 @@ impl ResponseData {
             code: "-1".to_string(),
             message: "请求失败:".to_string() + &message,
             data: "".to_string(),
-            channel: "".to_string(),
+            channel: "".to_string()
         }
     }
 }