Skip to content

Commit

Permalink
added fetch_tickers
Browse files Browse the repository at this point in the history
  • Loading branch information
8ball030 committed Jan 7, 2024
1 parent 7e38816 commit bae8e18
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
58 changes: 41 additions & 17 deletions lyra/lyra.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self, private_key, env, logger=None, verbose=False, subaccount_id=N
self.subaccount_id = self.fetch_subaccounts()['subaccount_ids'][0]
else:
self.subaccount_id = subaccount_id
self.ws = self.connect_ws()
self.login_client()

def create_account(self, wallet):
"""Call the create account endpoint."""
Expand Down Expand Up @@ -150,9 +152,7 @@ def create_order(
instrument_type = InstrumentType.PERP

signed_order = self._sign_order(order, base_asset_sub_id, instrument_type, _currency)
ws = self.connect_ws()
self.login_client(ws)
response = self.submit_order(signed_order, ws)
response = self.submit_order(signed_order)
return response

def _define_order(
Expand Down Expand Up @@ -181,11 +181,11 @@ def _define_order(
'signature': 'filled_in_below',
}

def submit_order(self, order, ws):
def submit_order(self, order):
id = str(int(time.time()))
ws.send(json.dumps({'method': 'private/order', 'params': order, 'id': id}))
self.ws.send(json.dumps({'method': 'private/order', 'params': order, 'id': id}))
while True:
message = json.loads(ws.recv())
message = json.loads(self.ws.recv())
if message['id'] == id:
return message['result']['order']

Expand All @@ -207,16 +207,18 @@ def connect_ws(self):
ws = create_connection(self.contracts['WS_ADDRESS'])
return ws

def login_client(self, ws):
def login_client(
self,
):
login_request = {
'method': 'public/login',
'params': self.sign_authentication_header(),
'id': str(int(time.time())),
}
ws.send(json.dumps(login_request))
self.ws.send(json.dumps(login_request))
# we need to wait for the response
while True:
message = json.loads(ws.recv())
message = json.loads(self.ws.recv())
if message['id'] == login_request['id']:
if "result" not in message:
raise Exception(f"Unable to login {message}")
Expand Down Expand Up @@ -321,27 +323,23 @@ def cancel(self, order_id, instrument_name):
Cancel an order
"""

ws = self.connect_ws()
self.login_client(ws)
id = str(int(time.time()))
payload = {"order_id": order_id, "subaccount_id": self.subaccount_id, "instrument_name": instrument_name}
ws.send(json.dumps({'method': 'private/cancel', 'params': payload, 'id': id}))
self.ws.send(json.dumps({'method': 'private/cancel', 'params': payload, 'id': id}))
while True:
message = json.loads(ws.recv())
message = json.loads(self.ws.recv())
if message['id'] == id:
return message['result']

def cancel_all(self):
"""
Cancel all orders
"""
ws = self.connect_ws()
self.login_client(ws)
id = str(int(time.time()))
payload = {"subaccount_id": self.subaccount_id}
ws.send(json.dumps({'method': 'private/cancel_all', 'params': payload, 'id': id}))
self.ws.send(json.dumps({'method': 'private/cancel_all', 'params': payload, 'id': id}))
while True:
message = json.loads(ws.recv())
message = json.loads(self.ws.recv())
if message['id'] == id:
return message['result']

Expand All @@ -366,3 +364,29 @@ def get_collaterals(self):
response = requests.post(url, json=payload, headers=headers)
results = response.json()["result"]['collaterals']
return results.pop()

def fetch_tickers(
self,
instrument_type: InstrumentType = InstrumentType.OPTION,
currency: UnderlyingCurrency = UnderlyingCurrency.BTC,
):
"""
Fetch tickers using the ws connection
"""
instruments = self.fetch_instruments(instrument_type=instrument_type, currency=currency)
instrument_names = [i['instrument_name'] for i in instruments]
id_base = str(int(time.time()))
ids_to_instrument_names = {
f'{id_base}_{enumerate}': instrument_name for enumerate, instrument_name in enumerate(instrument_names)
}
for id, instrument_name in ids_to_instrument_names.items():
payload = {"instrument_name": instrument_name}
self.ws.send(json.dumps({'method': 'public/get_ticker', 'params': payload, 'id': id}))
time.sleep(0.05) # otherwise we get rate limited...
results = {}
while ids_to_instrument_names:
message = json.loads(self.ws.recv())
if message['id'] in ids_to_instrument_names:
results[message['result']['instrument_name']] = message['result']
del ids_to_instrument_names[message['id']]
return results
6 changes: 6 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,9 @@ def test_get_collaterals(lyra_client):
"""Test get collaterals."""
collaterals = lyra_client.get_collaterals()
assert isinstance(collaterals, dict)


def test_get_tickers(lyra_client):
"""Test get tickers."""
tickers = lyra_client.fetch_tickers()
assert isinstance(tickers, dict)

0 comments on commit bae8e18

Please sign in to comment.