Browse Source

proto.eth.tx: new `swap_memo` attribute

The MMGen Project 7 months ago
parent
commit
d9e9fd6e4f

+ 1 - 3
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):

+ 3 - 5
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'))),

+ 3 - 3
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

+ 4 - 6
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):

+ 2 - 1
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

+ 1 - 3
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'))