Prechádzať zdrojové kódy

修改所有返回为Number类型数据的转换

gepangpang 2 rokov pred
rodič
commit
52d00a02da

+ 5 - 4
standard/src/binance_swap.rs

@@ -4,6 +4,7 @@ use std::result::Result;
 use std::str::FromStr;
 use async_trait::async_trait;
 use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
 use rust_decimal_macros::dec;
 use tokio::sync::mpsc::Sender;
 use tracing::{error, warn};
@@ -217,8 +218,8 @@ impl Platform for BinanceSwap {
                         quote_asset,
                         tick_size: Decimal::from_str(&price_filter["tickSize"].as_str().unwrap()).unwrap(),
                         amount_size: Decimal::from_str(lot_size_filter["stepSize"].as_str().unwrap()).unwrap(),
-                        price_precision: Decimal::from_str(&value["pricePrecision"].as_f64().unwrap().to_string()).unwrap(),
-                        amount_precision: Decimal::from_str(&value["quantityPrecision"].as_f64().unwrap().to_string()).unwrap(),
+                        price_precision: Decimal::from_f64(value["pricePrecision"].as_f64().unwrap()).unwrap(),
+                        amount_precision: Decimal::from_f64(value["quantityPrecision"].as_f64().unwrap()).unwrap(),
                         min_qty: Decimal::from_str(lot_size_filter["minQty"].as_str().unwrap()).unwrap(),
                         max_qty: Decimal::from_str(lot_size_filter["maxQty"].as_str().unwrap()).unwrap(),
                         min_notional: Decimal::from_str(price_filter["minPrice"].as_str().unwrap()).unwrap(),
@@ -260,8 +261,8 @@ impl Platform for BinanceSwap {
                         quote_asset,
                         tick_size: Decimal::from_str(&price_filter["tickSize"].as_str().unwrap()).unwrap(),
                         amount_size: Decimal::from_str(lot_size_filter["stepSize"].as_str().unwrap()).unwrap(),
-                        price_precision: Decimal::from_str(&value["pricePrecision"].as_f64().unwrap().to_string()).unwrap(),
-                        amount_precision: Decimal::from_str(&value["quantityPrecision"].as_f64().unwrap().to_string()).unwrap(),
+                        price_precision: Decimal::from_f64(value["pricePrecision"].as_f64().unwrap()).unwrap(),
+                        amount_precision: Decimal::from_f64(value["quantityPrecision"].as_f64().unwrap()).unwrap(),
                         min_qty: Decimal::from_str(lot_size_filter["minQty"].as_str().unwrap()).unwrap(),
                         max_qty: Decimal::from_str(lot_size_filter["maxQty"].as_str().unwrap()).unwrap(),
                         min_notional: Decimal::from_str(price_filter["minPrice"].as_str().unwrap()).unwrap(),

+ 6 - 5
standard/src/gate_handle.rs

@@ -1,5 +1,6 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
 use tracing::{debug, error};
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
@@ -99,8 +100,8 @@ pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
     debug!(?order);
     let status = order["status"].as_str().unwrap_or("");
     let text = order["text"].as_str().unwrap_or("");
-    let size = Decimal::from_str(&order["size"].as_f64().unwrap().to_string()).unwrap();
-    let left = Decimal::from_str(&order["left"].as_f64().unwrap().to_string()).unwrap();
+    let size = Decimal::from_f64(order["size"].as_f64().unwrap()).unwrap();
+    let left = Decimal::from_f64(order["left"].as_f64().unwrap()).unwrap();
 
     let amount = size * ct_val;
     let deal_amount = (size - left) * ct_val;
@@ -111,10 +112,10 @@ pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
     let rst_order = Order {
         id: order["id"].to_string(),
         custom_id: text.replace("t-my-custom-id_", ""),
-        price: Decimal::from_str(&order["price"].as_f64().unwrap().to_string()).unwrap(),
+        price: Decimal::from_f64(order["price"].as_f64().unwrap()).unwrap(),
         amount,
         deal_amount,
-        avg_price: Decimal::from_str(&order["fill_price"].as_f64().unwrap().to_string()).unwrap(),
+        avg_price: Decimal::from_f64(order["fill_price"].as_f64().unwrap()).unwrap(),
         status: custom_status,
         order_type: "limit".to_string(),
         trace_stack: TraceStack::default().on_special("120 gate_handle".to_string()),
@@ -135,7 +136,7 @@ pub fn format_depth_items(value: serde_json::Value) -> Vec<MarketOrder> {
     for value in value.as_array().unwrap() {
         depth_items.push(MarketOrder {
             price: Decimal::from_str(value["p"].as_str().unwrap()).unwrap(),
-            amount: Decimal::from_str(&value["s"].as_f64().unwrap().to_string()).unwrap(),
+            amount: Decimal::from_f64(value["s"].as_f64().unwrap()).unwrap(),
         })
     }
     return depth_items;

+ 9 - 8
standard/src/kucoin_handle.rs

@@ -1,5 +1,6 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
 use rust_decimal_macros::dec;
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
@@ -39,9 +40,9 @@ pub fn handle_special_ticker(res_data: ResponseData) -> SpecialDepth {
 
 pub fn format_special_ticker(data: serde_json::Value, label: String) -> SpecialDepth {
     let bp = Decimal::from_str(&data["bestBidPrice"].as_str().unwrap()).unwrap();
-    let bq = Decimal::from_str(&data["bestBidSize"].as_f64().unwrap().to_string()).unwrap();
+    let bq = Decimal::from_f64(data["bestBidSize"].as_f64().unwrap()).unwrap();
     let ap = Decimal::from_str(&data["bestAskPrice"].as_str().unwrap()).unwrap();
-    let aq = Decimal::from_str(&data["bestAskSize"].as_f64().unwrap().to_string()).unwrap();
+    let aq = Decimal::from_f64(data["bestAskSize"].as_f64().unwrap()).unwrap();
     let mp = (bp + ap) * dec!(0.5);
 
     let ticker_info = SpecialTicker { sell: ap, buy: bp, mid_price: mp };
@@ -63,14 +64,14 @@ pub fn handle_position(res_data: ResponseData, ct_val: Decimal) -> Vec<Position>
 
 pub fn format_position_item(position: &serde_json::Value, ct_val: Decimal) -> Position {
     let symbol = position["symbol"].as_str().unwrap_or("");
-    let real_leverage = Decimal::from_str(&position["realLeverage"].as_f64().unwrap().to_string()).unwrap();
+    let real_leverage = Decimal::from_f64(position["realLeverage"].as_f64().unwrap()).unwrap();
     let currency = position["settleCurrency"].as_str().unwrap_or("");
     let coin = &symbol[..symbol.find(currency).unwrap_or(0)];
-    let avg_entry_price = Decimal::from_str(&position["avgEntryPrice"].as_f64().unwrap().to_string()).unwrap();
-    let unrealised_pnl = Decimal::from_str(&position["unrealisedPnl"].as_f64().unwrap().to_string()).unwrap();
-    let pos_margin = Decimal::from_str(&position["posMargin"].as_f64().unwrap().to_string()).unwrap();
+    let avg_entry_price = Decimal::from_f64(position["avgEntryPrice"].as_f64().unwrap()).unwrap();
+    let unrealised_pnl = Decimal::from_f64(position["unrealisedPnl"].as_f64().unwrap()).unwrap();
+    let pos_margin = Decimal::from_f64(position["posMargin"].as_f64().unwrap()).unwrap();
 
-    let current_qty = Decimal::from_str(&position["currentQty"].as_f64().unwrap().to_string()).unwrap();
+    let current_qty = Decimal::from_f64(position["currentQty"].as_f64().unwrap()).unwrap();
     let amount = current_qty * ct_val;
     let position_mode = match amount {
         amount if amount > Decimal::ZERO => PositionModeEnum::Long,
@@ -146,7 +147,7 @@ pub fn format_depth_items(value: serde_json::Value) -> Vec<MarketOrder> {
     for value in value.as_array().unwrap() {
         depth_items.push(MarketOrder {
             price: Decimal::from_str(value[0].as_str().unwrap()).unwrap(),
-            amount: Decimal::from_str(&value[1].as_f64().unwrap().to_string()).unwrap(),
+            amount: Decimal::from_f64(value[1].as_f64().unwrap()).unwrap(),
         })
     }
     return depth_items;

+ 26 - 26
standard/src/kucoin_swap.rs

@@ -93,8 +93,8 @@ 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 balance = Decimal::from_str(&res_data_json["marginBalance"].as_f64().unwrap().to_string()).unwrap();
-            let available_balance = Decimal::from_str(&res_data_json["availableBalance"].as_f64().unwrap().to_string()).unwrap();
+            let balance = Decimal::from_f64(res_data_json["marginBalance"].as_f64().unwrap()).unwrap();
+            let available_balance = Decimal::from_f64(res_data_json["availableBalance"].as_f64().unwrap()).unwrap();
             let frozen_balance = balance - available_balance;
             let result = Account {
                 coin: symbol_array[1].to_string(),
@@ -156,12 +156,12 @@ impl Platform for KucoinSwap {
             let time = (Decimal::from_str(&*ticker_info["ts"].to_string()).unwrap() / dec!(1000000)).floor().to_i64().unwrap();
             let result = Ticker {
                 time,
-                high: Decimal::from_str(&ticker_info["bestAskPrice"].as_f64().unwrap().to_string()).unwrap(),
-                low: Decimal::from_str(&ticker_info["bestBidPrice"].as_f64().unwrap().to_string()).unwrap(),
-                sell: Decimal::from_str(&ticker_info["bestAskPrice"].as_f64().unwrap().to_string()).unwrap(),
-                buy: Decimal::from_str(&ticker_info["bestBidPrice"].as_f64().unwrap().to_string()).unwrap(),
-                last: Decimal::from_str(&ticker_info["price"].as_f64().unwrap().to_string()).unwrap(),
-                volume: Decimal::from_str(&ticker_info["size"].as_f64().unwrap().to_string()).unwrap(),
+                high: Decimal::from_f64(ticker_info["bestAskPrice"].as_f64().unwrap()).unwrap(),
+                low: Decimal::from_f64(ticker_info["bestBidPrice"].as_f64().unwrap()).unwrap(),
+                sell: Decimal::from_f64(ticker_info["bestAskPrice"].as_f64().unwrap()).unwrap(),
+                buy: Decimal::from_f64(ticker_info["bestBidPrice"].as_f64().unwrap()).unwrap(),
+                last: Decimal::from_f64(ticker_info["price"].as_f64().unwrap()).unwrap(),
+                volume: Decimal::from_f64(ticker_info["size"].as_f64().unwrap()).unwrap(),
             };
             Ok(result)
         } else {
@@ -179,12 +179,12 @@ impl Platform for KucoinSwap {
             let time = (Decimal::from_str(&*ticker_info["ts"].to_string()).unwrap() / dec!(1000000)).floor().to_i64().unwrap();
             let result = Ticker {
                 time,
-                high: Decimal::from_str(&ticker_info["bestAskPrice"].as_f64().unwrap().to_string()).unwrap(),
-                low: Decimal::from_str(&ticker_info["bestBidPrice"].as_f64().unwrap().to_string()).unwrap(),
-                sell: Decimal::from_str(&ticker_info["bestAskPrice"].as_f64().unwrap().to_string()).unwrap(),
-                buy: Decimal::from_str(&ticker_info["bestBidPrice"].as_f64().unwrap().to_string()).unwrap(),
-                last: Decimal::from_str(&ticker_info["price"].as_f64().unwrap().to_string()).unwrap(),
-                volume: Decimal::from_str(&ticker_info["size"].as_f64().unwrap().to_string()).unwrap(),
+                high: Decimal::from_f64(ticker_info["bestAskPrice"].as_f64().unwrap()).unwrap(),
+                low: Decimal::from_f64(ticker_info["bestBidPrice"].as_f64().unwrap()).unwrap(),
+                sell: Decimal::from_f64(ticker_info["bestAskPrice"].as_f64().unwrap()).unwrap(),
+                buy: Decimal::from_f64(ticker_info["bestBidPrice"].as_f64().unwrap()).unwrap(),
+                last: Decimal::from_f64(ticker_info["price"].as_f64().unwrap()).unwrap(),
+                volume: Decimal::from_f64(ticker_info["size"].as_f64().unwrap()).unwrap(),
             };
             Ok(result)
         } else {
@@ -207,9 +207,9 @@ impl Platform for KucoinSwap {
                 Some(value) => {
                     let base_asset = value["baseCurrency"].as_str().unwrap_or("").to_string();
                     let quote_asset = value["quoteCurrency"].as_str().unwrap_or("").to_string();
-                    let tick_size = Decimal::from_str(&value["tickSize"].as_f64().unwrap().to_string()).unwrap();
-                    let min_qty = Decimal::from_str(&value["lotSize"].as_f64().unwrap().to_string()).unwrap();
-                    let ct_val = Decimal::from_str(&value["multiplier"].as_f64().unwrap().to_string()).unwrap();
+                    let tick_size = Decimal::from_f64(value["tickSize"].as_f64().unwrap()).unwrap();
+                    let min_qty = Decimal::from_f64(value["lotSize"].as_f64().unwrap()).unwrap();
+                    let ct_val = Decimal::from_f64(value["multiplier"].as_f64().unwrap()).unwrap();
 
                     let amount_size = min_qty * ct_val;
                     let price_precision = Decimal::from_u32(tick_size.scale()).unwrap();
@@ -225,9 +225,9 @@ impl Platform for KucoinSwap {
                         price_precision,
                         amount_precision,
                         min_qty,
-                        max_qty: Decimal::from_str(&value["maxOrderQty"].as_f64().unwrap().to_string()).unwrap(),
+                        max_qty: Decimal::from_f64(value["maxOrderQty"].as_f64().unwrap()).unwrap(),
                         min_notional,
-                        max_notional: Decimal::from_str(&value["maxPrice"].as_f64().unwrap().to_string()).unwrap(),
+                        max_notional: Decimal::from_f64(value["maxPrice"].as_f64().unwrap()).unwrap(),
                         ct_val,
                     };
                     Ok(result)
@@ -253,9 +253,9 @@ impl Platform for KucoinSwap {
                 Some(value) => {
                     let base_asset = value["baseCurrency"].as_str().unwrap_or("").to_string();
                     let quote_asset = value["quoteCurrency"].as_str().unwrap_or("").to_string();
-                    let tick_size = Decimal::from_str(&value["tickSize"].as_f64().unwrap().to_string()).unwrap();
-                    let min_qty = Decimal::from_str(&value["lotSize"].as_f64().unwrap().to_string()).unwrap();
-                    let ct_val = Decimal::from_str(&value["multiplier"].as_f64().unwrap().to_string()).unwrap();
+                    let tick_size = Decimal::from_f64(value["tickSize"].as_f64().unwrap()).unwrap();
+                    let min_qty = Decimal::from_f64(value["lotSize"].as_f64().unwrap()).unwrap();
+                    let ct_val = Decimal::from_f64(value["multiplier"].as_f64().unwrap()).unwrap();
 
                     let amount_size = min_qty * ct_val;
                     let price_precision = Decimal::from_u32(tick_size.scale()).unwrap();
@@ -271,9 +271,9 @@ impl Platform for KucoinSwap {
                         price_precision,
                         amount_precision,
                         min_qty,
-                        max_qty: Decimal::from_str(&value["maxOrderQty"].as_f64().unwrap().to_string()).unwrap(),
+                        max_qty: Decimal::from_f64(value["maxOrderQty"].as_f64().unwrap()).unwrap(),
                         min_notional,
-                        max_notional: Decimal::from_str(&value["maxPrice"].as_f64().unwrap().to_string()).unwrap(),
+                        max_notional: Decimal::from_f64(value["maxPrice"].as_f64().unwrap()).unwrap(),
                         ct_val,
                     };
                     Ok(result)
@@ -626,9 +626,9 @@ impl Platform for KucoinSwap {
 
 pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
     let price = Decimal::from_str(order["price"].as_str().unwrap()).unwrap();
-    let size = Decimal::from_str(&order["size"].as_f64().unwrap().to_string()).unwrap();
+    let size = Decimal::from_f64(order["size"].as_f64().unwrap()).unwrap();
     let status = order["status"].as_str().unwrap_or("");
-    let filled_size = Decimal::from_str(&order["filledSize"].as_f64().unwrap().to_string()).unwrap();
+    let filled_size = Decimal::from_f64(order["filledSize"].as_f64().unwrap()).unwrap();
     let filled_value = Decimal::from_str(order["filledValue"].as_str().unwrap()).unwrap();
 
     let amount = size * ct_val;

+ 5 - 4
standard/src/okx_handle.rs

@@ -1,5 +1,6 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
+use rust_decimal::prelude::FromPrimitive;
 use rust_decimal_macros::dec;
 use tracing::trace;
 use exchanges::response_base::ResponseData;
@@ -133,11 +134,11 @@ pub fn format_position_item(position: serde_json::Value, ct_val: Decimal) -> Pos
     let real_leverage = dec!(-1);
     let currency = position["ccy"].as_str().unwrap_or("");
     let coin: Vec<&str> = symbol.split("-").collect();
-    let avg_entry_price = Decimal::from_str(&position["avgPx"].as_f64().unwrap().to_string()).unwrap();
-    let unrealised_pnl = Decimal::from_str(&position["unrealisedPnl"].as_f64().unwrap().to_string()).unwrap();
-    let pos_margin = Decimal::from_str(&position["posSide"].as_f64().unwrap().to_string()).unwrap();
+    let avg_entry_price = Decimal::from_f64(position["avgPx"].as_f64().unwrap()).unwrap();
+    let unrealised_pnl = Decimal::from_f64(position["unrealisedPnl"].as_f64().unwrap()).unwrap();
+    let pos_margin = Decimal::from_f64(position["posSide"].as_f64().unwrap()).unwrap();
 
-    let current_qty = Decimal::from_str(&position["pos"].as_f64().unwrap().to_string()).unwrap();
+    let current_qty = Decimal::from_f64(position["pos"].as_f64().unwrap()).unwrap();
     let amount = current_qty * ct_val;
     let position_mode = match amount {
         amount if amount > Decimal::ZERO => PositionModeEnum::Long,