THORChainMemo.parse(): return SwapAsset instance in tuple

This commit is contained in:
The MMGen Project 2025-06-07 10:34:57 +03:00
commit 9e280bed37
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 17 additions and 13 deletions

View file

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

View file

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

View file

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

View file

@ -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}'