How To Use Python In Web3

How To Use Python In Web3

ยท

7 min read

One of the characteristics of Man is "growth", this means that as long as man continues to exist, he'll continue evolving. This evolution also affects the mode of his operations.

Hey! Don't stray too far, I'm not dwelling on science today, maybe some other time.

Talking about the mode of man's operation, technology is one of the greatest (if not the greatest) development man has ever made. The Internet as the epicenter of technology also follows the path of evolution. You'd agree with me that there's a wide gap between the functions of the early internet and what we currently have.

The Web is an aspect of the internet that has in itself undergone changes and is still undergoing reforms too. That brings us to the reason for this article. With the whole buzz around Web3, beginners in tech could get confused or carried away unless there's someone to do some clarification. Don't mention, I'm here for you.๐Ÿค—

This article will:

  • Explain the concept of Web3
  • Demystify what you "heard" about Web3
  • Detail the link between Web2 & Web3
  • Show you how to flow in Web3 as a Python Developer

Before going into the aforementioned, permit me to digress briefly into the evolution of the Web.

Evolution of the Web

image.png

Web -- a short form of Word Wide Website, is a space where information is stored for easy access and sharing using the internet. As an individual or company, owning a website is like having a house on a street where people can easily locate you. Your website is the house, the domain is street while the world wide web is the entire estate.

The Web has evolved from Web 1.0 to the current Web3.0(although rumor has that Web5.0 is in the pipeline ). These phases of evolution in the Web were not necessarily named until recently.

Each phase has its peculiarity such that the difference between all of them is noticeable. Taking a look:

Web 1.0: This has to do with static websites i.e pure HTML or texts being displayed on the web browser.

Web 2.0: Seeing that Web1.0 has little or no interaction with the user, Web2.0 sprung out and has been here for quite a while. It is also still in use by more people than those in Web3. Web 2.0 brought about social media, made it possible for users to upload and download content anytime. It also improved the technology of accessing data from different places and devices. I could go on and on about the benefits of Web2.0 but like every other thing, it had it down sides with Data-Privacy taking the lead. This is where Web 3.0 comes in.

Web 3.0: There's a lot of technical terms associated with Web3.0 such as Blockchain, cryptocurrency, smart contract, NFT etc. One fact is this, Web3.0 is aimed at data structuring, privacy, decentralization and a lot more. This is why it could mean different things to different sectors.

Web3 in Brief

Web3 (a short form of Web3.0) is an upgraded form of the World Wide Web such that it is based on Blockchain Technology. Unlike Web2 that stores information in data centers known as servers, Web3 stores information in virtual wallets.

Web3 is aimed at taking off the middleman in communication and saving data in a decentralized network known as ledger. While the middleman is taken away, easy access is set between the buyer and the seller, the creator and the user, etc. For instance, peer-to-peer payment uses the Blockchain Technology.

"Web3 is the Free Web"

Although seen as the future of the world wide web, Web3 has its downsides in terms of control. If everyone has free access to the web without some sort of checks as done by the "middleman*, then everything is as good as being bare. We would go into that now.

Some Technologies in Web3

Due to the buzz around Web3, software developers and indeed programmers generally have begun to divert into building solutions that would work in Web3.

Who doesn't want to hit it big off a new technology?

image.png

The advent of Web3 and Blockchain technology brings about the need for Software Applications that can run on this technology. Some of these applications are:

  • Cryptocurrency
  • NFT(Non-Fungible Tokens)
  • Smart Contracts
  • Decentralized Apps

Common Programming Languages used in Web3:

Different programs and software applications developed in Web2 are done using various Programming languages, the same is also applicable in Web3. Although still emerging, building technologies for Web3 such as Blockchain, smart Contracts, NFTs, etc involves the use of programming languages such as:

  • Solidity
  • Rust
  • JavaScript
  • Python
  • C++

The most popularly used of this list is Solidity but a closer look at the list shows that some of these languages are also used to build technologies in Web2. So, you really do not need to start from scratch, rather find out how your chosen language works with Web3.

