Reputation: 143
pragma solidity ^0.4.17
contract Test {
event greeting(string name);
function say() pure public {
greeting('jack');
}
}
how to get the event data 'jack' when the say() function called in the python version web3.py? below is my python code.
contractAddress = '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
contract = w3.eth.contract(address=contractAddress, abi=abiJson['abi'])
accounts = w3.eth.accounts
def handle_event(event):
print(event)
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event(event)
time.sleep(poll_interval)
block_filter = w3.eth.filter({'fromBlock':'latest', 'address':contractAddress})
log_loop(block_filter, 2)
Upvotes: 4
Views: 13039
Reputation: 143
The problem has been solved; the right code should be look like below:
contractAddress = '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
contract = w3.eth.contract(address=contractAddress, abi=abiJson['abi'])
accounts = w3.eth.accounts
def handle_event(event):
receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])
result = contract.events.greeting.processReceipt(receipt)
print(result[0]['args'])
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event(event)
time.sleep(poll_interval)
block_filter = w3.eth.filter({'fromBlock':'latest', 'address':contractAddress})
log_loop(block_filter, 2)
Upvotes: 4
Reputation: 41
I found your code really helpful to see how to extract the info of an event, but I faced an issue. I'll write it here just in case it can help anyone.
The line
result = contract.events.greeting.processReceipt(receipt)
was throwing the error:
_parse_logs() missing 1 required positional argument: 'errors'
After some digging it seems to be a limitation of Web3py and it can be fixed with a previous instantiation of the event. So, the code would be:
contractAddress = '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
contract = w3.eth.contract(address=contractAddress, abi=abiJson['abi'])
accounts = w3.eth.accounts
greeting_Event = contract.events.greeting() # Modification
def handle_event(event):
receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])
result = greeting_Event.processReceipt(receipt) # Modification
print(result[0]['args'])
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event(event)
time.sleep(poll_interval)
block_filter = w3.eth.filter({'fromBlock':'latest', 'address':contractAddress})
log_loop(block_filter, 2)
Upvotes: 4