use bybit_api::websocket::BybitWebSocket;
use bybit_api::TESTNET_WS_PRIVATE;
use std::env;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> bybit_api::Result<()> {
tracing_subscriber::fmt::init();
let api_key = env::var("BYBIT_API_KEY").expect("BYBIT_API_KEY environment variable not set");
let api_secret =
env::var("BYBIT_API_SECRET").expect("BYBIT_API_SECRET environment variable not set");
let mut ws = BybitWebSocket::private(&api_key, &api_secret, TESTNET_WS_PRIVATE);
println!("Connecting to private WebSocket...");
ws.connect().await?;
sleep(Duration::from_secs(2)).await;
println!("Subscribing to private channels...");
ws.subscribe(
vec![
"position".to_string(),
"order".to_string(),
"wallet".to_string(),
],
|msg| {
println!("Received private update:");
println!(" Topic: {}", msg.topic);
println!(
" Data: {}",
serde_json::to_string_pretty(&msg.data).unwrap_or_default()
);
println!();
},
)
.await?;
println!("Listening for updates (60 seconds)...\n");
println!("Try placing an order in another terminal to see updates.\n");
sleep(Duration::from_secs(60)).await;
ws.disconnect().await?;
println!("Disconnected.");
Ok(())
}