Sending Transactions With Web3py

Web3.py is a library for interacting with the Ethereum network in Python. Interactions made on a blockchain network generally involve making transactions between addresses.

A blockchain is a data structure made of interconnected blocks. Each block on a blockchain contains data that cannot be modified. The blockchain is updated by adding blocks to the end of the chain. The data in a block contains information about a transaction made on the chain.

In this article, you will learn how you can make transactions on the Ethereum network with Web3.py. There are two types of accounts you’ll learn how to transfer tokens from:

  • Locked account, which requires your private key to operate with them.
  • Unlocked accounts, which do not require your private key to access them

Prerequisites

To fully understand this article, you need to know the following:

  • the Web3.py library
  • Python

Setting up a Local Network

To develop with the ethereum network you need to set up a local server. A local server allows you to simulate the blockchain network in your computer.

Geth is a tool that allows you to set up a blockchain server on your system. To install geth on your system, visit its download page.

For this section, you’ll use geth create a private network forked from the public Ethereum network. That means the local server will have the same data as the public network. The Ethereum network has different types of network. The following are the types of networks:

  • Mainnet
  • Rinkeby
  • Ropsten
  • Goerli

To set up a local network with geth, run this command and replace [network] with the name of the network you want the command to sync with:

geth --syncmode light -[network] --http

The command starts up the local server at http://127.0.0.8545. Before making any operation on the local server, make sure your Ethereum account is funded. Any transaction you make on the local server will not affect the public network.

Sending With Locked Accounts

In this section, you’ll learn how to unlock a locked account, and send transactions with the unlocked account.

To send a transaction from one account to another use the sender’s sign_transaction method:

from web3 import Web3

# connect to the server
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))

sender_address = '0xabcd..............................'
receiver = '0x1234.....................'

# Unlock the sender's account with its private key
sender = w3.eth.account.from_key('05d6c...............e')

# Initialize a transaction with the sender's account
signed_txn = sender.sign_transaction({
    'nonce': w3.eth.get_transaction_count(sender_address),
    'gas': 100000,
    'gasPrice': 100000,
    'to': receiver,
    'value': w3.toWei(5, 'ether')   # send 5 wei from sender to receiver
})

# One Wei equals 10^-18 Eth
# Wait for the server to mine the transaction
raw_transaction = signed_txn.rawTransaction
tx_hash = w3.eth.send_raw_transaction(raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

Gas is the name of the fee you pay to mine a transaction. gas and gasPrice are used to delegate some tokens for gas fees. Any unused gas token will be returned to the sender.

Sending Transactions with Unlocked Accounts

Sending tokens with an already unlocked account is very straightforward. To send a transaction from an unlocked account, use the w3.eth.sign_transaction method:

from web3 import Web3

# connect to the server
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))

sender = '0x12345......'
receiver = '0xabcdef......'

# create and sign a transaction
tx_hash = w3.eth.send_transaction({
    'to': receiver,
    'from': sender,
    'value': w3.toWei(5, 'ether'),    # send 5 wei from sender to receiver
})

# One Wei equals 10^-18 Eth
# Wait for the server to mine the transaction
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

Conclusion

This article covers the process of transferring tokens from one account to another. I hope this article helped you to understand how to make transactions with the Web3.py library. To further your knowledge on the topic be sure to check out the following links:

Comments

Popular Posts