cbat 0.0.19

The unofficial Rust crate for the Coinbase Advanced Trade API
Documentation
use cbat::prelude::*;
use chrono::{DateTime, Utc};

#[tokio::main]
async fn main() {
    // Initialize the client with your API credentials
    let client = Client::new(
        "public-api-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"),
    );

    // Get server time
    println!("=== Getting Server Time ===");
    match ApiPublic::get_public_server_time(&client).await {
        Ok(time) => println!("{:#?}", time),
        Err(e) => eprintln!("Error getting server time: {}", e),
    }

    // Get public product
    println!("\n=== Getting Public Product ===");
    match ApiPublic::get_public_product(&client, "BTC-USD").await {
        Ok(product) => println!("{:#?}", product),
        Err(e) => eprintln!("Error getting public product: {}", e),
    }

    // Get public product book
    println!("\n=== Getting Public Product Book ===");
    match ApiPublic::get_public_product_book(&client, "BTC-USD", Some(1), None).await {
        Ok(book) => println!("{:#?}", book),
        Err(e) => eprintln!("Error getting public product book: {}", e),
    }

    // List public products
    println!("\n=== Listing Public Products ===");
    match ApiPublic::list_public_products(
        &client,
        Some(1),
        None,
        Some(ProductType::Spot),
        None,
        None,
        None,
        None
    ).await {
        Ok(products) => println!("{:#?}", products),
        Err(e) => eprintln!("Error listing public products: {}", e),
    }

    // Get public market trades
    println!("\n=== Getting Public Market Trades ===");
    match ApiPublic::get_public_market_trades(&client, "BTC-USD", 1, None, None).await {
        Ok(trades) => println!("{:#?}", trades),
        Err(e) => eprintln!("Error getting public market trades: {}", e),
    }

    // Get public product candles
    println!("\n=== Getting Public Product Candles ===");
    let current_time: DateTime<Utc> = Utc::now();
    let epoch_time = current_time.timestamp();
    let start = epoch_time - 10_000;
    let end = epoch_time + 10_000;
    match ApiPublic::get_public_product_candles(
        &client,
        "BTC-USD",
        &start.to_string(),
        &end.to_string(),
        Granularity::OneMinute,
        Some(1)
    ).await {
        Ok(candles) => println!("{:#?}", candles),
        Err(e) => eprintln!("Error getting public product candles: {}", e),
    }
}