From 6d5d9731edfc2fb6470383bf651920d527ffab0d Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 21 Mar 2023 13:59:06 -0400 Subject: [PATCH] Implement `MktPair.from_msg()` constructor Handle case where `'dst'` field is just a `str` (in which case delegate to `.from_fqme()`) as well as do `Asset` loading and use our `Struct.copy()` to enforce type-casting to (for eg. `Decimal`s) such that we'll now capture typing errors despite IPC transport. Change `Symbol.tick_size` and `.lot_tick_size` defaults to decimal for proper casting and type `MktPair.atype: str` since `msgspec` can't cast to `AssetTypeName` without special handling.. --- piker/accounting/_mktinfo.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/piker/accounting/_mktinfo.py b/piker/accounting/_mktinfo.py index 5d9edbd6..fcb6c0f8 100644 --- a/piker/accounting/_mktinfo.py +++ b/piker/accounting/_mktinfo.py @@ -111,7 +111,7 @@ class Asset(Struct, frozen=True): ''' name: str - atype: AssetTypeName + atype: str # AssetTypeName # minimum transaction size / precision. # eg. for buttcoin this is a "satoshi". @@ -223,7 +223,7 @@ class MktPair(Struct, frozen=True): @classmethod def from_msg( - self, + cls, msg: dict[str, Any], ) -> MktPair: @@ -231,7 +231,17 @@ class MktPair(Struct, frozen=True): Constructor for a received msg-dict normally received over IPC. ''' - raise NotImplementedError + dst_asset_msg = msg.pop('dst') + if isinstance(dst_asset_msg, str): + return cls.from_fqme( + dst_asset_msg, + **msg, + ) + + # NOTE: we call `.copy()` here to ensure + # type casting! + dst = Asset(**dst_asset_msg).copy() + return cls(dst=dst, **msg).copy() @property def resolved(self) -> bool: @@ -264,7 +274,7 @@ class MktPair(Struct, frozen=True): broker=broker, **kwargs, - ) + ).copy() @property def key(self) -> str: @@ -425,8 +435,11 @@ class Symbol(Struct): ''' key: str - tick_size: Decimal = 0.01 - lot_tick_size: Decimal = 0.0 # "volume" precision as min step value + + # precision descriptors for price and vlm + tick_size: Decimal = Decimal('0.01') + lot_tick_size: Decimal = Decimal('0.0') + suffix: str = '' broker_info: dict[str, dict[str, Any]] = {}