Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds ws client (threaded) #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions dydx3/dydx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dydx3.modules.private import Private
from dydx3.modules.public import Public
from dydx3.modules.onboarding import Onboarding
from dydx3.modules.websocket import WSClient
from dydx3.starkex.helpers import private_key_to_public_key_pair_hex
from dydx3.starkex.starkex_resources.cpp_signature import (
get_cpp_lib,
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(
self._api_keys = None
self._eth = None
self._onboarding = None
self._ws = None

# Derive the public keys.
if stark_private_key is not None:
Expand Down Expand Up @@ -118,6 +120,21 @@ def __init__(
e,
)

@property
def ws(self):
'''
Get the ws client module, used for interacting with websocket endpoints.
'''
if self._ws:
return self._ws
host = self.host
if host.startswith('https'):
host = host.replace('https', 'wss')
if host.startswith('http'):
host = host.replace('http', 'wss')
self._ws = WSClient(host + '/v3/ws')
return self._ws

@property
def public(self):
'''
Expand Down
63 changes: 63 additions & 0 deletions dydx3/modules/websocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import json
from threading import Thread
import websocket


class WSClient(websocket.WebSocketApp):

callback = print

@staticmethod
def on_open(ws):
WSClient.callback('-- connection established --')

@staticmethod
def on_close(ws, close_status_code, close_msg):
WSClient.callback('-- connection closed --')

@staticmethod
def on_message(ws, message):
if json.loads(message)['type'] == 'channel_data':
WSClient.callback(message)

@staticmethod
def on_error(ws, message):
WSClient.callback(message)

def __init__(self, host):
self.uri = host
super().__init__(
self.uri,
on_open=self.on_open,
on_close=self.on_close,
on_message=self.on_message,
on_error=self.on_error
)
self._thread = None

def subscribe_to_orderbook(self, trading_pair):
self.send({'type': "subscribe", "channel": "v3_orderbook", "id": trading_pair})

def subscribe_to_trades(self, trading_pair):
self.send({'type': "subscribe", "channel": "v3_trades", "id": trading_pair})

def subscribe_to_markets(self):
self.send({'type': "subscribe", "channel": "v3_markets"})

def send(self, data, *args):
super().send(json.dumps(data), *args)

def start(self):
if self._thread:
return self._thread
self._thread = Thread(target=self.run_forever, args=())
self._thread.start()

def stop(self):
if not self._thread:
return
try:
self.close()
self._thread.join()
finally:
self._thread = None
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ six==1.12
sympy==1.6
tox==3.13.2
web3>=5.0.0,<6.0.0
websocket-client==1.2.1