gepangpang 2 vuotta sitten
vanhempi
commit
3702addfab
3 muutettua tiedostoa jossa 32 lisäystä ja 87 poistoa
  1. 12 70
      standard/src/kucoin_handle.rs
  2. 15 12
      standard/src/kucoin_swap.rs
  3. 5 5
      standard/tests/kucoin_handle_test.rs

+ 12 - 70
standard/src/kucoin_handle.rs

@@ -55,10 +55,10 @@ pub fn handle_position(res_data: ResponseData, amount_size: Decimal) -> Vec<Posi
     let res_data_str = res_data.data;
     let res_data_json: serde_json::Value = serde_json::from_str(&res_data_str).unwrap();
     let result = format_position_item(&res_data_json, amount_size);
-    return result;
+    return vec![result];
 }
 
-pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal) -> Vec<Position> {
+pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal) -> Position {
     let symbol = position["symbol"].as_str().unwrap_or("");
     let real_leverage = Decimal::from_str(&position["realLeverage"].to_string()).unwrap();
     let currency = position["settleCurrency"].as_str().unwrap_or("");
@@ -70,74 +70,16 @@ pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal)
     let current_qty = Decimal::from_str(&position["currentQty"].to_string()).unwrap();
     let amount = current_qty * amount_size;
     let position_mode = if amount > dec!(0) { PositionModeEnum::Long } else { PositionModeEnum::Short };
-    let mut position_array = Vec::new();
-    return match position_mode {
-        PositionModeEnum::Long => {
-            position_array.push(
-                Position {
-                    symbol: format!("{}_{}", coin, currency),
-                    margin_level: real_leverage,
-                    amount: amount.abs(),
-                    frozen_amount: dec!(0),
-                    price: avg_entry_price,
-                    profit: unrealised_pnl,
-                    position_mode: PositionModeEnum::Long,
-                    margin: pos_margin,
-                }
-            );
-            position_array.push(
-                Position {
-                    symbol: format!("{}_{}", coin, currency),
-                    margin_level: real_leverage,
-                    amount: dec!(0),
-                    frozen_amount: dec!(0),
-                    price: dec!(0),
-                    profit: dec!(0),
-                    position_mode: PositionModeEnum::Short,
-                    margin: dec!(0),
-                });
-            position_array
-        }
-        PositionModeEnum::Short => {
-            position_array.push(
-                Position {
-                    symbol: format!("{}_{}", coin, currency),
-                    margin_level: real_leverage,
-                    amount: dec!(0),
-                    frozen_amount: dec!(0),
-                    price: dec!(0),
-                    profit: dec!(0),
-                    position_mode: PositionModeEnum::Long,
-                    margin: dec!(0),
-                });
-            position_array.push(
-                Position {
-                    symbol: format!("{}_{}", coin, currency),
-                    margin_level: real_leverage,
-                    amount: amount.abs(),
-                    frozen_amount: dec!(0),
-                    price: Decimal::from_str(&position["avgEntryPrice"].to_string()).unwrap(),
-                    profit: Decimal::from_str(&position["unrealisedPnl"].to_string()).unwrap(),
-                    position_mode: PositionModeEnum::Short,
-                    margin: Decimal::from_str(&position["posMargin"].to_string()).unwrap(),
-                });
-            position_array
-        }
-        _ => {
-            position_array.push(
-                Position {
-                    symbol: format!("{}_{}", coin, currency),
-                    margin_level: real_leverage,
-                    amount,
-                    frozen_amount: dec!(0),
-                    price: Decimal::from_str(&position["avgEntryPrice"].to_string()).unwrap(),
-                    profit: Decimal::from_str(&position["unrealisedPnl"].to_string()).unwrap(),
-                    position_mode: PositionModeEnum::Both,
-                    margin: Decimal::from_str(&position["posMargin"].to_string()).unwrap(),
-                });
-            position_array
-        }
-    };
+    Position {
+        symbol: format!("{}_{}", coin, currency),
+        margin_level: real_leverage,
+        amount,
+        frozen_amount: dec!(0),
+        price: avg_entry_price,
+        profit: unrealised_pnl,
+        position_mode,
+        margin: pos_margin,
+    }
 }
 
 // 处理order信息

+ 15 - 12
standard/src/kucoin_swap.rs

