2

Goal: I want to print logs of the smart contract, like I do on web3.js, using web3.py.

On web3.js side, following code piece works:

var event = myContract.LogJob({}, {fromBlock:0, toBlock:'latest'});
event.watch(function(error, result) {
  console.log(JSON.stringify(result));
});

But on the web3.py side following line returns: [].

=> On my smart contract I did not use the emit keyword before (event Deposit(...)), could it be the reason?

Example Small Contract Piece:

pragma solidity ^0.4.20; 

contract Contract {

  function deposit(bytes32 _id) payable {
     LogJob(msg.sender);
  }
  event LogJob(address indexed _from);
}

--

web3.py call:

#!/usr/bin/env python -W ignore::DeprecationWarning

from __future__ import print_function
import os, sys
from web3 import Web3
import json
from web3.providers.rpc import HTTPProvider

os.chdir(sys.path[0]);

web3 = Web3(HTTPProvider('http://localhost:8545'))

fileAddr = open("address.json", "r")
contractAddress = fileAddr.read().replace("\n", "")

with open('abi.json', 'r') as abi_definition:
    abi = json.load(abi_definition)

Contract = web3.eth.contract(contractAddress, abi=abi);

transfer_filter = Contract.on('LogJob', {'filter': {'clusterAddr': clusterAddress}})
print(transfer_filter.get())

Returns:

Traceback (most recent call last):
  File "getClusterInfo.py", line 54, in <module>
    transfer_filter = Contract.on('LogJob', {'filter': {'clusterAddr': clusterAddress}})
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/decorators.py", line 13, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/web3/contract.py", line 345, in on
    argument_filter_names,
  File "/usr/local/lib/python2.7/dist-packages/web3/contract.py", line 647, in _find_matching_event_abi
    raise ValueError("No matching functions found")
ValueError: No matching functions found

=> On web3.py can we also filer events from specific blockNumber?

7
  • Could you update your question with some more info: Which version of web3.py are you on? (looks like v3) Would you add the code where you initialize the myContract instance?
    – carver
    Mar 23, 2018 at 16:09
  • You could try using ConciseContract -> Contract = web3.eth.contract(contractAddress, abi=abi,ContractFactoryClass=ConciseContract) as per this.
    – Lee
    Mar 28, 2018 at 15:54
  • It gives following output: NameError: name 'ConciseContract' is not defined
    – alper
    Mar 28, 2018 at 15:56
  • On re-reading - the answer suggests removing ` ConciseContract ` anyway...
    – Lee
    Mar 28, 2018 at 16:29
  • 1
    I'm out of ideas right now... perhaps @carver will see the update...
    – Lee
    Mar 28, 2018 at 16:34

1 Answer 1

2

This has been working for me (web3.py 4.0.0b11):

my_filter = contract.eventFilter('LogJob',{'fromBlock':0,'toBlock':'latest'})

print(my_filter.get_all_entries())

or

print(my_filter.get_new_entries())

After deploying contract like this:

contract = w3.eth.contract(abi=data,bytecode=bc)
tx_hash = contract.deploy(transaction={'from': w3.eth.accounts[0], 'gas': 10000000})
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
contract_address = tx_receipt['contractAddress']
contract_instance = w3.eth.contract(address=contract_address, abi=abi, ContractFactoryClass=ConciseContract)

Note, I do have the emit keyword in the contract so haven't checked that...

It could matter if you've used ConciseContract -> https://ethereum.stackexchange.com/a/31584/820

See the docs: http://web3py.readthedocs.io/en/latest/contracts.html#web3.contract.Contract.eventFilter

Also Web3.py EventFilter.get_new_entries() always empty

15
  • Its interesting that Events successfully show up on the web3.js side. I will try with emit keyword and see does it change anything. @atomh33ls
    – alper
    Mar 23, 2018 at 11:39
  • 1
    @Alper perhaps you could show how you created the myContract object?
    – Lee
    Mar 23, 2018 at 13:20
  • myContract was just example that has the event section on my code. I used remix.solidity in order to deploy my contract. @atomh33ls
    – alper
    Mar 23, 2018 at 13:23
  • @Alper what do you get with print(myContract)'? I get <class 'web3.utils.datatypes.Contract'>
    – Lee
    Mar 23, 2018 at 13:25
  • 1
    @Alper Looks like there might have been an update to the filter mechanism in v4, so that might be why my answer didn't work for you...
    – Lee
    Mar 28, 2018 at 15:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.