forked from Igosuki/binance-rs-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance_endpoints.rs
222 lines (194 loc) · 6.63 KB
/
binance_endpoints.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#[macro_use]
extern crate tracing;
use binance::account::*;
use binance::api::*;
use binance::config::Config;
use binance::errors::Error as BinanceLibError;
use binance::general::*;
use binance::market::*;
use binance::rest_model::{OrderSide, OrderType, SymbolPrice, TimeInForce};
use env_logger::Builder;
#[tokio::main]
async fn main() {
Builder::new().parse_default_env().init();
info!("running general endpoints");
general().await;
info!("running market endpoints");
market_data().await;
info!("running account (private) endpoints");
account().await;
}
async fn general() {
let general: General = Binance::new(None, None);
let ping = general.ping().await;
match ping {
Ok(answer) => info!("{:?}", answer),
Err(err) => {
match err {
BinanceLibError::BinanceError { response } => match response.code {
-1000_i32 => error!("An unknown error occured while processing the request"),
_ => error!("Non-catched code {}: {}", response.code, response.msg),
},
_ => error!("Other errors: {}.", err),
};
}
}
let result = general.get_server_time().await;
match result {
Ok(answer) => info!("Server Time: {}", answer.server_time),
Err(e) => error!("Error: {e}"),
}
let result = general.exchange_info().await;
match result {
Ok(answer) => info!("Exchange information: {:?}", answer),
Err(e) => error!("Error: {e}"),
}
}
async fn account() {
let market: Market = Binance::new(None, None);
let account: Account = Binance::new_with_env(&Config::testnet());
let symbol = "BTCUSDT";
let SymbolPrice { price, .. } = market.get_price(symbol).await.unwrap();
match account.get_account().await {
Ok(answer) => info!("{:?}", answer.balances),
Err(e) => error!("Error: {e}"),
}
match account.get_open_orders(symbol).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let limit_buy = OrderRequest {
symbol: symbol.to_string(),
quantity: Some(0.001),
price: Some(price),
order_type: OrderType::Limit,
side: OrderSide::Buy,
time_in_force: Some(TimeInForce::FOK),
..OrderRequest::default()
};
match account.place_order(limit_buy).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let market_buy = OrderRequest {
symbol: symbol.to_string(),
quantity: Some(0.001),
order_type: OrderType::Market,
side: OrderSide::Buy,
..OrderRequest::default()
};
match account.place_order(market_buy).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let limit_sell = OrderRequest {
symbol: symbol.to_string(),
quantity: Some(0.001),
price: Some(price),
order_type: OrderType::Limit,
side: OrderSide::Sell,
time_in_force: Some(TimeInForce::FOK),
..OrderRequest::default()
};
match account.place_order(limit_sell).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let market_sell = OrderRequest {
symbol: symbol.to_string(),
quantity: Some(0.001),
order_type: OrderType::Market,
side: OrderSide::Sell,
..OrderRequest::default()
};
match account.place_order(market_sell).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let order_id = 1_957_528;
let order_status = OrderStatusRequest {
symbol: symbol.to_string(),
order_id: Some(order_id),
..OrderStatusRequest::default()
};
match account.order_status(order_status).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
let order_cancellation = OrderCancellation {
symbol: symbol.to_string(),
order_id: Some(order_id),
..OrderCancellation::default()
};
match account.cancel_order(order_cancellation).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
match account.get_balance("BTC").await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
match account.trade_history(symbol).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
}
async fn market_data() {
let market: Market = Binance::new(None, None);
// Order book
match market.get_depth("BNBETH").await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// Latest price for ALL symbols
match market.get_all_prices().await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// Latest price for ONE symbol
match market.get_price("KNCETH").await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// Current average price for ONE symbol
match market.get_average_price("KNCETH").await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// Best price/qty on the order book for ALL symbols
match market.get_all_book_tickers().await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// Best price/qty on the order book for ONE symbol
match market.get_book_ticker("BNBETH").await {
Ok(answer) => info!("Bid Price: {}, Ask Price: {}", answer.bid_price, answer.ask_price),
Err(e) => error!("Error: {e}"),
}
// 24hr ticker price change statistics
match market.get_24h_price_stats("BNBETH").await {
Ok(answer) => info!(
"Open Price: {}, Higher Price: {}, Lower Price: {:?}",
answer.open_price, answer.high_price, answer.low_price
),
Err(e) => error!("Error: {e}"),
}
// last 10 5min klines (candlesticks) for a symbol:
match market.get_klines("BNBETH", "5m", 10, None, None).await {
Ok(answer) => info!("{:?}", answer),
Err(e) => error!("Error: {e}"),
}
// 10 latest (aggregated) trades
match market.get_agg_trades("BNBETH", None, None, None, Some(10)).await {
Ok(trades) => {
let trade = &trades[0]; // You need to iterate over them
println!(
"{} BNB Qty: {}, Price: {}",
if trade.maker { "SELL" } else { "BUY" },
trade.qty,
trade.price
)
}
Err(e) => println!("Error: {e}"),
}
}