0

I have an issue to manage with my listener in python, here is my code :

def handle_event(event, log_index):
    receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])

    contract_events = [contract.events._BlockOnSale, contract.events._BlockPurchased]

    for contract_event in contract_events:
        result_event = contract_event().processReceipt(receipt)
        for result in result_event:
            result_request = switcher[result['event']](result['args'], event['blockNumber'])
            print(result_request)



def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event, event['logIndex'])
        time.sleep(poll_interval)

my issue is that, if there is 2 transactions in the same block, I can have duplicate. I mean :

in the event_filter.get_new_entries(), I have two entries with exact same transaction hash, but logIndex is different. So I have 2 loop in my log_loop function. the issue is that the line :

receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])

is getting the two event in one shot because the transactionHash is the same.

So is there a way to properly manage this issue ? In a first point I do that :

def handle_event(event, log_index):
    receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])

    contract_events = [contract.events._BlockOnSale, contract.events._BlockPurchased]

    for contract_event in contract_events:
        result_event = contract_event().processReceipt(receipt)
        for result in result_event:
            if result['logIndex'] == log_index:
                result_request = switcher[result['event']](result['args'], event['blockNumber'])
                 print(result_request)

to process only the event with the good logIndex, but it is not acceptable : If I have 60 event in the same block, I will do a loop of 60x60 just to parse 60 event. So in the receipt, is there a way to get the event by transactionHash AND logIndex?

or in the get_new_entries, if the transactionHash is the same, to have only 1 entries ?

I want to stop having a lot of duplicate event to manage

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.