Build Dapp Blockchain By Python(Flask) Using Docker

Tram Ho

Previous articles in the “Building Dapp” series:

  1. Building decentralized applications (Dapp)
  2. Building decentralized applications (Dapp) with Reacjs
  3. Building decentralized applications (Dapp) with Vuejs
  4. Building decentralized applications (Dapp) with Cocos Creator
  5. Building decentralized applications (Dapp) with Unity
  6. Building decentralized applications (Dapp) with Angular

Going back to the dapp building series on different platforms, today, as the most inexperienced member of the team, I would like to continue with an article on building blockchain dapp using Python (Flask) used in the environment. docker

Paradigm

Above is the general model for a Dapp blockchain that will remain from the application and then use Web3 to interact with the Ethereum blockchain network. But because this is not a Javascript environment but a Python environment, instead of using Web3.js we will use Web3.py

Prepare

The environment has always been a headache to any developer. And myself is also a victim when installing web3.py forever after the error of something python guy on the machine, which he faced immediately installed. So I decided to close the plan on a board by using Docker for it quickly.

  • Docker: make sure the machine has docker $ docker --version installed
  • Next is … oh, but need to install between where to next

Deployment

Following is the configuration part of Dockerfile and Dokcer-composer

  • Dockerfile

    Web3.py requirement is from Python 3.6 and up

  • requirements.txt

  • docker-compose.yml

  • .env

  • app . py

    We will create an app flask frame with the libraries as below and simulate variables such as address or balance to show the test to the index.html file.

  • templates / index.html

    Minh has added a bit of css and images for it to be ugly, so a bit too long. Then dump the variables passed on the server to the index file as follows

  • static / css / style.css

    Because this file is quite long, I just illustrate the first few lines, what people should repo on github at the end of the lesson

The directory structure will look like this:

Now run cmd docker-compose up --build to docker to download images and build containers for us

So now that the build has finished, we will check the installation libraries to see if they are available by going to the docker to see:

  • The first is to check what the name or id of the container is running: docker ps and here its name is dapp_with_python_app_1

  • Now let’s go to check: docker exec dapp_with_python_app_1 pip freeze can be understood as docker exec <name_container> <cmd_run> then here we are checking to see if the pip library has downloaded and 2 things we need already have

Test it out

Deliciously there were 2 variables, the string was sent and displayed. Now it’s time to add this interactivity to the blockchian for the application. One thing to note is that in the result of docker-composer , it says that it is running on port 8888 but in fact, if you want to access the web from the browser, we have to go to port 9999 because in the docker-composer we have configured it as port 8888 ‘s mapping. Container with port 9999 of the real machine. And if you pay close attention to everyone looking back at the results of the docker ps we will see

Deploying Interaction With Blockchain

Use web3 to get balance from blockchian

  • app . py

We will go step by step to carry out and test the results so the address and balance are available

Now write the sendCoin function to transfer money. First we need to set private_key to sign the send and receive transactions, the next parameter is nonce to set the transaction when sending. Next we need to get the params that the form sends home using the request . Once we have the destination address address_des and the value we need to send value we will create the transaction with the parameters as tx variable below. signed_tx is the use of a private key to sign a transaction. Finally, perform a transaction with sendRawTransaction , the value it returns is a hash code that we can use this code on the web blockchain to check transactions with this hash code. After obtaining the hash code, we will go to the website to check the transaction. All parameters need to show that I will send to view send.index

  • app . py
  • templates / send.html

See which result

  • First enter the index page to enter the information

  • And check both on the first image metamark server and balance address, the next image will be the send and balance addresses

  • Then send now to see if this ETH is delivered to the destination address or not

  • The hash has been returned and we can get this hash to ropsten to check. But because the transaction needs to be confirmed, it takes a long time to check the transaction information. The simplest way is to check that the balance in the metamark account has changed as we can check the transaction

Conclude

So we have a framework for building dapp blockchain with python, hope that this article can help you in building dapp blockchain with python

Because Python is a server-side language, it is not like client languages ​​that may interact directly with the browser to connect to the metamark. So I had to use this method to connect and sign transactions with the private key on the server itself

This is the complete source code for this article and I have also added a few other functions to be suitable for development: Repo Github

Share the news now

Source : Viblo