Skip to content

Commit

Permalink
fixes: deps and bumping to web6
Browse files Browse the repository at this point in the history
  • Loading branch information
8ball030 committed May 30, 2024
1 parent 61fb0d4 commit d87993e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 112 deletions.
43 changes: 36 additions & 7 deletions lyra/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AsyncClient(BaseClient):

listener = None
subscribing = False
_ws = None

def __init__(
self,
Expand Down Expand Up @@ -53,13 +54,25 @@ def __init__(
print(f"Using subaccount id: {self.subaccount_id}")
self.message_queues = {}
self.connecting = False
# we make sure to get the event loop

@property
async def ws(self):
if self._ws is None:
self._ws = await self.connect_ws()
if not self._ws.connected:
self._ws = await self.connect_ws()
return self._ws

async def fetch_ticker(self, instrument_name: str):
"""
Fetch the ticker for a symbol
"""
id = str(int(time.time()))
payload = {"instrument_name": instrument_name}
if not self.ws:
await self.connect_ws()

self.ws.send(json.dumps({"method": "public/get_ticker", "params": payload, "id": id}))

# we now wait for the response
Expand Down Expand Up @@ -95,7 +108,7 @@ async def connect_ws(self):
self.connecting = True
session = aiohttp.ClientSession()
ws = await session.ws_connect(self.contracts['WS_ADDRESS'])
self.ws = ws
self._ws = ws
self.connecting = False
return ws

Expand Down Expand Up @@ -132,8 +145,8 @@ async def login_client(
'params': self.sign_authentication_header(),
'id': str(int(time.time())),
}
await self.ws.send_json(login_request)
async for data in self.ws:
await self._ws.send_json(login_request)
async for data in self._ws:
message = json.loads(data.data)
if message['id'] == login_request['id']:
if "result" not in message:
Expand Down Expand Up @@ -210,6 +223,8 @@ async def fetch_tickers(
instrument_type: InstrumentType = InstrumentType.OPTION,
currency: UnderlyingCurrency = UnderlyingCurrency.BTC,
):
if not self._ws:
await self.connect_ws()
instruments = await self.fetch_instruments(instrument_type=instrument_type, currency=currency)
instrument_names = [i['instrument_name'] for i in instruments]
id_base = str(int(time.time()))
Expand All @@ -218,17 +233,31 @@ async def fetch_tickers(
}
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}))
await asyncio.sleep(0.05) # otherwise we get rate limited...
await self._ws.send_json({'method': 'public/get_ticker', 'params': payload, 'id': id})
await asyncio.sleep(0.1) # otherwise we get rate limited...
results = {}
while ids_to_instrument_names:
message = json.loads(self.ws.recv())
message = await self._ws.receive()
if message is None:
continue
if 'error' in message:
raise Exception(f"Error fetching ticker {message}")
if message.type == aiohttp.WSMsgType.CLOSED:
# we try to reconnect
print(f"Erorr fetching ticker {message}...")
self._ws = await self.connect_ws()
return await self.fetch_tickers(instrument_type, currency)
message = json.loads(message.data)
if message['id'] in ids_to_instrument_names:
results[message['result']['instrument_name']] = message['result']
try:
results[message['result']['instrument_name']] = message['result']
except KeyError:
print(f"Error fetching ticker {message}")
del ids_to_instrument_names[message['id']]
return results

async def get_collaterals(self):
breakpoint()
return super().get_collaterals()

async def get_positions(self, currency: UnderlyingCurrency = UnderlyingCurrency.BTC):
Expand Down
101 changes: 1 addition & 100 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ readme = "README.md"
packages = [{include = "lyra"}]

[tool.poetry.dependencies]
python = ">=3.8.1,<=3.11.7"
requests = "^2.31.0"
web3 = "^5"
python = ">=3.8.1,<=3.11.9"
requests = "^2"
web3 = "^6"
websocket-client = ">=0.32.0,<1"
setuptools = "^68.2.2"
rich-click = "^1.7.1"
python-dotenv = ">=0.14.0,<0.18.0"
pandas = "~1"
websockets = "9.1"
python-socketio = {extras = ["asyncio-client"], version = "^5.11.2"}


[tool.poetry.scripts]
Expand Down

0 comments on commit d87993e

Please sign in to comment.