1
0
personal-data/personal_data/fetchers/defi_kucoin.py

96 lines
2.6 KiB
Python
Raw Normal View History

2024-06-02 16:01:18 +00:00
import abc
2024-05-16 22:24:01 +00:00
import dataclasses
import datetime
import logging
from collections.abc import Iterator, Mapping
2024-06-02 16:01:18 +00:00
import fin_depo
2024-05-17 22:33:47 +00:00
import kucoin.client
2024-05-16 22:24:01 +00:00
from frozendict import frozendict
from personal_data.data import DeduplicateMode, Scraper
from .. import secrets
logger = logging.getLogger(__name__)
# TODO: Move these into secrets!
client = kucoin.client.Client(
secrets.KUCOIN_KEY,
secrets.KUCOIN_SECRET,
secrets.KUCOIN_PASS,
)
2024-06-02 16:01:18 +00:00
2024-05-16 22:24:01 +00:00
@dataclasses.dataclass(frozen=True)
2024-06-02 16:00:55 +00:00
class FinanceDepoScraper(Scraper):
@abc.abstractmethod
def get_depo_fetcher(self):
pass
2024-06-02 15:42:16 +00:00
2024-06-02 16:00:55 +00:00
def scrape(self) -> Iterator[Mapping[str, object]]:
depo = self.get_depo_fetcher().get_depo()
2024-06-02 15:42:16 +00:00
data_point = {
2024-06-02 15:42:16 +00:00
'account.update_time': depo.updated_time,
}
2024-05-16 22:24:01 +00:00
2024-06-02 15:42:16 +00:00
for asset in depo.assets():
2024-06-02 16:01:18 +00:00
key = f'balance.{asset}'
2024-06-02 15:42:16 +00:00
data_point[key] = depo.get_amount_of_asset(asset)
print(data_point)
2024-05-16 22:24:01 +00:00
yield frozendict(data_point)
2024-05-18 19:35:58 +00:00
2024-06-02 16:00:55 +00:00
@dataclasses.dataclass(frozen=True)
class KrakenAccountBalances(FinanceDepoScraper):
dataset_name = 'defi_kraken_balance'
deduplicate_mode = DeduplicateMode.ONLY_LATEST
deduplicate_ignore_columns = ['account.update_time']
def get_depo_fetcher(self):
return fin_depo.defi_kraken.KrakenDepoFetcher(
2024-06-02 16:01:18 +00:00
kraken_key=secrets.KRAKEN_KEY,
kraken_secret=secrets.KRAKEN_SECRET,
2024-06-02 16:00:55 +00:00
)
2024-06-02 16:01:18 +00:00
2024-06-02 16:00:55 +00:00
@dataclasses.dataclass(frozen=True)
class KucoinAccountBalances(FinanceDepoScraper):
dataset_name = 'defi_kucoin_balance'
deduplicate_mode = DeduplicateMode.ONLY_LATEST
deduplicate_ignore_columns = ['account.update_time']
def get_depo_fetcher(self):
return fin_depo.defi_kucoin.KucoinDepoFetcher(
2024-06-02 16:01:18 +00:00
kucoin_key=secrets.KUCOIN_KEY,
kucoin_secret=secrets.KUCOIN_SECRET,
kucoin_pass=secrets.KUCOIN_PASS,
2024-06-02 16:00:55 +00:00
)
2024-06-02 16:01:18 +00:00
def addresses_to_data_points(addresses: list[dict[str, str]]) -> frozendict:
2024-05-18 20:03:34 +00:00
data_point = {}
data_point['account.update_time'] = datetime.datetime.now(tz=datetime.UTC)
2024-05-18 20:03:34 +00:00
data_point['account.num_deposit_addresses'] = len(addresses)
2024-05-18 20:03:34 +00:00
if len(addresses) > 0:
for k, v in addresses[-1].items():
data_point[f'deposit.{k}'] = v
return frozendict(data_point)
2024-05-18 19:35:58 +00:00
@dataclasses.dataclass(frozen=True)
class KucoinDepositAddresses(Scraper):
dataset_name = 'defi_kucoin_deposit_address'
deduplicate_mode = DeduplicateMode.ONLY_LATEST
deduplicate_ignore_columns = ['account.update_time']
def scrape(self) -> Iterator[Mapping[str, object]]:
2024-05-18 20:03:34 +00:00
addresses = client.get_deposit_address('MPC')
2024-05-18 20:03:34 +00:00
yield addresses_to_data_points(addresses)