Coder Social home page Coder Social logo

soos3d / web3.py-estimate-gas-fees Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 27 KB

This script shows how you can estimate gas fees on an EVM blockchain, using a Chainstack node. You can use it to simply retrieve information to create an app like eth gas station, or to calculate how much the gas fee should be when sending a transaction using a bot written using web3.py.

Python 100.00%

web3.py-estimate-gas-fees's Introduction

Web3.py estimate gas fees

This script shows how you can estimate gas fees on an EVM blockchain, using a Chainstack node. You can use it to simply retrieve information to create an app like eth gas station, or to calculate how much the gas fee should be when sending a transaction using a bot written using web3.py.

You can also check my Skillshare class where I show the fundamentals about web3.py. WEB3.py: Interact with the Blockchain Get 30 days for free using this link! https://skl.sh/3McEymV

There are two scripts here:

  • estimate_gas.py --> Retrieve and display information: Gas limit, base gas fee, priority gas fee(standard 2 Gwei, modifiable by the user), calculate hom much ETH will be used in gas.

  • transaction_gas.py --> Uses a function to estimate how many Gwei are needed for sending ETH to another address, and shows it a built transaction as an example.

Note that much of the code is just to make it look better once printed on the console. Like converting the Wei values into Gwei and Ether.

What is Web3.py?

Web3.py is a Python library for interacting with the Ethereum network (Or other networks based on the EVM).

It’s commonly found in decentralized apps (dapps) to help with sending transactions, interacting with smart contracts, reading block data, and a variety of other use cases.

The original API was derived from the Web3.js Javascript API but has since evolved toward the needs and creature comforts of Python developers. (source: Web3.py docs.)

How do I use this program to estimate gas fees?

1 - The Web3.py library must be installed in your environment.

Run this code to install it in your enviroment:

pip install web3

Note that on Windows, you will need to install the Microsoft C++ Build Tools to make it work.

2 - Have access to an HTTPS endpoint that allows creating the connection to the EVM. For the connection, it is recommended to use the service provided by chainstack.com, where you can create your personal node on the cloud. You can register and create one node for free. This is the recommended option as not all the HTTPS endpoints that can be found online support the methods that can be used through Web3.py.

162478127-94cd2344-72f1-4136-a220-8b2c8e52d194

Insert the URL you get from your Chainstack node in the 'node_url' variable.

# Node endpoint
node_url = "CHAINSTACK_NODE_URL"

# Create the node connection
web3 = Web3(Web3.HTTPProvider(node_url))

# Verify if the connection is successful
if web3.isConnected():
    print("Connection Successful")
    print('-' * 50)
else:
    print("Connection Failed")

Customize the priority fee if you want. On Ethereum, 2 Gwei is generally the minimum required for your transaction to be picked by the miners. This is referred as 'miner tip' as well, you can increase it to incetivice them to pick up and process your transaction faster.

# set up the miner tip in wei
priority_fee = 2000000000       # 2 Gwei in wei
print('Priority fee: ' + str(web3.fromWei(priority_fee, 'gwei')) + ' Gwei')

Now, you can run the program and you will receive a result like this in the console:

Connection Successful
Latest block: 14934388
Gas limit: 21000
Base fee: 72.960051418 Gwei
Priority fee: 2 Gwei
Reasonable fee: 74.960051418 Gwei
------------------------------
Ether paid as gas fee: 0.001574161079778 ETH

web3.py-estimate-gas-fees's People

Contributors

soos3d avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.