Use of Python in Web3

Python is the starting point for most programmers, a lot of developers started their coding journey from using the popular print() statement of python. Python is open source with rich libraries for various purposes and currently ranked as the most-in-demand coding language for 2022 according to a US Survey.

Python can be classified as Object Oriented Programming with a variety of applications; such as Web development, Data Science, Back End Development etc. Going further with the list would mean derailing from our point of discourse. Being a multifunctional language, Python also has its place in Web3. You're surprised right?

As a Developer who is current with trends, you may have been nursing the thought of becoming a Web3 Developer but the fear of learning a new language is stopping you. Understanding the semantics and syntax of a new language can be very tiring sometimes and I know how it feels.

Now, you can stick to Python and brush up your skills for Web3 instead. When building Web3 technologies such as Blockchain, some developers prefer to use varieties of languages in one project. This is why Python is used the most in development of Blockchain applications. Some applications built using Python include LUNA,NEO, Hyper-ledger etc.

The most popular python library used for Web3 is known as Web3.py.

Peep into Web3.py

Ethereum is an example of a smart contract in Blockchain technology. Just like there are hundreds of libraries in python performing different operations,Web3.py is a python library for interacting with Ethereum. It was derived from the Web3.js API.

To interact with Ethereum using Web3.py, you need to follow these steps:

  • Install it in your python environment by running:

$ pip install web3 on your terminal

  • Get the following by using the required import statements :

    Test Providers: This provider is responsible for auto-mining each client transaction into a block. It is imported by running the following lines of code in your Python Interpreter or code editor:

>>> from web3 import Web3, EthereumTesterProvider
>>> w3 = Web3(EthereumTesterProvider())
>>> w3.isConnected()
True

Local Providers: The local provider serves the HTTP and web-socket requests. It is responsible for the connection between the local node and the Ethereum client. It can be imported into your Python Interpreter or code editor

>>> from web3 import Web3

# IPCProvider:
>>> w3 = Web3(Web3.IPCProvider('./path/to/geth.ipc'))

# HTTPProvider:
>>> w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))

# WebsocketProvider:
>>> w3 = Web3(Web3.WebsocketProvider('wss://127.0.0.1:8546'))

>>> w3.isConnected()
True

Remote Providers: If you want to use nodes not available on your local machine, then remote Providers are your best options. Remote Providers such as Alchemy helps you interact with the Ethereum Blockchain ad long as you specify the end point.

>>> from web3 import Web3

>>> w3 = Web3(Web3.HTTPProvider('https://<your-provider-url>'))

>>> w3 = Web3(Web3.WebsocketProvider('wss://<your-provider-url>'))
  • Get the info of the Blockchain in question and configure it in the web3.py environment.
>>> w3.eth.get_block('latest')
{'difficulty': 1,
 'gasLimit': 6283185,
 'gasUsed': 0,
 'hash': HexBytes('0x53b983fe73e16f6ed8178f6c0e0b91f23dc9dad4cb30d0831f178291ffeb8750'),
 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'miner': '0x0000000000000000000000000000000000000000',
 'mixHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
 'nonce': HexBytes('0x0000000000000000'),
 'number': 0,
 'parentHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
 'proofOfAuthorityData': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000dddc391ab2bf6701c74d0c8698c2e13355b2e4150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'receiptsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'),
 'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
 'size': 622,
 'stateRoot': HexBytes('0x1f5e460eb84dc0606ab74189dbcfe617300549f8f4778c3c9081c119b5b5d1c1'),
 'timestamp': 0,
 'totalDifficulty': 1,
 'transactions': [],
 'transactionsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'),
 'uncles': []}

Following the above steps, you can now have access to the Ethereum contract created and also interact with it freely.

Conclusion

I know it's been quite a long journey coming, congratulations for reading this far. I'm glad you are now aware of the roles that python can play in Web3. I'm also hopeful that you'd make more research on getting started with Web3.py.

In subsequent articles, I hope to explain in detail how Web3.py works, until then don't stop learning and practicing.