diff --git a/src/api.rs b/src/api.rs index 4692e72..ca08294 100644 --- a/src/api.rs +++ b/src/api.rs @@ -17,39 +17,32 @@ lazy_static! { pub async fn orders_history() -> Vec { let now = chrono::offset::Local::now(); - let ago = now.checked_sub_signed(chrono::Duration::weeks(8)).unwrap(); - let assets = - [ - "LINKUSDT", - "UNIUSDT", - "1INCHUSDT", - "OPUSDT", - "ARBUSDT", - "SYNUSDT", - ]; - let mut os: Vec = - join_all(assets.iter().map(async move |a| { - match B - .get_all_orders(binance::account::OrdersQuery { - symbol: a.to_string(), - order_id: None, - start_time: Some(ago.timestamp_millis() as u64), - end_time: None, - limit: None, - recv_window: None, - }) - .await - { - Ok(r) => r, - Err(_e) => { - vec![] - } - } - })) - .await - .into_iter() - .flatten() - .collect(); + let ago = now + .checked_sub_signed(chrono::Duration::try_weeks(8).unwrap()) + .unwrap(); + let assets = [ + "LINKUSDT", + "UNIUSDT", + "1INCHUSDT", + "OPUSDT", + "ARBUSDT", + "SYNUSDT", + ]; + let mut os: Vec = join_all(assets.iter().map(|a: &&str| { + B.get_all_orders(binance::account::OrdersQuery { + symbol: a.to_string(), + order_id: None, + start_time: Some(ago.timestamp_millis() as u64), + end_time: None, + limit: None, + recv_window: None, + }) + })) + .await + .into_iter() + .flatten() + .flatten() + .collect(); os.sort_by(|o, n| n.time.cmp(&o.time)); os } @@ -79,22 +72,9 @@ pub async fn trade_spot( pub async fn balances() -> Vec { let assets = ["LINK", "UNI", "ARB", "OP", "SYN", "USDT", "OP"]; - join_all( - assets - .iter() - .map(async move |a| match B.get_balance(a.to_string()).await { - Ok(r) => r, - Err(e) => { - println!("Binance Error: {e}"); - Balance { - asset: a.to_string(), - free: 0.0, - locked: 0.0, - } - } - }), - ) - .await - .into_iter() - .collect() + join_all(assets.iter().map(|a| B.get_balance(a.to_string()))) + .await + .into_iter() + .flatten() + .collect() } diff --git a/src/main.rs b/src/main.rs index caf6da3..53249ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -#![feature(async_closure)] mod api; mod theme; mod views; mod ws; +use binance::rest_model::OrderStatus; use binance::ws_model::TradesEvent; use iced::font; use iced::widget::button; @@ -157,7 +157,7 @@ impl Application for App { v![ 0.5, h![0.5, pane![Market], pane![Trades]], - v![0.75, pane![Book], pane![Balances]] + v![0.6, pane![Book], pane![Balances]] ] ], pane![Orders] @@ -374,29 +374,43 @@ impl Application for App { } } binance::ws_model::WebsocketEvent::OrderUpdate(o) => { - self.data.orders.insert( - 0, - Order { - symbol: o.symbol, - order_id: o.order_id, - order_list_id: o.order_list_id as i32, - client_order_id: o.client_order_id.unwrap(), - price: o.price, - orig_qty: o.qty, - executed_qty: o.qty_last_executed, - cummulative_quote_qty: o.qty, - status: o.current_order_status, - time_in_force: o.time_in_force, - order_type: o.order_type, - side: o.side, - stop_price: o.stop_price, - iceberg_qty: o.iceberg_qty, - time: o.event_time, - update_time: o.trade_order_time, - is_working: false, - orig_quote_order_qty: o.qty, - }, - ); + let existing_order = self.data.orders.iter_mut().find(|order| { + // order.client_order_id == o.order_id&& + order.symbol == o.symbol + && order.side == o.side + && order.status == OrderStatus::PartiallyFilled + }); + + if let Some(order) = existing_order { + // Update the existing order with the new values + order.executed_qty += o.qty_last_executed; + order.cummulative_quote_qty += o.qty; + order.update_time = o.trade_order_time; + } else { + self.data.orders.insert( + 0, + Order { + symbol: o.symbol, + order_id: o.order_id, + order_list_id: o.order_list_id as i32, + client_order_id: o.client_order_id.unwrap(), + price: o.price, + orig_qty: o.qty, + executed_qty: o.qty_last_executed, + cummulative_quote_qty: o.qty, + status: o.current_order_status, + time_in_force: o.time_in_force, + order_type: o.order_type, + side: o.side, + stop_price: o.stop_price, + iceberg_qty: o.iceberg_qty, + time: o.event_time, + update_time: o.trade_order_time, + is_working: false, + orig_quote_order_qty: o.qty, + }, + ); + } } binance::ws_model::WebsocketEvent::BalanceUpdate(_p) => { // not needed imo? diff --git a/src/views/panes/balances.rs b/src/views/panes/balances.rs index 3a38662..63ffa91 100644 --- a/src/views/panes/balances.rs +++ b/src/views/panes/balances.rs @@ -52,6 +52,6 @@ pub fn balances_view<'a>(bs: &[Balance], ps: &'a HashMap) -> Elemen .width(Length::Fill) }) .map(Element::from), - )) + ).padding(8)) .into() } diff --git a/src/views/panes/book.rs b/src/views/panes/book.rs index 4b2dd50..d121db8 100644 --- a/src/views/panes/book.rs +++ b/src/views/panes/book.rs @@ -63,16 +63,22 @@ pub fn book_view( .map(Element::from), ); - let content = - column![ - header, - ask_rows, - t(format!("${}", book.1.iter().next_back().unwrap().0)), - bid_rows, - ] - .padding(12) - .spacing(10) - .max_width(500); + let content = column![ + header, + ask_rows, + t(format!( + "${}", + book.1 + .iter() + .next_back() + .unwrap_or((&String::new(), &0.0)) + .0 + )), + bid_rows, + ] + .padding(12) + .spacing(10) + .max_width(500); Container::new(scrollable(content)).into() } diff --git a/src/views/panes/market.rs b/src/views/panes/market.rs index a62b0b7..4a95a63 100644 --- a/src/views/panes/market.rs +++ b/src/views/panes/market.rs @@ -60,11 +60,13 @@ pub fn market_view<'a>(quote: &str, amt: &str, pair: &str) -> Element<'a, Messag row![ button("BUY") .style(iced::theme::Button::Custom(Box::new(GreenBtn {}))) - .padding(8), + .padding(8) + .on_press(Message::BuyPressed), Space::new(5.0, 0.0), button("Sell") .style(iced::theme::Button::Custom(Box::new(RedBtn {}))) .padding(8) + .on_press(Message::SellPressed) ], Space::new(Length::Fill, 1.0) ] diff --git a/src/views/panes/orders.rs b/src/views/panes/orders.rs index f805258..7341664 100644 --- a/src/views/panes/orders.rs +++ b/src/views/panes/orders.rs @@ -94,14 +94,14 @@ pub fn orders_view<'a>(os: &[Order], ps: &'a HashMap) -> Element<'a ] .width(Length::Fill), ) - .padding(12) + .padding(4) .into() }) .collect(); column![ header, - scrollable(Column::with_children(rows)).style(ScrollbarStyle::theme()) + scrollable(Column::with_children(rows).padding(8)).style(ScrollbarStyle::theme()) ] .into() }