Skip to content

Instantly share code, notes, and snippets.

@marianoeramirez
Last active May 22, 2023 18:27
Show Gist options
  • Save marianoeramirez/1db5271c1c0f89431983bce57f9e49c0 to your computer and use it in GitHub Desktop.
Save marianoeramirez/1db5271c1c0f89431983bce57f9e49c0 to your computer and use it in GitHub Desktop.
Binence Rust
use binance::api::*;
use binance::futures::userstream::*;
use binance::futures::websockets::*;
use std::sync::atomic::{AtomicBool};
fn main() {
user_stream();
}
fn user_stream() {
let api_key_user = Some("API_KEY".into());
let keep_running = AtomicBool::new(true); // Used to control the event loop
let user_stream: FuturesUserStream = Binance::new(api_key_user, None);
if let Ok(answer) = user_stream.start() {
println!("Data Stream Started ...");
let listen_key = answer.listen_key;
let mut web_socket = FuturesWebSockets::new(|event: FuturesWebsocketEvent| {
match event {
FuturesWebsocketEvent::AccountUpdate(account_update) => {
// println!(account_update);
dbg!(account_update);
// for balance in &account_update.balance {
// println!("Asset: {}, free: {}, locked: {}", balance.asset, balance.free, balance.locked);
// }
},
FuturesWebsocketEvent::OrderTrade(trade) => {
dbg!(trade);
// println!("Symbol: {}, Side: {}, Price: {}, Execution Type: {}", trade.symbol, trade.side, trade.price, trade.execution_type);
},
_ => (),
};
Ok(())
});
web_socket.connect(&FuturesMarket::USDM, &listen_key).unwrap(); // check error
if let Err(e) = web_socket.event_loop(&keep_running) {
match e {
err => {
println!("Error: {:?}", err);
}
}
} else {
println!("Not able to start an User Stream (Check your API_KEY)");
}
// match user_stream.keep_alive(&listen_key) {
// Ok(msg) => println!("Keepalive user data stream: {:?}", msg),
// Err(e) => println!("Error: {}", e),
// }
// match user_stream.close(&listen_key) {
// Ok(msg) => println!("Close user data stream: {:?}", msg),
// Err(e) => println!("Error: {}", e),
// }
} else {
println!("Not able to start an User Stream (Check your API_KEY)");
}
}
@marianoeramirez
Copy link
Author

[dependencies]
binance = { git = "https://github.com/wisespace-io/binance-rs.git" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment