Web3.py

將 Infura websocket 與 web3.py 一起使用時出現超時錯誤

  • November 8, 2021

當我執行以下程式碼時,有時它會成功執行,但大多數時候它會引發concurrent.futures._base.TimeoutError. 我能做些什麼來解決這個問題?

這是程式碼:

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)

嘗試將塊數限制為 1000

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

一位 infura 開發人員提到:

我們目前將響應限制為 1000 個響應塊,https://infura.io/docs/ethereum/json-rpc/eth_getLogs 90,此文件中概述了限制。

限制對我有用的塊數量。

web3.py 需要websocket_timeout=60WebsocketProvider 的參數:

from web3 import Web3
w3 = Web3(Web3.WebsocketProvider("ws://127.0.0.1:8546", websocket_timeout=60))

如果您使用的是 HTTP,則request_kwargs={'timeout':60}改為:

from web3 import Web3
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545", request_kwargs={'timeout': 60}))

文件: https ://web3py.readthedocs.io/en/stable/providers.html

引用自:https://ethereum.stackexchange.com/questions/76092