An introduction to Web3 and dApps with Python

Introduction

There is a growing number of [decentralized] applications that are built and run on decentralized networks like Ethereum. Many of which are disrupting established business models in finance and banking. In this article, we will be exploring the key concepts of Web3, dApps and how to interact with the Ethereum blockchain.

In this post, you will get a basic understanding of Web3 and dApps. You will also learn how to interact with the Ethereum blockchain using the web3.py library.

Prerequisites

  • Basic knowledge of Python
  • Access to an Infura.io hosted node

What is Web3

Web3 or Web3.0 is the third iteration of the evolution of the internet or the web.

The term was originally coined by Tim Berners-Lee know as the inventor of the world wide web. It was initially meant to describe the Semantic Web, i.e. a web in which machines could process and analyse data on the web.

The term has morphed, at least in the context of this article, to describe a permission-less web where applications are built on a decentralized network and data is stored on a public database known as a blockchain.

  • Web 1 or the first iteration of the web - the majority of the websites were static/read-only. Users only interact with the web by consuming content (not especially relevant for this article).
  • Web 2 or the current version of the internet - websites are read and write, user can interact with the web by both reading and writing content for example leaving product reviews on a Shopify store or posting a tweet. Data is stored by / service is run by a central authority. Permission must be given before accessing the data or using the service. 
  • Web3 or the next version of the internet - decentralized apps run on a public database. Anyone can participate without sharing personal data.

What is a DApp and how does it relate to Web3.py

A decentralized application (DApp) is an application that is built on a decentralized network. Instead of its backend code running on a centralized server, it runs on a decentralized peer-to-peer network like Ethereum.

The Ethereum peer-to-peer network is made up of individual nodes or clients, each of which runs an implementation of Ethereum that verifies transactions in each block. These nodes are the blockchain.

Web3.py is a Python library for connecting to Ethereum nodes. By connecting your application to an Ethereum node the application is able to read data from the blockchain and broadcast new transactions to the network. There are alternative libraries written in other languages that achieve the same outcome e.g. Web3.js a Javascript implementation of the same library.

Web3.py in practice

In practice you will use the library to perform some common tasks such as getting the latest block, looking up transactions or checking the balance of an account. More advanced use cases also include interacting with existing contracts or deploying new contacts.

Note that you will not write your smart contracts in Python, you will most likely use a smart contract language such as Vyper (great for Python devs), Solidity (great for beginners, big developer community etc.) Yul and Yul+ (recommended for more experienced devs) to write your smart contract. Deployment requires that they already be compiled. This can be done within Remix or any other contract development framework.

Example: Using Web3.py to interact with a node on the Ethereum blockchain

In this section, we will install web3.py and interact with an Ethereum node.

The Ethereum protocol defines a way for people to interact with smart contracts and each other over the Ethereum network. In order to obtain this information, the protocol requires a connection to nodes on the network.

A key decision is whether to use a local vs a hosted node. Local nodes require less trust than hosted ones and should be used if the node should manage keys. However, they use up significant bandwidth, disk space and computation as the machine is individually verifying all transactions on the network to provide the latest state.

Setting up a node

For this example, we will use a hosted node provided by Infura.io. Head over to Infura, sign up and create a new project. On the project dashboard under settings, take note of your project ID.

Install Web3.py

Create a new virtual env and install web3

$ mkdir crypto_demo
$ cd crypto_demo
$ python3 -m venv venv
$ source venv/bin/activate

(venv) $ pip install web3

Notes

  • python3 -m venv venv - create a new virtual environment
  • source venv/bin/activate - activate your virtual environment
  • pip install web3 - install web3

Connect to your node

Connect to your ethereum node.

export WEB3_INFURA_PROJECT_ID=XXXXXXXX

# start a new interactive python shell

(venv) $ python3

# inside the shell

Python 3.9.7 (default, Sep  3 2021, 12:37:55) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> from web3.auto.infura import w3
>>> w3.eth.block_number
13335882

