Web3.py – How to Call Functions on a Deployed Contract

contract-invocationpythonweb3.py

I have a working private network in which i have successfully deployed a contract using python. I have its address and i can see the deployment in the node log, but i cant call any functions and it seems like web3py is blocking them. For any call i make, ie:

mycontract=w3.eth.contract(address=contractAddress, abi=abi)
print(instance.functions.getId().call())

I receive the following error:

    Traceback (most recent call last):
  File "/home/user/eth/contracts/contract01/script.py", line 69, in <module>
    print(instance.functions.getId().call())
  File "/home/user/.local/lib/python3.10/site-packages/web3/contract/contract.py", line 461, in call
    return call_contract_function(
  File "/home/user/.local/lib/python3.10/site-packages/web3/contract/utils.py", line 96, in call_contract_function
    return_data = w3.eth.call(
  File "/home/user/.local/lib/python3.10/site-packages/web3/eth/eth.py", line 255, in call
    return self._durin_call(transaction, block_identifier, state_override)
  File "/home/user/.local/lib/python3.10/site-packages/web3/eth/eth.py", line 274, in _durin_call
    return self._call(transaction, block_identifier, state_override)
  File "/home/user/.local/lib/python3.10/site-packages/web3/module.py", line 68, in caller
    result = w3.manager.request_blocking(
  File "/home/user/.local/lib/python3.10/site-packages/web3/manager.py", line 232, in request_blocking
    return self.formatted_response(
  File "/home/user/.local/lib/python3.10/site-packages/web3/manager.py", line 205, in formatted_response
    raise ValueError(response["error"])
ValueError: {'code': -32000, 'message': 'invalid opcode: SHR'}

I've already deployed the contract so the connection to the network is not a problem and i can also succesfully make some calls like all_functions. The error message makes me think the call gets somehow blocked. Anyway i've read the docs and also tried everything i've found online (which is not much) but cant seem to find any solutions or even the cause.
The network was set up using geth 1.11.6, and the web3py version is 6.4.0.

Just in case there is something im not seeing, the function called is this:

string id="foo";

function getId() public view returns (string memory) {
    return id;
}

Update: Comment was really helpful. Byzantium and constantinople blocks were missing on genesis file.

Best Answer

As can be seen in this question, the genesis file was wrong. Big thanks to Ricardo Passos who found it. The Genesis and Constantinople blocks were not defined, which was the cause of the error.