Skip to content

Commit

Permalink
update to latest rust
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Mar 18, 2024
1 parent 0885ee5 commit 995a672
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 90 deletions.
82 changes: 31 additions & 51 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,32 @@ lazy_static! {

pub async fn orders_history() -> Vec<Order> {
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<Order> =
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<Order> = 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
}
Expand Down Expand Up @@ -79,22 +72,9 @@ pub async fn trade_spot(

pub async fn balances() -> Vec<Balance> {
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()
}
64 changes: 39 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion src/views/panes/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ pub fn balances_view<'a>(bs: &[Balance], ps: &'a HashMap<String, f32>) -> Elemen
.width(Length::Fill)
})
.map(Element::from),
))
).padding(8))
.into()
}
26 changes: 16 additions & 10 deletions src/views/panes/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 3 additions & 1 deletion src/views/panes/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down
4 changes: 2 additions & 2 deletions src/views/panes/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ pub fn orders_view<'a>(os: &[Order], ps: &'a HashMap<String, f32>) -> 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()
}

0 comments on commit 995a672

Please sign in to comment.