From 6ddf249a105a75cb8039e6b692ff500cce77ca1a Mon Sep 17 00:00:00 2001 From: everSin <36056348+sirEven@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:21:31 +0200 Subject: [PATCH] Tiny refactoring (#211) * Refactor CandlesResolution out of websocket. * Add type annotation in Candles channel subscription funcs. * Format two files. * fix NewlyUndercollateralized error in test_order --------- Co-authored-by: samtin0x <40127309+samtin0x@users.noreply.github.com> --- .../dydx_v4_client/indexer/candles_resolution.py | 11 +++++++++++ .../dydx_v4_client/indexer/socket/__init__.py | 3 ++- .../dydx_v4_client/indexer/socket/websocket.py | 15 +++------------ v4-client-py-v2/examples/markets_endpoints.py | 3 ++- v4-client-py-v2/examples/websocket_example.py | 3 ++- .../tests/indexer/socket/test_websocket.py | 2 +- 6 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 v4-client-py-v2/dydx_v4_client/indexer/candles_resolution.py diff --git a/v4-client-py-v2/dydx_v4_client/indexer/candles_resolution.py b/v4-client-py-v2/dydx_v4_client/indexer/candles_resolution.py new file mode 100644 index 00000000..d123e7f8 --- /dev/null +++ b/v4-client-py-v2/dydx_v4_client/indexer/candles_resolution.py @@ -0,0 +1,11 @@ +from enum import Enum + + +class CandlesResolution(Enum): + ONE_MINUTE = "1MIN" + FIVE_MINUTES = "5MINS" + FIFTEEN_MINUTES = "15MINS" + THIRTY_MINUTES = "30MINS" + ONE_HOUR = "1HOUR" + FOUR_HOURS = "4HOURS" + ONE_DAY = "1DAY" diff --git a/v4-client-py-v2/dydx_v4_client/indexer/socket/__init__.py b/v4-client-py-v2/dydx_v4_client/indexer/socket/__init__.py index 43986a97..fa7109a8 100644 --- a/v4-client-py-v2/dydx_v4_client/indexer/socket/__init__.py +++ b/v4-client-py-v2/dydx_v4_client/indexer/socket/__init__.py @@ -1 +1,2 @@ -from dydx_v4_client.indexer.socket.websocket import CandlesResolution, IndexerSocket +from dydx_v4_client.indexer.socket.websocket import IndexerSocket +from dydx_v4_client.indexer.candles_resolution import CandlesResolution diff --git a/v4-client-py-v2/dydx_v4_client/indexer/socket/websocket.py b/v4-client-py-v2/dydx_v4_client/indexer/socket/websocket.py index d5059dd9..7919b548 100644 --- a/v4-client-py-v2/dydx_v4_client/indexer/socket/websocket.py +++ b/v4-client-py-v2/dydx_v4_client/indexer/socket/websocket.py @@ -1,20 +1,11 @@ import json import ssl from dataclasses import dataclass, field -from enum import Enum import websocket from typing_extensions import Any, Callable, Optional, Self, Union - -class CandlesResolution(Enum): - ONE_MINUTE = "1MIN" - FIVE_MINUTES = "5MINS" - FIFTEEN_MINUTES = "15MINS" - THIRTY_MINUTES = "30MINS" - ONE_HOUR = "1HOUR" - FOUR_HOURS = "4HOURS" - ONE_DAY = "1DAY" +from dydx_v4_client.indexer.candles_resolution import CandlesResolution @dataclass @@ -78,10 +69,10 @@ def unsubscribe(self): class Candles(Channel): channel = "v4_candles" - def subscribe(self, id, resolution: CandlesResolution, batched=True) -> Self: + def subscribe(self, id: str, resolution: CandlesResolution, batched=True) -> Self: return super().subscribe(id=f"{id}/{resolution.value}", batched=batched) - def unsubscribe(self, id, resolution: CandlesResolution): + def unsubscribe(self, id: str, resolution: CandlesResolution): return super().unsubscribe(id=f"{id}/{resolution.value}") diff --git a/v4-client-py-v2/examples/markets_endpoints.py b/v4-client-py-v2/examples/markets_endpoints.py index 2e4ca8b8..76cff24f 100644 --- a/v4-client-py-v2/examples/markets_endpoints.py +++ b/v4-client-py-v2/examples/markets_endpoints.py @@ -1,5 +1,6 @@ import asyncio +from dydx_v4_client.indexer.candles_resolution import CandlesResolution from dydx_v4_client.indexer.rest.indexer_client import IndexerClient from dydx_v4_client.network import TESTNET @@ -82,7 +83,7 @@ async def test(): # Get perp market candles try: response = await client.markets.get_perpetual_market_candles( - market=MARKET_BTC_USD, resolution="1MIN" + market=MARKET_BTC_USD, resolution=CandlesResolution.ONE_MINUTE ) print(response) print("candles") diff --git a/v4-client-py-v2/examples/websocket_example.py b/v4-client-py-v2/examples/websocket_example.py index b8ed7aa2..f59bf762 100644 --- a/v4-client-py-v2/examples/websocket_example.py +++ b/v4-client-py-v2/examples/websocket_example.py @@ -1,6 +1,7 @@ import asyncio -from dydx_v4_client.indexer.socket.websocket import CandlesResolution, IndexerSocket +from dydx_v4_client.indexer.candles_resolution import CandlesResolution +from dydx_v4_client.indexer.socket.websocket import IndexerSocket from dydx_v4_client.network import TESTNET from tests.conftest import TEST_ADDRESS diff --git a/v4-client-py-v2/tests/indexer/socket/test_websocket.py b/v4-client-py-v2/tests/indexer/socket/test_websocket.py index 30ade217..0da59254 100644 --- a/v4-client-py-v2/tests/indexer/socket/test_websocket.py +++ b/v4-client-py-v2/tests/indexer/socket/test_websocket.py @@ -4,7 +4,7 @@ import pytest from dotenv import load_dotenv -from dydx_v4_client.indexer.socket.websocket import CandlesResolution +from dydx_v4_client.indexer.candles_resolution import CandlesResolution load_dotenv()