1/5
## Web3py Favorites In this lesson, we will explore the world of Web3py by building a simple but powerful project. This will help us learn the fundamentals of blockchain development. We'll learn how to deploy a smart contract, interact with that smart contract, and work with more advanced Python tooling. To get started, we need to be familiar with the Git repository associated with this course. We can download all of the code from that repository at any time: ```bash https://github.com/Cyfrin/web3py-favorites-cu ``` We'll dive into the `README.md` file where we have a description of what we'll be building: ``` # web3py Favorites This is from the Cyfrin Updraft Vyper Course. - web3py Favorites - Getting Started - Prerequisites - Optional prerequisites - Optional Gitpod or CodeSpaces - Installation - uv - pip/python - Quickstart - Deploying with Python - 1. Setup Tenderly Virtual Network - 2. Fund a wallet - 3. Get your RPC URL - The Unsafe Way - 4. Run the unsafe version - The Safer Way - 4. Encrypt your private key - 5. Run the safe version - Maintainer notes - Build a new requirements.txt ``` We have a file called `favorites.vy` which we'll interact with in this lesson: ``` # pragma version ^0.4.1 # @license MIT my_favorite_number: uint256 struct Person: favorite_number: uint256 name: String[100] # Static Array/List list_of_people: public(Person[5]) list_of_people_index: uint256 name_to_favorite_number: HashMap(String[100], uint256) @external def store(favorite_number: uint256): self.my_favorite_number = favorite_number @external def retrieve() -> uint256: return self.my_favorite_number @external def add_person(name: String[100], favorite_number: uint256): new_person: Person = Person(favorite_number, name) self.list_of_people[self.list_of_people_index] = new_person self.list_of_people_index += 1 self.name_to_favorite_number[name] = favorite_number ``` We'll also learn how to write a Python key encryptor script, which will help us deploy a smart contract: ``` from eth_key import Account import getpass from pathlib import Path import json KEYSTORE_PATH = Path("keystore.json") def main(): # input for your private key private_key = getpass.getpass("Enter your private key: ") my_account = Account.from_private_key(private_key) password = getpass.getpass("Enter a password: ") encrypted_account = my_account.encrypt(password) with KEYSTORE_PATH.open("w") as fp: print(f"Saving to {KEYSTORE_PATH}...") json.dump(encrypted_account, fp) if __name__ == "__main__": main() ``` **Code Spaces** We can use Code Spaces for this project. We'll create a Code Space for the repository. Once the Code Space has loaded, we can access the terminal. Then, we can run the following command: ```bash pip install -r requirements.txt ``` **Deploying with Python** We'll deploy a smart contract with Python. This will involve the following steps: * **Setup Tenderly Virtual Network** To get started with Web3py, we need to make sure that we have a Web3 provider that is able to interact with our blockchain. We can do this through Tenderly's virtual networks. Tenderly has a nice feature that allows us to get up and running with a virtual network very easily. * **Fund a wallet** We'll need a wallet to be able to interact with our blockchain. We can use Tenderly to get a test wallet that we can fund with some Ether. * **Get your RPC URL** We need to know the RPC URL associated with our virtual network. We can get that from the Tenderly dashboard. * **The Unsafe Way** The unsafe way involves hardcoding your private key directly into your Python script. This is generally not a good practice as this leaves your private key susceptible to security risks. We'll run the following command to create a file called `favorites_unsafe.py` ```bash touch favorites_unsafe.py ``` We'll add some code to this file: ```python from web3 import Web3 from web3.middleware import geth_poa_middleware from web3.exceptions import BadFunctionCallOutput from vyper import compile_lll # Input your RPC URL # RPC_URL = "https://rpc-mumbai.maticvigil.com/" RPC_URL = "https://virtual-mainnet.tenderly.co/fork/cd647361-44d3-43b5-8013-c75702600116/1" w3 = Web3(Web3.HTTPProvider(RPC_URL)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) print("Connecting...") if w3.is_connected(): print("Connected!") else: print("Not connected.") # Add code to favorites.vy - it should be a smart contract # Compile the Contract - to compile the smart contract favorites_compiled_lll = compile_lll( favorites_vy, optimize=False ) print("Favorites Compiled Successfully!") # Create Contract Object favorites_contract = w3.eth.contract( abi=favorites_compiled_lll["abi"], bytecode=favorites_compiled_lll["bytecode"] ) # Build a Transaction nonce = w3.eth.getTransactionCount("0x9f024882e119048302558080b91cfc1d61d1e39611556c6305736d1d16b1b800a4b1cfc1d61d1e39511556c630574000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A practical guide to deploying your first smart contract with web3.py - This lesson walks you through how to deploy your smart contract using web3.py, including building a transaction, encrypting your private key, and deploying to the blockchain.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar