Deploy and Interact with a Smart Contract using Web3.py
Web3.py is a Python library that allows developers to interact with Ethereum-based blockchains with Python. Rootstock has an Ethereum-like API available that is fully compatible with Ethereum-style JSON-RPC invocations. Therefore, developers can leverage this compatibility and use the Web3.py
library to interact with Rootstock similar to how developers interact with smart contracts on Ethereum.
In this guide, you'll learn how to use the Web3.py library to deploy and interact with smart contracts on Rootstock.
See tutorial on how to interact with Rootstock using Rust
Prerequisites
- A testnet account with tRBTC funds.
- An API KEY from the Rootstock RPC Service.
- Set up the project
- A Solidity Compiler installed -> see solidity compiler installation instructions
Set up the project and install dependencies:
# create a directory for the project
mkdir web3-python-guide && cd web3-python-guide
# install python 3.10
brew install python@3.10
# set up the development virtual environment
python3.10 -m venv env
source env/bin/activate
# install dependencies
pip install Web3 py-solc-x
Solidity compiler installation instructions for MacOs:
brew install solc-select
solc-select use 0.8.19 --always-install
solc --version
# Version: 0.8.19+commit.7dd6d404.Darwin.appleclang
Set Up Secrets for the Project
We will be using sensitive data that doesn’t have to be stored in the code, and instead we will store them in a .env
file.
For that, first lets install the package to read data from the .env
file:
pip install python-dotenv
Then, we will create a .env
file and add the secrets:
touch .env
add the following variables to the file:
Replace YOUR_APIKEY
with the API key from your dashboard.
# get this YOUR_APIKEY from the Rootstock RPC Service.
RPC_PROVIDER_APIKEY = '{YOUR_APIKEY}'
# this is the private key of the account from which you will deploy the contract
ACCOUNT_PRIVATE_KEY = '{YOUR_PRIVATE_KEY}'