From 265be8593c71ad70e1cc4c8569376748e04db726 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 30 Dec 2024 11:31:32 +0000 Subject: [PATCH] CoinProtocol.Base: make Lockable, tokensym init cleanups --- mmgen/main_addrimport.py | 2 -- mmgen/proto/eth/tw/ctl.py | 4 ++-- mmgen/proto/eth/tw/unspent.py | 4 ---- mmgen/protocol.py | 8 +++++--- mmgen/tool/common.py | 2 -- mmgen/tw/ctl.py | 3 ++- mmgen/tx/file.py | 7 +------ 7 files changed, 10 insertions(+), 20 deletions(-) diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index cc5fa76e..ab887361 100755 --- a/mmgen/main_addrimport.py +++ b/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, diff --git a/mmgen/proto/eth/tw/ctl.py b/mmgen/proto/eth/tw/ctl.py index f2b2781f..3dee4097 100755 --- a/mmgen/proto/eth/tw/ctl.py +++ b/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): diff --git a/mmgen/proto/eth/tw/unspent.py b/mmgen/proto/eth/tw/unspent.py index 1a817a4e..b19e5010 100755 --- a/mmgen/proto/eth/tw/unspent.py +++ b/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: diff --git a/mmgen/protocol.py b/mmgen/protocol.py index e9bfee5e..07fc731b 100755 --- a/mmgen/protocol.py +++ b/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 diff --git a/mmgen/tool/common.py b/mmgen/tool/common.py index 9ac5b38c..7cc1dda6 100755 --- a/mmgen/tool/common.py +++ b/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 diff --git a/mmgen/tw/ctl.py b/mmgen/tw/ctl.py index f244ddf2..3624d879 100755 --- a/mmgen/tw/ctl.py +++ b/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, diff --git a/mmgen/tx/file.py b/mmgen/tx/file.py index 0eb525a1..132d96fe 100755 --- a/mmgen/tx/file.py +++ b/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