DApp Development for Python Programmers
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.
Dapps Development includes three simple steps
- Deploying a smart contract on the blockchain network
- Reading data from the deployed smart contract
- 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
- Python 2.7 +
- Node.js
- 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 with getGreting()
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:
- Imported web3 library and all other required modules
- Initiated web3 provider by pointing to Ropsten Infura node
- Added an account address and private key for signing transaction. Don’t forget to add your credentials in the code.
- Initiated Contract instance by pointing to
abi
andbytecode
of Truffle compiled artifacts filegreeter.json
- Added construct_txn with parameters like nonce, gas, gasPrice. Here, gas refers to the maximum amount of computation resources that a transaction should use and paid for in Ethereum. gasPrice refers to the smallest amount of Ether for using such amount of gas for the transaction. to refers to the address where you are sending transaction. The to parameter is only required if you are sending Ether to an account or smart contract.
- Signed transaction with our private key and broadcasted on network.
- Logged transaction hash and deployed contract address in the console. According to Ethereum, the transacton processing time is <20 secs. So you must wait for 20 secs to get the deployed contract address. Your backend is successfully deployed on the Ethereum blockchain now. Now you can interact with your smart contract with this address. Copy this contract address.
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:
- Imported web3 library and all other required modules
- Initiated web3 provider by pointing to Ropsten Infura node
- Added an account address and private key for signing transaction
- Initiated Contract instance by pointing to
abi
andbytecode
of Truffle compiled artifacts filegreeter.json
- Created
tx
object for adding a greeting of “hello all my goody people” and built a transaction - Signed transaction with our private key and broadcasted on network.
- Log transaction hash in console. You can check transaction status on etherscan with your transaction hash. Once the transaction verified by miners, our greeting will be added on blockchain. You can retrieve greeting with
getGreeting()
function as described below.
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:
- Imported web3 library and all other required modules
- Initiated web3 provider by pointing to Ropsten Infura node
- Created
contract
instance by pointingabi
andcontract_Address
(from the previous step) - Called the
getGreeting()
method and printed the result in console
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