binance-sdk 63.0.0

This is a lightweight library that works as a connector to the Binance public API.
Documentation
use anyhow::{Context, Result};
use tokio::time::{Duration, sleep};
use tracing::info;

use binance_sdk::alpha::{
    AlphaWsStreams,
    websocket_streams::{
        PartialDepthStreamIntervalEnum, PartialDepthStreamLevelsEnum, PartialDepthStreamParams,
    },
};
use binance_sdk::config::ConfigurationWebsocketStreams;
use binance_sdk::logger;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialise logging
    logger::init();

    // Build WebSocket Streams config
    let ws_streams_conf = ConfigurationWebsocketStreams::builder().build()?;

    // Create the Alpha WebSocket Streams client
    let ws_streams_client = AlphaWsStreams::production(ws_streams_conf);

    // Connect to WebSocket
    let connection = ws_streams_client
        .connect()
        .await
        .context("Failed to connect to WebSocket Streams")?;

    // Setup the stream parameters
    let params = PartialDepthStreamParams::builder(
        "alpha_116usdt".to_string(),
        PartialDepthStreamLevelsEnum::Levels5,
        PartialDepthStreamIntervalEnum::Interval0ms,
    )
    .build()?;

    // Subscribe to the stream
    let stream = connection
        .partial_depth_stream(params)
        .await
        .context("Failed to subscribe to the stream")?;

    // Register callback for incoming messages
    stream.on_message(|data| {
        info!("{:?}", data);
    });

    // Disconnect after 20 seconds
    sleep(Duration::from_secs(20)).await;
    connection
        .disconnect()
        .await
        .context("Failed to disconnect WebSocket client")?;

    Ok(())
}