1 unstable release
| 0.1.0 | Aug 28, 2025 |
|---|
#484 in HTTP client
19KB
368 lines
Gas Network SDK
A Rust SDK for the Gas Network API, providing gas price prediction and optimization for blockchain transactions.
Features
- Multi-chain support: Ethereum, Polygon, Bitcoin, SEI, Optimism, Arbitrum, Base, Linea, Unichain
- Real-time gas price estimates with confidence levels
- Base fee and blob fee predictions (Ethereum only)
- Gas price distribution analysis (Ethereum only)
- Oracle integration for on-chain gas data
- Async/await support with tokio
- Comprehensive error handling
Installation
Add this to your Cargo.toml:
[dependencies]
gas-network-sdk = "0.1.0"
Quick Start
use gas_network_sdk::{GasNetworkClient, Chain};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create client with your API key
let client = GasNetworkClient::new("your_api_key".to_string())?;
// Get gas prices for Ethereum
let gas_prices = client.get_gas_prices(Chain::Ethereum).await?;
println!("Current gas prices: {:?}", gas_prices);
// Get next block estimate with 90% confidence
let estimate = client.get_next_block_estimate(Chain::Ethereum, Some(90)).await?;
println!("Next block estimate: {} gwei", estimate.price);
Ok(())
}
API Reference
Client Creation
let client = GasNetworkClient::new("your_api_key".to_string())?;
Gas Price Estimation
// Get comprehensive gas price data
let prices = client.get_gas_prices(Chain::Base).await?;
// Get specific confidence level estimate
let estimate = client.get_next_block_estimate(Chain::Ethereum, Some(95)).await?;
Base Fee Prediction (Ethereum only)
let base_fees = client.get_base_fee_estimates(Chain::Ethereum).await?;
println!("Current base fee: {} gwei", base_fees.base_fee_per_gas);
println!("Blob base fee: {} gwei", base_fees.blob_base_fee_per_gas);
// Get estimates for next 5 blocks
for block_estimate in &base_fees.estimated_base_fees {
// Each block contains estimates with different confidence levels
for (pending_block, estimates) in &block_estimate.pending_block {
for estimate in estimates {
println!("{}: Base fee {} gwei ({}% confidence)",
pending_block, estimate.base_fee, estimate.confidence);
}
}
}
Gas Distribution Analysis (Ethereum only)
let distribution = client.get_gas_distribution(Chain::Ethereum).await?;
Oracle Data
// Get oracle data for a specific chain ID
let oracle_data = client.get_oracle_data(1).await?; // Ethereum mainnet
Supported Chains
- Ethereum
- Polygon
- Bitcoin
- SEI
- Optimism
- Arbitrum
- Base
- Linea
- Unichain
Error Handling
The SDK uses a comprehensive error system:
use gas_network_sdk::{GasNetworkError, Result};
match client.get_gas_prices(Chain::Ethereum).await {
Ok(prices) => println!("Success: {:?}", prices),
Err(GasNetworkError::InvalidApiKey) => eprintln!("Invalid API key"),
Err(GasNetworkError::UnsupportedChain(chain)) => eprintln!("Unsupported chain: {}", chain),
Err(GasNetworkError::Api { message }) => eprintln!("API error: {}", message),
Err(e) => eprintln!("Other error: {}", e),
}
Authentication
You can optionally use an API key from Blocknative for higher rate limits. The API works without authentication but with rate limitations. Pass your API key when creating the client, or use a placeholder if you don't have one.
License
Licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.
Dependencies
~7–22MB
~256K SLoC