From 22f9b2552cfefd6064b67d3897b8f49b67488d6c Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 10 Jul 2022 19:07:54 -0400 Subject: [PATCH] Provide symbol norming via a classmethod + global table --- piker/brokers/kraken/api.py | 56 ++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/piker/brokers/kraken/api.py b/piker/brokers/kraken/api.py index 17b79027..707ae41a 100644 --- a/piker/brokers/kraken/api.py +++ b/piker/brokers/kraken/api.py @@ -141,6 +141,9 @@ class InvalidKey(ValueError): class Client: + # global symbol normalization table + _ntable: dict[str, str] = {} + def __init__( self, name: str = '', @@ -302,7 +305,9 @@ class Client: async def symbol_info( self, pair: Optional[str] = None, - ): + + ) -> dict[str, dict[str, str]]: + if pair is not None: pairs = {'pair': pair} else: @@ -328,6 +333,12 @@ class Client: if not self._pairs: self._pairs = await self.symbol_info() + ntable = {} + for restapikey, info in self._pairs.items(): + ntable[restapikey] = ntable['wsname'] = info['altname'] + + self._ntable.update(ntable) + return self._pairs async def search_symbols( @@ -425,6 +436,25 @@ class Client: else: raise BrokerError(errmsg) + @classmethod + def normalize_symbol( + cls, + ticker: str + ) -> str: + ''' + Normalize symbol names to to a 3x3 pair from the global + definition map which we build out from the data retreived from + the 'AssetPairs' endpoint, see methods above. + + ''' + symlen = len(ticker) + if symlen != 6: + ticker = cls._ntable[ticker] + else: + raise ValueError(f'Unhandled symbol: {ticker}') + + return ticker.lower() + @acm async def get_client() -> Client: @@ -443,27 +473,3 @@ async def get_client() -> Client: await client.cache_symbols() yield client - - -def normalize_symbol( - ticker: str -) -> str: - ''' - Normalize symbol names to to a 3x3 pair. - - ''' - remap = { - 'XXBTZEUR': 'XBTEUR', - 'XXMRZEUR': 'XMREUR', - - # ws versions? pretty weird.. - 'XBT/EUR': 'XBTEUR', - 'XMR/EUR': 'XMREUR', - } - symlen = len(ticker) - if symlen != 6: - ticker = remap[ticker] - else: - raise ValueError(f'Unhandled symbol: {ticker}') - - return ticker.lower()