我制作了一个小型机器人,它可以为我提供在 dex 上创建的最新配对。
但是大约 1 小时后,机器人开始运行,我不断收到这 2 个错误:
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
重新启动机器人后,我收到此错误:
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError
所以有时我得到第一个错误,有时我得到下面的错误。
我怎样才能解决这个问题?
我的 ws 连接如下所示:
wss = 'wss://speedy-nodes-nyc.moralis.io/APIKEY GOES HERE/bsc/mainnet/ws'
web3 = Web3(Web3.WebsocketProvider(wss))
print(web3.isConnected())
如果这很重要,我正在使用 python 和 asyncio,我与 asyncio 一起使用的代码也在这里:
def handle_event(event):
#print(Web3.toJSON(event))
# and whatever
pair = Web3.toJSON(event)
#print(pair)
async def log_loop(event_filter, poll_interval):
while True:
for PairCreated in event_filter.get_new_entries():
handle_event(PairCreated)
await asyncio.sleep(poll_interval)
# when main is called
# create a filter for the latest block and look for the "PairCreated" event for the uniswap factory contract
# run an async loop
# try to run the log_loop function above every 2 seconds
def mainlist():
event_filter = contract.events.PairCreated.createFilter(fromBlock='latest')
#block_filter = web3.eth.filter('latest')
# tx_filter = web3.eth.filter('pending')
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
asyncio.gather(
log_loop(event_filter,2 )))
# log_loop(block_filter, 2),
# log_loop(tx_filter, 2)))
finally:
# close loop to free up system resources
loop.close()