Gas is the fee that is required to process a transaction on the Ethereum blockchain. The price of gas fluctuates based on the amount of computation your transaction requires and transaction demand at the time of submission.
If the gas that you pay in your transaction is too low you will longer for the transaction to to confirm. If the gas that you pay in your transaction is too high you over paid. It is important not to overlook gas as the network might be congested which drives up prices.
Gas prices can be obtained from the ETH Gas Station website, Web3.py, and other sources. The Python code below prints out ETH gas prices from:
- The ETH Gas Station website using json. It print prices from slow to fast.
- Web3.py using eth.gasPrice
One can use this to compare calculated gas prices between the two sources. In addition you can use this in your code to calculate the price of gas before submitting a transaction to the Ethereum blockchain.
from web3 import Web3
import json
import requests
# https://ethgasstation.info/
infura_url = 'INSERTYOURINFRAURLHERE'
web3 = Web3(Web3.HTTPProvider(infura_url))
req = requests.get('https://ethgasstation.info/json/ethgasAPI.json')
t = json.loads(req.content)
print('safeLow', t['safeLow'])
print('average', t['average'])
print('fast', t['fast'])
print('fastest', t['fastest'])
#web3.eth.Eth.generateGasPrice`
gas_price1 = web3.eth.gasPrice
print(gas_price1/10**8)