Skip to content

Commit

Permalink
Tiny refactoring (#211)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
sirEven and samtin0x authored Jul 30, 2024
1 parent 18a654c commit 6ddf249
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
11 changes: 11 additions & 0 deletions v4-client-py-v2/dydx_v4_client/indexer/candles_resolution.py
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion v4-client-py-v2/dydx_v4_client/indexer/socket/__init__.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 3 additions & 12 deletions v4-client-py-v2/dydx_v4_client/indexer/socket/websocket.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}")


Expand Down
3 changes: 2 additions & 1 deletion v4-client-py-v2/examples/markets_endpoints.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion v4-client-py-v2/examples/websocket_example.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion v4-client-py-v2/tests/indexer/socket/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 6ddf249

Please sign in to comment.