Bladeren bron

优化代码

875428575@qq.com 2 jaren geleden
bovenliggende
commit
aa65a52a39

+ 4 - 1
exchanges/Cargo.toml

@@ -23,9 +23,12 @@ data-encoding = "2.4.0"
 
 hmac = "0.8.1"
 sha2 = "0.9.8"
-
 tokio-tungstenite = "0.14"
 
+##代替f64避免精度丢失
+rust_decimal = "1.32.0"
+rust_decimal_macros = "1.32.0"
+
 
 ##日志
 global = { path="../global" }

+ 256 - 0
exchanges/src/binance_swap_rest.rs

@@ -0,0 +1,256 @@
+use std::collections::BTreeMap;
+use reqwest::header::HeaderMap;
+use hex;
+use reqwest::Client;
+use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
+use rust_decimal_macros::dec;
+use crate::http_tool::RestTool;
+use crate::response_base::ResponseData;
+use tracing::trace;
+use ring::hmac;
+
+#[derive(Clone)]
+pub struct BinanceSwapRest {
+    label: String,
+    base_url: String,
+    client: reqwest::Client,
+    /*******参数*/
+    //登陆所需参数
+    login_param: BTreeMap<String, String>,
+    delays: Vec<i64>,
+    max_delay: i64,
+    avg_delay: Decimal,
+}
+
+impl BinanceSwapRest {
+    /*******************************************************************************************************/
+    /*****************************************获取一个对象****************************************************/
+    /*******************************************************************************************************/
+    pub fn new(is_colo: bool, login_param: BTreeMap<String, String>) -> BinanceSwapRest
+    {
+        return BinanceSwapRest::new_label("default-BinanceSwapRest".to_string(), is_colo, login_param);
+    }
+    pub fn new_label(label: String, is_colo: bool, login_param: BTreeMap<String, String>) -> BinanceSwapRest
+    {
+        let base_url = if is_colo {
+            trace!("不支持colo高速线路");
+            "https://fapi.binance.com".to_string()
+        } else {
+            "https://fapi.binance.com".to_string()
+        };
+
+        /*****返回结构体*******/
+        BinanceSwapRest {
+            label,
+            base_url: base_url.to_string(),
+            client: Client::new(),
+            login_param,
+            delays: vec![],
+            max_delay: 0,
+            avg_delay: dec!(0.0),
+        }
+    }
+
+    /*******************************************************************************************************/
+    /*****************************************rest请求函数********************************************************/
+    /*******************************************************************************************************/
+    // //账户信息
+    // pub async fn get_account1(&self) -> ResponseData {
+    //     let btree_map = serde_json::json!({
+    //         "recvWindow":"2000"
+    //      });
+    //
+    //     //组装登陆认证信息
+    //     let params = self.get_sign(btree_map.to_string());
+    //     let get_response = self.rest.http_toll(
+    //         "/fapi/v2/balance".to_string(),
+    //         "get".to_string(),
+    //         params,
+    //         self.get_headers(true),
+    //     ).await;
+    //     let mut res_data = RestTool::res_data_analysis(get_response);
+    //     res_data
+    // }
+
+    //账户信息
+    pub async fn get_account(&mut self) -> ResponseData {
+        let params = serde_json::json!({
+            "recvWindow":"2000"
+         });
+
+        let data = self.request("GET".to_string(),
+                                "".to_string(),
+                                format!("/fapi/v2/balance"),
+                                true,
+                                params.to_string(),
+        ).await;
+        data
+    }
+
+
+    /*******************************************************************************************************/
+    /*****************************************工具函数********************************************************/
+    /*******************************************************************************************************/
+    pub fn get_delays(&self) -> Vec<i64> {
+        self.delays.clone()
+    }
+    pub fn get_avg_delay(&self) -> Decimal {
+        self.avg_delay
+    }
+    fn get_delay_info(&mut self) {
+        let  last_100 = if self.delays.len() > 100 {
+            self.delays[self.delays.len() - 100..].to_vec()
+        } else {
+            self.delays.clone()
+        };
+
+        let max_value = last_100.iter().max().unwrap();
+        if max_value.clone().to_owned() > self.max_delay {
+            self.max_delay = max_value.clone().to_owned();
+        }
+
+        let sum: i64 = last_100.iter().sum();
+        let sum_v = Decimal::from_i64(sum).unwrap();
+        let len_v = Decimal::from_u64(last_100.len() as u64).unwrap();
+        self.avg_delay = (sum_v / len_v).round_dp(1);
+        self.delays = last_100.clone().into_iter().collect();
+    }
+
+    //调用请求
+    async fn request(&mut self,
+                     requesst_type: String,
+                     prefix_url: String,
+                     request_url: String,
+                     is_login: bool,
+                     params: String) -> ResponseData
+    {
+        // trace!("login_param:{:?}", self.login_param);
+        //解析账号信息
+        let mut access_key = "".to_string();
+        let mut secret_key = "".to_string();
+        if self.login_param.contains_key("access_key") {
+            access_key = self.login_param.get("access_key").unwrap().to_string();
+        }
+        if self.login_param.contains_key("secret_key") {
+            secret_key = self.login_param.get("secret_key").unwrap().to_string();
+        }
+        let mut is_login_param = true;
+        if access_key == "" || secret_key == "" {
+            is_login_param = false
+        }
+
+        //请求头配置-如果需要登陆则存在额外配置
+        let mut params_json: serde_json::Value = serde_json::from_str(params.clone().as_str()).unwrap();
+        // let mut body = "".to_string();
+        let timestamp = chrono::Utc::now().timestamp_millis().to_string();
+        params_json.as_object_mut().unwrap().insert("timestamp".to_string(), timestamp.parse().unwrap());
+
+
+        let mut headers = HeaderMap::new();
+        headers.insert("Content-Type", "application/json; charset=UTF-8".parse().unwrap());
+
+        //是否需要登陆-- 组装sing
+        if is_login {
+            if !is_login_param {
+                let e = ResponseData::error(self.label.clone(), "登陆参数错误!".to_string());
+                return e;
+            } else {//需要登陆-且登陆参数齐全
+                //组装sing
+                let sing = Self::sign(secret_key.clone(),
+                                      params_json.to_string(),
+                );
+                params_json.as_object_mut().unwrap().insert("signature".to_string(), serde_json::Value::from(sing.clone()));
+                // trace!("sing:{}", sing);
+                //组装header
+                headers.extend(Self::headers(access_key));
+            }
+        }
+
+        trace!("headers:{:?}", headers);
+        let get_response = self.http_toll(
+            format!("{}{}", prefix_url.clone(), request_url.clone()),
+            requesst_type.to_string(),
+            params_json.to_string(),
+            headers,
+        ).await;
+        let res_data = Self::res_data_analysis(get_response);
+        res_data
+    }
+
+    pub fn headers(access_key: String) -> HeaderMap {
+        let mut headers = HeaderMap::new();
+        headers.insert("X-MBX-APIKEY", access_key.parse().unwrap());
+        headers
+    }
+    pub fn sign(secret_key: String, params_str: String) -> String
+    {
+        /*签名生成*/
+        let params_str_v = RestTool::parse_params_to_str(params_str.to_string());
+        let message = format!("{}", params_str_v);
+
+        // let secret_key2 = "2b5eb11e18796d12d88f13dc27dbbd02c2cc51ff7059765ed9821957d82bb4d9";
+        // let message2 = "symbol=BTCUSDT&side=BUY&type=LIMIT&quantity=1&price=9000&timeInForce=GTC&recvWindow=5000&timestamp=1591702613943";
+        let signed_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_ref());
+        let sign = hex::encode(hmac::sign(&signed_key, message.as_bytes()).as_ref());
+
+        sign
+    }
+
+
+    async fn http_toll(&mut self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
+        let res_data: ResponseData;
+        /****请求接口与 地址*/
+        let url = format!("{}{}", self.base_url.to_string(), request_path);
+        let request_type = request_type.clone().to_uppercase();
+        let addrs_url = format!("{}?{}", url.clone(), RestTool::parse_params_to_str(params.clone()));
+        let params_json: serde_json::Value = serde_json::from_str(&params).unwrap();
+        trace!("url:{}",url);
+        trace!("addrs_url:{}",addrs_url);
+        trace!("params_json:{}",params_json);
+        trace!("headers:{:?}",headers);
+
+
+        let start_time = chrono::Utc::now().timestamp_millis();
+        let req = match request_type.as_str() {
+            "GET" => self.client.get(addrs_url.clone()).headers(headers),
+            "POST" => self.client.post(addrs_url.clone()).body(params).headers(headers),
+            "DELETE" => self.client.delete(addrs_url.clone()).headers(headers),
+            // "PUT" => self.client.put(url.clone()).json(&params),
+            _ => return Ok(ResponseData::error(self.label.clone(), format!("错误的请求类型:{}", request_type.clone()))), // 处理未知请求类型
+        };
+
+        let response = req.send().await?;
+        if response.status().is_success() {
+            // 读取响应的内容
+            let body = response.text().await?;
+            // trace!("ok-----{}", 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())
+        }
+        let time_array = chrono::Utc::now().timestamp_millis() - start_time;
+        self.delays.push(time_array);
+        self.get_delay_info();
+        Ok(res_data)
+    }
+
+    //res_data 解析
+    pub fn res_data_analysis(result: Result<ResponseData, reqwest::Error>) -> ResponseData {
+        match result {
+            Ok(res_data) => {
+                if res_data.code != "200" {
+                    res_data
+                } else {
+                    res_data
+                }
+            }
+            Err(err) => {
+                let error = ResponseData::error("".to_string(), format!("json 解析失败:{}", err));
+                error
+            }
+        }
+    }
+}

