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.25 --always-install
solc --version
# Version: 0.8.25+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}'
Deploy a smart contract
Write the smart contract
The contract to be compiled and deployed in the next section is a simple contract that stores a message, and will allow for setting different messages by sending a transaction.
You can get started by creating a file for the contract:
touch Greeter.sol
Next, add the Solidity code to the file:
// SPDX-License-Identifier: MIT
pragma solidity >0.5.0;
contract Greeter {
string public greeting;
constructor() public {
greeting = 'Hello';
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
function greet() view public returns (string memory) {
return greeting;
}
}
The constructor function, which runs when the contract is deployed, sets the initial value of the string variable stored on-chain to “Hello”. The setGreeting function adds the _greeting provided to the greeting, but a transaction needs to be sent, which modifies the stored data. Lastly, the greet function retrieves the stored value.
Compile the smart contract
We will create a script that uses the Solidity compiler to output the bytecode and interface (ABI) for the Greeter.sol contract. To get started, we will create a compile.py file by running:
touch compile.py
Next, we will create the script for this file and complete the following steps:
Import the solcx package, which will compile the source code
Compile the Greeter.sol contract using the solcx.compile_files function
Export the contract's ABI and bytecode
Code and paste the code below into compile.py;
import solcx
solcx.install_solc('0.8.25')
# Compile contract
temp_file = solcx.compile_files(
'Greeter.sol',
output_values=['abi', 'bin'],
solc_version='0.8.25'
)
# Export contract data
abi = temp_file['Greeter.sol:Greeter']['abi']
bytecode = temp_file['Greeter.sol:Greeter']['bin']
You can now run the script to compile the contract:
python compile.py