# Records the market channel for the markets in gamma-markets.json: one JSON # line per frame, {"received_at_us": , "text": }. import asyncio, json, sys, time import websockets markets = json.load(open(sys.argv[1])) tokens = [t for m in markets if m["enableOrderBook"] for t in json.loads(m["clobTokenIds"])] async def main(out, seconds): async with websockets.connect("wss://ws-subscriptions-clob.polymarket.com/ws/market", max_size=None) as ws: await ws.send(json.dumps({"type": "market", "assets_ids": tokens})) loop = asyncio.get_event_loop(); end = loop.time() + seconds; last_ping = loop.time() while loop.time() < end: if loop.time() - last_ping >= 10: await ws.send("PING"); last_ping = loop.time() try: text = await asyncio.wait_for(ws.recv(), 1) except asyncio.TimeoutError: continue received_at_us = time.time_ns() // 1000 out.write(json.dumps({"received_at_us": received_at_us, "text": text}) + "\n") with open(sys.argv[2], "w") as out: asyncio.run(main(out, float(sys.argv[3])))