Loading...
Like web3.js we can use web3.py to communicate with blockchain. This smartbook will be an introduction to web3.py. We will simply transafer some test ethers using web3.py that way we will get to know about web3.ps's working as it is little different that web3.js. You can refer this smartbook which did same interaction with blockchain but with implementation of web3.js library.
RPC url is required for our program to interact with a blockchain network. You can check different methods to connect through providers in this smartbook.
For this example provder we will be using is Infura via http. All we have to do is create an account and generate a project which will provide us with rpc url for created project's api key.
You can get test tokens from ropsten faucet. Enter your address and click on submit.
pip is the most used pacakage management system for python so we will be using it to install our web3 library using following command
> pip install web3
Let's create a web3 instace with http provider.
from web3 import Web3
web3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/<API>'))
Private_Key = '<Sender Private Key>'
from_address = '<Sender Public Address>'
to_address = '<Receiver Public Address>'
Transaction object which we will be creating require following parameters
We will be obtaining nonce with the help of getTransactionCount method.
nonce = web3.eth.getTransactionCount(from_address)
gasPrice = web3.toWei('50', 'gwei')
value = web3.toWei(0.1, 'ether')
tx = {
'nonce': nonce,
'to': to_address,
'value': value,
'gas': 2000000,
'gasPrice': gasPrice
}
Note that : the transaction object that we created here is a python dictionary.
This function will return a transaction that’s been signed by the node’s private key (i.e we have private key and a rpc node), but not yet submitted.
Syntax : web3.eth.account.sign_transaction( Tx Object, Private_Key )
Parameters :
Returns : Signed transaction object is returned.
Implementation :
#sign the transaction
signed_tx = web3.eth.account.sign_transaction(tx, Private_Key)
This function will send a signed and serialized transaction and will returns the transaction hash as a HexBytes object.
Syntax : web3.eth.send_raw_transaction(signed_tx.rawTransaction)
Parameters :
Returns : Transaction hash as a HexBytes object.
Implementation :
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
Here we will get transaction hash in the hex byte format we need to convert it into hex format.
This is how we can send our transaction on the ethereum network using web3.py
from web3 import Web3
web3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/<API>'))
Private_Key = '<Sender Private Key>'
from_address = '<Sender Public Address>'
to_address = '<Receiver Public Address>'
nonce = web3.eth.getTransactionCount(from_address)
gasPrice = web3.toWei('50', 'gwei')
value = web3.toWei(0.1, 'ether')
tx = {
'nonce': nonce,
'to': to_address,
'value': value,
'gas': 2000000,
'gasPrice': gasPrice
}
#sign the transaction
signed_tx = web3.eth.account.sign_transaction(tx, Private_Key)
#send transaction
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
#Print transaction hash in hex
print(web3.toHex(tx_hash))