Here is a quick example in Python to show how easy it is to interact with any Solidity blockchain smart contract on an EVM with some simple python code. I’ll be using the KUTULU contract on polygon test net for my example.
First, we need the following things:
Contract Information:
- RPC URL to specify the blockchain to connect to can be found onhttps://rpc.info/. For polygon test net, I’ll have to use:https://rpc-mumbai.matic.today
- Contract address, for the KUTHULU contract: 0x8099214d6CaE16744a541E8E8C5474AB373feC76
- Contract ABI, which are methods specified by thecreator of the contract. Can be found with the contract address via a chain explorere.g.https://mumbai.polygonscan.com/
Account information after signing up for an account on MetaMask:
- Your wallet address.
- Your private key.
- Make sure you have sufficient funds of MATICif you are going to perform a write to the blockchain! (https://faucet.polygon.technology/)
Next, we need the web3 module installed: pip install web3
I use Visual Studio Codes for my IDE. I recommend using Jupyter notebook as I find it way easier to test pieces of code as you progress. Since we are connecting the polygon test net, we need use the geth_poa_middleware, details can be found at:https://web3py.readthedocs.io/en/v5/middleware.html
With Jupyter notebook, I like to run chucks of code to check that everything is correct:
Note that I can see all the available functions in the contract because my contract object has been successfully returned. Awesome!
Let’s try out a simple read:
Nice, we just read a message stored on the blockchain!
Now let’s try to post a message:
Now my post will live on FOREVER!
Thank you,
Sam Ng
Identity and Blockchain Engineer @ Nyedis