[Ethereum] TimeoutError when using Infura websocket with web3.py

infuraweb3.pywebsocket

When I run the following code sometimes it executes successfully, but most of the time it raises a concurrent.futures._base.TimeoutError. What can I do to fix this?

Here is the code:

infura_url_ws = 'wss://mainnet.infura.io/ws/v3/MY-PROJECT-ID'
ws3 = Web3(Web3.WebsocketProvider(infura_url_ws))

abi = [{"name": "NewExchange", "inputs": [{"type": "address", "name": "token", "indexed": True}, {"type": "address", "name": "exchange", "indexed": True}], "anonymous": False, "type": "event"}] 
uniswap = ws3.eth.contract('0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95', abi=abi)

events = uniswap.events.NewExchange.createFilter(fromBlock=8567919).get_all_entries()
block_number = events[0]['blockNumber']
print(block_number)

Best Answer

Try limiting the amount of blocks to 1000

https://community.infura.io/t/getlogs-error-query-returned-more-than-1000-results/358/6

A infura dev mentions:

We currently limit response to blocks of 1000 responses, https://infura.io/docs/ethereum/json-rpc/eth_getLogs 90, the limitations are outlined in this doc.

Limiting the amount of blocks worked for me.

Related Topic