Notes

  • export WEB3_INFURA_PROJECT_ID=XXXXXXXX - set your project id as an environment variable
  • python3 - starts a new Python interactive shell
  • from web3.auto.infura import w3 - import the Web3 class, connects to the node using a provider and automatically assign it to the variable w3. Behind the scenes 
  • w3.eth.block_number - get the block number

Interacting with the Ethereum blockchain

Next, we will interact with the Ethereum blockchain by getting the latest information about the latest block.

>>> w3.eth.get_block('latest')

AttributeDict({
  'baseFeePerGas': 118640797130, 
  'difficulty': 9397575926741768, 
  'extraData': HexBytes('0x535a4c322f4254432e636f6d2f45a2c159001931d2'),
  'gasLimit': 29970705, 
  'gasUsed': 1630850, 
  'hash': HexBytes('0x97bbff11054c1974f0a4dfab55de3fd97858fb69ee6b62f08fb083b691d13f45'), 
  'logsBloom': HexBytes('0x0104000260000000002200030000000000000000000000000000000080080800300800000000020000004100400001010200800008002000000000008020000400800000000001080800001800004000010000000000400400480000000100000801000002000000000010008000088000000020600202000200009000080800400000000000010000000001000200040000000001040000010100800010002002c200000080200020020080800000000011000000000000002002021400000000000002000000002000100000000000000000000030002000000200009020008810a0000a020000020018000000000000014000200000000000000400401001'), 
  'miner': '0xEEa5B82B61424dF8020f5feDD81767f2d0D25Bfb', 
  'mixHash': HexBytes('0xc664d10f684cb10cb448b73282747636226a7525636170995417d294a5ebd9f9'), 
  'nonce': HexBytes('0x88a8b3cc5515f42f'), 
  'number': 13335906, 
  'parentHash': HexBytes('0x7f725b0b31dcc0f201047c4e06ed12421d4fde45618b0a2c0194d22868238454'), 
  'receiptsRoot': HexBytes('0x31074fd67984f3d446aea6c616e0cacf38147f4a11881a8884a7245a086b5249'), 
  'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'), 
  'size': 4859, 
  'stateRoot': HexBytes('0xb4224357fe56fc0100d92133e057ed48c0b003517cc69d01ae1a7a1b1dcc22e2'), 
  'timestamp': 1633124131, 'totalDifficulty': 31634469211456738395019, 
  'transactions': [HexBytes('0x5d9acc3d7b4fc85e61f7f229a65e6dfa877b042be8784e9f8e6d5db8cda1b35b'), HexBytes('0x8607706f523be5a80d478f294bcb25ef2c3609f8ce9c487d0920ffa3a6b1894e'), HexBytes('0x516fd357b5a4ac45e6344c53b439d926725d94cc992be7371c996297b3d75eb6'), HexBytes('0x4825cc8b4412ad45de266e9f9ec2045dd2cc67edc420ca42fc2dd411d2ef4455'), HexBytes('0x33336ae3765308bc4ffa663bb8b9d2eb9fe20bfb89403d26c84bd5b2495f01ba'), HexBytes('0x3f1a81678c180bd242ac261d9eef3b797668bb1a0592d45bf0a78f9bd988a3d9'), HexBytes('0x1628e8c8213d17711fffd00a47116de6e7ceefe009e7c61b4e46910d244b13a1')], 
  'transactionsRoot': HexBytes('0xaf9ec989b5931fb1d1dbdf3542216ba7c61480179952d2a3738c4cc0c3db014d'), 
  'uncles': []
})

Notes

  • w3.eth.get_block('latest') - Retrieve information on the latest blcl

Conclusion

In this article, we introduced some key concepts on DApps, Web3 and learnt how to set up a hosted node and interact with the Ethereum blockchain. In future articles, we will look at how to build basic Web3 DApps.

Subscribe for regular updates

Copyright © 2022 www.advantch.com