use gas_network_sdk::{GasNetworkClient, Chain, Result};
#[tokio::main]
async fn main() -> Result<()> {
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");
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),
}
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),
}
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);
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),
}
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),
}
println!("\n5. Supported chains:");
for chain in GasNetworkClient::supported_chains() {
println!(" - {}", chain.as_str());
}
println!("\n=== Example Complete ===");
Ok(())
}