Exploring Web3.py: A Guide for Python Developers on XDC Network

ruslan wing
6 min readApr 7, 2023

--

In this Tutorial, we will cover how to get started using the Web3.py library. If you’re a Python developer, Web3.py is your go-to library for interacting with The XDC Network Blockchain. Today I’ll show you how to use Web3.py to talk to the XDC Network blockchain.

Table of Contents

  • What is Web3.py?
  • Checking Account Balance
  • Sending XDC Transactions

What is Web3.py?

Web3.py enables you to fulfill the second responsibility: developing clients interacting with The XDC Network Blockchain. And not necessarily “clients” like user-facing applications (web applications, for example), but “clients” that transact with the blockchain by reading information from it, writing new transaction data to it, or executing business logic with smart contracts.

Since you’re writing Python, this “client” might be a script that scrapes blockchain data or a server process that executes a smart contract function, for example. Web3.py is a library collection that enables you to do these things: create XDC Network transactions, read and write data from smart contracts, create smart contracts, and so much more!

Web3.py talks to The XDC Network Blockchain with JSON RPC, which stands for “Remote Procedure Call” protocol. XDC Network is a peer-to-peer network of nodes that distributes all its data across each masternode in the network. In other words, each masternode on the network gets a copy of all the code and the data on the network. Web3.py allows us to request an individual XDC network Full node on behalf of the entire network with JSON RPC. This will allow us to read and write data to the network through a single node. It’s kind of like making HTTP requests to a JSON API on a web server.

Dependencies

First, you’ll want to ensure you have some Python version installed. I’m using version 3.6.7 in this series. Next, you’ll want to use an environment manager like venv, which ships with Python, or pyenv, which you can download here.

You can see if you have Python already installed on your machine by going to your terminal and typing:

$ python — version

Or

$ python3 — version

Make sure you have some version of Python 3 installed before continuing. If you have multiple Python versions installed and are forced to use Python3 to access Python version 3, please use Python3 instead of Python for all Python-specific terminal commands in this series.

Next, you’ll want to create a virtual environment with venv or pyenv to install Python dependencies.

Web3.py Library

You can install the Web3.py library with pip in your terminal like this:

$ pip install web3

Checking Account Balances

Now that all of your dependencies are installed, you can start developing with Web3.py! First, you should fire up the Python shell in your terminal like this:

$ python3
from web3 import Web3

Now you can access a variable to create a new Web3 connection! Before we generate a Web3 connection, we must first assign the Public RPC

web3 = Web3(Web3.HTTPProvider(“https://erpc.xinfin.network"))

Now you have a live Web3 connection that will allow you to talk to the XDC Network main net! We can ensure that we’re connected like this:

print(web3.isConnected())

This will return true if the connection is live. Next, we can check the latest block number like this:

print(web3.eth.blockNumber)

There’s the latest block number! Now let’s check the account balance for this account: 0xFc515EA2899bC4Ce74A69b59Ef49935C069f0dF0. We can see how much XDC this account holds by checking its balance

First, let’s assign the address to a variable:

account = “0xFc515EA2899bC4Ce74A69b59Ef49935C069f0dF0”

Now, let’s check the account balance like this:

balance = web3.eth.getBalance(“0xFc515EA2899bC4Ce74A69b59Ef49935C069f0dF0”)

Now we will use the print command and check the balance and see the number of XDC tokens holding by the particular address

print(web3.fromWei(balance, “ether”))

And that’s it! That’s the conclusion to the first part of this tutorial. Now you’ve seen what the Web3.py library is and you can get started using it to check XDC account balances.

Web3.py documentation will give you a thorough overview of what you can do with the library. I highly recommend browsing through it, even if you need help understanding every aspect of its functionality. You can find the full documentation for Web3.py here

https://web3py.readthedocs.io/en/stable/

Sending XDC Network Transactions

In addition to learning Web3.py, the purpose of this lesson is to help you understand the fundamentals of how transactions work on The XDC Network Blockchain. Whenever you create a transaction, you write data to the blockchain and update its state. Several ways exist, like sending XDC from one account to another, calling a smart contract function that writes data, and deploying a smart contract to the blockchain. We can get a greater understanding of these concepts by performing these actions with the Web3.py library and observing how each step works.

You will have to create two accounts on the XDC Network and make sure you have the private key of both the account

account_1 = ‘’ # Fill me in
account_2 = ‘’ # Fill me in

Now you will have put the private key of the account_1 to do the transaction

private_key = ‘’ # Fill me in

Now let’s build the XDC Network transaction that sends XDC, XDC cryptocurrency, form account_1 to account_2. We can build the transaction dictionary that will get broadcast to the blockchain like this:

nonce = web3.eth.getTransactionCount(account_1)
tx = {
‘nonce’: nonce,
‘to’: account_2,
‘value’: web3.toWei(0.1, ‘ether’),
‘gas’: 2000000,
‘gasPrice’: web3.toWei(‘50’, ‘gwei’),
}

Let me explain this code:

  • First, we get the fetch the account nonce, or transaction count. This is a required transaction field because it prevents the double-spend problem.
  • Next, we build the transaction object and fill in the nonce first.
  • Then we specify the account we’re sending XDC to account_2
  • Then we specify the gas limit of 2000000
  • Finally, we specify the gas price of 50 Gwei

Now we’ll sign the transaction with our private key to tell the network that account_1 is sending the XDC like this:

signed_tx = web3.eth.account.signTransaction(tx, private_key)

Finally, we’ll send the transaction to the XDC Network like this:

tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(web3.toHex(tx_hash))

This sends the raw transaction to the network and return a transaction hash. We can convert the transaction hash to hex and print it to the terminal with print(web3.toHex(tx_hash)).

Here is the TX hash for the transaction 0xbf4e7fb6e05ea61efe9b2b0a0da391cc2f73749d34936c2abef9a11bf70fc1ac you can also check it on https://explorer.xinfin.network/txs/0xbf4e7fb6e05ea61efe9b2b0a0da391cc2f73749d34936c2abef9a11bf70fc1ac#overview

Yay! You’ve successfully sent XDC from one account to another with Python!

Important tools on XDC Network

  • XDCPay:- The XDCPay is an extension for accessing XDC’s XDPoS-enabled distributed applications or “Dapps” in your browser! The extension injects the XDC’s XDPoS web3 API into every website’s javascript context so that dapps can read from the blockchain.
  • XinFin Remix:- XDC Network Remix, previously known as Browser Solidity, is a web browser-based IDE that allows you to write smart solidity contracts, then deploy and run the smart contract. The advantage of running XDC Network Remix from your local computer is that it can communicate with an XDC Network node client on your local machine via the XDC Network API. You can then execute your smart contracts in XDC Network Remix while connected to your local development blockchain, the TestNet blockchain, or the MainNet blockchain.

For more details, check out the detailed page on XDC Network tools and documents at https://www.xinfin.org/xdc-chain-network-tools-and-documents

If you have any questions, please free to post them on https://xdc.dev

--

--