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
?
myContract
instance?ConciseContract
->Contract = web3.eth.contract(contractAddress, abi=abi,ContractFactoryClass=ConciseContract)
as per this.NameError: name 'ConciseContract' is not defined