web3.py: Connect to Ethereum network with Web3 in python
I will never forget the year 2021 because it was the year that companies were literally throwing money at all developers. But the ones getting more money thrown than they could handle were blockchain and web3 developers.
In 2021 Money was cheap and so venture capital firms could borrow money at 0.5% and invest in a bunch of tech startups. Who in turn would throw money at a bunch of developers in hope of making the next big start-up (a lot of those in the web3 space).
The issue for me was I wouldn’t know which abstraction level to start learning at. The levels range from something as intricate as the math behind cryptography to something as simple as how connect to the binance api. In this blog, I’m going to cover what I wish I knew, which is how easy it can be to connect to the ethereum network with a package like web3.
Before we dive into the code, let’s understand what the web3 package does under the hood. We all know the basics of blockchains and how we have all of these individual nodes validating transactions.
In short, web3.py is a human-friendly wrapper around a bunch of slightly more confusing API calls to an ethereum node. It makes communicating with the ethereum network a lot easier by abstracting away many of the complexities.
To use web3 you need a connection to an ethereum node. The most common way to do this is via a provider like infura.io which will get you an endpoint to make requests to receive data about the blockchain. Infura do all the hard work of running the ethereum nodes for you in the background and you can simply call an endpoint.
If you don't have an ethereum node connection yet you can use the EthereumTesterProvider provided by web3.py.
Let’s start off by making a simple connection to the ethereum network. We will do this by importing the package and creating a web3 client which takes in our infura URL like so:
from web3 import Web3
We can then check we are connected by running the below and making sure it returns True:
w3.is_connected()
We are now connected to the ethereum network, so we can do some cool things like check the latest block:
w3.eth.get_block(’latest’)
This shows us all the latest information on the latest block, including the wallet address of the miner. We can then be super nosey and see how much money this miner has earned from mining as everything in the blockchain is completely public!
But you guys aren’t here to just be nosey you want to see some real use cases so let’s explore some with a focus on trading bots. So the most obvious one is we will want to be checking our portfolio value regularly for risk management purposes. So we can check our balance at any time with:
w3.eth.get_balance(’<your-wallet-address’)
This will return your balance. If its larger than you expected, don’t panic, this is because the ethereum network uses wei instead of eth. Think of this like using cents instead of dollars or pence instead of pounds. Which leads us onto our next useful web3 function, converting values to and from wei. Because if I want to trade 1 eth worth of a specific altcoin, I’d need to know what 1 eth is in wei to make that trade. We do this with:
w3.from_wei()
One of the most important methods you will use in web3 package when trading cryptocurrencies will be the method to get the transaction receipt of a trade. This will provide us all the information we need to know about our trades including how much we paid in fees. This is all crucial data we need to be storing and can be done with the method below:
w3.eth.wait_for_transaction_receipt(<your-txn-hash>)
Did you find this article helpful?