-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hacktivator-program #196
base: main
Are you sure you want to change the base?
hacktivator-program #196
Changes from 1 commit
7064354
342c445
0a8c1d0
0551f06
5273690
f01e4ac
8ceae38
a7354a5
1b9f256
e19fa18
18f6a9a
b58c2c5
d20ade4
77eac1f
e2808c6
ce2365d
da4adff
5f3edfd
e58ae1c
fd09210
33dbfde
96ed509
51f9228
d9eee0d
83dcb37
e9e776e
1a63016
1181e15
2701845
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
--- | ||
sidebar_label: Vyper Smart Contract on RootStock | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sidebar_position: 7 | ||
title: Deploying a Vyper Smart Contract to RootStock (RSK) Testnet using Python | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: "This guide walks through the process of deploying a smart contract to the RootStock (RSK) testnet using Python and Web3.py. We'll be deploying a simple Vyper contract that demonstrates how to interact with the RSK network." | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tags: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly check that these tags are enclosed in [] |
||
- rsk | ||
- rootstock | ||
- tutorials | ||
- resources | ||
- tests | ||
- web3py | ||
- vyper | ||
- smart contracts | ||
- python | ||
- developers | ||
--- | ||
|
||
This guide demonstrates how to deploy smart contracts written in Vyper to the RootStock (RSK) testnet using Python and Web3.py. RSK is a groundbreaking smart contract platform that's merge-mined with Bitcoin, offering unique advantages for developers: | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- **Bitcoin Compatibility**: Deploy smart contracts while leveraging Bitcoin's security and network effects | ||
- **EVM Compatibility**: Use familiar Ethereum tools and practices while building on Bitcoin | ||
- **Lower Fees**: Benefit from RSK's cost-effective transaction fees | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **Scalability**: Enjoy higher transaction throughput compared to the Bitcoin mainnet | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
We'll walk through creating a simple Vyper contract and deploying it to RSK's testnet, covering everything from environment setup to handling RSK-specific configurations. Whether you're an experienced Ethereum developer looking to expand to Bitcoin-based smart contracts, or just starting your blockchain journey, this guide will help you get up and running with RSK. | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Prerequisites | ||
|
||
- [uv](https://docs.astral.sh/uv/) | ||
- You'll know you've done it right if you can run `uv --version` and see a version number. | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- [git](https://git-scm.com/) | ||
- You'll know you've done it right if you can run `git --version` and see a version number. | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Helpful shortcut: | ||
|
||
```bash | ||
# For bash | ||
echo "source $HOME/.bashrc >> $HOME/.bash_profile" | ||
|
||
# For zsh | ||
echo "source $HOME/.zshenv >> $HOME/.zprofile" | ||
``` | ||
|
||
- Python 3.x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I install Python 3? its not clear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for this tutorial we will install python3.11 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great. Thank you, kindly include this with the command to install or a link. Using the pkg: https://www.python.org/downloads/ Alos add that user should install the certificate Check the python version: If there is a way to do this via homebrew for Mac, and others for Windows, also specify: https://www.python.org/downloads/ |
||
- A text editor | ||
- Basic understanding of smart contracts and Python | ||
- RSK testnet RBTC (will show you how to get this) | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Installation | ||
|
||
```bash | ||
git clone https://github.com/EdwinLiavaa/Web3py-Vyper-RootStock.git | ||
cd Web3py-Vyper-RootStock | ||
``` | ||
|
||
### uv | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a one line sentence of what this does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uv sync is a fast package management command that downloads and installs your project's Python dependencies while creating a lockfile for reproducible installations. |
||
uv sync | ||
``` | ||
|
||
### pip/python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with here. Add a descriptive title and also why the reader should run the following commands in a short sentence There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pip/python section creates a virtual environment (python -m venv ./venv), activates it (source ./venv/bin/activate), and installs the project dependencies from requirements.txt (pip install -r requirements.txt). |
||
|
||
```bash | ||
python -m venv ./venv | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This throws an error:
owanate@Owanates-MacBook-Pro Web3py-Vyper-RootStock % python -m venv ./venv This probably happened because of my comment above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like Python's venv module is not installed on your system. You are using a MacBook, so You can try fix this by: Reinstalling Python and make sure to check the option to install pip and venv during installation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Cool This works for me: |
||
source ./venv/bin/activate | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Quickstart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're creating a hello.py file here right? Instead of quickstart, add a descriptive header title There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We run the hello.py Python script as a quick test to verify that our project environment (either with UV or with pip/python virtual environment) is setup properly. |
||
|
||
```bash | ||
uv run hello.py # for UV | ||
# or | ||
python hello.py # for pip/python | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explain in what cases uv or pip should be used and what the command returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get this when I run the second command: (venv) owanate@Owanates-MacBook-Pro Web3py-Vyper-RootStock % python3 hello.py There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both uv run hello.py and python hello.py will run the script and output "Hello from web3py-Vyper-RootStock!", with UV being preferred for faster, modern projects and pip for traditional Python setups. |
||
|
||
## Setup Environment | ||
|
||
First, let's set up our Python environment and install the necessary packages: | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
# Create and activate virtual environment | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
|
||
# Install required packages | ||
pip install python-dotenv web3 vyper | ||
``` | ||
|
||
## Configuration | ||
|
||
Create a `.env` file in your project root with your configuration: | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```env | ||
RPC_URL="https://rpc.testnet.rootstock.io/[YOUR-API-KEY]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a note to replace API key with testnet api key Also link to guide on how to setup and account and create a testnet API key using the RPC API https://dev.rootstock.io/developers/rpc-api/rootstock/setup/ |
||
PRIVATE_KEY="your-private-key" # Never commit your real private key! | ||
MY_ADDRESS="your-wallet-address" | ||
``` | ||
THIS IS ONLY FOR TESTING - TYPICALLY YOU SHOULD NEVER SHARE YOUR PRIVATE KEY. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
## Get Testnet RBTC | ||
|
||
Before deploying, you'll need some testnet RBTC: | ||
|
||
1. Go to the RSK faucet: https://faucet.rsk.co/ | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2. Enter your wallet address | ||
3. Complete the captcha and request funds | ||
4. Wait a few minutes for the transaction to be confirmed | ||
|
||
## The Smart Contract | ||
|
||
Here's our simple Vyper contract (`favorites.vy`): | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
# @version ^0.3.7 | ||
|
||
favorite_number: public(uint256) | ||
owner: public(address) | ||
|
||
@external | ||
def __init__(): | ||
self.owner = msg.sender | ||
self.favorite_number = 0 | ||
|
||
@external | ||
def store(new_number: uint256): | ||
self.favorite_number = new_number | ||
``` | ||
|
||
## Deployment Script | ||
|
||
Here's our Python script to deploy the contract (`deploy_favorites_unsafe.py`): | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
from web3 import Web3 | ||
from dotenv import load_dotenv | ||
from vyper import compile_code | ||
import os | ||
|
||
load_dotenv() | ||
|
||
RPC_URL = os.getenv("RPC_URL") | ||
|
||
def main(): | ||
print("Let's read in the Vyper code and deploy it to the blockchain!") | ||
w3 = Web3(Web3.HTTPProvider(RPC_URL)) | ||
with open("favorites.vy", "r") as favorites_file: | ||
favorites_code = favorites_file.read() | ||
compliation_details = compile_code( | ||
favorites_code, output_formats=["bytecode", "abi"] | ||
) | ||
|
||
chain_id = 31 # RSK testnet chain ID | ||
|
||
print("Getting environment variables...") | ||
my_address = os.getenv("MY_ADDRESS") | ||
private_key = os.getenv("PRIVATE_KEY") | ||
|
||
# Check balance before deployment | ||
balance = w3.eth.get_balance(my_address) | ||
balance_in_rbtc = w3.from_wei(balance, "ether") | ||
print(f"Account balance: {balance_in_rbtc} RBTC") | ||
|
||
if balance == 0: | ||
print("Your account has no RBTC! Please get some testnet RBTC from the faucet:") | ||
print("1. Go to https://faucet.rsk.co/") | ||
print("2. Enter your address:", my_address) | ||
print("3. Complete the captcha and request funds") | ||
print("4. Wait a few minutes for the transaction to be confirmed") | ||
return | ||
|
||
# Create the contract in Python | ||
favorites_contract = w3.eth.contract( | ||
abi=compliation_details["abi"], bytecode=compliation_details["bytecode"] | ||
) | ||
|
||
# Submit the transaction that deploys the contract | ||
nonce = w3.eth.get_transaction_count(my_address) | ||
|
||
print("Building the transaction...") | ||
transaction = favorites_contract.constructor().build_transaction( | ||
{ | ||
"chainId": chain_id, | ||
"from": my_address, | ||
"nonce": nonce, | ||
"gas": 3000000, # Higher gas limit for RSK | ||
"gasPrice": w3.eth.gas_price * 2, # Double the gas price to ensure transaction goes through | ||
} | ||
) | ||
|
||
print("Signing transaction...") | ||
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key) | ||
print("Deploying contract...") | ||
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction) | ||
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) | ||
print(f"Contract deployed! Address: {tx_receipt.contractAddress}") | ||
|
||
if __name__ == "__main__": | ||
main() | ||
``` | ||
|
||
## Key Points About RSK Deployment | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. **Chain ID**: RSK testnet uses chain ID 31 | ||
2. **Gas Settings**: | ||
- We use a higher gas limit (3,000,000) for RSK | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- We double the gas price to ensure the transaction goes through | ||
3. **Transaction Type**: | ||
- RSK works best with legacy transactions (using `gasPrice` instead of EIP-1559 parameters) | ||
EdwinLiavaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Running the Deployment | ||
|
||
Execute the deployment script: | ||
|
||
```bash | ||
python deploy_favorites_unsafe.py | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @EdwinLiavaa Error when running deployment script: (venv) owanate@Owanates-MacBook-Pro Web3py-Vyper-RootStock % python3 deploy_favorites_unsafe.py Also specify troubleshooting options too, its usually a good idea to let the reader know how to solve possible errors they may encounter when running the commands. E.g, If you get this error: ModuleNotFoundError: No module named 'web3' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error occurs because the web3 package is not installed in your virtual environment. To fix it, you should run pip install web3 or pip install -r requirements.txt while your virtual environment is activated to install all required dependencies. |
||
``` | ||
## Useful Links | ||
|
||
The boilerplate used in this project was adopted from the Cyfrin Updraft Python and Viper Starter Kit: | ||
- [Cyfrin Updraft @cyfrinupdraft](https://updraft.cyfrin.io/courses/intermediate-python-vyper-smart-contract-development) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the guide to the quickstart section: https://dev.rootstock.io/developers/quickstart/
Vyper Starter Kit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very well noted. I have incorprated all recommended changes on my local machine. I will now finish the review before I move the guide to the quickstart section accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@owans I have moved guide to quickstart section.