From d9e9fd6e4fc12f6cba621b54fe98e22dbcf57b9d Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 21 Apr 2025 14:01:16 +0000 Subject: [PATCH] proto.eth.tx: new `swap_memo` attribute --- mmgen/proto/eth/tx/completed.py | 4 +--- mmgen/proto/eth/tx/info.py | 8 +++----- mmgen/proto/eth/tx/new.py | 6 +++--- mmgen/proto/eth/tx/new_swap.py | 10 ++++------ mmgen/tx/base.py | 3 ++- mmgen/tx/info.py | 4 +--- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/mmgen/proto/eth/tx/completed.py b/mmgen/proto/eth/tx/completed.py index 945a1a42..587b5178 100755 --- a/mmgen/proto/eth/tx/completed.py +++ b/mmgen/proto/eth/tx/completed.py @@ -19,9 +19,7 @@ class Completed(Base, TxBase.Completed): fn_fee_unit = 'Mwei' def get_swap_memo_maybe(self): - o = self.txobj - if o['to'] and o['data']: - return bytes.fromhex(o['data']) + return self.swap_memo.encode() if getattr(self, 'swap_memo', None) else None @property def send_amt(self): diff --git a/mmgen/proto/eth/tx/info.py b/mmgen/proto/eth/tx/info.py index c455ae86..18b58ba8 100755 --- a/mmgen/proto/eth/tx/info.py +++ b/mmgen/proto/eth/tx/info.py @@ -40,7 +40,7 @@ class TxInfo(TxInfo): Start gas: {G} Kwei Nonce: {n} Data: {d} - """.strip().replace('\t', '') + """.strip().replace('\t', '') + ('\nMemo: {m}' if tx.is_swap else '') t = tx.txobj td = t['data'] to_addr = t[self.to_addr_key] @@ -49,10 +49,8 @@ class TxInfo(TxInfo): t = to_addr.hl(0) if to_addr else blue('None'), a = t['amt'].hl(), n = t['nonce'].hl(), - d = ( - blue('None') if not td - else pink(bytes.fromhex(td).decode()) if tx.is_swap - else '{}... ({} bytes)'.format(td[:40], len(td)//2)), + d = blue('None') if not td else '{}... ({} bytes)'.format(td[:40], len(td)//2), + m = pink(tx.swap_memo) if tx.is_swap else None, c = tx.proto.dcoin if len(tx.outputs) else '', g = yellow(tx.pretty_fmt_fee(t['gasPrice'].to_unit('Gwei'))), G = yellow(tx.pretty_fmt_fee(t['startGas'].to_unit('Kwei'))), diff --git a/mmgen/proto/eth/tx/new.py b/mmgen/proto/eth/tx/new.py index 839acec8..14d43120 100755 --- a/mmgen/proto/eth/tx/new.py +++ b/mmgen/proto/eth/tx/new.py @@ -68,7 +68,7 @@ class New(Base, TxBase.New): async def create_serialized(self, *, locktime=None): assert len(self.inputs) == 1, 'Transaction has more than one input!' o_num = len(self.outputs) - o_ok = 0 if self.usr_contract_data and not self.is_swap else 1 + o_ok = 0 if self.usr_contract_data else 1 assert o_num == o_ok, f'Transaction has {o_num} output{suf(o_num)} (should have {o_ok})' await self.make_txobj() odict = {k:v if v is None else str(v) for k, v in self.txobj.items() if k != 'token_to'} @@ -97,8 +97,8 @@ class New(Base, TxBase.New): data_arg = cmd_args.pop() lc = 1 assert data_arg.startswith('data:'), f'{data_arg}: invalid data arg (must start with "data:")' - self.usr_contract_data = data_arg.removeprefix('data:').encode() - self.set_gas_with_data(self.usr_contract_data) + self.swap_memo = data_arg.removeprefix('data:') + self.set_gas_with_data(self.swap_memo.encode()) if lc == 0 and self.usr_contract_data and 'Token' not in self.name: return diff --git a/mmgen/proto/eth/tx/new_swap.py b/mmgen/proto/eth/tx/new_swap.py index 515f17a4..1e77cf05 100755 --- a/mmgen/proto/eth/tx/new_swap.py +++ b/mmgen/proto/eth/tx/new_swap.py @@ -19,15 +19,13 @@ class NewSwap(New, TxNewSwap): desc = 'Ethereum swap transaction' def update_data_output(self, trade_limit): - data = bytes.fromhex(self.txobj['data']) if self.is_bump else self.usr_contract_data - parsed_memo = self.swap_proto_mod.Memo.parse(data.decode()) - memo = self.swap_proto_mod.Memo( + parsed_memo = self.swap_proto_mod.Memo.parse(self.swap_memo) + self.swap_memo = str(self.swap_proto_mod.Memo( self.recv_proto, self.recv_asset, self.recv_proto.coin_addr(parsed_memo.address), - trade_limit = trade_limit) - self.usr_contract_data = str(memo).encode() - self.set_gas_with_data(self.usr_contract_data) + trade_limit = trade_limit)) + self.set_gas_with_data(self.swap_memo.encode()) @property def vault_idx(self): diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index 498ad005..b44779c8 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -85,7 +85,8 @@ class Base(MMGenObject): 'swap_proto': None, 'swap_quote_expiry': None, 'swap_recv_addr_mmid': None, - 'swap_recv_asset_spec': None} + 'swap_recv_asset_spec': None, + 'swap_memo': 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/info.py b/mmgen/tx/info.py index 8c40edf2..7ad0b52a 100755 --- a/mmgen/tx/info.py +++ b/mmgen/tx/info.py @@ -74,9 +74,7 @@ class TxInfo: if tx.is_swap: from ..swap.proto.thorchain import Memo, name - data = ( - (tx.usr_contract_data or bytes.fromhex(tx.txobj['data'])) if tx.proto.is_evm - else tx.data_output.data) + data = tx.swap_memo.encode() 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'))