Well, that’s a complicated one. So let’s break it down.

As line 259 indicates, we aren’t calling the function createPost, rather we are building a transaction. However as you see the buildTransaction functions takes in a dictionary of data as an argument which is very imperative to understand.

chainId: Simply represents the network number we are connected to, since each Ethereum Network has its own chain ID. In this case 3 represents Ropsten Network.

gas: Since we already know each transaction requires gas to be executed. This parameter simple represents the highest amount of gas we are willing to pay for the execution of our transaction.

gasPrice: Gas price indicates the amount of gas to paid in Wei for per unit of Gas

nonce: This simply keeps the count of the previous transactions made by the user.

Once we build this transaction with all parameters filled as per our choice, now its time for signing this transaction with the signTransaction function itself. This function takes the transaction as well as the private key as argument.

Finally, we know a transaction is completed when we receive its transaction receipt using the waitForTransactionReceipt function. Once we get this receipt we can be ensure that the record has been added to the blockchain successfully.

d. Making A Payment With Ether

Function for sending ether

Sending ether to a contract or a different account involves similar steps, as seen before, like building a transaction, signing it with private key as well as getting a confirmation receipt.

However, it does involve two additional fields like,

to: symbolizes the receiver of the payment

value: this is the amount to be transferred.

Well, that’s all you need to do in order to transfer funds from one account to another.

e. Calling an Event

Well, what is an event in the first place?

Events in Solidity may be seen as real time records in the form of transaction logs that are not only stored in blockchain but can also be accessed at any given time by the with the help of the address of the contract.

Although, I haven’t used any events in my code, the official documentation of Web3.py shows a clear procedure to call your events.

myContract = web3.eth.contract(address=contract_address, abi=contract_abi)

tx_hash = myContract.functions.myFunction().transact()

receipt = web3.eth.getTransactionReceipt(tx_hash)

myContract.events.myEvent().processReceipt(receipt)

Alright. These are the most imperative calls that you might want to use while interacting with your smart contract.

Although we have quite clearly understood the recommended procedures to interact with a contract using web3.py and call for particular function, there still lies one last thing to be understood, i.e., What do we do after the contract response back?

Since we are using Django Web Framework, there is a very strong need for us to understand how do we handle the responses of the contract and use it as per our need.

Handling Contract’s Responses in Django

There are 2 possible ways the contract might respond back.

First, you might have a made a transaction(added something to the Blockchain or made a payment). In this case, you will simple get back a transaction receipt. There is nothing more you would like to do with it since its just a confirmation that your transaction has executed. Secondly, you might call a function that returns you a list of data. Well, this is what we are actually interested in.

So let’s understand the Second Case with the help of some codes I wrote to handle a similar situation.

Function To pass posts_data to the template

This is literally a simple function, isn’t it?

Here, all we actually did was call get_posts_from_contract() function. What this function does? Although its quite self-explanatory but have a look yourself.

Function To Call posts array from contract

Our smart contract has an array of data that contains all the posts from the users, their tipped amount as well as the address of the author. In this function, we simply call that array and return it.

Therefore, when the posts() function calls the get_post_from_smart_contract() function, it basically gets a nested list of data that looks something like this.

Nested List Returned By WEB3.py

Pretty Ugly, right?

Now the most crucial question that arises is, How do we handle this list in our template?

Let’s Move To The Template Section in Django

Since the data from the contract is a nested list, the effective of handling a list of data in Django Template is:

{% for data in data_from_views.py %}



...

<h1>{{ data.0 }}</h2> <h1>{{ data.1 }}</h2>...



{% endfor %}

And this is exactly how I handled it in my templates. The code is shown below:

Template Handling all the posts from the list

LET’S WRAP UP

Well, you just learned the entire process of not only interacting with a smart contract with Python, but also handling its responses in the way we want.

My Front-End Skill Sucks

As soon as you run the development server, the above template will be rendered and you can have a complete website that not only interacts with the blockchain but also let’s you write on it as well as make payments.

All the posts from Blockchain

Great! So now that we are done, you can grab a cup of coffee and try this all over again by yourself.

Just in case any dilemma pops up in your brain, Feel Free to Comment Below.