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

45 lines
1.2 KiB
Python
Raw Normal View History

2024-05-16 22:24:01 +00:00
import dataclasses
import datetime
import logging
from collections.abc import Iterator, Mapping
from decimal import Decimal
2024-05-17 23:05:02 +00:00
import krakenex
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!
2024-05-17 23:05:02 +00:00
client = krakenex.API(
secrets.KRAKEN_KEY,
secrets.KRAKEN_SECRET,
2024-05-16 22:24:01 +00:00
)
@dataclasses.dataclass(frozen=True)
2024-05-17 23:05:02 +00:00
class KrakenAccountBalances(Scraper):
dataset_name = 'defi_kraken_balance'
2024-05-16 22:24:01 +00:00
deduplicate_mode = DeduplicateMode.ONLY_LATEST
deduplicate_ignore_columns = ['account.update_time']
def scrape(self) -> Iterator[Mapping[str, object]]:
data_point = {
'account.update_time': datetime.datetime.now(tz=datetime.UTC),
}
2024-05-16 22:24:01 +00:00
2024-05-17 23:05:02 +00:00
result = client.query_private('Balance')
for account, balance_str in result['result'].items():
key = f'balance.{account}'
balance = Decimal(balance_str)
balance = balance if balance != 0 else Decimal(0)
data_point[key] = data_point.get(key, Decimal(0)) + balance
del key, balance
2024-05-16 22:24:01 +00:00
yield frozendict(data_point)
2024-05-17 23:05:02 +00:00