Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions polymarket_copier/core/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ async def _ws_connect_and_listen(self) -> None:
self._ws_healthy = True
logger.info("WebSocket connected: %s", self._ws_url)

# The subscription snapshot is per socket, so reconnects must resync it.
self._last_subscribed = set()
await self._maybe_update_subscription(ws)
heartbeat = asyncio.create_task(self._ws_heartbeat(ws))
try:
if self._subscribed_tokens:
await self._ws_send_subscription(ws, list(self._subscribed_tokens))

async for raw_msg in ws:
if self._stop_event.is_set():
break
Expand Down
39 changes: 39 additions & 0 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,45 @@ async def wake_sub_then_timeout_ping():
assert monitor._last_subscribed == {"tok-1"}
assert wait_calls == 2

@pytest.mark.asyncio
async def test_initial_ws_subscription_is_sent_once(self, monkeypatch):
"""Initial sync must update the snapshot so the first message does not resend it."""
sent = []
monitor = TradeMonitor(tracked_wallets=["0xabc"], on_trade=_noop_trade)
monitor.subscribe_token("tok-1")

class FakeWS:
def __init__(self):
self._messages = ["[]"]

async def send(self, msg):
sent.append(json.loads(msg))

def __aiter__(self):
return self

async def __anext__(self):
if self._messages:
return self._messages.pop()
raise StopAsyncIteration

class FakeConnection:
async def __aenter__(self):
return ws

async def __aexit__(self, *exc):
return False

ws = FakeWS()
monkeypatch.setattr(
"polymarket_copier.core.monitor.websockets.connect",
lambda *args, **kwargs: FakeConnection(),
)

await monitor._ws_connect_and_listen()

assert sent == [{"type": "market", "assets_ids": ["tok-1"]}]


# ─── C5: set_wallets — rebalance without KeyError on new wallets ──────────────

Expand Down
Loading