"""See `KrakenDepoFetcher` for documentation.""" import datetime import logging from decimal import Decimal from collections.abc import Iterator import fin_defs import krakenex from .data import ( Depo, DepoFetcher, DepoSingle, DepositDetails, DoubleRegister, TradeOrderDetails, WithdrawalDetails, ) logger = logging.getLogger(__name__) class KrakenDepoFetcher(DepoFetcher): """Depository fetcher for [Kraken](https://www.kraken.com), the online crypto currency exchange. Requirements for use: - Account on [Kraken](https://www.kraken.com). - Have performed Know Your Customer (KYC) for your account. - Created API key from [Kraken Pro settings](https://pro.kraken.com/app/settings/api). API key must have the **Query Funds Permission**, and **should not have any additional permissions**. Employ principle of least priviledge. - Install [`krakenex`](https://pypi.org/project/krakenex/) library. Depository structure: A `DepoSingle`. No nesting. """ def __init__(self, kraken_key: str, kraken_secret: str): self.assert_param('kraken_key', str, kraken_key) self.assert_param('kraken_secret', str, kraken_secret) self.client = krakenex.API( kraken_key, kraken_secret, ) def get_depo(self) -> Depo: now = datetime.datetime.now(tz=datetime.UTC) result = self.client.query_private('Balance') assets: dict[fin_defs.Asset, Decimal] = {} for ticker, balance_str in result['result'].items(): asset = parse_asset_from_ticker(ticker) balance = Decimal(balance_str) if balance != 0: assets[asset] = assets.get(asset, Decimal(0)) + balance del ticker, balance_str, asset, balance return DepoSingle( name='Kraken', _assets=assets, updated_time=now, ) def _get_withdrawals(self) -> list[WithdrawalDetails]: raise NotImplementedError("work in progress") """ json = self.client.query_private('WithdrawStatus',data={'start':1}) results = [] for v in json['result']: asset = parse_asset_from_ticker(v['asset']) results.append(WithdrawalDetails( withdrawn=fin_defs.AssetAmount(asset, Decimal(v['amount'])), fee=fin_defs.AssetAmount(asset, Decimal(v['fee'])), executed_time = datetime.datetime.fromtimestamp(v['time'],tz=datetime.UTC), raw_details = v, )) return results """ def _get_deposits(self) -> list[DepositDetails]: raise NotImplementedError("work in progress") """ json = self.client.query_private('DepositStatus',data={'start':1}) print(json) results = [] for v in json['result']: asset = parse_asset_from_ticker(v['asset']) results.append(DepositDetails( deposit=fin_defs.AssetAmount(asset, Decimal(v['amount'])), fee=fin_defs.AssetAmount(asset, Decimal(v['fee'])), executed_time = datetime.datetime.fromtimestamp(v['time'],tz=datetime.UTC), raw_details = v, )) return results """ def _get_historic_spot_orders(self) -> Iterator[TradeOrderDetails]: raise NotImplementedError("work in progress") """ json = self.client.query_private('ClosedOrders',data={'start':1}) for order_id, v in json['result']['closed'].items(): assert v['descr']['pair'] == 'USDTEUR', 'Only sell USDT for EUR is supported' assert v['descr']['type'] == 'sell', 'Only sell USDT for EUR is supported' asset_input = fin_defs.USDT asset_output = fin_defs.EUR yield TradeOrderDetails( input =fin_defs.AssetAmount(asset_input, Decimal(v['vol_exec'])), output =fin_defs.AssetAmount(asset_output, Decimal(v['cost'])), fee = fin_defs.AssetAmount(asset_output, Decimal(v['fee'])), executed_time = datetime.datetime.fromtimestamp(v['closetm'],tz=datetime.UTC), order_id=order_id, raw_order_details= v, ) del order_id, v """ def _get_double_registers(self) -> list[DoubleRegister]: json = self.client.query_private('Ledgers',data={'start':1}) return list(parse_from_ledger(json['result']['ledger'].values())) def parse_asset_from_ticker(ticker: str) -> fin_defs.Asset: account = ticker.removesuffix('.HOLD') if account == 'ZEUR': return fin_defs.EUR return fin_defs.WELL_KNOWN_SYMBOLS[account] def parse_from_ledger(ledger_items: list[dict[str,str]]) -> Iterator: collected_items = {} for item in ledger_items: collected_items.setdefault(item['refid'], []).append(item) for refid, items in collected_items.items(): print(refid, items) assert len(items) <= 2 if items[0]['type'] == 'deposit': assert len(items) == 1 asset = parse_asset_from_ticker(items[0]['asset']) yield DepositDetails( deposit = fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])), fee = fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])), executed_time = datetime.datetime.fromtimestamp(items[0]['time'],tz=datetime.UTC), raw_details = items[0], ) elif items[0]['type'] == 'withdrawal': assert len(items) == 1 asset = parse_asset_from_ticker(items[0]['asset']) yield WithdrawalDetails( withdrawn = -fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])), fee = fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])), executed_time = datetime.datetime.fromtimestamp(items[0]['time'],tz=datetime.UTC), raw_details = items[0], ) else: assert len(items) == 2, items assert items[0]['type'] in {'receive', 'trade'} assert items[1]['type'] in {'spend', 'trade'} assert float(items[0]['amount']) > 0 assert float(items[1]['amount']) < 0 asset_input = parse_asset_from_ticker(items[1]['asset']) asset_output = parse_asset_from_ticker(items[0]['asset']) input = -fin_defs.AssetAmount(asset_input, Decimal(items[1]['amount'])) output = fin_defs.AssetAmount(asset_output, Decimal(items[0]['amount'])) fee_of_input = fin_defs.AssetAmount(asset_input, Decimal(items[1]['fee'])) fee_of_output = fin_defs.AssetAmount(asset_output, Decimal(items[0]['fee'])) details = TradeOrderDetails( input = input, output = output, fee = fee_of_input if fee_of_input.amount != 0 else fee_of_output, executed_time = datetime.datetime.fromtimestamp(items[1]['time'],tz=datetime.UTC), order_id = refid, raw_order_details = items[0], ) yield details