gas-network-sdk 0.1.0

Rust SDK for Gas Network API - gas price prediction and optimization
Documentation
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Chain {
    Ethereum,
    Polygon,
    Bitcoin,
    Sei,
    Optimism,
    Arbitrum,
    Base,
    Linea,
    Unichain,
}

impl Chain {
    pub fn as_str(&self) -> &'static str {
        match self {
            Chain::Ethereum => "ethereum",
            Chain::Polygon => "polygon",
            Chain::Bitcoin => "bitcoin",
            Chain::Sei => "sei",
            Chain::Optimism => "optimism",
            Chain::Arbitrum => "arbitrum",
            Chain::Base => "base",
            Chain::Linea => "linea",
            Chain::Unichain => "unichain",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasPriceEstimate {
    pub confidence: u8,
    pub price: f64,
    #[serde(rename = "maxPriorityFeePerGas")]
    pub max_priority_fee_per_gas: Option<f64>,
    #[serde(rename = "maxFeePerGas")]
    pub max_fee_per_gas: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasPriceResponse {
    pub system: String,
    pub network: String,
    pub unit: String,
    #[serde(rename = "maxPrice")]
    pub max_price: f64,
    #[serde(rename = "currentBlockNumber")]
    pub current_block_number: u64,
    #[serde(rename = "msSinceLastBlock")]
    pub ms_since_last_block: u64,
    #[serde(rename = "blockPrices")]
    pub block_prices: Vec<BlockPrice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub estimated_prices: Option<Vec<GasPriceEstimate>>,
}

impl GasPriceResponse {
    /// Get estimated prices from the first block for convenience
    pub fn estimated_prices(&self) -> Vec<GasPriceEstimate> {
        self.block_prices
            .first()
            .map(|block| block.estimated_prices.clone())
            .unwrap_or_default()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockPrice {
    #[serde(rename = "blockNumber")]
    pub block_number: u64,
    #[serde(rename = "estimatedTransactionCount")]
    pub estimated_transaction_count: u32,
    #[serde(rename = "baseFeePerGas")]
    pub base_fee_per_gas: Option<f64>,
    #[serde(rename = "blobBaseFeePerGas")]
    pub blob_base_fee_per_gas: Option<f64>,
    #[serde(rename = "estimatedPrices")]
    pub estimated_prices: Vec<GasPriceEstimate>,
}


#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasDistribution {
    pub price: f64,
    pub transaction_count: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributionResponse {
    pub system: String,
    pub network: String,
    pub unit: String,
    #[serde(rename = "maxPrice")]
    pub max_price: f64,
    #[serde(rename = "currentBlockNumber")]
    pub current_block_number: u64,
    #[serde(rename = "msSinceLastBlock")]
    pub ms_since_last_block: u64,
    #[serde(rename = "topNDistribution")]
    pub top_n_distribution: TopNDistribution,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopNDistribution {
    pub distribution: Vec<(f64, u32)>,
    pub n: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OraclePayload {
    pub height: u64,
    pub timestamp: u64,
    pub systemid: u32,
    pub chainid: u64,
    pub payloads: Vec<OracleRecord>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OracleRecord {
    pub typ: u32,
    pub value: String,
}

#[derive(Debug, Clone, Copy)]
pub enum OracleType {
    BaseFeePerGas = 107,
    BlobBaseFeePerGas = 112,
    PredictedMaxPriorityFeePerGas = 322,
}

impl From<OracleType> for u32 {
    fn from(oracle_type: OracleType) -> u32 {
        oracle_type as u32
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyRequest {
    pub api_key: String,
}

// Base Fee API Response Types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseFeeResponse {
    pub system: String,
    pub network: String,
    pub unit: String,
    #[serde(rename = "currentBlockNumber")]
    pub current_block_number: u64,
    #[serde(rename = "msSinceLastBlock")]
    pub ms_since_last_block: u64,
    #[serde(rename = "baseFeePerGas")]
    pub base_fee_per_gas: f64,
    #[serde(rename = "blobBaseFeePerGas")]
    pub blob_base_fee_per_gas: f64,
    #[serde(rename = "estimatedBaseFees")]
    pub estimated_base_fees: Vec<PendingBlockBaseFee>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingBlockBaseFee {
    #[serde(flatten)]
    pub pending_block: HashMap<String, Vec<BaseFeeEstimate>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseFeeEstimate {
    pub confidence: u8,
    #[serde(rename = "baseFee")]
    pub base_fee: f64,
    #[serde(rename = "blobBaseFee")]
    pub blob_base_fee: f64,
    #[serde(rename = "priorityFee")]
    pub priority_fee: Vec<PriorityFee>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriorityFee {
    #[serde(rename = "blobCount")]
    pub blob_count: u8,
    pub tip: f64,
}