From 96314330728fe14f1bcc77e574ac0e1488f0595a Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 15 Feb 2025 09:54:19 +0000 Subject: [PATCH] tx.new: get_autochg_addr(): add `desc` param --- mmgen/tw/addresses.py | 12 ++++++------ mmgen/tx/new.py | 23 ++++++++++++----------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index e1b15ec8..87792c02 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -274,7 +274,7 @@ class TwAddresses(TwView): return bool(e.recvd) return None # addr not in tracking wallet - def get_change_address(self, al_id, bot=None, top=None, exclude=None): + def get_change_address(self, al_id, bot=None, top=None, exclude=None, desc=None): """ Get lowest-indexed unused address in tracking wallet for requested AddrListID. Return values on failure: @@ -326,14 +326,14 @@ class TwAddresses(TwView): d.twmmid.hl(), yellow('has a label,'), d.comment.hl2(encl='‘’'), - yellow(',\n but allowing it for change anyway by user request') + yellow(f',\n but allowing it for {desc} anyway by user request') )) return d else: break return False - def get_change_address_by_addrtype(self, mmtype, exclude=None): + def get_change_address_by_addrtype(self, mmtype, exclude, desc): """ Find the lowest-indexed change addresses in tracking wallet of given address type, present them in a menu and return a single change address chosen by the user. @@ -352,9 +352,9 @@ class TwAddresses(TwView): c = yellow(' <== has a label!') if d.comment else '' ) - prompt = '\nChoose a change address:\n\n{}\n\nEnter a number> '.format( - '\n'.join(format_line(n, d) for n, d in enumerate(addrs, 1)) - ) + prompt = '\nChoose a {desc}:\n\n{items}\n\nEnter a number> '.format( + desc = desc, + items = '\n'.join(format_line(n, d) for n, d in enumerate(addrs, 1))) from ..ui import line_input while True: diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index ec90f370..135e82df 100755 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -194,25 +194,23 @@ class New(Base): return _pa(arg, mmid, coin_addr, amt, None) - async def get_autochg_addr(self, proto, arg, parsed_args): + async def get_autochg_addr(self, proto, arg, exclude, desc): from ..tw.addresses import TwAddresses al = await TwAddresses(self.cfg, proto, get_data=True) - exclude = [a.mmid for a in parsed_args if a.mmid] - if is_mmgen_addrtype(proto, arg): - res = al.get_change_address_by_addrtype(MMGenAddrType(proto, arg), exclude=exclude) - desc = 'of address type' + if obj := get_obj(MMGenAddrType, proto=proto, id_str=arg, silent=True): + res = al.get_change_address_by_addrtype(obj, exclude=exclude, desc=desc) + req_desc = f'of address type {arg!r}' else: - res = al.get_change_address(arg, exclude=exclude) - desc = 'from address list' + res = al.get_change_address(arg, exclude=exclude, desc=desc) + req_desc = f'from address list {arg!r}' if res: return res - die(2, 'Tracking wallet contains no {t}addresses {d} {a!r}'.format( + die(2, 'Tracking wallet contains no {t}addresses {d}'.format( t = '' if res is None else 'unused ', - d = desc, - a = arg)) + d = req_desc)) async def process_cmdline_args(self, cmd_args, ad_f, ad_w): @@ -228,8 +226,11 @@ class New(Base): if a.data: self.add_output(None, self.proto.coin_amt('0'), data=a.data) else: + exclude = [a.mmid for a in parsed_args if a.mmid] self.add_output( - coinaddr = a.addr or (await self.get_autochg_addr(self.proto, a.arg, parsed_args)).addr, + coinaddr = a.addr or ( + await self.get_autochg_addr(self.proto, a.arg, exclude=exclude, desc='change address') + ).addr, amt = self.proto.coin_amt(a.amt or '0'), is_chg = not a.amt)