cbat 0.0.19

The unofficial Rust crate for the Coinbase Advanced Trade API
Documentation
use cbat::prelude::*;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    // Initialize the client with your API credentials
    let client = Arc::new(Client::new(
        "websocket-example",
        std::env::var("CBAT_KEY_NAME").expect("CBAT_KEY_NAME must be set"),
        std::env::var("CBAT_KEY_SECRET").expect("CBAT_KEY_SECRET must be set"),
    ));

    // Subscribe to ticker channel using helper API
    println!("=== Subscribing to Ticker Channel ===");
    match ApiWebSocket::subscribe_ticker(&client, vec!["BTC-USD".to_string()]).await {
        Ok(_) => println!("Subscribed to ticker channel"),
        Err(e) => eprintln!("Error subscribing to ticker channel: {}", e),
    }

    // Subscribe to candles channel using helper API
    println!("\n=== Subscribing to Candles Channel ===");
    match ApiWebSocket::subscribe_candles(&client, vec!["BTC-USD".to_string()]).await {
        Ok(_) => println!("Subscribed to candles channel"),
        Err(e) => eprintln!("Error subscribing to candles channel: {}", e),
    }

    // Subscribe to level2 channel using helper API
    println!("\n=== Subscribing to Level2 Channel ===");
    match ApiWebSocket::subscribe_level2(&client, vec!["BTC-USD".to_string()]).await {
        Ok(_) => println!("Subscribed to level2 channel"),
        Err(e) => eprintln!("Error subscribing to level2 channel: {}", e),
    }

    // Subscribe to user channel using helper API (requires authentication)
    println!("\n=== Subscribing to User Channel ===");
    match ApiWebSocket::subscribe_user(&client, None).await {
        Ok(_) => println!("Subscribed to user channel"),
        Err(e) => eprintln!("Error subscribing to user channel: {}", e),
    }

    println!("\n=== Subscriptions Complete ===");
    println!("Note: This example demonstrates subscription only.");
    println!("For a full example with message listening, see the WebSocketClient documentation.");
}