I want is to listen to the event "IPFSHash" defined in a Solidity contract in a python module using web3.py (with blockchain created by ganache-cli, transactions triggered through Remix IDE also connected through Web3 Provider). I tried using this construct:
event_filter = my_contract.events.IpfsHash.createFilter(fromBlock='latest')
while{True}:
event_filter.get_new_entries()
However, I don't get any new entries in the filter when triggering the event through Remix. I can however call the getHash function to confirm that the value of "hash" is changed in the contract and the event has been emitted (full code below).
Thanks!!!
Web3.py code:
import json
from web3.providers.rpc import HTTPProvider
from web3 import Web3
import time
w3 = Web3(HTTPProvider('http://localhost:8545'))
contract_address =
Web3.toChecksumAddress("0xa293277dcd6f2e98ba63583fb81ed72f7555c8ef")
with open('contract_abi.json', 'r', encoding='utf-8') as abi_file:
contract_abi = json.loads(abi_file.read())
my_contract = w3.eth.contract(address=contract_address, abi=contract_abi)
event_filter = my_contract.events.IpfsHash.createFilter(fromBlock='latest')
poll_interval = 2
def handle_event(event):
print("Event triggered")
while True:
print("New entries: ", len(event_filter.get_new_entries()))
for event in event_filter.get_new_entries():
handle_event(event)
print("Get Hash:", contract.functions.getHash().call())
time.sleep(poll_interval)
Solidity contract code:
pragma solidity ^0.4.21;
contract HashEventTest{
bytes public hash;
event IpfsHash(
bytes ipfs_hash,
address sender
);
function HashEventTest(bytes _hash) public {
hash = _hash;
}
function setHash(bytes _hash) public {
hash = _hash;
emit IpfsHash(hash, msg.sender);
}
function getHash() public constant returns (bytes){
return hash;
}
}