In this short tutorial, we will explain how to connect to the Anyblock JSON-RPC Node API Access and use it in Python with the help of web3.py to interact with the Ethereum network. Furthermore, first simple use-cases will be shown.

The Anyblock Node API grants access to both Bitcoin- and Ethereum-based networks like Litecoin, BSC, and xDai using a JSON-RPC API. It relieves you of running a custom node for each chain, as this is handled by Anyblock instead. And archive data, as well as Ethereum block and transaction traces, are also included. Get started for free now!

What does RPC stand for?

RPC stands for Remote Procedure Call. This means that one client executes a procedure or function on another system. In other words, it is a client-server interaction where the client tells the server the procedure to be executed, the server then executes the procedure and sends a response to the client. There are different protocols for RPCs. The Anyblock Node API is a JSON-RPC interface. This means that both the requests and responses are serialized and returned in (the) JSON format.

What is web3.py used for?

web3.py is a Python library that allows users to interact with the Ethereum blockchain. The library makes it possible to develop decentralized applications or to analyze the activities on the blockchain. Furthermore, web3.py enables us to interact with smart contracts and to send transactions. All this is possible because web3.py can connect to nodes (e.g. the ones operated by Anyblock Analytics as the basis for the JSON-RPC Node API).  These nodes are active on Ethereum and are responsible for storing the blockchain data. This allows us to query the network and for example get the transaction, block, or event data.

How to use web3.py with Anyblock JSON-RPC Node API

Step 1
The first step is to create an account with Anyblock Analytics or to log in to your existing one.

Anyblock account login interface

Image: Anyblock account login interface. Click to enlarge.

Step 2

After account creation or login, navigate to the RPC tab in your profile. There you will find all the RPC endpoints for the different networks expanded by your personal API key. In this example, we are working with the Ethereum Mainnet. Therefore, the following URL should be copied.

Anyblock Account RPC tab

Image: URL to copy under the Anyblock Account RPC tab. Click to enlarge.

If Python is not installed on your system yet, it is necessary to install it either directly via the command line or to download the installation file from the web. Subsequently, it is necessary to install the Python package manager pip. Best, open a command line and execute the following command:

python3 -m pip install --user --upgrade pip

Step 4

It is always recommended to create new projects in virtual environments. This way the packages can be managed separately, you can create an isolated Python installation and you do not influence the system Python interpreter. Using virtual environments requires the installation of the “virtualenv” package (for further documentation, visit this tutorial).

python3 -m pip install --user virtualenv
Step 5
Create virtual environment

Image: Create virtual environment command line. Click to enlarge.

Next, we have to create a virtual environment. Therefore we first have to create a new project directory in the user folder:

mkdir anyblock_rpc_example

After creating the directory, we need to change to this folder and create a virtual environment.

python3 -m venv env

Now the environment can be started: 

source env/bin/activate
To close the environment, simply execute the following command:
deactivate
Step 6

In the following step, we install the web3.py package with pip. This allows it to be called and used in Python applications and scripts.

pip install web3
Step 7

Now we are ready to use the Anyblock JSON-RPC Node API with web3.py. For this purpose, it is necessary to create a Python script in your text or code editor of choice. We use Vim.

vim rpc_example.py
Then insert the following source code replacing YOUR-API-KEY-HERE with the API key from your account page:
from web3 import web3

web3 = Web3(Web3.HTTPProvider('https://api.anyblock.tools/ethereum/ethereum/mainnet/rpc/YOUR-API-KEY-HERE/'))
print("Client Connected?: ", web3.isConnected())
Next, we import the web3.py package and then create the client configured in the web3 variable. The Web3.HTTPProvider method is used to initialize a connection to an RPC-Provider with the JSON-RPC protocol.
The last line shows whether we have successfully connected to the RPC provider, or not. If so, True is output here, otherwise False. We now have successfully connected to the Anyblock JSON-RPC Node API.

To run the script in the command line, change to its directory and type:

./Python3 rpc_example.py
Anyblock RPC example

Image: Connecting to the Anyblock JSON-RPC Node API. Click to enlarge.

Following, we will show you some features of web3.py and RPCs. Feel free to check out the web3.py documentation for more examples.

Examples

Information about the latest block, or any other, can be obtained with method web3.eth.get_block(). The output is presented in a Python dictionary (JSON).

# Latest Block 
print("Information about the latest Block: " , web3.eth.get_block('latest'))
Transaction information can be obtained with method web3.eth.get_transaction():
# Transaction Information
print("Information about Transaction : " , web3.eth.get_transaction(“https://etherscan.io/tx/0x111eab00a47cfdba2271e65b04522543fd1cb97fcc660a85cc96781a425b443f”))
Transaction information

Image: Transaction information. Click to enlarge.

Let’s look at a more detailed example. This is about the POAP token. In this example, we will interact with a smart contract.

First, the ABI (Application Binary Interface) of the POAP token is retrieved using the etherscan API and stored in a variable.

Then a contract instance is created using method web3.eth.contract(address= POAP_CONTRACT, abi = ABI).

Finally, one of the ERC-721 functions is called for the POAP contract instance. With the help of function “tokenURI” and the passing of a token ID, the URI can be found out as well as where the metadata of this particular NFT is stored.

Again here, replace YOUR-API-KEY-HERE with the API key from your account page.

from web3 import Web3
import urllib.request
import json

web3 = Web3(Web3.HTTPProvider('https://api.anyblock.tools/ethereum/ethereum/mainnet/rpc/YOUR_API_KEY'))
print("Client Connected?: ", web3.isConnected())

# load the ABI
response = urllib.request.urlopen("http://api.etherscan.io/api?module=contract&action=getabi&address=0x96d0ec5abb022ebf3a269344b2593ed909a77948&format=raw")
ABI = json.loads(response.read()) # Loading the Application Binary Interface
# POAP Contract which has tokenURI Method
POAP_CONTRACT = "0x22C1f6050E56d2876009903609a2cC3fEf83B415"

tokenInst = web3.eth.contract(address=POAP_CONTRACT, 
                              abi=ABI)

print("Token Metadata for Token 437283: " , tokenInst.functions.tokenURI(437283).call()) # Calling the defined method
Poap token metadata

Image: Retrieving POAP token metadata. Click to enlarge.

We hope that this short tutorial could help you to use the Anyblock JSON-RPC Node API with Python and web3.py. Of course, there are countless other examples of how to use web3.py. For those, it is worth having a look at the web3.py documentation.

Interested or questions?

 

Freddy Zwanzger
(Co-Founder & Chief Data Officer)
freddy@anyblockanalytics.com
+49 6131 3272372

    

Looking to create an Anyblock account? Takes seconds and it’s free!

>> You can also browse related posts below or go back to all posts.

Pin It on Pinterest