gas-network-sdk 0.1.0

Rust SDK for Gas Network API - gas price prediction and optimization
Documentation
use gas_network_sdk::{GasNetworkClient, Chain, Result};

#[tokio::main]
async fn main() -> Result<()> {
    // Create client with API key from environment or use placeholder
    let api_key = std::env::var("GAS_NETWORK_API_KEY")
        .unwrap_or_else(|_| "your_api_key_here".to_string());
    
    let client = GasNetworkClient::new(api_key)?;

    println!("=== Gas Network SDK Example ===\n");

    // Example 1: Get gas prices for Ethereum
    println!("1. Getting gas prices for Ethereum...");
    match client.get_gas_prices(Chain::Ethereum).await {
        Ok(prices) => {
            println!("✓ Current block: {}", prices.current_block_number);
            println!("✓ Max price: {} {}", prices.max_price, prices.unit);
            let estimated = prices.estimated_prices();
            if let Some(estimate) = estimated.first() {
                println!("✓ Recommended price: {} {} ({}% confidence)", 
                    estimate.price, prices.unit, estimate.confidence);
            }
        }
        Err(e) => println!("✗ Error: {}", e),
    }

    // Example 2: Get next block estimate with specific confidence
    println!("\n2. Getting next block estimate (90% confidence)...");
    match client.get_next_block_estimate(Chain::Ethereum, Some(90)).await {
        Ok(estimate) => {
            println!("✓ Price: {} gwei", estimate.price);
            if let Some(max_fee) = estimate.max_fee_per_gas {
                println!("✓ Max fee per gas: {} gwei", max_fee);
            }
        }
        Err(e) => println!("✗ Error: {}", e),
    }

    // Example 3: Get base fee estimates (Ethereum only)
    println!("\n3. Getting base fee estimates...");
    match client.get_base_fee_estimates(Chain::Ethereum).await {
        Ok(base_fees) => {
            println!("✓ Current block: {}", base_fees.current_block_number);
            println!("✓ Current base fee: {} {}", base_fees.base_fee_per_gas, base_fees.unit);
            println!("✓ Blob base fee: {} {}", base_fees.blob_base_fee_per_gas, base_fees.unit);
            
            // Show first pending block estimate
            if let Some(first_block) = base_fees.estimated_base_fees.first() {
                if let Some((pending_block, estimates)) = first_block.pending_block.iter().next() {
                    if let Some(estimate) = estimates.first() {
                        println!("{} estimate: {} {} ({}% confidence)", 
                            pending_block, estimate.base_fee, base_fees.unit, estimate.confidence);
                    }
                }
            }
        }
        Err(e) => println!("✗ Error: {}", e),
    }

    // Example 4: Get gas distribution (Ethereum only)
    println!("\n4. Getting gas price distribution...");
    match client.get_gas_distribution(Chain::Ethereum).await {
        Ok(distribution) => {
            println!("✓ Current block: {}", distribution.current_block_number);
            println!("✓ Distribution entries: {}", distribution.top_n_distribution.distribution.len());
            if let Some((price, count)) = distribution.top_n_distribution.distribution.first() {
                println!("✓ Highest price: {} {} ({} transactions)", 
                    price, distribution.unit, count);
            }
        }
        Err(e) => println!("✗ Error: {}", e),
    }

    // Example 5: Show supported chains
    println!("\n5. Supported chains:");
    for chain in GasNetworkClient::supported_chains() {
        println!("  - {}", chain.as_str());
    }

    println!("\n=== Example Complete ===");
    Ok(())
}