Generalize `MktPair.from_msg()` handling

Accept a msg with any of:
- `.src: Asset` and `.dst: Asset`
- `.src: str` and `.dst: str`
- `.src: Asset` and `.dst: str`

but not the final combo tho XD
Also, fix `.key` to properly cast any `.src: Asset` to string!
pre_overruns_ctxcancelled
Tyler Goodlet 2023-03-23 16:29:31 -04:00
parent 4236e5c3b1
commit 2142c13228
1 changed files with 21 additions and 5 deletions

View File

@ -236,16 +236,31 @@ class MktPair(Struct, frozen=True):
''' '''
dst_asset_msg = msg.pop('dst') dst_asset_msg = msg.pop('dst')
src_asset_msg = msg.pop('src')
if isinstance(dst_asset_msg, str): if isinstance(dst_asset_msg, str):
src: str = str(src_asset_msg)
assert isinstance(src, str)
return cls.from_fqme( return cls.from_fqme(
dst_asset_msg, dst_asset_msg,
src=src,
**msg, **msg,
) )
# NOTE: we call `.copy()` here to ensure else:
# type casting! # NOTE: we call `.copy()` here to ensure
dst = Asset(**dst_asset_msg).copy() # type casting!
return cls(dst=dst, **msg).copy() dst = Asset(**dst_asset_msg).copy()
if not isinstance(src_asset_msg, str):
src = Asset(**src_asset_msg).copy()
else:
src = str(src_asset_msg)
return cls(
dst=dst,
src=src,
**msg,
).copy()
@property @property
def resolved(self) -> bool: def resolved(self) -> bool:
@ -292,7 +307,8 @@ class MktPair(Struct, frozen=True):
''' '''
return maybe_cons_tokens( return maybe_cons_tokens(
[str(self.dst), self.src], [str(self.dst),
str(self.src)],
delim_char='', delim_char='',
) )