0% found this document useful (0 votes)
95 views

WatPy - Building Smart Contract Applications Python, Solidity, Flask - 2019 - Sep

The document discusses building smart contract applications using Python, Solidity, and Flask. It provides instructions on setting up requirements like Ganache-CLI, interacting with smart contracts via Web3.py, building and deploying a basic storage smart contract, and creating a web-based dApp interface with Flask. The tutorial demonstrates fetching account balances, sending transactions, defining smart contract functions, and routing a Flask app to display forms, submit transactions, and return registration confirmations.

Uploaded by

hareesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

WatPy - Building Smart Contract Applications Python, Solidity, Flask - 2019 - Sep

The document discusses building smart contract applications using Python, Solidity, and Flask. It provides instructions on setting up requirements like Ganache-CLI, interacting with smart contracts via Web3.py, building and deploying a basic storage smart contract, and creating a web-based dApp interface with Flask. The tutorial demonstrates fetching account balances, sending transactions, defining smart contract functions, and routing a Flask app to display forms, submit transactions, and return registration confirmations.

Uploaded by

hareesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Building Smart Contract Applications:

Python, Solidity, & Flask


September 18, 2019
Michael Free

WatPy + Bitcoin Bay KW Meetup @ Terminal.io


Learning Outcomes
● Provide a Python “cheatsheet” for the upcoming ETHWaterloo 2 Hackathon in November
● Learn how to work with Ganache-CLI and Python to Perform Basic Ethereum Functions
● Build a Basic Storage Solidity Smart Contract
● Becoming familiar with Web3.py to Build a dApp
● Using Flask to Build a Web-based Python Application with Ethereum
Install Requirements
● GitHub Repository: https://github.com/Michael-Free/PyDemo
● Built on top of a vanilla Ubuntu Server 18.04 LTS install:

● Solc v0.4.25 is required:

● This demo/tutorial uses Ganache-CLI and requires other libraries in requirements.txt


Getting Started - Ganache-CLI
● An Ethereum Blockchain Emulator. Lightweight no need to run a node.
● When started with no parameters: 10 ETH addresses created
● Each address will have 100 ETH by default.
● Other information displayed:
○ HD Wallet mnemonic key: used to import these accounts into a wallet or other applications
■ Metamask, Parity, etc
○ Gas Limit and Gas Price:
■ Gas Limit - The amount of fuel is required to execute an operation or run a particular smart
contract function.
■ Gas Price - Price set by the contract or the network, to execute the operation. This is
variable. Choosing a lower gas price, means a lower-priority to execute the transaction
(takes longer).
■ Transaction Cost = Gas Limit * Gas Price
○ Host address and port ganache-cli is listening on
Getting Started - Ganache-CLI
Getting Started - Web3.py

● Web3 is an API to the Ethereum Blockchain to build applications.


● There are many implementations. The most widely used implementation is Web3.js, which Web3.py is
derived from.
● Let’s startup a python terminal and import the web3 libraries:

● Set Gananche-CLI as the Blockchain Provider:


Getting Started - Web3.py
● Let’s see if we can find out the gas price:

● Let’s see if we can get the balance of one of our ethereum accounts:

● Create a new account for yourself:

● The new balance of that account is zero:


Getting Started - Web3.py
● Interacting with Ganache-CLI with Python and Web3.py
○ Get a list of your personal accounts:

○ Notice that this lists the ethereum addresses started by ganache-cli.


○ The call from python to Web3.py can be observed from the ganache-cli terminal as well.
Getting Started - Web3.py
● Send some ETH to your new account from your other accounts:

● The response is the transaction hash registered on the blockchain. Here is the output of the transaction
in ganache-cli:
Explaining Smart Contracts
● Self-Executing contracts that exist on a blockchain
○ Think of it like a computer program
● Contracts can store terms between a buyer and a seller directly written into lines of code (soldity)

● Transactions with the contract are recorded on the blockchain.

● The goal is to provide fully self-executing and self-enforcing contracts, improving on our existing
framework.
Explaining Smart Contracts
● Moving Parts in the Next Step:
○ Solidity - the smart contract language that is most commonly used. This is what this
demo/tutorial will be using.
○ Solc - Solc is a binary and commandline interface for the Solidity Compiler (LLLC).
○ LLLC - the Lovely Little Language Compiler. This binary will translate Solidity Contracts into a
Ethereum-Blockchain executable format.
○ Py-Solc - The python wrapper for the the solc binary.
Building a Smart Contract
Learning More About Solidity
There are plenty of online resources for learning more about Solidity. For exploring more, take a
look at some of the provided documentation and sample contract-implementations:

● Solidity Documentation: https://solidity.readthedocs.io/en/v0.4.24/


● OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts
● BlockGeeks: https://github.com/blockgeeks/workshop/tree/master/src/contracts
Deploying Contracts (deploycontract.py)
● 2 ways to deploy a contract with Python:
○ Inline Code
■ What this demonstration will use.
○ Importing the Contract as a File
■ Not covered by this demo, but best practices provided in README.md
Deploying with Inline Solidity Code

deploycontract.py
Deploying Contracts

deploycontract.py
Using Flask to Build a dApp - Libraries
Flask Requirements

dapp.py
Web3 Requirements

dapp.py
Using Flask to Build a dApp - Input Form
Define an Input Form

dapp.py
Using Flask to Build a dApp - App Routing
Application Routing: 3 Basic Routes

dapp.py
Application Routing - home (/)

dapp.py
HTML Templates - index.html
Basic template for application:

Variables can be passed to


HTML templates.

The ’contractaddress’
variable is inserted into
the template with double
curly brackets:

{{ contractaddress }} All other routes will


inherit this HTML
template.

Their content will be


shown between these 2
tags in index.html:

{% block content %}
{% endblock %}

templates/index.html
HTML Templates - home.html

This template is part of


index.html

Nothing too interesting


happening here.

Basically selecting the 1 Insert this HTML between


menu option. these two tags in
index.html:
Just showing how to insert {% block content %}
some code into a template. {% endblock %}

templates/home.html
What it looks like
Application Routing - Register (/register) - GET

Call the form class created


earlier.

n creates an index for each


ethereum address.
Return the form into the
register.html template.

Add +1 for each address Return the contract


created by ganache-cli address to the
(0-9). register.html template.

dapp.py
HTML Templates - register.html
Import the index.html
template

Call the ethereum


addresses dropdown
menu & label from the
registerform class.

Call the serial number


input field & label from the
registerform class.

templates/register.html
What it looks like
Application Routing - Registered (/registered) - POST

Call the setRegistration


contract function.

Pass the address and Create some vars to pass


serial number string from to the template from the
the form to the contract contract transaction

Print some of this info in


the flask server window

Pass some variables to the


templates. Not all are
used - add them yourself!

dapp.py
HTML Templates - registered.html

Import the index.html


template

Call vars passed from the


dapp.py /registered route

templates/registered.html
What it looks like

You might also like