diff --git a/src/polymarket_insider_tracker/ingestor/models.py b/src/polymarket_insider_tracker/ingestor/models.py index ac7b29e..618ccba 100644 --- a/src/polymarket_insider_tracker/ingestor/models.py +++ b/src/polymarket_insider_tracker/ingestor/models.py @@ -86,7 +86,7 @@ class Orderbook: bids: tuple[OrderbookLevel, ...] asks: tuple[OrderbookLevel, ...] tick_size: Decimal - timestamp: datetime = field(default_factory=datetime.utcnow) + timestamp: datetime = field(default_factory=lambda: datetime.now(UTC)) @classmethod def from_clob_orderbook(cls, orderbook: Any) -> "Orderbook": diff --git a/src/polymarket_insider_tracker/ingestor/websocket.py b/src/polymarket_insider_tracker/ingestor/websocket.py index b481f8d..133e915 100644 --- a/src/polymarket_insider_tracker/ingestor/websocket.py +++ b/src/polymarket_insider_tracker/ingestor/websocket.py @@ -9,8 +9,9 @@ from dataclasses import dataclass from enum import Enum from typing import Any -import websockets from websockets.asyncio.client import ClientConnection +from websockets.asyncio.client import connect as ws_connect +from websockets.exceptions import ConnectionClosed from polymarket_insider_tracker.ingestor.models import TradeEvent @@ -156,7 +157,7 @@ class TradeStreamHandler: await self._set_state(ConnectionState.CONNECTING) try: - ws = await websockets.connect( + ws = await ws_connect( self._host, ping_interval=self._ping_interval, ping_timeout=self._ping_interval * 2, @@ -227,7 +228,7 @@ class TradeStreamHandler: else: logger.debug("Received binary message (%d bytes)", len(message)) - except websockets.ConnectionClosed as e: + except ConnectionClosed as e: logger.warning("Connection closed: %s", e) raise except Exception as e: @@ -284,7 +285,7 @@ class TradeStreamHandler: while self._running: try: await self._listen(self._ws) - except (websockets.ConnectionClosed, Exception) as e: + except (ConnectionClosed, Exception) as e: if not self._running: break diff --git a/tests/ingestor/test_websocket.py b/tests/ingestor/test_websocket.py index bb51804..37c671a 100644 --- a/tests/ingestor/test_websocket.py +++ b/tests/ingestor/test_websocket.py @@ -231,7 +231,10 @@ class TestTradeStreamHandler: mock_ws = AsyncMock() mock_ws.send = AsyncMock() - with patch("websockets.connect", AsyncMock(return_value=mock_ws)): + with patch( + "polymarket_insider_tracker.ingestor.websocket.ws_connect", + AsyncMock(return_value=mock_ws), + ): ws = await handler._connect() assert ws is mock_ws @@ -333,7 +336,10 @@ class TestTradeStreamHandlerIntegration: mock_ws = MockWebSocket(handler, trade_message) - with patch("websockets.connect", AsyncMock(return_value=mock_ws)): + with patch( + "polymarket_insider_tracker.ingestor.websocket.ws_connect", + AsyncMock(return_value=mock_ws), + ): # Run with timeout to prevent hanging try: await asyncio.wait_for(handler.start(), timeout=1.0)