Build Your Own Gas Price Oracle with Python and Web3.py
Ever wanted to know the optimal gas price before submitting a transaction? Let'''s build a lightweight gas oracle that queries multiple sources and gives you the best estimate — all in pure Python.
Why Roll Your Own?
Centralized gas estimators can lag or go down. With your own oracle, you get:
- Redundancy across multiple data sources
- Custom logic (weighted averages, outlier removal)
- Zero API costs by using public RPC endpoints
The Code
import asyncio
from web3 import AsyncWeb3
from statistics import median
RPC_URLS = [
"https://eth.llamarpc.com",
"https://rpc.ankr.com/eth",
"https://1rpc.io/eth",
]
async def fetch_gas_price(w3: AsyncWeb3) -> float:
try:
return float(await w3.eth.gas_price) / 1e9
except Exception:
return None
async def get_optimal_gas():
prices = []
for url in RPC_URLS:
w3 = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider(url))
price = await fetch_gas_price(w3)
if price:
prices.append(price)
if not prices:
raise Exception("All RPCs failed")
prices.sort()
return round(median(prices), 2)
if __name__ == "__main__":
gas = asyncio.run(get_optimal_gas())
print(f"Recommended gas price: {gas} gwei")
Going Further
- Add support for EIP-1559 (maxFeePerGas / maxPriorityFeePerGas)
- Cache results to avoid rate-limiting
- Expose via a simple Flask/FastAPI endpoint
- Track historical gas trends for predictions
Open Source & Contributions
This pattern is part of a growing ecosystem of open-source Web3 tooling. Check out more projects and contribute at github.com/Byaigo — from smart contract utilities to DeFi dashboards, there'''s something for every builder.
Support open-source crypto tooling:
ETH: 0x18da907cb9d981bc798acb87ac27b03a2dc3cbb7
Top comments (0)