deribit: switch to `rapidfuzz` API

ib_py311_fixes
Tyler Goodlet 2023-09-21 19:44:27 -04:00
parent d4833eba21
commit 46d83e9ca9
1 changed files with 33 additions and 18 deletions

View File

@ -52,8 +52,11 @@ from cryptofeed.defines import (
) )
from cryptofeed.symbols import Symbol from cryptofeed.symbols import Symbol
from piker.data.types import Struct from piker.data import (
from piker.data import def_iohlcv_fields def_iohlcv_fields,
match_from_pairs,
Struct,
)
from piker.data._web_bs import ( from piker.data._web_bs import (
open_jsonrpc_session open_jsonrpc_session
) )
@ -79,7 +82,7 @@ _testnet_ws_url = 'wss://test.deribit.com/ws/api/v2'
class JSONRPCResult(Struct): class JSONRPCResult(Struct):
jsonrpc: str = '2.0' jsonrpc: str = '2.0'
id: int id: int
result: Optional[dict] = None result: Optional[list[dict]] = None
error: Optional[dict] = None error: Optional[dict] = None
usIn: int usIn: int
usOut: int usOut: int
@ -289,24 +292,29 @@ class Client:
currency: str = 'btc', # BTC, ETH, SOL, USDC currency: str = 'btc', # BTC, ETH, SOL, USDC
kind: str = 'option', kind: str = 'option',
expired: bool = False expired: bool = False
) -> dict[str, Any]:
"""Get symbol info for the exchange.
""" ) -> dict[str, dict]:
'''
Get symbol infos.
'''
if self._pairs: if self._pairs:
return self._pairs return self._pairs
# will retrieve all symbols by default # will retrieve all symbols by default
params = { params: dict[str, str] = {
'currency': currency.upper(), 'currency': currency.upper(),
'kind': kind, 'kind': kind,
'expired': str(expired).lower() 'expired': str(expired).lower()
} }
resp = await self.json_rpc('public/get_instruments', params) resp: JSONRPCResult = await self.json_rpc(
results = resp.result 'public/get_instruments',
params,
instruments = { )
# convert to symbol-keyed table
results: list[dict] | None = resp.result
instruments: dict[str, dict] = {
item['instrument_name'].lower(): item item['instrument_name'].lower(): item
for item in results for item in results
} }
@ -319,6 +327,7 @@ class Client:
async def cache_symbols( async def cache_symbols(
self, self,
) -> dict: ) -> dict:
if not self._pairs: if not self._pairs:
self._pairs = await self.symbol_info() self._pairs = await self.symbol_info()
@ -329,17 +338,23 @@ class Client:
pattern: str, pattern: str,
limit: int = 30, limit: int = 30,
) -> dict[str, Any]: ) -> dict[str, Any]:
data = await self.symbol_info() '''
Fuzzy search symbology set for pairs matching `pattern`.
matches = fuzzy.extractBests( '''
pattern, pairs: dict[str, Any] = await self.symbol_info()
data, matches: dict[str, Pair] = match_from_pairs(
pairs=pairs,
query=pattern.upper(),
score_cutoff=35, score_cutoff=35,
limit=limit limit=limit
) )
# repack in dict form
return {item[0]['instrument_name'].lower(): item[0] # repack in name-keyed table
for item in matches} return {
pair['instrument_name'].lower(): pair
for pair in matches.values()
}
async def bars( async def bars(
self, self,