1
0
fin-depo/fin_depo/defi_kraken.py

88 lines
2.8 KiB
Python
Raw Normal View History

2024-06-20 21:43:45 +00:00
"""See `KrakenDepoFetcher` for documentation."""
2024-06-02 14:14:46 +00:00
import datetime
import logging
2024-05-29 20:29:15 +00:00
from decimal import Decimal
2024-06-02 14:14:46 +00:00
2024-05-29 20:29:15 +00:00
import fin_defs
import krakenex
2024-06-02 14:14:46 +00:00
2024-12-18 16:41:33 +00:00
from .data import (
DepoFetcher,
DepoSingle,
DepositDetails,
DoubleRegister,
TradeOrderDetails,
WithdrawalDetails,
)
2024-05-29 20:29:15 +00:00
logger = logging.getLogger(__name__)
2024-07-18 22:22:29 +00:00
class KrakenDepoFetcher(DepoFetcher):
2024-06-20 21:43:45 +00:00
"""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.
2024-07-18 22:55:12 +00:00
- Install [`krakenex`](https://pypi.org/project/krakenex/) library.
2024-06-20 21:43:45 +00:00
Depository structure: A `DepoSingle`. No nesting.
"""
2024-05-29 20:29:15 +00:00
def __init__(self, kraken_key: str, kraken_secret: str):
2024-07-18 22:22:29 +00:00
self.assert_param('kraken_key', str, kraken_key)
self.assert_param('kraken_secret', str, kraken_secret)
2024-05-29 20:29:15 +00:00
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] = {}
2024-09-05 23:35:09 +00:00
for ticker, balance_str in result['result'].items():
asset = parse_asset_from_ticker(ticker)
2024-05-29 20:29:15 +00:00
balance = Decimal(balance_str)
if balance != 0:
assets[asset] = assets.get(asset, Decimal(0)) + balance
2024-09-05 23:35:09 +00:00
del ticker, balance_str, asset, balance
2024-05-29 20:29:15 +00:00
return DepoSingle(
2024-06-02 14:14:46 +00:00
name='Kraken',
2024-06-02 16:33:25 +00:00
_assets=assets,
2024-06-02 14:14:46 +00:00
updated_time=now,
2024-05-29 20:29:15 +00:00
)
2024-09-05 23:35:09 +00:00
2024-11-01 10:14:22 +00:00
2024-12-18 16:41:33 +00:00
def _get_withdrawals(self) -> list[WithdrawalDetails]:
raise NotImplementedError("_get_withdrawals is a work in progress")
def _get_deposits(self) -> list[DepositDetails]:
raise NotImplementedError("_get_deposits is a work in progress")
def _get_historic_spot_orders(self) -> Iterator[TradeOrderDetails]:
raise NotImplementedError("_get_historic_spot_orders is a work in progress")
def _get_double_registers(self) -> list[DoubleRegister]:
double_registers: list[DoubleRegister] = []
double_registers += self._get_deposits()
double_registers += self._get_withdrawals()
double_registers += self._get_historic_spot_orders()
double_registers.sort(key=lambda x: x.executed_time)
return double_registers
2024-09-05 23:35:09 +00:00
def parse_asset_from_ticker(ticker: str) -> fin_defs.Asset:
account = ticker.removesuffix('.HOLD')
if account == 'ZEUR':
return fin_defs.EUR
2024-10-20 09:03:31 +00:00
return fin_defs.WELL_KNOWN_SYMBOLS[account]