Python SDK for Gas Network API - gas price prediction and optimization
Project description
Gas Network SDK
A Python 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 httpx
- Type-safe with Pydantic models
- Comprehensive error handling
Installation
pip install gas-network-sdk
Quick Start
import asyncio
from gas_network_sdk import GasNetworkClient, Chain
async def main():
# Create client with your API key (optional)
client = GasNetworkClient(api_key="your_api_key_here")
# Get gas prices for Ethereum
gas_prices = await client.get_gas_prices(Chain.ETHEREUM)
print(f"Current gas prices: {gas_prices}")
# Get next block estimate with 90% confidence
estimate = await client.get_next_block_estimate(Chain.ETHEREUM, confidence_level=90)
print(f"Next block estimate: {estimate.price} gwei")
await client.close()
# Run the example
asyncio.run(main())
API Reference
Client Creation
from gas_network_sdk import GasNetworkClient
# With API key (recommended for higher rate limits)
client = GasNetworkClient(api_key="your_api_key")
# Without API key (rate limited)
client = GasNetworkClient()
Gas Price Estimation
# Get comprehensive gas price data
prices = await client.get_gas_prices(Chain.BASE)
# Get specific confidence level estimate
estimate = await client.get_next_block_estimate(Chain.ETHEREUM, confidence_level=95)
Base Fee Prediction (Ethereum only)
base_fees = await client.get_base_fee_estimates(Chain.ETHEREUM)
print(f"Current base fee: {base_fees.base_fee_per_gas} gwei")
print(f"Blob base fee: {base_fees.blob_base_fee_per_gas} gwei")
# Get estimates for next 5 blocks
for block_estimate in base_fees.estimated_base_fees:
for pending_block, estimates in block_estimate.pending_block.items():
for estimate in estimates:
print(f"{pending_block}: Base fee {estimate.base_fee} gwei ({estimate.confidence}% confidence)")
Gas Distribution Analysis (Ethereum only)
distribution = await client.get_gas_distribution(Chain.ETHEREUM)
print(f"Current block: {distribution.current_block_number}")
for price, count in distribution.top_n_distribution.distribution:
print(f"Price: {price} gwei, Transactions: {count}")
Oracle Data
# Get oracle data for a specific chain ID
oracle_data = await client.get_oracle_data(1) # Ethereum mainnet
Supported Chains
- Ethereum
- Polygon
- Bitcoin
- SEI
- Optimism
- Arbitrum
- Base
- Linea
- Unichain
Error Handling
The SDK uses comprehensive error handling:
from gas_network_sdk import GasNetworkError, UnsupportedChainError, APIError
try:
prices = await client.get_gas_prices(Chain.ETHEREUM)
print(f"Success: {prices}")
except UnsupportedChainError as e:
print(f"Unsupported chain: {e}")
except APIError as e:
print(f"API error: {e}")
except GasNetworkError as e:
print(f"Other error: {e}")
Context Manager Usage
async with GasNetworkClient(api_key="your_api_key") as client:
prices = await client.get_gas_prices(Chain.ETHEREUM)
print(prices)
# Client is automatically closed
Authentication
You can optionally use an API key from Blocknative for higher rate limits. The API works without authentication but with rate limitations.
License
Licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gas_network_sdk-0.1.0.tar.gz.
File metadata
- Download URL: gas_network_sdk-0.1.0.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42603b64aa694743f6b843f8d979bc2b0f7e722711f8b70979e136ce3fea3124
|
|
| MD5 |
215ab6a3193777439a0657ae91573e53
|
|
| BLAKE2b-256 |
c9d80e759cca310c512d08c2a95a3d274da58c5393e6ac167350c6b6a1a839a3
|
File details
Details for the file gas_network_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gas_network_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7891a1189d31f92de19c1b61fa040b6a1b6794e8240420a6bdcb429b7e319447
|
|
| MD5 |
e7b13e4d2b93cff78b9c189e215bd123
|
|
| BLAKE2b-256 |
05bfdcad1bd76440a12a26dd30149478db7c79ebbe37f3a6880eca0ac5615777
|