I wrote a simple solidity contract to write and read from the contract. I followed https://ethereum.stackexchange.com/questions/134720/execute-a-contract-function-from-web3-py answer. However, when I run the contract using below Python code, it neither writes values into mapping structure nor retrieve values from functions. The contract mapping structure already has a few entries which are written during execution in remix IDE in sepolia test network. I want do the same thing using Python. Please find the code below and correct me if I am wrong somewhere.

from web3 import Web3, EthereumTesterProvider

provider_url = "https://sepolia.infura.io/v3/f9xxxxxxxxxxxxxxxxxxxxxxxx" # url of sepolia n/w provided by infura

w3 = Web3(Web3.HTTPProvider(provider_url))
print (w3.isConnected())
abi = '[{"inputs": [ {"internalType": "string", "name": "HPW", "type": "string"} ], "name": "retrieve_reg_details", "outputs": [ {  "components": [ { "internalType": "string", "name": "NVID", "type": "string" }, { "internalType": "uint256[]", "name": "teq_coff", "type": "uint256[]" }, { "internalType": "int256", "name": "degree", "type": "int256" }, { "internalType": "int256", "name": "Cv", "type": "int256" }, { "internalType": "int256", "name": "revoc_status", "type": "int256" } ], "internalType": "struct Reg_SC1.reg_struct", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "string", "name": "HPWi", "type": "string" }, { "internalType": "string", "name": "NVID", "type": "string" }, { "internalType": "uint256[]", "name": "teq_coff", "type": "uint256[]" }, { "internalType": "int256", "name": "degree", "type": "int256" }, { "internalType": "int256", "name": "Cv", "type": "int256" }, { "internalType": "int256", "name": "revoc_status", "type": "int256" }], "name": "store_reg_details", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "string", "name": "", "type": "string" } ], "name": "store_veh_reg", "outputs": [ { "internalType": "string", "name": "NVID", "type": "string" }, { "internalType": "int256", "name": "degree", "type": "int256" }, { "internalType": "int256", "name": "Cv", "type": "int256" }, { "internalType": "int256", "name": "revoc_status", "type": "int256"}], "stateMutability": "view", "type": "function" }]'

acct_address = "0x87xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
sc_address = "0x4bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
private_key = "45xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

nonce = w3.eth.getTransactionCount(acct_address)

cont_instance = w3.eth.contract(address = sc_address, abi = abi) # creates an instance of contract

Chain_id = w3.eth.chain_id
print ("chainn__id is ", Chain_id)
call_function = cont_instance.functions.store_reg_details("veh3", "nd98d", [2,6,2], 6, 21, 1).buildTransaction({"chainId": Chain_id, "from": acct_address, "nonce": nonce})

signed_tx = w3.eth.account.sign_transaction(call_function, private_key=private_key)

send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)

tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)

print("tx receipt is ", tx_receipt)

get_latest_data = cont_instance.functions.retrieve_reg_details('veh1').call() # run 

print("Details of veh 1 is ", get_latest_data)

Below is my Solidity code

//SPDX-License-Identifier: MIT

pragma solidity >=0.8.2 <0.9.0;

contract Reg_SC1 
    struct reg_struct
       string NVID ;
       uint[] teq_coff ;
       int degree ;
       int Cv ;
       int revoc_status ;

    uint256 number ;
    // string HPW ;

    mapping (string => reg_struct) public store_veh_reg ;

    function store_reg_details(string memory HPWi, string memory NVID, uint[] memory teq_coff, int degree, int Cv, int  revoc_status) public 
        store_veh_reg[HPWi] = reg_struct(NVID, teq_coff, degree, Cv, revoc_status) ;

    function retrieve_reg_details(string memory HPW) public view returns (reg_struct memory)
        return store_veh_reg[HPW];

Here is the output I get in terminal. I think the data values are not written into the contract. enter image description here

enter image description here

Thank you.

  • Can you share the exact contract code used? One odd thing is you are writing veh3 and reading veh1.
    – Ismael
    Feb 13, 2023 at 4:39
  • Sir, I already stored veh1 details from remix. It was successfully. Now I want read it from python code. So, I wrote veh1 in retrieve_reg_details call. veh3 details in store_reg_details call is a new entry I want to write from python. The code is exact contract I shared in this question. I just masked the acct_address, contract address, and my private key. Code is same. Thank you. Feb 13, 2023 at 5:33
  • You shared the python code, but you haven't shared the solidity code.
    – Ismael
    Feb 13, 2023 at 5:39
  • Sorry. I updated solidity code in question. Feb 13, 2023 at 5:51
  • Sir, I shared the solidity contract code in question. Please check .. Feb 14, 2023 at 5:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.