Building a Python Application to query Gas Prices on the BNB Chain

The goal of NodeReal, a one-stop shop for blockchain infrastructure and services, is to empower developers by helping them "Make their Web3 Real." The support for effective Enhanced APIs, starting with NFT, Fungible Tokens, Debug Pro APIs, and Gas Tracker API is something we are going to use today.
Copy down the Gas Tracker API Endpoint: https://api.nodereal.io/node/gasInfo.
For more information about Gas Tracker API, you can view the documentation here.
🤓 #Web3MadeEasy Tutorial Series 🤓
— NodeReal (@Nodereal_io) February 23, 2023
Time to #MakeWeb3Real so let's dive into our latest tutorial where we cover Gas Tracker #API
Specifically, we will show you how to create a #Python Application locally that reads the #Gasprice on @BNBCHAIN https://t.co/HNhfeLTNoB
Prerequisites:
Knowledge of Python, Python Text Editor
The codes application are as follows:
#read gas price from https://api.nodereal.io/node/gasInfo
1. import requests
2. import matplotlib.pyplot as plt
3. def getGasPrice():
4. url = "https://api.nodereal.io/node/gasInfo"
5. response = requests.get(url)
6. data = response.json()
7. return data
8.
9. gas_data = getGasPrice()
10. print(gas_data)
11.
12. # Extract gas prices
13. rapid_gas_price = gas_data["data"]["rapid"]
14. fast_gas_price = gas_data["data"]["fast"]
15. standard_gas_price = gas_data["data"]["standard"]
16.
17.
18. # Create bar chart
19. labels = ['Rapid', 'Fast', 'Standard']
20. prices = [rapid_gas_price, fast_gas_price, standard_gas_price]
21. plt.bar(labels, prices)
22. plt.ylabel('Gwei')
23. plt.title('Gas Prices')
24. plt.show()
Codes explanation:
Line 1 to 2
These lines import the necessary Python libraries. requests is a library for making HTTP requests and matplotlib is a library for creating visualizations.
Line 3 to 7
This function sends an HTTP GET request to the specified URL (https://api.nodereal.io/node/gasInfo) and retrieves gas price data in JSON format. The function then returns this data.
Line 9 to 10
These lines import the necessary Python libraries. requests is a library for making HTTP requests and matplotlib is a library for creating visualizations.
Line 12 to 15
These lines of code extract the gas prices from the gas_data object and assign them to three separate variables: rapid_gas_price, fast_gas_price, and standard_gas_price.
Line 18 to 24
These lines of code create a bar chart using the extracted gas price data. The labels variable contains the labels for the x-axis, and the prices variable contains the corresponding gas prices for each label. The plt.bar() function creates the bar chart using these values. The plt.ylabel() and plt.title() functions set the y-axis label and chart title, respectively. Finally, plt.show() displays the chart.
Congratulations! Now you know how to create a Python application locally to query Gas Prices on BNB Chain. Stay tuned for more tutorials!
About NodeReal
NodeReal is a one-stop blockchain infrastructure and service provider that embraces the high-speed blockchain era and empowers developers by “Make your Web3 Real”. We provide scalable, reliable, and efficient blockchain solutions for everyone, aiming to support the adoption, growth, and long-term success of the Web3 ecosystem.
Join Our Community
Join our community to learn more about NodeReal and stay up to date!