Skip to content

Commit

Permalink
Add stream ticker test
Browse files Browse the repository at this point in the history
  • Loading branch information
guilledk committed Mar 10, 2023
1 parent 77fbc7e commit 926ab1d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
31 changes: 19 additions & 12 deletions piker/brokers/deribit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
46 changes: 46 additions & 0 deletions tests/test_deribit.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 926ab1d

Please sign in to comment.