From 80252e235bd6b63aba5d140987dfbe5b2ae2ae12 Mon Sep 17 00:00:00 2001 From: Sik Yoon Date: Tue, 21 May 2024 16:02:06 +0900 Subject: [PATCH] Fix update record --- src/future/order.rs | 138 ++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 89 deletions(-) diff --git a/src/future/order.rs b/src/future/order.rs index 80d581e..83d35b6 100644 --- a/src/future/order.rs +++ b/src/future/order.rs @@ -349,30 +349,6 @@ pub async fn limit_order_close( println!("Closing {} {} (Partially filled)", entry_coin_info.position, entry_coin_info.symbol); } - let base_qty_ordered = rust_decimal::prelude::FromStr::from_str( - T.get("origQty").unwrap().as_str().unwrap(), - ) - .unwrap(); - - let cummulative_quote_qty = rust_decimal::prelude::FromStr::from_str( - T.get("cumQuote").unwrap().as_str().unwrap(), - ) - .unwrap(); - // let base_asset_precision = exchange_info_vec - // .iter() - // .find(|exchange_info| exchange_info.symbol == element.symbol) - // .unwrap() - // .base_asset_precision; - let entry_price = decimal_div(cummulative_quote_qty, base_qty_ordered) - .round_dp_with_strategy(8, RoundingStrategy::ToZero); - - update_values.push((String::from("used_usdt"), cummulative_quote_qty.to_string())); - update_values.push((String::from("entry_price"), entry_price.to_string())); - update_values.push((String::from("base_qty_ordered"), base_qty_ordered.to_string())); - - // reflect available_usdt in [asset_manage_announcement] - // sub_available_usdt(cummulative_quote_qty).await; - sub_future_available_usdt(cummulative_quote_qty).await; let update_condition = vec![(String::from("id"), entry_coin_info.id.to_string())]; update_record2(&update_table_name, &update_values, &update_condition) @@ -723,76 +699,60 @@ pub async fn query_open_closing_order( .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); + 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") + { + // update values in [future_ordered_coin_list] + let table_name = String::from("future_ordered_coin_list"); + let mut value_build = String::new(); + value_build.push_str(T.get("status").unwrap().as_str().unwrap()); - // 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('\''); + // calculate values to be updated - // 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); - // } - // } + + let usdt_size = rust_decimal::prelude::FromStr::from_str( + T.get("cumQuote").unwrap().as_str().unwrap(), + ) + .unwrap(); + + let mut pnl = Decimal::new(0, 8); + if order.position.contains("Long") { + pnl = decimal_sub(usdt_size, order.used_usdt); + } else { + pnl = decimal_sub(order.used_usdt, usdt_size); + } + let expected_get_usdt = decimal_add(order.used_usdt, pnl); + + let update_values = vec![ + (String::from("status"), value_build), // status + (String::from("pnl"), pnl.to_string()), // pnl + (String::from("expected_get_usdt"), expected_get_usdt.to_string()), // base_qty_ordered + ]; + let update_condition = vec![ + (String::from("order_id"), order.order_id.to_string()), + (String::from("symbol"), order.symbol.clone()), + ]; + update_record2(&table_name, &update_values, &update_condition) + .await + .unwrap(); + + + println!("closed {} {}", order.position, order.symbol); + + } + } + Err(e) => { + log::warn!("query sell order failed!: {}", body); + } + } Ok(()) }