jackofcrypto

Posted on Dec 21, 2021Read on Mirror.xyz

Ethereum Transactions and Web3.py

By the end of this post, readers will be able to:

  • Articulate how nodes and the mempool play roles in adding transactions to the Ethereum blockchain.
  • Explain how gas fees help determine which transactions are selected for addition to the Ethereum blockchain.
  • Utilize Web3.py as a provider to access account addresses and balances, and convert ether into different denominations.
  • Use Web3.py to define the parameters required for an Ethereum transaction, including the sender, receiver, gas, and ether.

Adding Transactions to the Blockchain

Similar to Bitcoin, Ethereum has a blockchain that stores data in blocks. Ethereum blocks store data such as transactions and smart contracts. The part of the block that stores the transactions is an attribute known as the transactions attribute. On the Bitcoin blockchain, this attribute is called a record attribute.

Once the blockchain receives the transaction, the network holds the transaction in a waiting area inside the memory of the network's nodes. The waiting area is known as a mempool.

Mempool sequence

The diagram depicts the process of bundling transactions from the mempool into blocks on the chain.

After an Ethereum participant creates a transaction, it is sent to the network mempools where it waits to be picked up by a miner. A miner bundles transactions from their node’s mempool into a block and then mines the new block.

Once the block has been mined, it is sent across the rest of the network for validation; it is then added to the ledger.

Complete transaction cycle

Notice that this process includes the following steps for proof of work:

  • Blockchain participants create transactions.
  • Miners bundle transactions waiting in the mempools into new blocks.
  • Miners compete to complete proof of work and mine new blocks.
  • A successful miner sends the new block around the network for validation and receives a block reward.
  • The new block is linked to the blockchain.
  • All blockchain nodes update their copies of the ledger with the new block.
  • The transaction has been inalterably written onto the blockchain.

Keep in mind, Ethereum is currently a proof of work chain, but is transitioning to proof of stake in the upcoming ETH 2.0 merge.

Ethereum Transaction Fees

A transaction fee on any blockchain is an incentive given to people or miners to run the blockchain on their machines; otherwise, miners would have high energy costs and no return. Each blockchain network has its own rules for calculating these fees, and pricing can vary even within one network.

Often, transaction fees increase or decrease based on supply and demand. Computational power on the Ethereum network and space within each block are both limited, and demand for them fluctuates. During periods of high demand, the number of transactions sitting in an Ethereum node’s mempool may exceed the number of transactions that can fit into a single block. The node’s miner will decide which transactions to put in a new block and which to leave in the mempool until later. Miners will prioritize transactions that pay higher fees.

Ethereum's transaction fee is known as a gas fee. Gas fees are calculated by multiplying the units of gas required to validate the transaction by the price of each gas unit. You can find additional information about Ethereum gas in this helpful article: Ethereum Gas Explained

Introduction to Web3.py

What is Web3.py?

  • Web3.py is a library that allows us to talk to Ethereum nodes in Python.
  • Web3.py is a software development kit (SDK) like any other SDK you've used to talk to other APIs, but this time the API originates from an Ethereum node.
  • Web3.py can help you read block data, sign and send transactions, and deploy and interact with contracts.
  • Web3.py allows us to communicate with the blockchain and serves as a window for reading the ledger.

pip install web3 in your terminal or Git Bash.

In Jupyter Notebook - import Web3, then the EthereumTesterProvider module.

from web3 import Web3
from web3 import EthereumTesterProvider

The EthereumTesterProvider instance includes several pre-populated mock-blockchain participant accounts, each containing 1,000,000 ether. These mock accounts can be used to test transactions.

Web3.py provides Python functions for performing various blockchain operations, such as accessing information on the latest block added to the chain or viewing the balance of cryptocurrency in an account.

To get a clear representation of how much ether we have, we need to convert the integer representation of our account balance from wei to ether.

Denominations of Ether

Transactions with Web3.py

Once we are connected to a Web3 mock blockchain instance, we can use several Web3.py methods to create and send ether-based transactions from one account to another.

Call web3.eth.send_transaction and pass in a few parameters.

  • from: the 42-character Ethereum account address from which the transaction is sent.
  • to: the 42-character Ethereum account address to which the transaction is sent.
  • gas: an integer representing the maximum units of gas to be used in mining the transaction. A standard ether transfer requires at least 21,000 units of gas. Any unused gas will be returned to the sender.
  • value: an integer representing the amount of money, in wei, that this transaction will transfer from the sender to the recipient.
transaction_hash_code = w3.eth.send_transaction({
    'to': receiver,
    'from': sender,
    'gas': gas,
    'value': value
})

Until next time, here’s a twitter thread summary of this post:

https://twitter.com/jackofcrypto/status/1473409689517912065?s=20

Recommended Reading