Skip to content

Instantly share code, notes, and snippets.

@elmariachi111
Created November 14, 2024 20:51
Show Gist options
  • Save elmariachi111/8d376ebc080690f78fe065db1ef8608d to your computer and use it in GitHub Desktop.
Save elmariachi111/8d376ebc080690f78fe065db1ef8608d to your computer and use it in GitHub Desktop.
read gas price from etherscan
#!/usr/bin/env python3
import requests
import os
def get_gas_price():
"""
Gets current gas price using the Etherscan API
Returns price in gwei
"""
ETHERSCAN_API_KEY = "xxxx"
url = f"https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey={ETHERSCAN_API_KEY}"
try:
response = requests.get(url)
data = response.json()
if data["status"] == "1" and data["message"] == "OK":
gas_price = float(data["result"]["SafeGasPrice"])
return f"Gas Price: {gas_price:.1f} gwei"
return "Error fetching gas price"
except Exception as e:
return f"Error: {e}"
if __name__ == "__main__":
print(get_gas_price())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment