From 39cee8c601260eaa78bb221f3f3d973abf039868 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 28 Nov 2024 00:45:46 +0100 Subject: [PATCH] Ruff --- fin_depo/data.py | 3 ++ fin_depo/defi_kucoin.py | 66 +++++++++++++++++++++++++------------ test/test_kucoin.py | 8 +++-- test/test_kucoin_trading.py | 1 - 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/fin_depo/data.py b/fin_depo/data.py index f8b2056..b0dd2e6 100644 --- a/fin_depo/data.py +++ b/fin_depo/data.py @@ -132,6 +132,7 @@ class TradeOrderDetails: order_id: object raw_order_details: object + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True) class WithdrawalDetails: @@ -140,6 +141,7 @@ class WithdrawalDetails: executed_time: datetime.datetime raw_details: object + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True) class DepositDetails: @@ -148,6 +150,7 @@ class DepositDetails: executed_time: datetime.datetime raw_details: object + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True) class DoubleRegister: diff --git a/fin_depo/defi_kucoin.py b/fin_depo/defi_kucoin.py index c99c8e2..fc8f89c 100644 --- a/fin_depo/defi_kucoin.py +++ b/fin_depo/defi_kucoin.py @@ -7,11 +7,18 @@ from collections.abc import Iterator from decimal import Decimal import fin_defs -from fin_defs import AssetAmount import kucoin.client +from fin_defs import AssetAmount -from .data import (DepoFetcher, DepoGroup, DepoSingle, TradeOrderDetails, - WithdrawalDetails, DepositDetails, DoubleRegister) +from .data import ( + DepoFetcher, + DepoGroup, + DepoSingle, + DepositDetails, + DoubleRegister, + TradeOrderDetails, + WithdrawalDetails, +) logger = logging.getLogger(__name__) @@ -21,8 +28,11 @@ def parse_asset_from_ticker(ticker: str) -> fin_defs.Asset: return fin_defs.CryptoCurrency('KCS', coingecko_id='kucoin-shares') return fin_defs.WELL_KNOWN_SYMBOLS[ticker] + def order_from_json(order_details: dict): - (asset, base_asset) = [parse_asset_from_ticker(a) for a in order_details['symbol'].split('-')] + (asset, base_asset) = [ + parse_asset_from_ticker(a) for a in order_details['symbol'].split('-') + ] # Convert from kucoin results if order_details['side'] == 'buy': @@ -183,17 +193,21 @@ class KucoinDepoFetcher(DepoFetcher): for item in raw_details['items']: withdrawn_asset = parse_asset_from_ticker(item['currency']) withdrawn_amount = Decimal(item['amount']) - fee_asset = withdrawn_asset # Assumed + fee_asset = withdrawn_asset # Assumed fee_amount = Decimal(item['fee']) - executed_time=datetime.datetime.fromtimestamp( + executed_time = datetime.datetime.fromtimestamp( item['createdAt'] / 1000, tz=datetime.UTC, ) - withdrawals.append(WithdrawalDetails(AssetAmount(withdrawn_asset, withdrawn_amount), - AssetAmount(fee_asset, fee_amount), - executed_time, - item)) + withdrawals.append( + WithdrawalDetails( + AssetAmount(withdrawn_asset, withdrawn_amount), + AssetAmount(fee_asset, fee_amount), + executed_time, + item, + ), + ) del item return withdrawals @@ -205,16 +219,21 @@ class KucoinDepoFetcher(DepoFetcher): for item in raw_details['items']: deposit_asset = parse_asset_from_ticker(item['currency']) deposit_amount = Decimal(item['amount']) - fee_asset = deposit_asset # Assumed + fee_asset = deposit_asset # Assumed fee_amount = Decimal(item['fee']) - executed_time=datetime.datetime.fromtimestamp( + executed_time = datetime.datetime.fromtimestamp( item['createdAt'] / 1000, tz=datetime.UTC, ) - deposits.append(DepositDetails(AssetAmount(deposit_asset, deposit_amount), - AssetAmount(fee_asset, fee_amount), - executed_time, item)) + deposits.append( + DepositDetails( + AssetAmount(deposit_asset, deposit_amount), + AssetAmount(fee_asset, fee_amount), + executed_time, + item, + ), + ) del item return deposits @@ -223,7 +242,7 @@ class KucoinDepoFetcher(DepoFetcher): end_time = datetime.datetime.now(tz=datetime.UTC) for _weeks_back in range(20): end_time = end_time - datetime.timedelta(days=7) - timestamp = int(end_time.timestamp()*1000) + timestamp = int(end_time.timestamp() * 1000) print(timestamp) raw_details = self.kucoin_client.get_orders(end=timestamp) yield from (order_from_json(item) for item in raw_details['items']) @@ -235,10 +254,16 @@ class KucoinDepoFetcher(DepoFetcher): spot_trades = self._get_historic_spot_orders() double_registers = [] - double_registers += [DoubleRegister(d.deposit, None, d.executed_time) for d in deposits] - double_registers += [DoubleRegister(None, d.withdrawn, d.executed_time) for d in withdrawals] - double_registers += [DoubleRegister(d.input, d.output, d.executed_time) for d in spot_trades] - double_registers.sort(key=lambda x:x.executed_time) + double_registers += [ + DoubleRegister(d.deposit, None, d.executed_time) for d in deposits + ] + double_registers += [ + DoubleRegister(None, d.withdrawn, d.executed_time) for d in withdrawals + ] + double_registers += [ + DoubleRegister(d.input, d.output, d.executed_time) for d in spot_trades + ] + double_registers.sort(key=lambda x: x.executed_time) return double_registers def _get_order_details( @@ -253,7 +278,6 @@ class KucoinDepoFetcher(DepoFetcher): order_details = self._get_order_with_retries(order_id, num_retries=10) return order_from_json(order_details) - def _get_order_with_retries( self, order_id: str, diff --git a/test/test_kucoin.py b/test/test_kucoin.py index 613c85c..f4ee006 100644 --- a/test/test_kucoin.py +++ b/test/test_kucoin.py @@ -1,7 +1,5 @@ import datetime -from decimal import Decimal -import fin_defs import pytest import fin_depo @@ -19,6 +17,7 @@ fin_depo.defi_kucoin.logger.setLevel('INFO') NOW = datetime.datetime.now(tz=datetime.UTC) + def get_fetcher() -> fin_depo.defi_kucoin.KucoinDepoFetcher: return fin_depo.defi_kucoin.KucoinDepoFetcher( secrets.KUCOIN_KEY, @@ -26,6 +25,7 @@ def get_fetcher() -> fin_depo.defi_kucoin.KucoinDepoFetcher: secrets.KUCOIN_PASS, ) + @needs_secrets def test_get_depo(): """Can inspect kucoin depository.""" @@ -35,21 +35,25 @@ def test_get_depo(): for nested_depo in depo.nested: assert isinstance(nested_depo, fin_depo.data.DepoSingle) + @needs_secrets def test_get_withdrawals(): withdrawals = get_fetcher()._get_withdrawals() assert len(withdrawals) > 0 + @needs_secrets def test_get_deposits(): deposits = get_fetcher()._get_deposits() assert len(deposits) > 0 + @needs_secrets def test_get_historic_spot_orders(): orders = get_fetcher()._get_historic_spot_orders() assert next(orders) + @needs_secrets def test_get_double_registers(): double_registers = get_fetcher()._get_double_registers() diff --git a/test/test_kucoin_trading.py b/test/test_kucoin_trading.py index 0bbb24c..54a1a09 100644 --- a/test/test_kucoin_trading.py +++ b/test/test_kucoin_trading.py @@ -106,4 +106,3 @@ def test_place_sell_side_order(): assert order_details.fee_amount is not None assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10) -