Skip to main content

Installation

Add Havklo to your Cargo.toml:
[dependencies]
kraken-sdk = { git = "https://github.com/hitakshiA/Havklo_sdk" }
tokio = { version = "1", features = ["full"] }

Stream Your First Orderbook

1

Create the Client

Use the builder pattern to configure your connection:
use kraken_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = KrakenClient::builder(vec!["BTC/USD".into()])
        .with_depth(Depth::D10)
        .with_book(true)
        .connect()
        .await?;
2

Query Market Data

Access orderbook state at any time:
    loop {
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;

        if let Some(spread) = client.spread("BTC/USD") {
            println!("Spread: {}", spread);
        }

        if let Some(bid) = client.best_bid("BTC/USD") {
            println!("Best bid: {}", bid);
        }

        if let Some(ask) = client.best_ask("BTC/USD") {
            println!("Best ask: {}", ask);
        }
    }
}
3

Run It

cargo run
Output:
Spread: 0.50
Best bid: 67432.50
Best ask: 67433.00

Process Events

For reactive processing, consume the event stream:
use kraken_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = KrakenClient::builder(vec!["BTC/USD".into()])
        .with_book(true)
        .connect()
        .await?;

    let mut events = client.events();

    while let Some(event) = events.recv().await {
        match event {
            Event::Market(MarketEvent::OrderbookSnapshot { symbol, snapshot }) => {
                println!("{}: Snapshot received", symbol);
                println!("  Bids: {}, Asks: {}", snapshot.bids.len(), snapshot.asks.len());
            }
            Event::Market(MarketEvent::OrderbookUpdate { symbol, snapshot }) => {
                if let Some(spread) = snapshot.spread() {
                    println!("{}: Spread = {}", symbol, spread);
                }
            }
            Event::Connection(ConnectionEvent::Disconnected { reason }) => {
                println!("Disconnected: {:?}", reason);
            }
            _ => {}
        }
    }

    Ok(())
}
The SDK automatically validates CRC32 checksums on every update. If a mismatch is detected, it resyncs the orderbook transparently.

What’s Next?