From e4e9bcadf82c78801648589a6a0080947e343add Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Fri, 10 Mar 2023 17:09:18 -0300 Subject: [PATCH] Add stream ticker test --- piker/brokers/deribit/api.py | 31 ++++++++++++++---------- tests/test_deribit.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 tests/test_deribit.py diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 628a37f8e2..94d17ca086 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -154,19 +154,13 @@ def __init__( json_rpc: Callable, append_hooks: Callable, update_types: Callable, + key_id: str | None = None, + key_secret: str | None = None ) -> None: self._pairs: dict[str, Any] = None - - config = get_config().get('deribit', {}) - - if ('key_id' in config) and ('key_secret' in config): - self._key_id = config['key_id'] - self._key_secret = config['key_secret'] - - else: - self._key_id = None - self._key_secret = None + self._key_id = key_id + self._key_secret = key_secret self.json_rpc = json_rpc self.append_hooks = append_hooks @@ -383,14 +377,24 @@ async def get_client( is_brokercheck: bool = False ) -> Client: + config = get_config().get('deribit', {}) + + ws_url = config.get('ws_url', _ws_url) + key_id = config.get('key_id', None) + key_secret = config.get('key_secret', None) + async with ( trio.open_nursery() as n, open_jsonrpc_session( - _ws_url, + ws_url, response_type=JSONRPCResult ) as control_functions ): - client = Client(*control_functions) + client = Client( + *control_functions, + key_id=key_id, + key_secret=key_secret + ) _refresh_token: Optional[str] = None _access_token: Optional[str] = None @@ -581,6 +585,9 @@ async def sub_hook(msg): client.append_hooks({ 'request': [sub_hook] }) + client.update_types({ + 'request': JSONRPCSubRequest + }) resp = await client.json_rpc( 'private/subscribe', {'channels': channels}) diff --git a/tests/test_deribit.py b/tests/test_deribit.py new file mode 100644 index 0000000000..5bb3336721 --- /dev/null +++ b/tests/test_deribit.py @@ -0,0 +1,46 @@ +import trio +import pytest +import tractor + +from piker import config + +from piker.brokers.deribit import api as deribit +from piker.brokers.deribit.api import _testnet_ws_url + +from piker._cacheables import open_cached_client + + +TESTNET_KEY_ID: str | None = None +TESTNET_KEY_SECRET: str | None = None + +@pytest.mark.skipif( + not TESTNET_KEY_ID or not TESTNET_KEY_SECRET, + reason='configure a deribit testnet key pair before running this test' +) +def test_deribit_get_ticker(open_test_pikerd): + + async def _test_main(): + async with open_test_pikerd() as (_, _, _, _): + async with open_cached_client('deribit') as client: + + symbols = await client.symbol_info() + + syms = list(symbols.keys()) + sym = syms[int(len(syms) / 2)] + + async with deribit.maybe_open_ticker_feed(sym) as tick_stream: + async for typ, msg in tick_stream: + assert typ == 'ticker' + assert 'open_interest' in msg['data'] + break + + + + config.write({ + 'deribit': { + 'ws_url': _testnet_ws_url, + 'key_id': TESTNET_KEY_ID, + 'key_secret': TESTNET_KEY_SECRET + } + }) + trio.run(_test_main)