Getting Started
Ethereum gas fees are probably something you’ve heard about, but might not know how to check for yourself. In Web3 development, one of the biggest hurdles you’ll run into is the Ethereum transaction costs, aka gas prices. Just like the price of gas for your car, the cost of Ethereum gas fluctuates from time to time. When utilizing an Ethereum network and making transactions, there’s an associated cost.
While Ether and Ethereum blockchain technology are versatile and can be used to build DeFi applications, smart contracts, and more, you still have to pay to do so. Like any business, there are associated costs that must be taken into account. Below, we’ve put together an overview of how to access Ethereum gas prices for your personal and professional convenience.
How to Build an Ethereum Gas Fee Tracker
File Setup
Mkdir Web3_Gas_Calculations
Cd Web3_Gas_Calculations
Touch gas_calc.py
Dependencies
Install Web3.py
pip install web3
Navigate to infura.io and connect to the web3 API. Get the URL Needed
Establish connection with Infura
Set your Environment Variable
Export INFURA_URL=<<YOUR_INFURA_URL>>
Connect to Infura
import os
from web3 import Web3
try:
web3 = Web3(Web3.HTTPProvider(INFURA_URL))
print("Connected to Eth Node (Infura): ", web3.isConnected())
except Exception as e:
print(e)
print("INFURA CONNECTION FAILED")
In the command line, execute:
python gas_calc.py
This will print if you are connected:
Connected to Eth Node (Infura): True
Calculations on the Latest Block
This will set block data equal to a dictionary of values related to the latest block.
block_data = web3.eth.getBlock('latest')
Get Current Gas Price, GasUsed, and Gas Limit
Assign these variables by calling the web3 library
currentGasPrice = web3.eth.gas_price
gasUsed = block_data.gasUsed
gasLimit = block_data.gasLimit
Create Functions to calculate base fee per gas, burnt fees, gas used percentage, and gas target percentage
Add these functions to your file in the order they appear. Burnt fees require a base fee per gas for the calculation.
base_fee_per_gas
Next, create a function that calculates the base fee per gas. Using fromWei, we convert the base fee to ether. Base fee was not always listed within a block. It will not always be available.
def get_base_fee_per_gas():
global base_fee_per_gas
try:
base_fee_per_gas = web3.fromWei(block_data.baseFeePerGas, "ether")
except AttributeError:
print('BASE FEE NOT AVAILABLE')
base_fee_per_gas = None
return base_fee_per_gas
burnt_fees
def get_burnt_fees():
global burnt_fees
try:
burnt_fees = get_base_fee_per_gas() * block_data.gasUsed
except:
burnt_fees = str(0)
return burnt_fees
gas_used_percentage
def get_gas_used_percentage():
global gasUsedPercentage
try:
gasUsedPercentage = (block_data.gasUsed / gasLimit) * 100
if gasUsedPercentage > 0:
gasUsedPercentage
else:
gasUsedPercentage = 0
except:
gasUsedPercentage = 0
print("GAS USED PERCENTAGE ERROR")
return gasUsedPercentage
gas_target_percentage
def get_gas_target_percentage():
global gasTargetPercentage
try:
get_gas_used_percentage()
if gasUsedPercentage < 50:
gasTargetPercentage = -100 + 2 * gasUsedPercentage
else:
gasTargetPercentage = 100 - 2 * (100 - gasUsedPercentage)
except:
print("ERROR GAS TARGET PERCENTAGE")
return gasTargetPercentage
With all these variables and functions created, we can print out a simple report from the script.
print("---------------------------")
print("Current Gas Price: " + str(currentGasPrice))
print("-------Block Gas Data-------")
print("Gas Used: ", gasUsed)
print("Gas Limit: ", gasLimit)
print("Base Fee Per Gas: ", get_base_fee_per_gas())
print("Burnt Fee: ", get_burnt_fees())
print("Gas Used Percentage: ", get_gas_used_percentage())
print("Gas Target Percentage: ",
get_gas_target_percentage())
This data can be used within any application you build. It took some time to write all this code, and it seems like there should be an easier way. Astra Block is the solution to that problem.
Connected to Eth Node (Infura): True
---------------------------
Current Gas Price: 32264456935
-------Block Gas Data-------
Gas Used: 14136173
Gas Limit: 30000000
Base Fee Per Gas: 3.1914456935E-8
Burnt Fee: 0.451148284434209755
Gas Used Percentage: 47.12057666666667
Gas Target Percentage: -5.758846666666656
Next Steps
This guide to accessing Ethereum gas fees using Infura is built around necessity. While easier than some other methods and boasting the core advantage of not having to maintain a node, it could always be easier.
Datastax has an easier solution for you: Astra Block. With the entire Ethereum blockchain indexed and at your disposal, building the Web3 application of your dreams has never been easier. Access Ethereum gas prices with incredible ease.Utilize the exact data you need at a low cost without IPFS nodes or complex RPC calls. With Astra Block, Web3 development has never been easier.