tx.base: new swap_attrs attribute

This commit is contained in:
The MMGen Project 2025-04-21 14:01:16 +00:00
commit 8ffb8fb76a
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 21 additions and 23 deletions

View file

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

View file

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

View file

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

View file

@ -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]()

View file

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