From 8ffb8fb76a64adba690d25be21bd783909a48c9d Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 21 Apr 2025 14:01:16 +0000 Subject: [PATCH] tx.base: new `swap_attrs` attribute --- mmgen/tx/base.py | 9 +++++---- mmgen/tx/bump.py | 10 ++-------- mmgen/tx/completed.py | 5 ++--- mmgen/tx/file.py | 15 +++++++++------ mmgen/tx/info.py | 5 +++-- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index e69a9891..c0653a8e 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -81,10 +81,11 @@ class Base(MMGenObject): signed = False is_bump = False is_swap = False - swap_proto = None - swap_quote_expiry = None - swap_recv_addr_mmid = None - swap_recv_asset_spec = None + swap_attrs = { + 'swap_proto': None, + 'swap_quote_expiry': None, + 'swap_recv_addr_mmid': None, + 'swap_recv_asset_spec': None} file_format = 'json' non_mmgen_inputs_msg = f""" This transaction includes inputs with non-{gc.proj_name} addresses. When diff --git a/mmgen/tx/bump.py b/mmgen/tx/bump.py index 900fc47c..e2d30c98 100755 --- a/mmgen/tx/bump.py +++ b/mmgen/tx/bump.py @@ -22,12 +22,6 @@ class Bump(Completed, NewSwap): ext = 'rawtx' bump_output_idx = None is_bump = True - swap_attrs = ( - 'is_swap', - 'swap_proto', - 'swap_quote_expiry', - 'swap_recv_addr_mmid', - 'swap_recv_asset_spec') def __init__(self, *, check_sent, new_outputs, **kwargs): @@ -38,9 +32,9 @@ class Bump(Completed, NewSwap): if new_outputs: if self.is_swap: - from .base import Base + self.is_swap = False for attr in self.swap_attrs: - setattr(self, attr, getattr(Base, attr)) + setattr(self, attr, None) self.outputs = self.OutputList(self) self.cfg = kwargs['cfg'] # must use current cfg opts, not those from orig_tx diff --git a/mmgen/tx/completed.py b/mmgen/tx/completed.py index 68f90583..346d69d2 100755 --- a/mmgen/tx/completed.py +++ b/mmgen/tx/completed.py @@ -85,12 +85,11 @@ class Completed(Base): network = self.cfg.network, tokensym = None if p.chain == p.asset else p.asset, need_amt = True) - if self.swap_recv_addr_mmid: - mmid = self.swap_recv_addr_mmid + if mmid := getattr(self, 'swap_recv_addr_mmid', None): + pass elif self.cfg.allow_non_wallet_swap: from ..util import ymsg ymsg('Warning: allowing swap to non-wallet address (--allow-non-wallet-swap)') - mmid = None else: raise ValueError('Swap to non-wallet address forbidden (override with --allow-non-wallet-swap)') return self.Output(proto, addr=p.address, mmid=mmid, amt=proto.coin_amt('0')) diff --git a/mmgen/tx/file.py b/mmgen/tx/file.py index 8fde31d9..28dd5f47 100755 --- a/mmgen/tx/file.py +++ b/mmgen/tx/file.py @@ -70,11 +70,7 @@ class MMGenTxFile(MMGenObject): 'comment': MMGenTxComment, 'coin_txid': CoinTxID, 'sent_timestamp': None, - 'is_swap': None, - 'swap_proto': None, - 'swap_quote_expiry': None, - 'swap_recv_addr_mmid': None, - 'swap_recv_asset_spec': None} + 'is_swap': None} def __init__(self, tx): self.tx = tx @@ -112,6 +108,11 @@ class MMGenTxFile(MMGenObject): if k in data: setattr(tx, k, v(data[k]) if v else data[k]) + if tx.is_swap: + for k, v in tx.swap_attrs.items(): + if k in data: + setattr(tx, k, v(data[k]) if v else data[k]) + for k in ('inputs', 'outputs'): setattr(tx, k, eval_io_data(tx, data[k], desc=k)) @@ -277,7 +278,9 @@ class MMGenTxFile(MMGenObject): for e in tx.outputs] } | { k: getattr(tx, k) for k in self.extra_attrs if getattr(tx, k) - }) + } | ({ + k: getattr(tx, k) for k in tx.swap_attrs if getattr(tx, k, None) + } if tx.is_swap else {})) return '{{"{}":{},"chksum":"{}"}}'.format(self.data_label, data, make_chksum_6(data)) fmt_data = {'json': format_data_json, 'legacy': format_data_legacy}[tx.file_format]() diff --git a/mmgen/tx/info.py b/mmgen/tx/info.py index b7ec085e..8c40edf2 100755 --- a/mmgen/tx/info.py +++ b/mmgen/tx/info.py @@ -78,13 +78,14 @@ class TxInfo: (tx.usr_contract_data or bytes.fromhex(tx.txobj['data'])) if tx.proto.is_evm else tx.data_output.data) if Memo.is_partial_memo(data): + recv_mmid = getattr(tx, 'swap_recv_addr_mmid', None) p = Memo.parse(data.decode('ascii')) yield ' {} {}\n'.format(magenta('DEX Protocol:'), blue(name)) yield ' Swap: {}\n'.format(orange(f'{tx.send_asset.name} => {tx.recv_asset.name}')) yield ' Dest: {}{}\n'.format( cyan(p.address), - orange(f' ({tx.swap_recv_addr_mmid})') if tx.swap_recv_addr_mmid else '') - if not tx.swap_recv_addr_mmid: + orange(f' ({recv_mmid})') if recv_mmid else '') + if not recv_mmid: yield yellow(' Warning: swap destination address is not a wallet address!\n') enl = ('\n', '')[bool(terse)]