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(
        "products-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 best bid/ask for a product
    println!("=== Getting Best Bid/Ask ===");
    match ApiProducts::get_best_bid_ask(&client, Some(vec!["BTC-USD"])).await {
        Ok(bid_ask) => println!("{:#?}", bid_ask),
        Err(e) => eprintln!("Error getting best bid/ask: {}", e),
    }

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

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

    // Get specific product
    println!("\n=== Getting Specific Product ===");
    match ApiProducts::get_product(&client, "BTC-USD", None).await {
        Ok(product) => println!("{:#?}", product),
        Err(e) => eprintln!("Error getting product: {}", e),
    }

    // Get product candles
    println!("\n=== Getting 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 ApiProducts::get_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 product candles: {}", e),
    }

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