1
0

More flexible datastructures for double registers.

This commit is contained in:
Jon Michael Aanes 2024-11-29 11:06:43 +01:00
parent ef9aa91499
commit 9c5f3db41d
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 50 additions and 27 deletions

View File

@ -1,6 +1,7 @@
"""Datastructures for `fin-depo`."""
import abc
import dataclassabc
import dataclasses
import datetime
from collections.abc import Iterable, Mapping
@ -107,8 +108,32 @@ class DepoFetcher(abc.ABC):
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class TradeOrderDetails:
class DoubleRegister(abc.ABC):
@property
@abc.abstractmethod
def input(self) -> AssetAmount | None:
raise NotImplementedError
@property
@abc.abstractmethod
def output(self) -> AssetAmount | None:
raise NotImplementedError
@property
@abc.abstractmethod
def fee(self) -> AssetAmount | None:
raise NotImplementedError
@property
@abc.abstractmethod
def executed_time(self) -> datetime.datetime:
raise NotImplementedError
@enforce_typing.enforce_types
@dataclassabc.dataclassabc(frozen=True)
class TradeOrderDetails(DoubleRegister):
"""Information about an executed trade.
Includes both well-structured data about the order, and unstructured data
@ -136,26 +161,34 @@ class TradeOrderDetails:
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class WithdrawalDetails:
@dataclassabc.dataclassabc(frozen=True)
class WithdrawalDetails(DoubleRegister):
withdrawn: AssetAmount
fee: AssetAmount
executed_time: datetime.datetime
raw_details: object
@property
def input(self) -> AssetAmount:
return self.withdrawn
@property
def output(self) -> None:
return None
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class DepositDetails:
@dataclassabc.dataclassabc(frozen=True)
class DepositDetails(DoubleRegister):
deposit: AssetAmount
fee: AssetAmount
executed_time: datetime.datetime
raw_details: object
@property
def input(self) -> None:
return None
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class DoubleRegister:
input: AssetAmount | None
output: AssetAmount | None
executed_time: datetime.datetime
@property
def output(self) -> AssetAmount:
return self.deposit

View File

@ -247,20 +247,10 @@ class KucoinDepoFetcher(DepoFetcher):
del _weeks_back, raw_details
def _get_double_registers(self) -> list[DoubleRegister]:
deposits = self._get_deposits()
withdrawals = self._get_withdrawals()
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: list[DoubleRegister] = []
double_registers += self._get_deposits()
double_registers += self._get_withdrawals()
double_registers += self._get_historic_spot_orders()
double_registers.sort(key=lambda x: x.executed_time)
return double_registers

View File

@ -31,7 +31,7 @@ def test_place_market_order_requires_allow_trades():
)
with pytest.raises(PermissionError) as m:
fetcher.place_market_order(fin_defs.MPC, Decimal(1), fin_defs.USDT)
fetcher.place_market_order(fin_defs.AssetAmount(fin_defs.MPC, Decimal(1)), fin_defs.USDT)
assert 'KucoinDepoFetcher.allow_trades is not enabled: Cannot make trades' in str(m)