Browse Source

CoinProtocol.Base: make Lockable, tokensym init cleanups

The MMGen Project 2 months ago
parent
commit
265be8593c

+ 0 - 2
mmgen/main_addrimport.py

@@ -137,8 +137,6 @@ def check_opts(twctl):
 
 async def main():
 	from .tw.ctl import TwCtl
-	if cfg.token_addr:
-		proto.tokensym = 'foo' # hack to trigger 'Token' in proto.base_proto_subclass()
 
 	twctl = await TwCtl(
 		cfg        = cfg,

+ 2 - 2
mmgen/proto/eth/tw/ctl.py

@@ -201,8 +201,8 @@ class EthereumTokenTwCtl(EthereumTwCtl):
 
 		self.decimals = self.get_param('decimals')
 		self.symbol   = self.get_param('symbol')
-
-		proto.tokensym = self.symbol
+		if mode == 'i' and not proto.tokensym:
+			proto.tokensym = self.symbol
 
 	@property
 	def data_root(self):

+ 0 - 4
mmgen/proto/eth/tw/unspent.py

@@ -115,10 +115,6 @@ class EthereumTokenTwUnspentOutputs(EthereumTwUnspentOutputs):
 
 	has_amt2 = True
 
-	async def __init__(self, proto, *args, **kwargs):
-		await super().__init__(proto, *args, **kwargs)
-		self.proto.tokensym = self.twctl.symbol
-
 	async def get_data(self, *args, **kwargs):
 		await super().get_data(*args, **kwargs)
 		for e in self.data:

+ 5 - 3
mmgen/protocol.py

@@ -23,6 +23,7 @@ protocol: Coin protocol base classes and initializer
 from collections import namedtuple
 
 from .cfg import gc
+from .base_obj import Lockable
 from .objmethods import MMGenObject
 
 decoded_wif = namedtuple('decoded_wif', ['sec', 'pubkey_type', 'compressed'])
@@ -48,7 +49,7 @@ class CoinProtocol(MMGenObject):
 		'xmr': proto_info('Monero',          4)
 	}
 
-	class Base(MMGenObject):
+	class Base(Lockable):
 		base_proto = None
 		base_proto_coin  = None
 		base_coin  = None
@@ -56,6 +57,7 @@ class CoinProtocol(MMGenObject):
 		chain_names = None
 		networks   = ('mainnet', 'testnet', 'regtest')
 		decimal_prec = 28
+		_set_ok = ('tokensym',)
 
 		def __init__(self, cfg, coin, name, network, tokensym=None, need_amt=False):
 			self.cfg        = cfg
@@ -173,7 +175,7 @@ class CoinProtocol(MMGenObject):
 		def viewkey(self, viewkey_str):
 			raise NotImplementedError(f'{self.name} protocol does not support view keys')
 
-		def base_proto_subclass(self, cls, modname, sub_clsname=None):
+		def base_proto_subclass(self, cls, modname, sub_clsname=None, is_token=False):
 			"""
 			magic module loading and class selection
 			"""
@@ -181,7 +183,7 @@ class CoinProtocol(MMGenObject):
 
 			clsname = (
 				self.mod_clsname
-				+ ('Token' if self.tokensym else '')
+				+ ('Token' if self.tokensym or is_token else '')
 				+ cls.__name__)
 
 			import importlib

+ 0 - 2
mmgen/tool/common.py

@@ -38,8 +38,6 @@ class tool_cmd_base(MMGenObject):
 		if self.need_proto:
 			from ..protocol import init_proto_from_cfg
 			self.proto = proto or cfg._proto or init_proto_from_cfg(cfg, need_amt=True)
-			if cfg.token:
-				self.proto.tokensym = cfg.token.upper()
 
 		if self.need_addrtype:
 			from ..addr import MMGenAddrType

+ 2 - 1
mmgen/tw/ctl.py

@@ -56,7 +56,8 @@ class TwCtl(MMGenObject, metaclass=AsyncInit):
 	tw_fn = 'tracking-wallet.json'
 
 	def __new__(cls, cfg, proto, *args, **kwargs):
-		return MMGenObject.__new__(proto.base_proto_subclass(cls, 'tw.ctl'))
+		return MMGenObject.__new__(
+			proto.base_proto_subclass(cls, 'tw.ctl', is_token=kwargs.get('token_addr')))
 
 	async def __init__(
 			self,

+ 1 - 6
mmgen/tx/file.py

@@ -35,12 +35,7 @@ def get_proto_from_coin_id(tx, coin_id, chain):
 	from ..protocol import CoinProtocol, init_proto
 	network = CoinProtocol.Base.chain_name_to_network(tx.cfg, coin, chain)
 
-	proto = init_proto(tx.cfg, coin, network=network, need_amt=True)
-
-	if tokensym:
-		proto.tokensym = tokensym
-
-	return proto
+	return init_proto(tx.cfg, coin, network=network, need_amt=True, tokensym=tokensym)
 
 def eval_io_data(tx, data, desc):
 	if not (desc == 'outputs' and tx.proto.base_coin == 'ETH'): # ETH txs can have no outputs