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
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:

View File

@ -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,

View File

@ -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()

View File

@ -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)