56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
"""See `KrakenDepoFetcher` for documentation."""
|
|
|
|
import datetime
|
|
import logging
|
|
from decimal import Decimal
|
|
|
|
import fin_defs
|
|
import krakenex
|
|
|
|
from .data import Depo, DepoFetcher, DepoSingle
|
|
|
|
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.
|
|
|
|
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 account_raw, balance_str in result['result'].items():
|
|
account = account_raw.removesuffix('.HOLD')
|
|
asset = fin_defs.WELL_KNOWN_SYMBOLS[account]
|
|
balance = Decimal(balance_str)
|
|
if balance != 0:
|
|
assets[asset] = assets.get(asset, Decimal(0)) + balance
|
|
del account, account_raw, balance_str, asset, balance
|
|
|
|
return DepoSingle(
|
|
name='Kraken',
|
|
_assets=assets,
|
|
updated_time=now,
|
|
)
|