DApp Development for Python Programmers - Level Up Coding (original) (raw)

DApp Development for Python Programmers

Salman Dabbakuti

Image Courtesy Nick Willams at hackernoon

What is a DApp

“DApp” stands for Decentralized Application. Like traditional apps, decentralized applications have a front-end (client side) and back-end (server side). The user interface for a DApp can be written in any language (just like traditional applications) and can make calls to its back-end.

So, how do Dapps differ from traditional apps? A DApp has its backend code running on decentralized peer-to-peer network (i.e a blockchain). You might have heard about BitTorrent, Tor, Popcorn Time — they are DApps that run on peer-to-peer network but not on blockchain.

A blockchain DApp has its own backend code called smart contracts that can be deployed to a blockchain (most commonly Ethereum). A smart contract defines the logic of the entire DApp. It is the lifeblood of using a blockchain as a backend.

You might think “This is a joke right? You are talking about a traditional front-end and connecting it to a blockchain. Where can a Python developer take advantage of it?” Here comes the point — web3. Web3 is a collection of libraries which allow you to interact with a local or remote Ethereum blockchain. Simply, web3 is the bridge of communication to your backend (blockchain). Fortuntely, Ethereum developers have made a python library web3.py for interacting with Ethereum. Its API is derived from the JavaScript version of web3. So, apart from web3.js, we can interact with the blockchain via web3.py library too.

Architecture of DAPP

Dapps Development includes three simple steps

  1. Deploying a smart contract on the blockchain network
  2. Reading data from the deployed smart contract
  3. Sending transactions to the deployed smart contract

We are going to do these three operations step by step on a python environment with the web3.py library.

To interact with the blockchain, we must be connected to any fully synced node. For this tutorial, we are pointing to an Infura node. Make sure you have an Ethereum wallet (create Ethereum wallet with the Metamask chrome extension or myetherwallet and store your private key safely) and add some test Ether in it for doing operations.

Pre-Requisites

  1. Python 2.7 +
  2. Node.js
  3. Truffle

npm install -g truffle

4. Pip

npm i pip

5. web3.py

pip install web3

6. Infura Project API

Go to Infura site and register. Create a new project and copy the Ropsten Infura RPC URL. We are going to deploy our smart contract to Ropsten test network.

The Smart contract

Every programmer has executed a “hello world” program in their favorite programming language to understand the basics of running the language. Here is our simple “hello world” version of greeter smart contract in the Solidity language where we can add a greeting on the blockchain and retrieve it. Solidity is the most common language to write smart contracts which compiles to byte code that can be executed on the Ethereum Virtual Machine running on nodes.

You can add greeting with greet() method by passing a string value and retrieve greeting withgetGreting() method.

1. Deploying a Smart Contract on Blockchain Network

a) Creating a project:

mkdir pythonDappcd pythonDapptruffle init

After successful initialization of the project, go to your folder and create greeter.sol file in your /contracts directory. Before deploying a contract on the network, we must compile it and build the artifacts.

b) Compilation of the smart contract:

So for compilation, we are going to use the Truffle solc compiler. In your master directory, run below command:

truffle compile

        (or)

truffle.cmd compile #(for windows only)

Above command will compile your contracts in the /contracts directory and create binary artifacts file greeter.json in /build directory.

c) Deploying the contract:

Open your python IDLE editor and create a new file in the master directory deploy.py with the below code and then run py deploy.py in your directory.

Here is what I did:

2. Sending Transactions to the Deployed Contract

In our contract, there is a method greet(). We can add a greeting to our contract with this method alone. Let’s see how we can do it with web3.py.

Open your python IDLE editor and create a new file sign.py with below code. Then run py sign.py in your directory.

Here is what I did:

3. Reading Data from Deployed Smart Contract

In our contract, there is a method getGreeting() which retrieves our added greeting in the blockchain. We are going to call this method with web3.py

Open your python IDLE editor and create a new file read.py with below code. Run py read.py to read greeting

Here is what I did:

Conclusion

You should now have an understanding of how to deploy, interact, and sign transactions with web3.py. Let’s combine all pieces together by having a look at real-time DApp that I have created with web3.py and used flask for a frontend server here. It will help you understand deeper about DApp development with Python.

If you’re also familiar with JavaScript, you can have a look at a similar article for web3.js here