@@ -76,7 +76,6 @@ impl Platform for KucoinSwap {
         if res_data.code == "200" {
             let res_data_str = &res_data.data;
             let result = res_data_str.clone();
-            println!("{}", res_data.data);
             Ok(result)
         } else {
             Err(Error::new(ErrorKind::Other, res_data.message))
@@ -114,7 +113,7 @@ impl Platform for KucoinSwap {
             let res_data_str = &res_data.data;
             let res_data_json: serde_json::Value = serde_json::from_str(res_data_str).unwrap();
             let result = kucoin_handle::format_position_item(&res_data_json, amount_size);
-            Ok(result)
+            Ok(vec![result])
         } else {
             Err(Error::new(ErrorKind::Other, res_data.message))
         }
@@ -129,7 +128,7 @@ impl Platform for KucoinSwap {
             let res_data_json: Vec<serde_json::Value> = serde_json::from_str(res_data_str).unwrap();
             let mut result = Vec::new();
             for item in res_data_json.iter() {
-                result.extend(kucoin_handle::format_position_item(item, amount_size))
+                result.push(kucoin_handle::format_position_item(item, amount_size))
             }
             Ok(result)
         } else {
@@ -165,6 +164,7 @@ impl Platform for KucoinSwap {
         let res_data = self.request.get_market_details().await;
         if res_data.code == "200" {
             let res_data_str = &res_data.data;
+            println!("{}", res_data_str);
             let res_data_json: Vec<serde_json::Value> = serde_json::from_str(res_data_str).unwrap();
             let market_info = res_data_json.iter().find(|item| item["symbol"].as_str().unwrap() == symbol_format);
             match market_info {
@@ -176,8 +176,11 @@ impl Platform for KucoinSwap {
                     let quote_asset = value["quoteCurrency"].as_str().unwrap_or("").to_string();
                     let tick_size = Decimal::from_str(&value["tickSize"].to_string()).unwrap();
                     let amount_size = Decimal::from_str(&value["multiplier"].to_string()).unwrap();
+                    let min_qty = Decimal::from_str(&value["lotSize"].to_string()).unwrap();
+
                     let price_precision = Decimal::from_u32(tick_size.scale()).unwrap();
                     let amount_precision = Decimal::from_u32(amount_size.scale()).unwrap();
+                    let min_notional = min_qty * amount_size;
 
                     let result = Market {
                         symbol: format!("{}_{}", base_asset, quote_asset),
@@ -187,9 +190,9 @@ impl Platform for KucoinSwap {
                         amount_size,
                         price_precision,
                         amount_precision,
-                        min_qty: Decimal::from_str(&value["lotSize"].to_string()).unwrap(),
+                        min_qty,
                         max_qty: Decimal::from_str(&value["maxOrderQty"].to_string()).unwrap(),
-                        min_notional: Default::default(),
+                        min_notional,
                         max_notional: Decimal::from_str(&value["maxPrice"].to_string()).unwrap(),
                         ct_val: Default::default(),
                     };
@@ -233,7 +236,6 @@ impl Platform for KucoinSwap {
     async fn take_order(&mut self, custom_id: &str, origin_side: &str, price: Decimal, amount: Decimal) -> Result<Order, Error> {
         let symbol_format = format!("{}M", utils::format_symbol(self.symbol.clone(), ""));
         let amount_size = self.market.amount_size;
-        let mut status = "";
         let mut params = json!({
             "clientOid": custom_id,
             "symbol": symbol_format,
@@ -245,19 +247,15 @@ impl Platform for KucoinSwap {
         match origin_side {
             "kd" => {
                 params["side"] = json!("buy");
-                status = "NEW";
             }
             "pd" => {
-                params["side"] = json!("sell");
-                status = "REMOVE";
+                params["side"] = json!("sell")
             }
             "kk" => {
                 params["side"] = json!("sell");
-                status = "NEW";
             }
             "pk" => {
                 params["side"] = json!("buy");
-                status = "REMOVE";
             }
             _ => { error!("下单参数错误"); }
         };
@@ -274,7 +272,7 @@ impl Platform for KucoinSwap {
                 amount,
                 deal_amount: dec!(0),
                 avg_price: dec!(0),
-                status: status.to_string(),
+                status: "NEW".to_string(),
                 order_type: "".to_string(),
             };
             Ok(result)
@@ -365,6 +363,11 @@ impl Platform for KucoinSwap {
                         result_sd.send(result).await.unwrap();
                     }
                     Err(error) => {
+                        let mut err_order = Order::new();
+                        err_order.custom_id = (*cid).clone();
+                        err_order.status = "REMOVE".to_string();
+
+                        result_sd.send(err_order).await.unwrap();
                         err_sd.send(error).await.unwrap();
                     }
                 }

+ 5 - 5
standard/tests/kucoin_handle_test.rs

@@ -8,7 +8,7 @@ use crate::exchange_test::test_new_exchange_wss;
 const SYMBOL: &str = "BLZ_USDT";
 
 // 测试订阅深度订阅
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
 #[instrument(level = "TRACE")]
 async fn test_get_wss_depth() {
     global::log_utils::init_log_with_trace();
@@ -20,7 +20,7 @@ async fn test_get_wss_depth() {
 }
 
 // 测试订阅Ticker信息
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
 #[instrument(level = "TRACE")]
 async fn test_get_wss_ticker() {
     global::log_utils::init_log_with_trace();
@@ -32,7 +32,7 @@ async fn test_get_wss_ticker() {
 }
 
 // 测试订阅Account信息
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
 #[instrument(level = "TRACE")]
 async fn test_get_wss_account() {
     global::log_utils::init_log_with_trace();
@@ -44,7 +44,7 @@ async fn test_get_wss_account() {
 }
 
 // 测试订阅Position信息
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
 #[instrument(level = "TRACE")]
 async fn test_get_wss_position() {
     global::log_utils::init_log_with_trace();
@@ -56,7 +56,7 @@ async fn test_get_wss_position() {
 }
 
 // 测试订阅Orders信息
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
 #[instrument(level = "TRACE")]
 async fn test_get_wss_orders() {
     global::log_utils::init_log_with_trace();