diff --git a/src/future/order.rs b/src/future/order.rs index 3d3b91e..80d581e 100644 --- a/src/future/order.rs +++ b/src/future/order.rs @@ -336,7 +336,7 @@ pub async fn limit_order_close( let mut update_values = vec![]; update_values.push((String::from("order_type"), (String::from("CLOSING")))); update_values.push((String::from("order_id"), T.get("orderId").unwrap().as_u64().unwrap().to_string())); - update_values.push((String::from("transact_time"), server_epoch.to_string())); + update_values.push((String::from("registered_server_epoch"), server_epoch.to_string())); // status if T.get("status").unwrap().as_str().unwrap() == "NEW" { @@ -423,6 +423,21 @@ pub async fn monitoring_unfilled_order( let open_closing_orders = select_open_closing_orders().await; if !open_closing_orders.is_empty() { + let server_epoch = get_server_epoch().await; + for element in open_closing_orders { + query_open_closing_order(&element, &client).await; + } + + // let orders_outdated = open_closing_orders + // .iter() + // .filter(|&element| server_epoch - element.registered_server_epoch >= 30_000) + // .collect::>(); // wait up to 30 secs + // let orders_to_be_queried = open_closing_orders + // .iter() + // .filter(|&element| server_epoch - element.registered_server_epoch < 30_000) + // .collect::>(); + + } Ok(()) @@ -670,6 +685,117 @@ pub async fn query_open_positioning_order( Ok(()) } +pub async fn query_open_closing_order( + order: &PositionCoinList, + client: &Client, +) -> Result<(), Box> { + // building URL and API-keys + let mut url = String::new(); + let mut api_key = String::new(); + unsafe { + if RUNNING_MODE == TEST { + url.push_str(FUTURES_URL_TEST); + api_key = API_KEY_TESTNET.to_string(); + } else if RUNNING_MODE == REAL { + url.push_str(FUTURES_URL); + api_key = API_KEY.to_string(); + } + } + + let endpoint_url = "/fapi/v1/order?"; + url.push_str(endpoint_url); + + let mut url_build = String::new(); + + // add parameters into URL + url_build.push_str("&symbol="); + url_build.push_str(&order.symbol); + url_build.push_str("&orderId="); + url_build.push_str(order.order_id.to_string().as_str()); + + hmac_signature(&mut url_build).await; + url.push_str(&url_build); + + let res = client + .get(&url) + .header("X-MBX-APIKEY", api_key) + .send() + .await?; + + let body = res.text_with_charset("utf-8").await.unwrap(); + println!("body: {}", body); + // deserialize JSON and then update record in table + let v = serde_json::from_str::(body.as_str()); + + // match v { + // Ok(T) => { + // if T.get("status") + // .is_some_and(|a| a.as_str().unwrap() == "FILLED") + // || T.get("status") + // .is_some_and(|a| a.as_str().unwrap() == "PARTIALLY_FILLED") + // { + // if exchange_info_map.contains_key(&order.symbol) + // && trade_fee_map.contains_key(&order.symbol) + // { + // let quote_asset_precision = exchange_info_map + // .get(&order.symbol) + // .unwrap() + // .quote_asset_precision; + // let trade_fee = trade_fee_map.get(&order.symbol).unwrap().takercommission; + // let get_usdt = rust_decimal::prelude::FromStr::from_str( + // T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(), + // ) + // .unwrap(); + // let get_usdt_fee_adjusted = + // decimal_mul(get_usdt, decimal_sub(dec!(1), trade_fee)) + // .round_dp_with_strategy( + // quote_asset_precision, + // RoundingStrategy::ToZero, + // ); + // let ordered_base_qty = rust_decimal::prelude::FromStr::from_str( + // T.get("executedQty").unwrap().as_str().unwrap(), + // ) + // .unwrap(); + // let sell_price = decimal_div(get_usdt, ordered_base_qty) + // .round_dp_with_strategy(quote_asset_precision, RoundingStrategy::ToZero); + // let pure_profit_percent = decimal_mul( + // decimal_sub(decimal_div(get_usdt_fee_adjusted, order.used_usdt), dec!(1)), + // dec!(100), + // ) + // .round_dp(2); + + // let table_name = String::from("sell_ordered_coin_list"); + // let mut value_build = String::from("\'"); + // value_build.push_str(T.get("status").unwrap().as_str().unwrap()); + // value_build.push('\''); + + // let update_values = vec![ + // (String::from("status"), value_build), + // (String::from("get_usdt"), get_usdt.to_string()), + // ( + // String::from("get_usdt_fee_adjusted"), + // get_usdt_fee_adjusted.to_string(), + // ), + // (String::from("sell_price"), sell_price.to_string()), + // ( + // String::from("pure_profit_percent"), + // pure_profit_percent.to_string(), + // ), + // ]; + // let update_condition = vec![(String::from("id"), order.id.to_string())]; + // update_record3(&table_name, &update_values, &update_condition) + // .await + // .unwrap(); + // } + // } + // } + // Err(e) => { + // log::warn!("query sell order failed!: {}", body); + // } + // } + Ok(()) +} + // pub async fn set_initial_leverage(symbol: &String, leverage_ratio: i32, client: &Client) -> Result<(), Box> { // building URL and API-keys