Skip to main content

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:

  1. 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
  2. Contract address, for the KUTHULU contract: 0x8099214d6CaE16744a541E8E8C5474AB373feC76
  3. 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:

  1. Your wallet address.
  2. Your private key.
  3. 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

import web3 from web3.middleware import geth_poa_middleware url = ‘https://rpc-mumbai.matic.today’ #connect to the blockchain w3 = web3.Web3(web3.HTTPProvider(url)) abi = [{“inputs”:[{“internalType”:”contract IERC20″,”name”:”_paymentToken”,”type”:”address”},{“internalType”:”address”,”name”:”_userProfiles”,”type”:”address”},{“internalType”:”address”,”name”:”_kutils”,”type”:”address”}],”stateMutability”:”nonpayable”,”type”:”constructor”},{“anonymous”:False,”inputs”:[{“indexed”:False,”internalType”:”address”,”name”:”account”,”type”:”address”}],”name”:”Paused”,”type”:”event”},{“anonymous”:False,”inputs”:[{“indexed”:False,”internalType”:”address”,”name”:”account”,”type”:”address”}],”name”:”Unpaused”,”type”:”event”},{“anonymous”:False,”inputs”:[{“indexed”:True,”internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″},{“indexed”:True,”internalType”:”address”,”name”:”poster”,”type”:”address”}],”name”:”logEraseMsg”,”type”:”event”},{“anonymous”:False,”inputs”:[{“indexed”:True,”internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″},{“indexed”:True,”internalType”:”address”,”name”:”postedBy”,”type”:”address”},{“indexed”:False,”internalType”:”string”,”name”:”message”,”type”:”string”},{“indexed”:False,”internalType”:”string[]”,”name”:”hashtags”,”type”:”string[]”},{“indexed”:False,”internalType”:”address[]”,”name”:”taggedAccounts”,”type”:”address[]”}],”name”:”logMsgPostMsg1″,”type”:”event”},{“anonymous”:False,”inputs”:[{“indexed”:True,”internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″},{“indexed”:True,”internalType”:”address”,”name”:”proxy”,”type”:”address”},{“indexed”:False,”internalType”:”string”,”name”:”uri”,”type”:”string”},{“indexed”:False,”internalType”:”uint256[4]”,”name”:”attribs”,”type”:”uint256[4]”},{“indexed”:False,”internalType”:”uint256[]”,”name”:”inGroups”,”type”:”uint256[]”}],”name”:”logMsgPostMsg2″,”type”:”event”},{“inputs”:[],”name”:”Blocking”,”outputs”:[{“internalType”:”contract IBlocking”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”DOOM”,”outputs”:[{“internalType”:”contract IDOOM”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Groups”,”outputs”:[{“internalType”:”contract IGroups”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Hashtags”,”outputs”:[{“internalType”:”contract IHashtags”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”KUtils”,”outputs”:[{“internalType”:”contract IKUtils”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Likes”,”outputs”:[{“internalType”:”contract ILikes”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”MessageData”,”outputs”:[{“internalType”:”contract IMessageData”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Posts”,”outputs”:[{“internalType”:”contract IPosts”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Tagged”,”outputs”:[{“internalType”:”contract ITagged”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”Tips”,”outputs”:[{“internalType”:”contract ITips”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”newAdmin”,”type”:”address”}],”name”:”addAdmin”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”commentCounter”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”costToPost”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”cutToPoster”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″}],”name”:”eraseMsg”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”userToFollow”,”type”:”address”}],”name”:”followUser”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”usrAddress”,”type”:”address”},{“internalType”:”uint256″,”name”:”startFrom”,”type”:”uint256″},{“internalType”:”bool”,”name”:”getUserComments”,”type”:”bool”},{“internalType”:”bool”,”name”:”getUserReposts”,”type”:”bool”}],”name”:”getMsgIDsByAddress”,”outputs”:[{“internalType”:”uint256[]”,”name”:””,”type”:”uint256[]”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”string”,”name”:”hashtag”,”type”:”string”},{“internalType”:”uint256″,”name”:”startFrom”,”type”:”uint256″}],”name”:”getMsgIDsByHashtag”,”outputs”:[{“internalType”:”uint256[]”,”name”:””,”type”:”uint256[]”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”taggedUser”,”type”:”address”},{“internalType”:”uint256″,”name”:”startFrom”,”type”:”uint256″}],”name”:”getMsgIDsByTag”,”outputs”:[{“internalType”:”uint256[]”,”name”:””,”type”:”uint256[]”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”uint256[]”,”name”:”msgIDs”,”type”:”uint256[]”}],”name”:”getMsgsByIDs”,”outputs”:[{“internalType”:”string[][]”,”name”:””,”type”:”string[][]”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″},{“internalType”:”uint256″,”name”:”startFrom”,”type”:”uint256″},{“internalType”:”bool”,”name”:”isRepost”,”type”:”bool”}],”name”:”getSubIDsByPost”,”outputs”:[{“internalType”:”uint256[]”,”name”:””,”type”:”uint256[]”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”maxHashtags”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”maxMessageLength”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”maxMsgReturnCount”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”maxTaggedAccounts”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”msgCounter”,”outputs”:[{“internalType”:”uint256″,”name”:””,”type”:”uint256″}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”pause”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”paused”,”outputs”:[{“internalType”:”bool”,”name”:””,”type”:”bool”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”paymentToken”,”outputs”:[{“internalType”:”contract IERC20″,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[{“internalType”:”string”,”name”:”message”,”type”:”string”},{“internalType”:”string[]”,”name”:”_hashtags”,”type”:”string[]”},{“internalType”:”address[]”,”name”:”taggedAccounts”,”type”:”address[]”},{“internalType”:”string”,”name”:”uri”,”type”:”string”},{“internalType”:”uint256[4]”,”name”:”attribs”,”type”:”uint256[4]”},{“internalType”:”uint256[]”,”name”:”inGroups”,”type”:”uint256[]”}],”name”:”postMsg”,”outputs”:[],”stateMutability”:”payable”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”oldAdmin”,”type”:”address”}],”name”:”removeAdmin”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”uint256″,”name”:”_maxMsgReturnCount”,”type”:”uint256″},{“internalType”:”uint256″,”name”:”_maxMessageLength”,”type”:”uint256″}],”name”:”setParams”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”uint256″,”name”:”msgID”,”type”:”uint256″}],”name”:”toggleLike”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”address”,”name”:”userToUnfollow”,”type”:”address”}],”name”:”unfollowUser”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”unpause”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”contract IERC20″,”name”:”_payments”,”type”:”address”},{“internalType”:”address[]”,”name”:”contracts”,”type”:”address[]”}],”name”:”updateContracts”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[{“internalType”:”uint256″,”name”:”_costToPost”,”type”:”uint256″},{“internalType”:”uint256″,”name”:”_cutToPoster”,”type”:”uint256″}],”name”:”updateCosts”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”userProfiles”,”outputs”:[{“internalType”:”contract IUserProfiles”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”}] contract_address = “0x8099214d6CaE16744a541E8E8C5474AB373feC76” # inject the middleware to the outerlayer w3.middleware_onion.inject(geth_poa_middleware, layer=0) # get contract object contract = w3.eth.contract(address=contract_address, abi = abi) # Account information # Your wallet address caller = “” # Your private key key = “” # Message to post message = “Hello world!” hashtags = [] taggedAccounts = [] uri = “” attribs = [1,0,0,0] inGroups = [] # Reading from the blockchain contract.functions.getMsgsByIDs(54).call() # Writing to the blockchain # DOOM tokens are needed for this contract, make sure you have sufficient funds before you write! nonce = w3.eth.getTransactionCount(caller) chain_id = w3.eth.chain_id call_function = contract.functions.postMsg( message, hashtags, taggedAccounts, uri, attribs, inGroups ).build_transaction( {“chainId”: chain_id, “from”: caller, “nonce”: nonce} ) signed_tx = w3.eth.account.sign_transaction(call_function, private_key=key) send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction) tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx) print(tx_receipt)