Prechádzať zdrojové kódy

binance现货格式化数据测试

gepangpang 2 rokov pred
rodič
commit
45dd65c43c

+ 7 - 0
standard/src/handle_info.rs

@@ -32,6 +32,9 @@ impl HandleSwapInfo {
     // 处理特殊Ticket信息
     pub fn handle_special_ticker(exchange: ExchangeEnum, res_data: ResponseData) -> SpecialDepth {
         match exchange {
+            ExchangeEnum::BinanceSpot => {
+                binance_handle::handle_special_ticker(res_data)
+            }
             ExchangeEnum::BinanceSwap => {
                 binance_handle::handle_special_ticker(res_data)
             }
@@ -87,6 +90,10 @@ impl HandleSwapInfo {
         let mut depth_asks: Vec<MarketOrder>;
         let mut depth_bids: Vec<MarketOrder>;
         match exchange {
+            ExchangeEnum::BinanceSpot => {
+                depth_asks = binance_handle::format_depth_items(res_data_json["asks"].clone());
+                depth_bids = binance_handle::format_depth_items(res_data_json["bids"].clone());
+            }
             ExchangeEnum::BinanceSwap => {
                 depth_asks = binance_handle::format_depth_items(res_data_json["a"].clone());
                 depth_bids = binance_handle::format_depth_items(res_data_json["b"].clone());

+ 31 - 0
standard/tests/binance_spot_handle_test.rs

@@ -0,0 +1,31 @@
+mod exchange_test;
+use tracing::instrument;
+use exchanges::binance_spot_ws::BinanceSpotSubscribeType;
+use standard::exchange::ExchangeEnum;
+use crate::exchange_test::test_new_exchange_wss;
+
+const SYMBOL: &str = "BLZ_USDT";
+
+// 测试订阅深度信息
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+#[instrument(level = "TRACE")]
+async fn test_get_wss_depth() {
+    global::log_utils::init_log_with_trace();
+
+    let binance_subscribe_type = vec![
+        BinanceSpotSubscribeType::PuDepth20levels100ms,
+    ];
+    test_new_exchange_wss(ExchangeEnum::BinanceSpot, SYMBOL, binance_subscribe_type, "depth").await;
+}
+
+// 测试订阅Ticker信息
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+#[instrument(level = "TRACE")]
+async fn test_get_wss_ticker() {
+    global::log_utils::init_log_with_trace();
+
+    let binance_subscribe_type = vec![
+        BinanceSpotSubscribeType::PuBookTicker,
+    ];
+    test_new_exchange_wss(ExchangeEnum::BinanceSpot, SYMBOL, binance_subscribe_type, "ticker").await;
+}

+ 91 - 42
standard/tests/exchange_test.rs

@@ -4,16 +4,18 @@ use std::io::Error;
 use std::sync::Arc;
 use std::sync::atomic::AtomicBool;
 use std::time::Duration;
-use rust_decimal_macros::dec;
 use tokio::sync::mpsc::{channel, Receiver, Sender};
 use tokio::try_join;
 use tracing::trace;
-use exchanges::kucoin_swap_ws::{KucoinSubscribeType, KucoinSwapWs, KucoinWsType};
+use exchanges::binance_spot_ws::{BinanceSpotSubscribeType, BinanceSpotWs, BinanceSpotWsType};
+// use exchanges::kucoin_swap_ws::{KucoinSubscribeType, KucoinSwapWs, KucoinWsType};
 use exchanges::response_base::ResponseData;
 use standard::exchange::{Exchange, ExchangeEnum};
-use standard::{kucoin_handle, Order, Platform, utils};
+// use standard::{binance_handle, kucoin_handle, Order, Platform, utils};
+use standard::{binance_handle, Order, Platform, utils};
 
 // 创建实体
+#[allow(dead_code)]
 pub async fn test_new_exchange(exchange: ExchangeEnum, symbol: &str) -> Box<dyn Platform> {
     utils::proxy_handle();
     let (order_sender, _order_receiver): (Sender<Order>, Receiver<Order>) = channel(1024);
@@ -65,36 +67,24 @@ pub async fn test_new_exchange(exchange: ExchangeEnum, symbol: &str) -> Box<dyn
 }
 
 #[allow(dead_code)]
-pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subscriber_type: T, mold: &str) where Vec<KucoinSubscribeType>: From<T> {
+pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subscriber_type: T, mold: &str) where Vec<BinanceSpotSubscribeType>: From<T> {
     utils::proxy_handle();
 
     match exchange {
-        ExchangeEnum::BinanceSwap => {
-            panic!("该交易所不支持!")
-        }
-        ExchangeEnum::GateSwap => {
-            panic!("该交易所不支持!")
-        }
-        ExchangeEnum::KucoinSwap => {
-            let symbol_format = format!("{}M", utils::format_symbol(symbol.to_string(), ""));
-            let symbol_back = utils::format_symbol(symbol.to_string(), "_");
-            let name = format!("kucoin_usdt_swap@{}", symbol.to_string().to_lowercase());
+        ExchangeEnum::BinanceSpot => {
+            let symbol_format = utils::format_symbol(symbol.to_string(), "").to_uppercase();
+            trace!(symbol_format);
+            let name = format!("binance_usdt_swap@{}", symbol.to_string().to_lowercase());
             let bool_v1 = Arc::new(AtomicBool::new(true));
 
             let (res_sender, mut res_receiver): (Sender<ResponseData>, Receiver<ResponseData>) = channel(1024);
             let mut params: BTreeMap<String, String> = BTreeMap::new();
-            let access_key = env::var("kucoin_access_key").unwrap_or("".to_string());
-            let secret_key = env::var("kucoin_secret_key").unwrap_or("".to_string());
-            let pass_key = env::var("kucoin_pass_key").unwrap_or("".to_string());
+            let access_key = env::var("binance_access_key").unwrap_or("".to_string());
+            let secret_key = env::var("binance_secret_key").unwrap_or("".to_string());
             params.insert("access_key".to_string(), access_key);
             params.insert("secret_key".to_string(), secret_key);
-            params.insert("pass_key".to_string(), pass_key);
             let mut exchange_wss;
-            if ["depth", "ticker"].contains(&mold) {
-                exchange_wss = KucoinSwapWs::new_label(name, false, params, KucoinWsType::Public, res_sender).await
-            } else {
-                exchange_wss = KucoinSwapWs::new_label(name, false, params, KucoinWsType::Private, res_sender).await
-            }
+            exchange_wss = BinanceSpotWs::new_label(name, false, params, BinanceSpotWsType::PublicAndPrivate, res_sender);
             exchange_wss.set_subscribe(subscriber_type.into());
 
             let t1 = tokio::spawn(async move {
@@ -108,27 +98,16 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
                     if let Ok(received) = res_receiver.try_recv() {
                         match mold_clone.as_str() {
                             "depth" => {
-                                let result = kucoin_handle::handle_special_depth(received);
-                                trace!(?result)
+                                if received.data != "" {
+                                    let result = binance_handle::handle_special_depth(received);
+                                    trace!(?result)
+                                }
                             }
                             "ticker" => {
-                                let result = kucoin_handle::handle_special_ticker(received);
-                                trace!(?result)
-                            }
-                            "account" => {
-                                trace!(?received);
-                                let result = kucoin_handle::handle_account_info(received, symbol_back.clone());
-                                trace!(?result)
-                            }
-                            "position" => {
-                                trace!(?received);
-                                let result = kucoin_handle::handle_position(received, dec!(1));
-                                trace!(?result)
-                            }
-                            "orders" => {
-                                trace!(?received);
-                                let result = kucoin_handle::handle_order(received, dec!(0.001));
-                                trace!(?result)
+                                if received.data != "" {
+                                    let result = binance_handle::handle_special_ticker(received);
+                                    trace!(?result)
+                                }
                             }
                             _ => {
                                 panic!("没有该命令!mode={}", mold_clone)
@@ -139,6 +118,76 @@ pub async fn test_new_exchange_wss<T>(exchange: ExchangeEnum, symbol: &str, subs
             });
             try_join!(t1, t2).unwrap();
         }
+        ExchangeEnum::BinanceSwap => {
+            panic!("该交易所不支持!")
+        }
+        ExchangeEnum::GateSwap => {
+            panic!("该交易所不支持!")
+        }
+        ExchangeEnum::KucoinSwap => {
+            // let symbol_format = format!("{}M", utils::format_symbol(symbol.to_string(), ""));
+            // let symbol_back = utils::format_symbol(symbol.to_string(), "_");
+            // let name = format!("kucoin_usdt_swap@{}", symbol.to_string().to_lowercase());
+            // let bool_v1 = Arc::new(AtomicBool::new(true));
+            //
+            // let (res_sender, mut res_receiver): (Sender<ResponseData>, Receiver<ResponseData>) = channel(1024);
+            // let mut params: BTreeMap<String, String> = BTreeMap::new();
+            // let access_key = env::var("kucoin_access_key").unwrap_or("".to_string());
+            // let secret_key = env::var("kucoin_secret_key").unwrap_or("".to_string());
+            // let pass_key = env::var("kucoin_pass_key").unwrap_or("".to_string());
+            // params.insert("access_key".to_string(), access_key);
+            // params.insert("secret_key".to_string(), secret_key);
+            // params.insert("pass_key".to_string(), pass_key);
+            // let mut exchange_wss;
+            // if ["depth", "ticker"].contains(&mold) {
+            //     exchange_wss = KucoinSwapWs::new_label(name, false, params, KucoinWsType::Public, res_sender).await
+            // } else {
+            //     exchange_wss = KucoinSwapWs::new_label(name, false, params, KucoinWsType::Private, res_sender).await
+            // }
+            // exchange_wss.set_subscribe(subscriber_type.into());
+            //
+            // let t1 = tokio::spawn(async move {
+            //     exchange_wss.custom_subscribe(bool_v1, vec![symbol_format]).await;
+            // });
+            // let mold_arc = Arc::new(mold.to_string());
+            // let t2 = tokio::spawn(async move {
+            //     let mold_clone = Arc::clone(&mold_arc);
+            //     loop {
+            //         tokio::time::sleep(Duration::from_millis(1)).await;
+            //         if let Ok(received) = res_receiver.try_recv() {
+            //             match mold_clone.as_str() {
+            //                 "depth" => {
+            //                     let result = kucoin_handle::handle_special_depth(received);
+            //                     trace!(?result)
+            //                 }
+            //                 "ticker" => {
+            //                     let result = kucoin_handle::handle_special_ticker(received);
+            //                     trace!(?result)
+            //                 }
+            //                 "account" => {
+            //                     trace!(?received);
+            //                     let result = kucoin_handle::handle_account_info(received, symbol_back.clone());
+            //                     trace!(?result)
+            //                 }
+            //                 "position" => {
+            //                     trace!(?received);
+            //                     let result = kucoin_handle::handle_position(received, dec!(1));
+            //                     trace!(?result)
+            //                 }
+            //                 "orders" => {
+            //                     trace!(?received);
+            //                     let result = kucoin_handle::handle_order(received, dec!(0.001));
+            //                     trace!(?result)
+            //                 }
+            //                 _ => {
+            //                     panic!("没有该命令!mode={}", mold_clone)
+            //                 }
+            //             }
+            //         }
+            //     }
+            // });
+            // try_join!(t1, t2).unwrap();
+        }
         _ => {
             panic!("该交易所不支持!")
         }