cefi_rs/exchanges/
binance.rs1use crate::{
2 interface_http::InterfaceHttp,
3 trade::*,
4 types::{Orderbook, OrderbookLevel},
5};
6use async_trait::async_trait;
7use cefi_rs_binance::{http::BinanceHttp, types::OrderBook as BinanceOrderBook};
8
9pub struct BinanceHttpWrapper {
10 client: BinanceHttp,
11}
12
13impl BinanceHttpWrapper {
14 pub fn new(api_key: String, api_secret: String) -> Self {
15 Self {
16 client: BinanceHttp::new(api_key, api_secret),
17 }
18 }
19}
20
21#[async_trait]
22impl InterfaceHttp for BinanceHttpWrapper {
23 async fn get_server_time(&self) -> anyhow::Result<u64> {
24 let server_time = self
25 .client
26 .check_server_time()
27 .await
28 .map_err(|e| anyhow::anyhow!("{}", e))?;
29 Ok(server_time.server_time)
30 }
31
32 async fn get_orderbook(
33 &self,
34 symbol: &String,
35 limit: Option<i32>,
36 ) -> anyhow::Result<Orderbook> {
37 let orderbook = self
38 .client
39 .get_orderbook(symbol, limit)
40 .await
41 .map_err(|e| anyhow::anyhow!("{}", e))?;
42
43 todo!()
44 }
46
47 async fn place_order(&self, params: &PlaceOrderParams) -> anyhow::Result<PlaceOrderResponse> {
48 todo!()
49 }
50
51 async fn cancel_order(&self, order_id: &String) -> anyhow::Result<CancelOrderResponse> {
52 todo!()
53 }
54
55 async fn cancel_all_orders(&self, symbol: &String) -> anyhow::Result<CancelAllOrdersResponse> {
56 todo!()
57 }
58
59 async fn amend_order(
60 &self,
61 order_id: &String,
62 params: &AmendOrderParams,
63 ) -> anyhow::Result<AmendOrderResponse> {
64 todo!()
65 }
66}
67
68impl Orderbook {
69 fn from_binance_orderbook(orderbook: BinanceOrderBook, symbol: String) -> Self {
70 Orderbook {
71 symbol: symbol,
72 asks: orderbook
73 .asks
74 .into_iter()
75 .map(|ask| OrderbookLevel {
76 price: ask[0].parse::<f64>().unwrap(),
77 amount: ask[1].parse::<f64>().unwrap(),
78 })
79 .collect(),
80 bids: orderbook
81 .bids
82 .into_iter()
83 .map(|bid| OrderbookLevel {
84 price: bid[0].parse::<f64>().unwrap(),
85 amount: bid[1].parse::<f64>().unwrap(),
86 })
87 .collect(),
88 timestamp_ms: orderbook.event_time,
89 }
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use crate::exchanges::binance::BinanceHttpWrapper;
96
97 use super::*;
98
99 #[tokio::test]
100 async fn test_get_orderbook() {
101 let binance = BinanceHttpWrapper::new("".to_string(), "".to_string());
102 let orderbook = binance
103 .get_orderbook(&"BTCUSDT".to_string(), Some(5))
104 .await
105 .unwrap();
106 println!("orderbook: {:?}", orderbook);
107 }
108}