0

I'm struggling to understand how to properly poll events emitted from contracts. The provider I'm using is Infura. My code boils down to the following:

filter = target_event.createFilter(fromBlock='latest', toBlock='latest')
filter.get_all_entries()  # https://ethereum.stackexchange.com/a/36070
while True:
    event_list = filter.get_new_entries()
    print(event_list)
    time.sleep(30)

However, sometimes the result of event_list contain duplicate events or contains events from the previous poll

[AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc', 'amount0In': 202115004858193736, 'amount1In': 0, 'amount0Out': 0, 'amount1Out': 11531582701392718163}), 'event': 'Swap', 'logIndex': 310, 'transactionIndex': 150, 'transactionHash': HexBytes('0x08c80cb15e05468a4639b3e19fd4d9ec39f1a342c86af70dadf6bdd550913077'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0x058a27096fbce19551957db533736e3517176aae6b1615c155441aa6388bfaea'), 'blockNumber': 10803004}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x343E3a490c9251dC0eaA81Da146ba6ABe6C78b2d', 'amount0In': 0, 'amount1In': 32571538434184860160, 'amount0Out': 567376343539258042, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 236, 'transactionIndex': 84, 'transactionHash': HexBytes('0xce0714184433cee4ee6da3e16fe6fc854af07139d02511b7e9ea983a1ce55f4d'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xdab7a466808beb7ed85054fdd4edf64ec035edd6728f34e83a882f15032cbc33'), 'blockNumber': 10803005}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x8B97e11549B7662664Dab4ba694a75759e8eB40b', 'amount0In': 0, 'amount1In': 2533384640774189012, 'amount0Out': 44118589552568984, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 416, 'transactionIndex': 131, 'transactionHash': HexBytes('0xd808e35ed40c588ea63fbd71c21748b250829a3f78b6b1e077c549adc8c36181'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xdab7a466808beb7ed85054fdd4edf64ec035edd6728f34e83a882f15032cbc33'), 'blockNumber': 10803005})]

[AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x343E3a490c9251dC0eaA81Da146ba6ABe6C78b2d', 'amount0In': 0, 'amount1In': 32571538434184860160, 'amount0Out': 567376343539258042, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 236, 'transactionIndex': 84, 'transactionHash': HexBytes('0xce0714184433cee4ee6da3e16fe6fc854af07139d02511b7e9ea983a1ce55f4d'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xdab7a466808beb7ed85054fdd4edf64ec035edd6728f34e83a882f15032cbc33'), 'blockNumber': 10803005}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x8B97e11549B7662664Dab4ba694a75759e8eB40b', 'amount0In': 0, 'amount1In': 2533384640774189012, 'amount0Out': 44118589552568984, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 416, 'transactionIndex': 131, 'transactionHash': HexBytes('0xd808e35ed40c588ea63fbd71c21748b250829a3f78b6b1e077c549adc8c36181'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xdab7a466808beb7ed85054fdd4edf64ec035edd6728f34e83a882f15032cbc33'), 'blockNumber': 10803005}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x343E3a490c9251dC0eaA81Da146ba6ABe6C78b2d', 'amount0In': 0, 'amount1In': 32571538434184860160, 'amount0Out': 567376343539258042, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 100, 'transactionIndex': 28, 'transactionHash': HexBytes('0xce0714184433cee4ee6da3e16fe6fc854af07139d02511b7e9ea983a1ce55f4d'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xc3a35b65ef22e3df187327c07eb298949d2727bfd2ceefecd25338a72cae5bcf'), 'blockNumber': 10803006}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x8B97e11549B7662664Dab4ba694a75759e8eB40b', 'amount0In': 0, 'amount1In': 2534112830414141134, 'amount0Out': 44131270651407806, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 274, 'transactionIndex': 80, 'transactionHash': HexBytes('0xd808e35ed40c588ea63fbd71c21748b250829a3f78b6b1e077c549adc8c36181'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0xc3a35b65ef22e3df187327c07eb298949d2727bfd2ceefecd25338a72cae5bcf'), 'blockNumber': 10803006}), AttributeDict({'args': AttributeDict({'sender': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'to': '0x1FcEC0f5B245Afa2B0ca81A785073d6F9DE790bb', 'amount0In': 0, 'amount1In': 13801238945027575759, 'amount0Out': 240318030422157735, 'amount1Out': 0}), 'event': 'Swap', 'logIndex': 108, 'transactionIndex': 72, 'transactionHash': HexBytes('0xdf7db33c631c649f2e1bb0c974ee906f09bb9e67799a0150cd83a94cd41eaed6'), 'address': '0x2fDbAdf3C4D5A8666Bc06645B8358ab803996E28', 'blockHash': HexBytes('0x1f63e1a2a836678fec4a8629b468f5456b26aa393886586c4bfe2705878203c8'), 'blockNumber': 10803008})]

Here the tx 0xce0714184433cee4ee6da3e16fe6fc854af07139d02511b7e9ea983a1ce55f4d is in both event_lists and also occurs twice in the second array.

I have tried using the following for the filter but these problems still persist

filter = target_event.createFilter(fromBlock='latest')
filter = target_event.createFilter(fromBlock=0, toBlock='latest')

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.