From 9e280bed37d22e354bcc028bd3b67df115f9f356 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 7 Jun 2025 10:34:57 +0300 Subject: [PATCH] THORChainMemo.parse(): return `SwapAsset` instance in tuple --- mmgen/swap/asset.py | 10 +++++++--- mmgen/swap/proto/thorchain/memo.py | 8 ++++---- mmgen/tx/completed.py | 8 ++++---- test/modtest_d/swap.py | 4 ++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mmgen/swap/asset.py b/mmgen/swap/asset.py index 869d9475..14e3260e 100644 --- a/mmgen/swap/asset.py +++ b/mmgen/swap/asset.py @@ -50,10 +50,10 @@ class SwapAsset: return '\n'.join(gen_good()) + '\n'.join(gen_bad()) @classmethod - def get_full_name(cls, s): + def init_from_memo(cls, s): for d in cls.assets_data.values(): if s in (d.abbr, d.full_name): - return d.full_name or f'{d.name}.{d.name}' + return cls(d.name or d.full_name, 'recv') die('SwapAssetError', f'{s!r}: unrecognized asset name or abbreviation') @property @@ -86,7 +86,11 @@ class SwapAsset: @property def short_name(self): - return self.asset or self.chain + return self.data.name or self.data.full_name.split('.', 1)[1] + + @property + def tokensym(self): + return None if self.data.name else self.data.full_name.split('.', 1)[1] @property def memo_asset_name(self): diff --git a/mmgen/swap/proto/thorchain/memo.py b/mmgen/swap/proto/thorchain/memo.py index 0d53c26c..9517d439 100755 --- a/mmgen/swap/proto/thorchain/memo.py +++ b/mmgen/swap/proto/thorchain/memo.py @@ -72,11 +72,11 @@ class THORChainMemo: function = get_id(cls.function_abbrevs, get_item('function'), 'function') - chain, asset = SwapAsset.get_full_name(get_item('asset')).split('.') + asset = SwapAsset.init_from_memo(get_item('asset')) address = get_item('address') - if chain in SwapAsset.evm_chains: + if asset.chain in SwapAsset.evm_chains: assert address.startswith('0x'), f'{address}: address does not start with ‘0x’' assert len(address) == 42, f'{address}: address has incorrect length ({len(address)} != 42)' address = address.removeprefix('0x') @@ -104,9 +104,9 @@ class THORChainMemo: ret = namedtuple( 'parsed_memo', - ['proto', 'function', 'chain', 'asset', 'address', 'trade_limit', 'stream_interval', 'stream_quantity']) + ['proto', 'function', 'asset', 'address', 'trade_limit', 'stream_interval', 'stream_quantity']) - return ret(proto_name, function, chain, asset, address, limit_int, int(interval), int(quantity)) + return ret(proto_name, function, asset, address, limit_int, int(interval), int(quantity)) def __init__(self, swap_cfg, proto, asset, addr, *, trade_limit): diff --git a/mmgen/tx/completed.py b/mmgen/tx/completed.py index 39e1429a..df8c4237 100755 --- a/mmgen/tx/completed.py +++ b/mmgen/tx/completed.py @@ -83,14 +83,14 @@ class Completed(Base): from ..protocol import init_proto text = memo_bytes.decode('ascii') p = Memo.parse(text) + r = self.recv_asset assert p.function == 'SWAP', f'‘{p.function}’: unsupported function in swap memo ‘{text}’' - aname = p.chain + (f'.{p.asset}' if p.asset != p.chain else '') - assert aname == self.recv_asset.name, f'invalid memo: {aname} != {self.recv_asset.name}' + assert p.asset.name == r.name, f'invalid memo: {p.asset.name} != {r.name}' proto = init_proto( self.cfg, - p.chain, + r.coin, network = self.cfg.network, - tokensym = None if p.chain == p.asset else p.asset, + tokensym = r.tokensym, need_amt = True) if mmid := getattr(self, 'swap_recv_addr_mmid', None): pass diff --git a/test/modtest_d/swap.py b/test/modtest_d/swap.py index d0cb8bdf..269f684b 100755 --- a/test/modtest_d/swap.py +++ b/test/modtest_d/swap.py @@ -123,8 +123,8 @@ class unit_tests: vmsg(pformat(p._asdict())) assert p.proto == 'THORChain' assert p.function == 'SWAP' - assert p.chain == coin.upper() - assert p.asset == token or coin.upper() + assert p.asset.chain == coin.upper() + assert p.asset.coin == token or coin.upper() assert p.address == addr.views[addr.view_pref] assert p.trade_limit == limit_chk assert p.stream_interval == si or swap_cfg.si.dfl, f'{p.stream_interval} != {swap_cfg.si.dfl}'