Browse Source

minor fixes and cleanups

The MMGen Project 6 months ago
parent
commit
0dd09321fd
3 changed files with 42 additions and 25 deletions
  1. 19 11
      mmgen/swap/proto/thorchain/memo.py
  2. 4 1
      mmgen/tx/new_swap.py
  3. 19 13
      test/modtest_d/swap.py

+ 19 - 11
mmgen/swap/proto/thorchain/memo.py

@@ -121,19 +121,18 @@ class THORChainMemo:
 		return ret(proto_name, function, chain, asset, address, limit_int, int(interval), int(quantity))
 
 	def __init__(self, proto, asset, addr, *, trade_limit=None):
-		self.proto = proto
-		self.asset = asset
+
+		from ....amt import UniAmt
+		from ....addr import is_coin_addr
+
+		assert trade_limit is None or isinstance(trade_limit, UniAmt), f'{type(trade_limit)} != {UniAmt}'
+		assert is_coin_addr(proto, addr)
+
 		assert asset.chain == proto.coin, f'{asset.chain} != {proto.coin}'
 		assert asset.asset == getattr(proto, 'tokensym', None), (
 			f'{asset.asset} != {getattr(proto, "tokensym", None)}')
 		assert asset.direction == 'recv', f'{asset.direction} != ‘recv’'
-		if trade_limit is None:
-			self.trade_limit = UniAmt('0')
-		else:
-			assert type(trade_limit) is UniAmt, f'{type(trade_limit)} != {UniAmt}'
-			self.trade_limit = trade_limit
-		from ....addr import is_coin_addr
-		assert is_coin_addr(proto, addr)
+
 		self.addr = addr.views[addr.view_pref]
 		assert not ':' in self.addr # colon is record separator, so address mustn’t contain one
 
@@ -142,13 +141,22 @@ class THORChainMemo:
 			assert is_hex_str(self.addr), f'{self.addr}: address is not a hexadecimal string'
 			self.addr = '0x' + self.addr
 
+		self.proto = proto
+		self.asset = asset
+		self.trade_limit = trade_limit
+
 	def __str__(self):
 		from . import ExpInt4
 		try:
-			tl_enc = ExpInt4(self.trade_limit.to_unit('satoshi')).enc
+			tl_enc = (
+				0 if self.trade_limit is None else
+				ExpInt4(self.trade_limit.to_unit('satoshi')).enc)
 		except Exception as e:
 			die('SwapMemoParseError', str(e))
-		suf = '/'.join(str(n) for n in (tl_enc, self.stream_interval, self.stream_quantity))
+		suf = '/'.join(str(n) for n in (
+			tl_enc,
+			self.stream_interval,
+			self.stream_quantity))
 		ret = ':'.join([
 			self.function_abbrevs[self.function],
 			self.asset.memo_asset_name,

+ 4 - 1
mmgen/tx/new_swap.py

@@ -156,7 +156,10 @@ class NewSwap(New):
 				'To sign this transaction, autosign or txsign must be invoked'
 				' with --allow-non-wallet-swap'))
 
-		memo = sp.Memo(self.recv_proto, self.recv_asset, recv_output.addr)
+		memo = sp.Memo(
+			self.recv_proto,
+			self.recv_asset,
+			recv_output.addr)
 
 		# this goes into the transaction file:
 		self.swap_recv_addr_mmid = recv_output.mmid

+ 19 - 13
test/modtest_d/swap.py

@@ -5,11 +5,13 @@ test.modtest_d.swap: swap unit tests for the MMGen suite
 """
 
 from mmgen.color import cyan
+from mmgen.cfg import Config
+from mmgen.amt import UniAmt
+from mmgen.swap.proto.thorchain import SwapAsset, Memo
+from mmgen.protocol import init_proto
 
 from ..include.common import cfg, vmsg, make_burn_addr
 
-from mmgen.swap.proto.thorchain import SwapAsset
-
 class unit_tests:
 
 	def asset(self, name, ut, desc='SwapAsset class'):
@@ -30,9 +32,7 @@ class unit_tests:
 		return True
 
 	def memo(self, name, ut, desc='Swap transaction memo'):
-		from mmgen.protocol import init_proto
-		from mmgen.amt import UniAmt
-		from mmgen.swap.proto.thorchain import Memo
+
 		for coin, addrtype, asset_name, token in (
 			('ltc', 'bech32',     'LTC',      None),
 			('bch', 'compressed', 'BCH',      None),
@@ -45,19 +45,25 @@ class unit_tests:
 
 			vmsg(f'\nTesting asset {cyan(asset_name)}:')
 
-			for limit, limit_chk in (
-				('123.4567',   12340000000),
-				('1.234567',   123400000),
-				('0.01234567', 1234000),
-				('0.00012345', 12345),
-				(None, 0),
+			for limit, limit_chk, suf in (
+				('123.4567',   12340000000, '1234e7/3/0'),
+				('1.234567',   123400000,   '1234e5/3/0'),
+				('0.01234567', 1234000,     '1234e3/3/0'),
+				('0.00012345', 12345,       '12345/3/0'),
+				(None,         0,           '0/3/0'),
 			):
 				vmsg('\nTesting memo initialization:')
-				m = Memo(proto, asset, addr, trade_limit=UniAmt(limit) if limit else None)
+				m = Memo(
+					proto,
+					asset,
+					addr,
+					trade_limit = None if limit is None else UniAmt(limit))
 				vmsg(f'str(memo):  {m}')
 				vmsg(f'repr(memo): {m!r}')
 				vmsg(f'limit:      {limit}')
 
+				assert str(m).endswith(':' + suf), f'{m} doesn’t end with {suf}'
+
 				p = Memo.parse(m)
 				limit_dec = UniAmt(p.trade_limit, from_unit='satoshi')
 				vmsg(f'limit_dec:  {limit_dec.hl()}')
@@ -110,7 +116,7 @@ class unit_tests:
 				proto = init_proto(cfg, coin, need_amt=True)
 				addr = make_burn_addr(proto, 'C')
 				asset = SwapAsset(coin, 'send')
-				Memo(proto, asset, addr)
+				Memo(proto, asset, addr, trade_limit=None)
 
 			def bad11():
 				SwapAsset('XYZ', 'send')