use lighter_rs::ws_client::{OrderBook, WsClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
tracing::info!("╔═══════════════════════════════════════════════════╗");
tracing::info!("║ Lighter RS - WebSocket Order Book Example ║");
tracing::info!("╚═══════════════════════════════════════════════════╝\n");
let client = WsClient::builder()
.host("api-testnet.lighter.xyz")
.order_books(vec![0, 1]) .build()?;
tracing::info!("Connecting to WebSocket...");
tracing::info!("Subscriptions: markets 0, 1\n");
let on_order_book_update = |market_id: String, order_book: OrderBook| {
tracing::info!("═══ Order Book Update: Market {} ═══", market_id);
tracing::info!("\n 📈 Top 5 Asks (Sell Orders):");
for (i, ask) in order_book.asks.iter().take(5).enumerate() {
tracing::info!(
" {}. Price: {:>12} | Size: {:>12}",
i + 1,
ask.price,
ask.size
);
}
tracing::info!("\n 📉 Top 5 Bids (Buy Orders):");
for (i, bid) in order_book.bids.iter().take(5).enumerate() {
tracing::info!(
" {}. Price: {:>12} | Size: {:>12}",
i + 1,
bid.price,
bid.size
);
}
if let (Some(best_ask), Some(best_bid)) = (order_book.asks.first(), order_book.bids.first())
{
if let (Ok(ask_price), Ok(bid_price)) =
(best_ask.price.parse::<f64>(), best_bid.price.parse::<f64>())
{
let spread = ask_price - bid_price;
let spread_bps = (spread / bid_price) * 10000.0;
tracing::info!("\n 💰 Spread: {:.2} ({:.2} bps)", spread, spread_bps);
}
}
tracing::info!("\n{}\n", "─".repeat(50));
};
let on_account_update = |_account_id: String, _account_data: serde_json::Value| {};
tracing::info!("Starting WebSocket stream...");
tracing::info!("Press Ctrl+C to stop\n");
tracing::info!("{}\n", "═".repeat(50));
client.run(on_order_book_update, on_account_update).await?;
Ok(())
}