Resources

Web3

Web2 is also known as the “social web” or the “participatory web” and refers to websites focused on user generated content. The term is used to describe the change from a past with static websites and the “old web” to a newer more interactive version of the internet that we are used to today. Web3 refers to the contemporary (future) change from Web2 to dApps and an infrastructure run on the blockchain that are open source, trustless and permissionless. This implies that Web3 applications are free from censorship, downtime and the requirement of personal data. Some current disadvantages with dApps are scalability and transaction speed, user interface and cost.

However, Web3.py is also a Python library for interacting with the Ethereum blockchain. The Web3.py package is used in dApp development to, e.g., read block data and send transactions. Web3.py use “providers” to connect to a node to get and send information to the Ethereum network. From the Web3.py docs there are three ways to connect to a node,

Further, if you want to run a local node, e.g.,

It is common to use a Node-as-a-Service provider that you can connect Web3.py to, popular alternatives are alchemy and infura.

However, if you want a development environment Web3.py also include the EthereumTesterProvider, linking Web3.py to a simulated node. The simulated node has some initial accounts with some initial balance, but no transactions has yet taken place and only an initial genesis block 0 is present.

Python

Install the necessary Python libraries,

pipenv install web3
pipenv install 'web3[tester]'

Test out Web3.py together with the simulated node,

# import packages
from web3 import Web3

# connect to simulated node
w3 = Web3(Web3.EthereumTesterProvider())
Web3.isConnected(w3)

# preloaded list of accounts
print(w3.eth.accounts)
a0 = w3.eth.accounts[0]
a1 = w3.eth.accounts[1]
b0 = w3.eth.get_balance(a0)
print(w3.fromWei(b1, 'ether'))

# genesis block data
block_0 = w3.eth.get_block('latest')
print(block_0)

# send a transactions that will be immidiately mined in the simulation
tx_hash = w3.eth.send_transaction({
   'from': a0,
   'to': a1,
   'value': w3.toWei(3, 'ether')
})

# view transaction
t0 = w3.eth.get_transaction(tx_hash)
print(t0['gas'])
print(t0['gasPrice'])

# view transaction receipt
r0 = w3.eth.get_transaction_receipt(tx_hash)
print(r0)