+ 1 - 7
exchanges/src/binance_usdt_swap_rest.rs

@@ -11,7 +11,6 @@ pub struct BinanceUsdtSwapRest {
     pub label: String,
     login_param: BTreeMap<String, String>,
     rest: RestTool,
-    response_time: i64,
 }
 
 impl BinanceUsdtSwapRest {
@@ -35,7 +34,6 @@ impl BinanceUsdtSwapRest {
             label,
             login_param,
             rest: RestTool::new(base_url.to_string()),
-            response_time: 0,
         }
     }
 
@@ -65,9 +63,6 @@ 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();
@@ -98,8 +93,7 @@ impl BinanceUsdtSwapRest {
         if self.login_param.contains_key("secret_key") {
             secret_key = self.login_param.get("secret_key").unwrap()
         }
-        // trace!("---参数:{}", params_str);
-        // trace!("---参数:{}", params.to_string());
+
         /*签名生成*/
         let params_str = RestTool::parse_params_to_str(params.to_string());
         // trace!("---参数:{}", params_str);

+ 58 - 25
exchanges/src/gate_swap_rest.rs

@@ -4,6 +4,9 @@ use ring::{digest};
 use hex;
 use hmac::{Hmac, Mac, NewMac};
 use reqwest::Client;
+use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
+use rust_decimal_macros::dec;
 use crate::http_tool::RestTool;
 use crate::response_base::ResponseData;
 use sha2::Sha512;
@@ -11,14 +14,15 @@ use tracing::trace;
 
 #[derive(Clone)]
 pub struct GateSwapRest {
-    pub label: String,
+    label: String,
     base_url: String,
     client: reqwest::Client,
     /*******参数*/
     //登陆所需参数
     login_param: BTreeMap<String, String>,
-    response_time: i64,
-
+    delays: Vec<i64>,
+    max_delay: i64,
+    avg_delay: Decimal,
 }
 
 impl GateSwapRest {
@@ -44,7 +48,9 @@ impl GateSwapRest {
             base_url: base_url.to_string(),
             client: Client::new(),
             login_param,
-            response_time: 0,
+            delays: vec![],
+            max_delay: 0,
+            avg_delay: dec!(0.0),
         }
     }
 
@@ -52,7 +58,7 @@ impl GateSwapRest {
     /*****************************************rest请求函数********************************************************/
     /*******************************************************************************************************/
     //获取服务器当前时间
-    pub async fn get_server_time(&self) -> ResponseData {
+    pub async fn get_server_time(&mut self) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -64,7 +70,7 @@ impl GateSwapRest {
         data
     }
     //查询个人交易费率
-    pub async fn wallet_fee(&self) -> ResponseData {
+    pub async fn wallet_fee(&mut self) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -76,7 +82,7 @@ impl GateSwapRest {
         data
     }
     //查询合约账户
-    pub async fn get_account(&self, settle: String) -> ResponseData {
+    pub async fn get_account(&mut self, settle: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -88,7 +94,7 @@ impl GateSwapRest {
         data
     }
     //用户仓位列表
-    pub async fn get_user_position(&self, settle: String) -> ResponseData {
+    pub async fn get_user_position(&mut self, settle: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -100,7 +106,7 @@ impl GateSwapRest {
         data
     }
     //双仓模式下的持仓信息
-    pub async fn get_position(&self, settle: String, contract: String) -> ResponseData {
+    pub async fn get_position(&mut self, settle: String, contract: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -112,7 +118,7 @@ impl GateSwapRest {
         data
     }
     //获取所有合约交易行情统计
-    pub async fn get_ticker(&self, settle: String) -> ResponseData {
+    pub async fn get_ticker(&mut self, settle: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -124,7 +130,7 @@ impl GateSwapRest {
         data
     }
     //查询所有的合约信息
-    pub async fn get_market_details(&self, settle: String) -> ResponseData {
+    pub async fn get_market_details(&mut self, settle: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -136,7 +142,7 @@ impl GateSwapRest {
         data
     }
     //查询单个订单详情
-    pub async fn get_order_details(&self, settle: String, order_id: String) -> ResponseData {
+    pub async fn get_order_details(&mut self, settle: String, order_id: String) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
@@ -148,7 +154,7 @@ impl GateSwapRest {
         data
     }
     //查询合约订单列表
-    pub async fn get_orders(&self, settle: String, status: String) -> ResponseData {
+    pub async fn get_orders(&mut self, settle: String, status: String) -> ResponseData {
         let params = serde_json::json!({
             "status":status
          });
@@ -162,7 +168,7 @@ impl GateSwapRest {
     }
 
     //下单:side-下单方向,pos_side-持仓方向
-    pub async fn order(&self,
+    pub async fn order(&mut self,
                        settle: String,
                        pos_side: String,
                        side: String,
@@ -209,7 +215,7 @@ impl GateSwapRest {
         data
     }
     //合约交易下单
-    pub async fn swap_order(&self, settle: String, params: serde_json::Value) -> ResponseData {
+    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),
@@ -219,7 +225,7 @@ impl GateSwapRest {
         data
     }
     //设置持仓模式
-    pub async fn setting_dual_mode(&self, settle: String, dual_mode: bool) -> ResponseData {
+    pub async fn setting_dual_mode(&mut self, settle: String, dual_mode: bool) -> ResponseData {
         let params = serde_json::json!({
                 "dual_mode":dual_mode,
              });
@@ -232,7 +238,7 @@ impl GateSwapRest {
         data
     }
     //更新双仓模式下的杠杆
-    pub async fn setting_dual_leverage(&self, settle: String, symbol: String, leverage: String) -> ResponseData {
+    pub async fn setting_dual_leverage(&mut self, settle: String, symbol: String, leverage: String) -> ResponseData {
         let params = serde_json::json!({
                 "leverage":leverage,
              });
@@ -245,7 +251,7 @@ impl GateSwapRest {
         data
     }
     //交易账户互转
-    pub async fn wallet_transfers(&self, currency: String, from: String, to: String, amount: String, settle: String) -> ResponseData {
+    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,
@@ -262,7 +268,7 @@ impl GateSwapRest {
         data
     }
     //撤销单个订单
-    pub async fn cancel_order(&self, settle: String, order_id: String) -> ResponseData {
+    pub async fn cancel_order(&mut self, settle: String, order_id: String) -> ResponseData {
         let params = serde_json::json!({
              });
 
@@ -275,7 +281,7 @@ impl GateSwapRest {
         data
     }
     //弃用
-    pub async fn swap_bazaar_order(&self, text: String, origin_side: String, settle: String, contract: String, size: i64) -> ResponseData {
+    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,
@@ -312,7 +318,7 @@ impl GateSwapRest {
     }
 
     //批量取消状态为 open 的订单
-    pub async fn cancel_orders(&self, settle: String, contract: String) -> ResponseData {
+    pub async fn cancel_orders(&mut self, settle: String, contract: String) -> ResponseData {
         let params = serde_json::json!({
             "contract":contract
              });
@@ -330,11 +336,33 @@ impl GateSwapRest {
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
-    fn get_response_time(&self) -> i64 {
-        self.response_time
+    pub fn get_delays(&self) -> Vec<i64> {
+        self.delays.clone()
+    }
+    pub fn get_avg_delay(&self) -> Decimal {
+        self.avg_delay
+    }
+    fn get_delay_info(&mut self) {
+        let  last_100 = if self.delays.len() > 100 {
+            self.delays[self.delays.len() - 100..].to_vec()
+        } else {
+            self.delays.clone()
+        };
+
+        let max_value = last_100.iter().max().unwrap();
+        if max_value.clone().to_owned() > self.max_delay {
+            self.max_delay = max_value.clone().to_owned();
+        }
+
+        let sum: i64 = last_100.iter().sum();
+        let sum_v = Decimal::from_i64(sum).unwrap();
+        let len_v = Decimal::from_u64(last_100.len() as u64).unwrap();
+        self.avg_delay = (sum_v / len_v).round_dp(1);
+        self.delays = last_100.clone().into_iter().collect();
     }
+
     //调用请求
-    async fn request(&self,
+    async fn request(&mut self,
                      requesst_type: String,
                      prefix_url: String,
                      request_url: String,
@@ -446,7 +474,7 @@ impl GateSwapRest {
     }
 
 
-    async fn http_toll(&self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
+    async fn http_toll(&mut self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
         let res_data: ResponseData;
         /****请求接口与 地址*/
         let url = format!("{}{}", self.base_url.to_string(), request_path);
@@ -458,6 +486,8 @@ impl GateSwapRest {
         // trace!("params_json:{}",params_json);
         // trace!("headers:{:?}",headers);
 
+
+        let start_time = chrono::Utc::now().timestamp_millis();
         let req = match request_type.as_str() {
             "GET" => self.client.get(addrs_url.clone()).headers(headers),
             "POST" => self.client.post(addrs_url.clone()).body(params).headers(headers),
@@ -477,6 +507,9 @@ impl GateSwapRest {
             // trace!("error-----{}", body);
             res_data = ResponseData::error(self.label.clone(), body.to_string())
         }
+        let time_array = chrono::Utc::now().timestamp_millis() - start_time;
+        self.delays.push(time_array);
+        self.get_delay_info();
         Ok(res_data)
     }
 

+ 3 - 3
exchanges/src/http_tool.rs

@@ -75,8 +75,8 @@ impl RestTool {
                     serde_json::Value::String(s) => s.clone(),
                     _ => value.to_string()
                 };
-                // trace!("Key: {}", key);
-                // trace!("Value: {}", formatted_value);
+                trace!("Key: {}", key);
+                trace!("Value: {}", formatted_value);
                 // let formatted_value = match value {
                 //     Value::String(s) => s.clone(),
                 //     _ => value.to_string()
@@ -86,7 +86,7 @@ impl RestTool {
                 params_str = format_str;
             }
         }
-        // trace!("---json-转字符串拼接:{}",params_str);
+        trace!("---json-转字符串拼接:{}",params_str);
         params_str.to_string()
     }
     //res_data 解析

+ 52 - 20
exchanges/src/kucoin_swap_rest.rs

@@ -2,6 +2,9 @@ use std::collections::BTreeMap;
 use reqwest::header::HeaderMap;
 use hmac::{Hmac, Mac, NewMac};
 use reqwest::{Client};
+use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
+use rust_decimal_macros::dec;
 use sha2::Sha256;
 use tracing::trace;
 use crate::http_tool::RestTool;
@@ -16,7 +19,9 @@ pub struct KucoinSwapRest {
     //是否需要登陆
     //登陆所需参数
     login_param: BTreeMap<String, String>,
-    response_time:i64,
+    delays: Vec<i64>,
+    max_delay: i64,
+    avg_delay: Decimal,
 
 }
 
@@ -43,26 +48,28 @@ impl KucoinSwapRest {
             base_url,
             client: Client::new(),
             login_param,
-            response_time:0,
+            delays: vec![],
+            max_delay: 0,
+            avg_delay: dec!(0.0),
         }
     }
 
     /*******************************************************************************************************/
     /*****************************************rest请求函数********************************************************/
     /*******************************************************************************************************/
-    pub async fn get_server_time(&self) -> ResponseData {
+    pub async fn get_server_time(&mut self) -> ResponseData {
         let params = serde_json::json!({
          });
         let data = self.request("GET".to_string(),
                                 "/api/v1".to_string(),
                                 "/timestamp".to_string(),
-                                true,
+                                false,
                                 params.to_string(),
         ).await;
         data
     }
     //查询合约账户
-    pub async fn get_account(&self, contract: String) -> ResponseData {
+    pub async fn get_account(&mut self, contract: String) -> ResponseData {
         let params = serde_json::json!({
             "currency":contract
          });
@@ -75,7 +82,7 @@ impl KucoinSwapRest {
         data
     }
     //获取仓位信息
-    pub async fn get_position(&self, symbol: String) -> ResponseData {
+    pub async fn get_position(&mut self, symbol: String) -> ResponseData {
         let params = serde_json::json!({
             "symbol":symbol
          });
@@ -88,7 +95,7 @@ impl KucoinSwapRest {
         data
     }
     //查询所有的合约信息
-    pub async fn get_market_details(&self) -> ResponseData {
+    pub async fn get_market_details(&mut self) -> ResponseData {
         let params = serde_json::json!({});
 
         let data = self.request("GET".to_string(),
@@ -100,7 +107,7 @@ impl KucoinSwapRest {
         data
     }
     //实时行情
-    pub async fn get_ticker(&self, symbol: String) -> ResponseData {
+    pub async fn get_ticker(&mut self, symbol: String) -> ResponseData {
         let params = serde_json::json!({
             "symbol":symbol
          });
@@ -113,7 +120,7 @@ impl KucoinSwapRest {
         data
     }
     //查看订单列表
-    pub async fn get_orders(&self, status: String, symbol: String) -> ResponseData {
+    pub async fn get_orders(&mut self, status: String, symbol: String) -> ResponseData {
         let mut params = serde_json::json!({
             "symbol":symbol
          });
@@ -129,7 +136,7 @@ impl KucoinSwapRest {
         data
     }
     //获取用户仓位列表
-    pub async fn get_positions(&self, currency: String) -> ResponseData {
+    pub async fn get_positions(&mut self, currency: String) -> ResponseData {
         let params = serde_json::json!({
             "currency":currency
          });
@@ -142,7 +149,7 @@ impl KucoinSwapRest {
         data
     }
     //单个订单详情
-    pub async fn get_orders_details(&self, order_id: String, client_id: String) -> ResponseData {
+    pub async fn get_orders_details(&mut self, order_id: String, client_id: String) -> ResponseData {
         let mut params = serde_json::json!({   });
         let mut url = String::from("");
         if order_id != "" {
@@ -160,7 +167,7 @@ impl KucoinSwapRest {
         ).await;
         data
     }
-    pub async fn swap_order(&self, params: serde_json::Value) -> ResponseData {
+    pub async fn swap_order(&mut self, params: serde_json::Value) -> ResponseData {
         let data = self.request("POST".to_string(),
                                 "/api/v1".to_string(),
                                 format!("/orders"),
@@ -170,7 +177,7 @@ impl KucoinSwapRest {
         data
     }
     //下单
-    pub async fn swap_bazaar_order(&self,
+    pub async fn swap_bazaar_order(&mut self,
                                    client_oid: String,
                                    symbol: String,
                                    origin_side: String,
@@ -218,7 +225,7 @@ impl KucoinSwapRest {
     }
 
     //单个撤单
-    pub async fn cancel_order(&self, order_id: String, client_id: String) -> ResponseData {
+    pub async fn cancel_order(&mut self, order_id: String, client_id: String) -> ResponseData {
         let mut params = serde_json::json!({   });
         let mut url = String::from("");
         if order_id != "" {
@@ -237,7 +244,7 @@ impl KucoinSwapRest {
     }
 
     //获取合约令牌-公共
-    pub async fn get_public_token(&self) -> ResponseData {
+    pub async fn get_public_token(&mut self) -> ResponseData {
         let params = serde_json::json!({});
         let data = self.request("POST".to_string(),
                                 "/api/v1".to_string(),
@@ -248,7 +255,7 @@ impl KucoinSwapRest {
         data
     }
     //获取合约令牌-私有
-    pub async fn get_private_token(&self) -> ResponseData {
+    pub async fn get_private_token(&mut self) -> ResponseData {
         let params = serde_json::json!({});
         let data = self.request("POST".to_string(),
                                 "/api/v1".to_string(),
@@ -261,11 +268,32 @@ impl KucoinSwapRest {
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
     /*******************************************************************************************************/
-    fn get_response_time(&self) -> i64 {
-        self.response_time
+    pub fn get_delays(&self) -> Vec<i64> {
+        self.delays.clone()
+    }
+    pub fn get_avg_delay(&self) -> Decimal {
+        self.avg_delay
+    }
+    fn get_delay_info(&mut self) {
+        let last_100 = if self.delays.len() > 100 {
+            self.delays[self.delays.len() - 100..].to_vec()
+        } else {
+            self.delays.clone()
+        };
+
+        let max_value = last_100.iter().max().unwrap();
+        if max_value.clone().to_owned() > self.max_delay {
+            self.max_delay = max_value.clone().to_owned();
+        }
+
+        let sum: i64 = last_100.iter().sum();
+        let sum_v = Decimal::from_i64(sum).unwrap();
+        let len_v = Decimal::from_u64(last_100.len() as u64).unwrap();
+        self.avg_delay = (sum_v / len_v).round_dp(1);
+        self.delays = last_100.clone().into_iter().collect();
     }
     //调用请求
-    pub async fn request(&self,
+    pub async fn request(&mut self,
                          method: String,
                          prefix_url: String,
                          request_url: String,
@@ -404,7 +432,7 @@ impl KucoinSwapRest {
     }
 
 
-    async fn http_toll(&self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
+    async fn http_toll(&mut self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
         let res_data: ResponseData;
         /****请求接口与 地址*/
         let url = format!("{}{}", self.base_url.to_string(), request_path);
@@ -417,6 +445,7 @@ impl KucoinSwapRest {
         trace!("url:{}", url);
         trace!("addrs_url:{}", addrs_url);
 
+        let start_time = chrono::Utc::now().timestamp_millis();
         let req = match request_type.as_str() {
             "GET" => self.client.get(addrs_url.clone()).headers(headers),
             "POST" => self.client.post(url.clone()).body(params).headers(headers),
@@ -436,6 +465,9 @@ impl KucoinSwapRest {
             // trace!("error-----{}", body);
             res_data = ResponseData::error(self.label.clone(), body.to_string())
         }
+        let time_array = chrono::Utc::now().timestamp_millis() - start_time;
+        self.delays.push(time_array);
+        self.get_delay_info();
         Ok(res_data)
     }
 

+ 1 - 1
exchanges/src/kucoin_swap_ws.rs

@@ -120,7 +120,7 @@ impl KucoinSwapWs {
     }
     //根据当前类型获取对应的频道 地址 与 token
     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 mut kucoin_exc = KucoinSwapRest::new(false, login_param.clone());
         let res_data = match ws_type {
             KucoinWsType::Public => {
                 kucoin_exc.get_public_token().await

+ 1 - 0
exchanges/src/lib.rs

@@ -15,5 +15,6 @@ pub mod kucoin_swap_ws;
 pub mod okx_swap_ws;
 pub mod binance_swap_ws;
 pub mod okx_swap_rest;
+pub mod binance_swap_rest;
 
 

+ 21 - 9
exchanges/tests/test.rs

@@ -19,6 +19,7 @@ use tokio::sync::mpsc::channel;
 use tokio::try_join;
 use tracing::{debug, trace};
 use tracing::field::debug;
+use exchanges::binance_swap_rest::BinanceSwapRest;
 use exchanges::gate_swap_ws::{GateSubscribeType, GateSwapWs, GateWsType};
 
 
@@ -43,11 +44,11 @@ async fn test_import() {
     //gate-rest -账户信息
     // demo_rest_gate().await;
     //gate-ws-public-private频道
-    demo_ws_gate().await;
+    // demo_ws_gate().await;
 
 
     //kucoin_rest -账户信息
-    // demo_rest_kucoin().await;
+    demo_rest_kucoin().await;
 
 
     //Kucoin-ws--公共频道
@@ -213,9 +214,11 @@ async fn demo_rest_kucoin() {
     btree_map.insert("secret_key".to_string(), "".to_string());
     btree_map.insert("pass_key".to_string(), "".to_string());
 
-    let kucoin_exc = KucoinSwapRest::new(false, btree_map);
+    let mut kucoin_exc = KucoinSwapRest::new(false, btree_map);
     let res_data = kucoin_exc.get_server_time().await;
     trace!("kucoin_exc-rest - get_server_time- {:?}", res_data);
+    trace!("kucoin_exc-rest-get_delays{:?}", kucoin_exc.get_delays() );
+    trace!("kucoin_exc-rest -get_avg_delay{:?}", kucoin_exc.get_avg_delay());
     // let res_data = kucoin_exc.get_account("USDT".to_string()).await;
     // trace!("kucoin_exc-rest - get_account- {:?}", res_data);
     // let res_data = kucoin_exc.get_position("XBTUSDM".to_string()).await;
@@ -249,12 +252,15 @@ async fn demo_rest_kucoin() {
 
 async fn demo_rest_gate() {
     let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
-    btree_map.insert("access_key".to_string(), "".to_string());
-    btree_map.insert("secret_key".to_string(), "".to_string());
+    btree_map.insert("access_key".to_string(), "4181c882718a95e72122ac1d52c88533".to_string());
+    btree_map.insert("secret_key".to_string(), "de82d1507b843ff08d81a0e9b878b721359f274937216b307834b570b676fa3c".to_string());
 
-    let gate_exc = GateSwapRest::new(false, btree_map);
+    let mut gate_exc = GateSwapRest::new(false, btree_map);
     let res_data = gate_exc.get_account("usdt".to_string()).await;
+
     trace!("gate-rest -账户信息{:?}", res_data);
+    trace!("gate-rest -get_delays{:?}", gate_exc.get_delays() );
+    trace!("gate-rest -get_avg_delay{:?}", gate_exc.get_avg_delay());
     // let res_data = gate_exc.get_position("usdt".to_string(), "CYBER_USDT".to_string()).await;
     // trace!("gate-rest -持仓信息{:?}", res_data);
     // let res_data = gate_exc.get_ticker("usdt".to_string()).await;
@@ -309,12 +315,18 @@ async fn demo_rest_gate() {
 
 async fn demo_rest_ba() {
     let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
-    btree_map.insert("access_key".to_string(), "".to_string());
-    btree_map.insert("secret_key".to_string(), "".to_string());
+    btree_map.insert("access_key".to_string(), "Z2KWEFuiTR26d0OrRh0UVnALiKwftEGKdaz6AHU7xAqZqkKwhb8OPWlG5uAGqGBI".to_string());
+    btree_map.insert("secret_key".to_string(), "w3HvCw17OHEDjpMwlfywtl3tLAfSOql81nXgLk4HTA2yQ4Qph0ilPkKiLgOOY7tQ".to_string());
 
-    let ba_exc = BinanceUsdtSwapRest::new(false, btree_map);
+    // let ba_exc = BinanceUsdtSwapRest::new(false, btree_map);
+    // let res_data = ba_exc.get_account().await;
+    let mut ba_exc = BinanceSwapRest::new(false, btree_map);
     let res_data = ba_exc.get_account().await;
+
+
     trace!("币安-rest-获取账户信息{:?}", res_data);
+    trace!("币安-rest- -get_delays{:?}", ba_exc.get_delays() );
+    trace!("币安-rest--get_avg_delay{:?}", ba_exc.get_avg_delay());
 }
 
 async fn demo_pub_ws_ba() {