Add stream ticker test

deribit_updates
Guillermo Rodriguez 2023-03-10 17:09:18 -03:00
parent 77fbc7eb86
commit 926ab1dfa6
No known key found for this signature in database
GPG Key ID: EC3AB66D5D83B392
2 changed files with 65 additions and 12 deletions

View File

@ -154,19 +154,13 @@ class Client:
json_rpc: Callable, json_rpc: Callable,
append_hooks: Callable, append_hooks: Callable,
update_types: Callable, update_types: Callable,
key_id: str | None = None,
key_secret: str | None = None
) -> None: ) -> None:
self._pairs: dict[str, Any] = None self._pairs: dict[str, Any] = None
self._key_id = key_id
config = get_config().get('deribit', {}) self._key_secret = key_secret
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.json_rpc = json_rpc self.json_rpc = json_rpc
self.append_hooks = append_hooks self.append_hooks = append_hooks
@ -383,14 +377,24 @@ async def get_client(
is_brokercheck: bool = False is_brokercheck: bool = False
) -> Client: ) -> 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 ( async with (
trio.open_nursery() as n, trio.open_nursery() as n,
open_jsonrpc_session( open_jsonrpc_session(
_ws_url, ws_url,
response_type=JSONRPCResult response_type=JSONRPCResult
) as control_functions ) as control_functions
): ):
client = Client(*control_functions) client = Client(
*control_functions,
key_id=key_id,
key_secret=key_secret
)
_refresh_token: Optional[str] = None _refresh_token: Optional[str] = None
_access_token: Optional[str] = None _access_token: Optional[str] = None
@ -581,6 +585,9 @@ async def open_ticker_feed(
client.append_hooks({ client.append_hooks({
'request': [sub_hook] 'request': [sub_hook]
}) })
client.update_types({
'request': JSONRPCSubRequest
})
resp = await client.json_rpc( resp = await client.json_rpc(
'private/subscribe', {'channels': channels}) 'private/subscribe', {'channels': channels})

View File

@ -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)