Creating a fully functional dApp involves various components, including smart contracts (usually written in Solidity), a front-end interface, and integration with a blockchain network (e.g., Ethereum). Here, I will provide a simplified example that focuses on the core concept of a dApp using Python and the Ethereum blockchain via the Web3.py library. This example will demonstrate how to interact with a basic smart contract.

Prerequisites

Before starting, ensure you have the following installed:

  1. Python (version 3.6 or later)
  2. Web3 Package ($ pip install web3)
  3. Ganache (a local Ethereum blockchain for testing)
  4. Jupyter Notebook (optional)

Write a Simple Smart Contract

To do this, we are going to need a smart contract. Luckily, there is an example from a previous post about Solidity. The smart contract stores information about renting a yacht. The Solidity code was written by me using Remix and no, you may not rent my yacht. Start mining BitCoin and buy your own!

Compile this smart contract using an online Solidity compiler like Remix, and obtain the ABI (Application Binary Interface) and bytecode.

A version of the ABI & bytecode I will be using can be found in this repository.

Python Script to Deploy and Interact with the Smart Contract

Below is a Python script that connects to a local Ethereum blockchain (e.g., Ganache), deploys the YachtRental contract, and interacts with it.

from web3 import Web3
import json

# connect to local Ethereum node (Ganache)
ganache_url = "http://127.0.0.1:8545"
web3 = Web3(Web3.HTTPProvider(ganache_url))

# check if connected
print(f"Connected to the local Ethereum (Ganche) network: {web3.is_connected()}")

# set the default account (the first account Ganache provides)
web3.eth.default_account = web3.eth.accounts[0]

# ABI and Bytecode obtained from compiling the Solidity contract
with open('ABI', 'rb') as a:
    abi = json.loads(a.read())
with open('Bytecode') as b:
    bytecode = b.read()


# deploy the contract
YachtRental = web3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = YachtRental.constructor().transact()
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
contract_address = tx_receipt.contractAddress

# interact with the deployed contract
yacht_rental = web3.eth.contract(address=contract_address, abi=abi)

# check the availability of the Yacht
availability = yacht_rental.functions.available().call()
print(f"Is the yacht available? {availability}")

# obtain the daily rate and book the yacht for 2 days and send the appropriate amount of Ether
num_days = 2
daily_rate = yacht_rental.functions.ratePerDay().call()
total_cost = num_days * daily_rate

tx_hash = yacht_rental.functions.bookYacht(num_days).transact({'value': total_cost})
web3.eth.wait_for_transaction_receipt(tx_hash)

# get the yacht's availability after booking
availability = yacht_rental.functions.available().call()
print(f"Is the yacht available after booking? {availability}")

Running the dApp

  1. Start Ganache: Run Ganache to simulate a local Ethereum blockchain.
  2. Compile and Deploy: Compile the YachtRental contract using Remix or any Solidity compiler. Replace the abi and bytecode in the Python script with those generated from the compilation.
  3. Run the Python Script: Execute the Python script. It will deploy the smart contract to the local blockchain and then rent the yacht.

How the dApp Works

The Python script connects to the local Ethereum blockchain (Ganache) and deploys the YachtRental smart contract. The contract’s address is stored in the contract_address variable. The script then interacts with the deployed contract by checking the yacht’s availability and then submitting a transaction to rent it using the bookYacht function.

This example illustrates the basic structure of a dApp: a blockchain-based smart contract and a Python-based interface that interacts with the contract. While this is a simple example, real-world dApps would include more complex logic, a front-end interface (e.g., using a JavaScript framework such as React), and integration with various blockchain services.