1
0

Ruff
Some checks failed
Python Ruff Code Quality / ruff (push) Failing after 23s
Run Python tests (through Pytest) / Test (push) Successful in 28s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 24s

This commit is contained in:
Jon Michael Aanes 2024-11-28 00:45:46 +01:00
parent c1ae958458
commit 39cee8c601
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
4 changed files with 54 additions and 24 deletions

View File

@ -132,6 +132,7 @@ class TradeOrderDetails:
order_id: object order_id: object
raw_order_details: object raw_order_details: object
@enforce_typing.enforce_types @enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class WithdrawalDetails: class WithdrawalDetails:
@ -140,6 +141,7 @@ class WithdrawalDetails:
executed_time: datetime.datetime executed_time: datetime.datetime
raw_details: object raw_details: object
@enforce_typing.enforce_types @enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class DepositDetails: class DepositDetails:
@ -148,6 +150,7 @@ class DepositDetails:
executed_time: datetime.datetime executed_time: datetime.datetime
raw_details: object raw_details: object
@enforce_typing.enforce_types @enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class DoubleRegister: class DoubleRegister:

View File

@ -7,11 +7,18 @@ from collections.abc import Iterator
from decimal import Decimal from decimal import Decimal
import fin_defs import fin_defs
from fin_defs import AssetAmount
import kucoin.client import kucoin.client
from fin_defs import AssetAmount
from .data import (DepoFetcher, DepoGroup, DepoSingle, TradeOrderDetails, from .data import (
WithdrawalDetails, DepositDetails, DoubleRegister) DepoFetcher,
DepoGroup,
DepoSingle,
DepositDetails,
DoubleRegister,
TradeOrderDetails,
WithdrawalDetails,
)
logger = logging.getLogger(__name__) 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.CryptoCurrency('KCS', coingecko_id='kucoin-shares')
return fin_defs.WELL_KNOWN_SYMBOLS[ticker] return fin_defs.WELL_KNOWN_SYMBOLS[ticker]
def order_from_json(order_details: dict): 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 # Convert from kucoin results
if order_details['side'] == 'buy': if order_details['side'] == 'buy':
@ -185,15 +195,19 @@ class KucoinDepoFetcher(DepoFetcher):
withdrawn_amount = Decimal(item['amount']) withdrawn_amount = Decimal(item['amount'])
fee_asset = withdrawn_asset # Assumed fee_asset = withdrawn_asset # Assumed
fee_amount = Decimal(item['fee']) fee_amount = Decimal(item['fee'])
executed_time=datetime.datetime.fromtimestamp( executed_time = datetime.datetime.fromtimestamp(
item['createdAt'] / 1000, item['createdAt'] / 1000,
tz=datetime.UTC, tz=datetime.UTC,
) )
withdrawals.append(WithdrawalDetails(AssetAmount(withdrawn_asset, withdrawn_amount), withdrawals.append(
WithdrawalDetails(
AssetAmount(withdrawn_asset, withdrawn_amount),
AssetAmount(fee_asset, fee_amount), AssetAmount(fee_asset, fee_amount),
executed_time, executed_time,
item)) item,
),
)
del item del item
return withdrawals return withdrawals
@ -207,14 +221,19 @@ class KucoinDepoFetcher(DepoFetcher):
deposit_amount = Decimal(item['amount']) deposit_amount = Decimal(item['amount'])
fee_asset = deposit_asset # Assumed fee_asset = deposit_asset # Assumed
fee_amount = Decimal(item['fee']) fee_amount = Decimal(item['fee'])
executed_time=datetime.datetime.fromtimestamp( executed_time = datetime.datetime.fromtimestamp(
item['createdAt'] / 1000, item['createdAt'] / 1000,
tz=datetime.UTC, tz=datetime.UTC,
) )
deposits.append(DepositDetails(AssetAmount(deposit_asset, deposit_amount), deposits.append(
DepositDetails(
AssetAmount(deposit_asset, deposit_amount),
AssetAmount(fee_asset, fee_amount), AssetAmount(fee_asset, fee_amount),
executed_time, item)) executed_time,
item,
),
)
del item del item
return deposits return deposits
@ -223,7 +242,7 @@ class KucoinDepoFetcher(DepoFetcher):
end_time = datetime.datetime.now(tz=datetime.UTC) end_time = datetime.datetime.now(tz=datetime.UTC)
for _weeks_back in range(20): for _weeks_back in range(20):
end_time = end_time - datetime.timedelta(days=7) end_time = end_time - datetime.timedelta(days=7)
timestamp = int(end_time.timestamp()*1000) timestamp = int(end_time.timestamp() * 1000)
print(timestamp) print(timestamp)
raw_details = self.kucoin_client.get_orders(end=timestamp) raw_details = self.kucoin_client.get_orders(end=timestamp)
yield from (order_from_json(item) for item in raw_details['items']) 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() spot_trades = self._get_historic_spot_orders()
double_registers = [] double_registers = []
double_registers += [DoubleRegister(d.deposit, None, d.executed_time) for d in deposits] double_registers += [
double_registers += [DoubleRegister(None, d.withdrawn, d.executed_time) for d in withdrawals] DoubleRegister(d.deposit, None, d.executed_time) for d in deposits
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(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 return double_registers
def _get_order_details( def _get_order_details(
@ -253,7 +278,6 @@ class KucoinDepoFetcher(DepoFetcher):
order_details = self._get_order_with_retries(order_id, num_retries=10) order_details = self._get_order_with_retries(order_id, num_retries=10)
return order_from_json(order_details) return order_from_json(order_details)
def _get_order_with_retries( def _get_order_with_retries(
self, self,
order_id: str, order_id: str,

View File

@ -1,7 +1,5 @@
import datetime import datetime
from decimal import Decimal
import fin_defs
import pytest import pytest
import fin_depo import fin_depo
@ -19,6 +17,7 @@ fin_depo.defi_kucoin.logger.setLevel('INFO')
NOW = datetime.datetime.now(tz=datetime.UTC) NOW = datetime.datetime.now(tz=datetime.UTC)
def get_fetcher() -> fin_depo.defi_kucoin.KucoinDepoFetcher: def get_fetcher() -> fin_depo.defi_kucoin.KucoinDepoFetcher:
return fin_depo.defi_kucoin.KucoinDepoFetcher( return fin_depo.defi_kucoin.KucoinDepoFetcher(
secrets.KUCOIN_KEY, secrets.KUCOIN_KEY,
@ -26,6 +25,7 @@ def get_fetcher() -> fin_depo.defi_kucoin.KucoinDepoFetcher:
secrets.KUCOIN_PASS, secrets.KUCOIN_PASS,
) )
@needs_secrets @needs_secrets
def test_get_depo(): def test_get_depo():
"""Can inspect kucoin depository.""" """Can inspect kucoin depository."""
@ -35,21 +35,25 @@ def test_get_depo():
for nested_depo in depo.nested: for nested_depo in depo.nested:
assert isinstance(nested_depo, fin_depo.data.DepoSingle) assert isinstance(nested_depo, fin_depo.data.DepoSingle)
@needs_secrets @needs_secrets
def test_get_withdrawals(): def test_get_withdrawals():
withdrawals = get_fetcher()._get_withdrawals() withdrawals = get_fetcher()._get_withdrawals()
assert len(withdrawals) > 0 assert len(withdrawals) > 0
@needs_secrets @needs_secrets
def test_get_deposits(): def test_get_deposits():
deposits = get_fetcher()._get_deposits() deposits = get_fetcher()._get_deposits()
assert len(deposits) > 0 assert len(deposits) > 0
@needs_secrets @needs_secrets
def test_get_historic_spot_orders(): def test_get_historic_spot_orders():
orders = get_fetcher()._get_historic_spot_orders() orders = get_fetcher()._get_historic_spot_orders()
assert next(orders) assert next(orders)
@needs_secrets @needs_secrets
def test_get_double_registers(): def test_get_double_registers():
double_registers = get_fetcher()._get_double_registers() double_registers = get_fetcher()._get_double_registers()

View File

@ -106,4 +106,3 @@ def test_place_sell_side_order():
assert order_details.fee_amount is not None assert order_details.fee_amount is not None
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10) assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)