Created
November 14, 2024 20:51
-
-
Save elmariachi111/8d376ebc080690f78fe065db1ef8608d to your computer and use it in GitHub Desktop.
read gas price from etherscan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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