kraken: drop `Client.cache_assets()`, simpler `.pair_info()`, drop `.mkt_info()`

rekt_pps
Tyler Goodlet 2023-04-20 11:52:23 -04:00
parent d0e01ff9b6
commit 0d93871c88
1 changed files with 40 additions and 62 deletions

View File

@ -43,7 +43,6 @@ from piker import config
from piker.data.types import Struct from piker.data.types import Struct
from piker.accounting._mktinfo import ( from piker.accounting._mktinfo import (
Asset, Asset,
MktPair,
digits_to_dec, digits_to_dec,
) )
from piker.brokers._util import ( from piker.brokers._util import (
@ -274,9 +273,10 @@ class Client:
for sym, bal in by_bsmktid.items() for sym, bal in by_bsmktid.items()
} }
async def get_assets(self) -> dict[str, dict]: async def get_assets(self) -> dict[str, Asset]:
''' '''
Get all assets available for trading and xfer. Load and cache all asset infos and pack into
our native ``Asset`` struct.
https://docs.kraken.com/rest/#tag/Market-Data/operation/getAssetInfo https://docs.kraken.com/rest/#tag/Market-Data/operation/getAssetInfo
@ -292,27 +292,21 @@ class Client:
''' '''
resp = await self._public('Assets', {}) resp = await self._public('Assets', {})
return resp['result'] assets = resp['result']
async def cache_assets(self) -> None:
'''
Load and cache all asset infos and pack into
our native ``Asset`` struct.
'''
assets = await self.get_assets()
for bs_mktid, info in assets.items(): for bs_mktid, info in assets.items():
altname = self._altnames[bs_mktid] = info['altname']
aname = self._altnames[bs_mktid] = info['altname']
aclass = info['aclass'] aclass = info['aclass']
self.assets[bs_mktid] = Asset( self.assets[bs_mktid] = Asset(
name=aname.lower(), name=altname.lower(),
atype=f'crypto_{aclass}', atype=f'crypto_{aclass}',
tx_tick=digits_to_dec(info['decimals']), tx_tick=digits_to_dec(info['decimals']),
info=info, info=info,
) )
return self.assets
async def get_trades( async def get_trades(
self, self,
fetch_limit: int | None = None, fetch_limit: int | None = None,
@ -475,57 +469,42 @@ class Client:
async def pair_info( async def pair_info(
self, self,
pair: str | None = None, pair_patt: str | None = None,
) -> dict[str, Pair] | Pair: ) -> dict[str, Pair] | Pair:
'''
Query for a tradeable asset pair (info), or all if no input
pattern is provided.
if pair is not None: https://docs.kraken.com/rest/#tag/Market-Data/operation/getTradableAssetPairs
pairs = {'pair': pair}
else:
pairs = None # get all pairs
resp = await self._public('AssetPairs', pairs) '''
# get all pairs by default, or filter
# to whatever pattern is provided as input.
pairs: dict[str, str] | None = None
if pair_patt is not None:
pairs = {'pair': pair_patt}
resp = await self._public(
'AssetPairs',
pairs,
)
err = resp['error'] err = resp['error']
if err: if err:
symbolname = pairs['pair'] if pair else None raise SymbolNotFound(pair_patt)
raise SymbolNotFound(f'{symbolname}.kraken')
pairs = resp['result'] pairs: dict[str, Pair] = {
if pair is not None:
_, data = next(iter(pairs.items()))
return Pair(**data)
else:
return {
key: Pair(**data) key: Pair(**data)
for key, data in pairs.items() for key, data in resp['result'].items()
} }
# always cache so we can possibly do faster lookup
self._pairs.update(pairs)
async def mkt_info( if pair_patt is not None:
self, return next(iter(pairs.items()))[1]
pair_str: str,
) -> MktPair: return pairs
(
bs_mktid, # str
pair_info, # Pair
) = Client.normalize_symbol(pair_str)
dst_asset = self.assets[pair_info.base]
# NOTE XXX parse out the src asset name until we figure out
# how to get the src asset's `Pair` info from kraken..
src_key = pair_str.lstrip(dst_asset.name.upper()).lower()
return MktPair(
dst=dst_asset,
price_tick=pair_info.price_tick,
size_tick=pair_info.size_tick,
bs_mktid=bs_mktid,
src=src_key,
broker='kraken',
)
async def cache_symbols(self) -> dict: async def cache_symbols(self) -> dict:
''' '''
@ -538,14 +517,15 @@ class Client:
''' '''
if not self._pairs: if not self._pairs:
self._pairs.update(await self.pair_info()) pairs = await self.pair_info()
assert self._pairs == pairs
# table of all ws and rest keys to their alt-name values. # table of all ws and rest keys to their alt-name values.
ntable: dict[str, str] = {} ntable: dict[str, str] = {}
for rest_key in list(self._pairs.keys()): for rest_key in list(pairs.keys()):
pair: Pair = self._pairs[rest_key] pair: Pair = pairs[rest_key]
altname = pair.altname altname = pair.altname
wsname = pair.wsname wsname = pair.wsname
ntable[altname] = ntable[rest_key] = ntable[wsname] = altname ntable[altname] = ntable[rest_key] = ntable[wsname] = altname
@ -561,7 +541,6 @@ class Client:
async def search_symbols( async def search_symbols(
self, self,
pattern: str, pattern: str,
limit: int = None,
) -> dict[str, Any]: ) -> dict[str, Any]:
''' '''
@ -672,8 +651,7 @@ class Client:
the 'AssetPairs' endpoint, see methods above. the 'AssetPairs' endpoint, see methods above.
''' '''
ticker = cls._ntable[ticker] return cls._ntable[ticker].lower()
return ticker.lower(), cls._pairs[ticker]
@acm @acm
@ -693,7 +671,7 @@ async def get_client() -> Client:
# at startup, load all symbols, and asset info in # at startup, load all symbols, and asset info in
# batch requests. # batch requests.
async with trio.open_nursery() as nurse: async with trio.open_nursery() as nurse:
nurse.start_soon(client.cache_assets) nurse.start_soon(client.get_assets)
await client.cache_symbols() await client.cache_symbols()
yield client yield client