2024-06-20 21:43:45 +00:00
|
|
|
"""See `KucoinDepoFetcher` for documentation."""
|
|
|
|
|
2024-05-29 20:29:15 +00:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2024-06-02 14:14:46 +00:00
|
|
|
import fin_defs
|
2024-05-29 20:29:15 +00:00
|
|
|
import kucoin.client
|
|
|
|
|
2024-06-02 14:14:46 +00:00
|
|
|
from .data import DepoGroup, DepoSingle
|
2024-05-29 20:29:15 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-06-02 14:14:46 +00:00
|
|
|
class KucoinDepoFetcher:
|
2024-06-20 21:43:45 +00:00
|
|
|
"""Depository fetcher for [Kucoin](https://www.kucoin.com), the online crypto currency exchange.
|
|
|
|
|
|
|
|
Requirements for use:
|
|
|
|
- Account on [Kucoin](https://www.kucoin.com).
|
|
|
|
- Have performed Know Your Customer (KYC) for your account.
|
|
|
|
- Created API key from [settings menu](https://www.kucoin.com/account/api).
|
|
|
|
API key must have the **General Permission**, and **should not have
|
|
|
|
any additional permissions**. Employ principle of least priviledge.
|
|
|
|
|
|
|
|
Depository structure: An upper level depo split for each of the
|
|
|
|
sub-accounts (Funding, Trading, Margin, Futures...)
|
|
|
|
"""
|
|
|
|
|
2024-05-29 20:29:15 +00:00
|
|
|
def __init__(self, kucoin_key: str, kucoin_secret: str, kucoin_pass: str):
|
2024-06-02 16:33:25 +00:00
|
|
|
assert kucoin_key is not None, 'Missing kucoin_key'
|
|
|
|
assert kucoin_secret is not None, 'Missing kucoin_secret'
|
|
|
|
assert kucoin_pass is not None, 'Missing kucoin_pass'
|
2024-05-29 20:29:15 +00:00
|
|
|
self.client = kucoin.client.Client(
|
|
|
|
kucoin_key,
|
|
|
|
kucoin_secret,
|
|
|
|
kucoin_pass,
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_depo(self) -> DepoGroup:
|
|
|
|
now = datetime.datetime.now(tz=datetime.UTC)
|
|
|
|
assets_by_account_type: dict[str, dict[fin_defs.Asset, Decimal]] = {}
|
|
|
|
for account_data in self.client.get_accounts():
|
|
|
|
asset = fin_defs.WELL_KNOWN_SYMBOLS[account_data['currency']]
|
2024-06-02 14:14:46 +00:00
|
|
|
balance = Decimal(account_data['balance'])
|
2024-06-02 16:33:25 +00:00
|
|
|
assets_for_account_type = assets_by_account_type.setdefault(
|
2024-06-02 15:24:53 +00:00
|
|
|
account_data['type'],
|
|
|
|
{},
|
2024-06-02 14:14:46 +00:00
|
|
|
)
|
|
|
|
assets_for_account_type[asset] = (
|
|
|
|
assets_for_account_type.get(asset, Decimal(0)) + balance
|
|
|
|
)
|
2024-06-02 16:33:25 +00:00
|
|
|
del account_data, asset, balance, assets_for_account_type
|
2024-06-02 14:14:46 +00:00
|
|
|
return DepoGroup(
|
|
|
|
'Kucoin',
|
|
|
|
now,
|
|
|
|
[
|
|
|
|
DepoSingle('Kucoin ' + account_type, now, assets)
|
|
|
|
for account_type, assets in assets_by_account_type.items()
|
|
|
|
],
|
|
|
|
)
|