Jelajahi Sumber

k线和depth数据,做完了。

skyffire 6 bulan lalu
induk
melakukan
ac00a6083c
2 mengubah file dengan 116 tambahan dan 14 penghapusan
  1. 113 11
      src/exchange/mexc_spot_client.rs
  2. 3 3
      src/utils/rest_utils.rs

+ 113 - 11
src/exchange/mexc_spot_client.rs

@@ -62,6 +62,67 @@ impl MexcSpotClient {
         data
     }
 
+    // 获取平台可API交易的交易对
+    pub async fn default_symbols(&mut self) -> Response {
+        let data = self.request("GET".to_string(),
+                                "/api/v3".to_string(),
+                                "/defaultSymbols".to_string(),
+                                false,
+                                Value::Null,
+        ).await;
+
+        data
+    }
+
+    // 获取交易规则和交易对信息
+    // 用法	        举例
+    // 不需要交易对	curl -X GET "https://api.mexc.com/api/v3/exchangeInfo"
+    // 单个交易对	    curl -X GET "https://api.mexc.com/api/v3/exchangeInfo?symbol=MXUSDT"
+    // 多个交易对	    curl -X GET "https://api.mexc.com/api/v3/exchangeInfo?symbols=MXUSDT,BTCUSDT"
+    pub async fn exchange_info(&mut self, params: Value) -> Response {
+        let data = self.request("GET".to_string(),
+                                "/api/v3".to_string(),
+                                "/exchangeInfo".to_string(),
+                                false,
+                                params,
+        ).await;
+
+        data
+    }
+
+    // 获取深度信息
+    // 参数名	数据类型	是否必须	   说明	        取值范围
+    // symbol	string	是	     交易对名称	    如:BTCUSDT
+    // limit	integer	否	     返回的条数      	默认 100; 最大 5000
+    pub async fn depth(&mut self, params: Value) -> Response {
+        let data = self.request("GET".to_string(),
+                                "/api/v3".to_string(),
+                                "/depth".to_string(),
+                                false,
+                                params,
+        ).await;
+
+        data
+    }
+
+    // 获取k线信息
+    // 参数名	    数据类型	    是否必须	   说明	            取值范围
+    // symbol	    string	    是	     交易对名称	        如:BTCUSDT
+    // interval	    ENUM	    是	    见枚举定义:k线间隔     1m 5m 15m 30m 60m 4h 1d 1W 1M
+    // startTime    long	    否	                        如:1652848049876
+    // endTime	    long	    否	                        如:1652848650458
+    // limit	    integer	    否	                        默认 500; 最大 1000
+    pub async fn klines(&mut self, params: Value) -> Response {
+        let data = self.request("GET".to_string(),
+                                "/api/v3".to_string(),
+                                "/klines".to_string(),
+                                false,
+                                params,
+        ).await;
+
+        data
+    }
+
     // 一些工具函数
     pub fn get_delays(&self) -> Vec<i64> {
         self.delays.clone()
@@ -91,7 +152,7 @@ impl MexcSpotClient {
         self.delays = last_100.clone().into_iter().collect();
     }
 
-    //调用请求
+    // 发送请求
     pub async fn request(&mut self,
                          method: String,
                          prefix_url: String,
@@ -100,7 +161,7 @@ impl MexcSpotClient {
                          mut params: Value) -> Response
     {
         // trace!("login_param:{:?}", self.login_param);
-        //解析账号信息
+        // 解析账号信息
         let mut access_key = "".to_string();
         let mut secret_key = "".to_string();
         let mut passphrase = "".to_string();
@@ -210,17 +271,14 @@ impl MexcSpotClient {
                        headers: HeaderMap) -> Response {
         let url = format!("{}{}", self.base_url.to_string(), request_path);
         let request_type = request_type.clone().to_uppercase();
-        let addrs_url: String = if RestUtils::parse_params_to_str(params.clone()) == "" {
+
+        let params_str = RestUtils::parse_params_to_str(params.clone());
+        let addrs_url: String = if params_str == "" {
             url.clone()
         } else {
-            format!("{}?{}", url.clone(), RestUtils::parse_params_to_str(params.clone()))
+            format!("{}?{}", url.clone(), params_str)
         };
 
-        // trace!("url-----:???{}", url.clone());
-        // trace!("addrs_url-----:???{}", addrs_url.clone());
-        // trace!("params-----:???{}", params.clone());
-        // trace!("body-----:???{}", body.clone());
-
         let request_builder = match request_type.as_str() {
             "GET" => self.client.get(addrs_url.clone()).headers(headers),
             "POST" => self.client.post(url.clone()).body(body).headers(headers),
@@ -237,8 +295,6 @@ impl MexcSpotClient {
         let is_success = response.status().is_success();
         let text = response.text().await.unwrap();
 
-        // trace!("text:???{:?}",text);
-
         if is_success {
             self.on_success_data(&text)
         } else {
@@ -284,6 +340,52 @@ mod tests {
     use crate::exchange::mexc_spot_client::MexcSpotClient;
     use crate::utils::log_setup::setup_logging;
 
+    #[tokio::test]
+    async fn test_mexc_spot_klines() {
+        let guard = setup_logging().unwrap();
+        let mut client = MexcSpotClient::new_with_tag("MexcSpot".to_string(), None);
+
+        let params = serde_json::json!({
+            "symbol": "BTCUSDT",
+            "interval": "1m",
+            "limit": 101,
+        });
+        info!("{}", serde_json::to_string_pretty(&client.klines(params).await.data).unwrap());
+    }
+
+    #[tokio::test]
+    async fn test_mexc_spot_depth() {
+        let guard = setup_logging().unwrap();
+        let mut client = MexcSpotClient::new_with_tag("MexcSpot".to_string(), None);
+
+        let params = serde_json::json!({
+            "symbol": "BTCUSDT",
+            "limit": 101,
+        });
+        info!("{}", serde_json::to_string_pretty(&client.depth(params).await.data).unwrap());
+    }
+
+    #[tokio::test]
+    async fn test_mexc_spot_exchange_info() {
+        let guard = setup_logging().unwrap();
+        let mut client = MexcSpotClient::new_with_tag("MexcSpot".to_string(), None);
+
+        // info!("{}", serde_json::to_string_pretty(&client.exchange_info(serde_json::Value::Null).await.data).unwrap());
+        let params = serde_json::json!({
+            "symbols": "BTCUSDT"
+        });
+        info!("{}", serde_json::to_string_pretty(&client.exchange_info(params).await.data).unwrap());
+        // &client.exchange_info(params).await;
+    }
+
+    #[tokio::test]
+    async fn test_mexc_spot_default_symbols() {
+        let guard = setup_logging().unwrap();
+        let mut client = MexcSpotClient::new_with_tag("MexcSpot".to_string(), None);
+
+        info!("{}", serde_json::to_string_pretty(&client.default_symbols().await.data).unwrap());
+    }
+
     #[tokio::test]
     async fn test_mexc_spot_time() {
         let guard = setup_logging().unwrap();

+ 3 - 3
src/utils/rest_utils.rs

@@ -1,4 +1,4 @@
-use tracing::trace;
+use tracing::{info, trace};
 use crate::exchange::response_base::Response;
 
 #[derive(Clone)]
@@ -33,7 +33,7 @@ impl RestUtils {
                 params_str = format_str;
             }
         }
-        trace!("---json-转字符串拼接:{}",params_str);
+        // info!("---json-转字符串拼接:{}", params_str);
         params_str.to_string()
     }
     //res_data 解析
@@ -48,7 +48,7 @@ impl RestUtils {
                     let code = json_value["code"].as_str().unwrap();
                     let data = serde_json::to_string(&json_value["data"]).unwrap();
                     let msg = json_value["msg"].as_str().unwrap();
-                    
+
                     let success = Response::new("".to_string(),
                                                     code.parse().unwrap(),
                                                     msg.parse().unwrap(),