Web3.py – How to Access Contract Functions Using Web3.py?

eventspythonsolidityweb3.py

I have already created contract and deployed to ethereum mainnet. Now I wanna read logs. But I can't as calling print(contract_instance.address) and contract_instance.eventFilter('Transfers') show me error log. What am I doing wrong?

import web3

from web3 import Web3, KeepAliveRPCProvider, IPCProvider
from web3.contract import ConciseContract
from solc import compile_source


ADDRESS = "..."
START_BLOCK = 4551217
END_BLOCK = 4599662

contract_source_code = '''...
'''


compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:CustomToken']


web3 = Web3(KeepAliveRPCProvider(host='...', port='8545'))

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS, ContractFactoryClass=ConciseContract)
print(contract_instance.address)

transfer_filter = contract_instance.eventFilter('Transfers')

Error logs:

Traceback (most recent call last):
  File "analysis.py", line 488, in <module>
    print(contract_instance.creator())
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 803, in __call__
    return self.__prepared_function(**kwargs)(*args)
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 832, in call_contract_function
    transaction=transaction,
  File "/usr/local/lib/python3.6/site-packages/web3/utils/decorators.py", line 13, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 692, in _prepare_transaction
    fn_kwargs,
  File "/usr/local/lib/python3.6/site-packages/eth_utils/string.py", line 85, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 731, in _encode_transaction_data
    fn_name, args, kwargs,
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 660, in _get_function_info
    fn_abi = cls._find_matching_fn_abi(fn_name, args, kwargs)
  File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 624, in _find_matching_fn_abi
    raise ValueError("No matching functions found")
ValueError: No matching functions found

Best Answer

v3 vs v4

Update: The question was written using web3.py v3, when v3 was the latest stable version. v4 is now stable, and preferred. Note that there are docs available for both versions: v3 (old) and v4 (current).

eventFilter in v3

eventFilter is only available in v4. The simplest solution is now to upgrade, with pip install --upgrade web3. If you can't, then use the v3 event methods

Classic vs ConciseContract

It looks like you want to use the classic contract interface, since ConciseContract is better suited for simple contract calls. So remove the ConciseContract variant in this line:

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS, ContractFactoryClass=ConciseContract)

The changed line (below) would then provide access to the on and pastEvents methods.

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS)

Contract Argument Order

Also, the positional ABI argument option is deprecated in v3, and removed in v4. So you should use this line instead, which works in both v3 and v4:

contract_instance = web3.eth.contract(ADDRESS, abi=contract_interface['abi'])