4
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)
1
  • A lot more background would help: What happens when you run the code? What version of web3py are you using? What kind of node are you using? How are you triggering the event? Commented Apr 16, 2018 at 18:25

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.