0%

evm | websocket 对接

目前一共有下面几种订阅方式

  • alchemy_newFullPendingTransactions
    • 部分节点可能不支持
    • 返回 pending 池中所有待交易的信息
  • newHeads
    • 基础信息,头信息
  • logs
    • 带有两种参数
    • 具体看上面的参考资料

python 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import asyncio
import json
import aiohttp

blocknumber = 11


async def ws():
global blocknumber
session = aiohttp.ClientSession()
async with session.ws_connect('wss://bsc-mainnet.nodereal.io/ws/v1/***') as ws:
await ws.send_str('{"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["logs"]}')
async for msg in ws:
try:
_blocknumber = int(json.loads(msg.data).get("params").get("result").get("blockNumber"), 16)
if blocknumber != _blocknumber:
print(blocknumber)
blocknumber = _blocknumber
except Exception as e:
pass


if __name__ == '__main__':
asyncio.run(ws())
请我喝杯咖啡吧~