1
0
fin-depo/fin_depo/defi_kraken.py

56 lines
1.7 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
from .data import Depo, DepoSingle
2024-05-29 20:29:15 +00:00
logger = logging.getLogger(__name__)
2024-06-02 14:14:46 +00:00
class KrakenDepoFetcher:
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.
Depository structure: A `DepoSingle`. No nesting.
"""
2024-05-29 20:29:15 +00:00
def __init__(self, kraken_key: str, kraken_secret: str):
2024-06-02 16:33:25 +00:00
assert kraken_key is not None, 'Missing kraken_key'
assert kraken_secret is not None, 'Missing 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] = {}
for account, balance_str in result['result'].items():
2024-06-02 16:33:25 +00:00
account = account.removesuffix('.HOLD')
2024-05-29 20:29:15 +00:00
asset = fin_defs.WELL_KNOWN_SYMBOLS[account]
balance = Decimal(balance_str)
if balance != 0:
assets[asset] = assets.get(asset, Decimal(0)) + balance
del account, balance_str, asset, balance
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
)