From 3b1839c734ffd7a449e8b285866f97dc594930bc Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 7 Oct 2026 16:40:39 +0000 Subject: [PATCH] rpc: new `get_remote_rpc()` function --- mmgen/main_remote.py | 12 +++++++----- mmgen/main_txsend.py | 2 +- mmgen/rpc/__init__.py | 9 ++++++--- mmgen/swap/util.py | 5 +++++ mmgen/tx/base.py | 12 +++++++----- mmgen/tx/new_swap.py | 2 +- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mmgen/main_remote.py b/mmgen/main_remote.py index bd76d3b8..28295de3 100644 --- a/mmgen/main_remote.py +++ b/mmgen/main_remote.py @@ -54,8 +54,10 @@ def main(): match cfg.coin: case 'RUNE': - from .proto.rune.rpc.remote import THORChainRemoteRPCClient as cls - cmds = get_cmds(cls.mods) + from .rpc import get_remote_rpc + from .protocol import init_proto + rpc = get_remote_rpc(cfg, init_proto(cfg, cfg.coin)) + cmds = get_cmds(rpc.mods) case _: cmds = () @@ -66,13 +68,13 @@ def main(): case ['help', cmd]: if cmd in cmds: from .tool.help import gen_tool_cmd_usage - Msg('\n'.join(gen_tool_cmd_usage(None, cmd, lambda x: cls))) + Msg('\n'.join(gen_tool_cmd_usage(None, cmd, lambda x: rpc))) else: fs = '‘{}’: invalid command\n\nSupported commands:\n{}' die(1, fs.format(cmd, fmt_list(cmds, fmt='col', indent=' ' * 4))) case [cmd, *cmd_args] if cmd in cmds: - args, kwargs = process_args(cmd, cmd_args, cls) - res = getattr(cls(cfg, cfg._proto), cmd)(*args, **kwargs) + args, kwargs = process_args(cmd, cmd_args, rpc) + res = getattr(rpc, cmd)(*args, **kwargs) import json from .ui import colorize_json text = json.dumps(res, indent=2) diff --git a/mmgen/main_txsend.py b/mmgen/main_txsend.py index b6733d7d..a2253c29 100644 --- a/mmgen/main_txsend.py +++ b/mmgen/main_txsend.py @@ -176,7 +176,7 @@ async def process_tx(tx): return 0 if tx.is_swap: - await tx.check_swap_network() + tx.check_swap_network() if not tx.check_swap_expiry(): die(1, 'Swap quote has expired. Please re-create the transaction') diff --git a/mmgen/rpc/__init__.py b/mmgen/rpc/__init__.py index a02c66fe..104ea156 100644 --- a/mmgen/rpc/__init__.py +++ b/mmgen/rpc/__init__.py @@ -15,6 +15,11 @@ import importlib from ..util import die, fmt, fmt_list from ..obj import NonNegativeInt +def get_remote_rpc(cfg, proto): + return getattr(importlib.import_module( + f'mmgen.proto.{proto.base_proto_coin.lower()}.rpc.remote'), + proto.base_proto + 'RemoteRPCClient')(cfg=cfg, proto=proto) + async def rpc_init( cfg, proto = None, @@ -30,9 +35,7 @@ async def rpc_init( die(1, f'rpc_init() not supported for {proto.name} protocol!') if proto.rpc_type == 'remote': - return getattr(importlib.import_module( - f'mmgen.proto.{proto.base_proto_coin.lower()}.rpc.remote'), - proto.base_proto + 'RemoteRPCClient')(cfg=cfg, proto=proto) + return get_remote_rpc(cfg, proto) from ..daemon import CoinDaemon diff --git a/mmgen/swap/util.py b/mmgen/swap/util.py index c3282f85..37d7aea1 100644 --- a/mmgen/swap/util.py +++ b/mmgen/swap/util.py @@ -14,6 +14,11 @@ def get_swap_proto_mod(swap_proto_name): import importlib return importlib.import_module(f'mmgen.swap.proto.{swap_proto_name}') +def get_swap_proto_rpc(cfg, coin): + from ..protocol import init_proto + from ..rpc import get_remote_rpc + return get_remote_rpc(cfg, init_proto(cfg, coin)) + def init_swap_proto(cfg, asset): from ..protocol import init_proto return init_proto( diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index dcb22204..f3b4d375 100644 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -221,6 +221,11 @@ class Base(MMGenObject): keypress_confirm(self.cfg, 'Continue?', default_yes=True, do_exit=True) # swap methods: + @cached_property + def swap_proto_rpc(self): + from ..swap.util import get_swap_proto_rpc + return get_swap_proto_rpc(self.cfg, self.swap_proto_mod.coin) + @cached_property def swap_proto_mod(self): from ..swap.util import get_swap_proto_mod @@ -243,11 +248,8 @@ class Base(MMGenObject): x = '[unknown]' return SwapAsset._ad(x, x, x, x, x) - async def check_swap_network(self): - from ..protocol import init_proto - from ..rpc import rpc_init - proto = init_proto(self.cfg, self.swap_proto_mod.coin) - rpc = await rpc_init(self.cfg, proto) + def check_swap_network(self): + rpc = self.swap_proto_rpc data = rpc.inbound_addrs() for asset in (self.send_asset, self.recv_asset): if asset.name == 'RUNE': diff --git a/mmgen/tx/new_swap.py b/mmgen/tx/new_swap.py index 313e03fe..b8edce55 100644 --- a/mmgen/tx/new_swap.py +++ b/mmgen/tx/new_swap.py @@ -121,7 +121,7 @@ class NewSwap(New): await parse() - await self.check_swap_network() + self.check_swap_network() for asset in (self.send_asset, self.recv_asset): if asset.name not in sa.tested: