From 85e5359e7ce3a167e400bb0300c809df0f97dfe1 Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 12 May 2022 13:49:42 +0000 Subject: [PATCH] Add settings module, implement for balance tool --- chainlib/eth/cli/wallet.py | 3 --- chainlib/eth/runnable/balance.py | 37 +++++++++++++++------------- chainlib/eth/settings.py | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 chainlib/eth/settings.py diff --git a/chainlib/eth/cli/wallet.py b/chainlib/eth/cli/wallet.py index 1f0cc6a..0318338 100644 --- a/chainlib/eth/cli/wallet.py +++ b/chainlib/eth/cli/wallet.py @@ -15,6 +15,3 @@ class Wallet(BaseWallet): """ def __init__(self, checksummer=AddressChecksum): super(Wallet, self).__init__(EIP155Signer, checksummer=checksummer, keystore=DictKeystore()) - - - diff --git a/chainlib/eth/runnable/balance.py b/chainlib/eth/runnable/balance.py index e6b0965..ba6f949 100644 --- a/chainlib/eth/runnable/balance.py +++ b/chainlib/eth/runnable/balance.py @@ -10,6 +10,13 @@ from hexathon import ( strip_0x, even, ) +from chainlib.settings import ChainSettings +from chainlib.chain import ChainSpec +from funga.eth.signer import EIP155Signer +from chainlib.jsonrpc import ( + jsonrpc_result, + IntSequenceGenerator, + ) # local imports import chainlib.eth.cli @@ -24,22 +31,24 @@ from chainlib.eth.cli.config import ( ) from chainlib.eth.cli.log import process_log from chainlib.eth.address import AddressChecksum -from chainlib.jsonrpc import ( - jsonrpc_result, - IntSequenceGenerator, - ) from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.gas import ( OverrideGasOracle, balance, ) -from chainlib.chain import ChainSpec -from funga.eth.signer import EIP155Signer +from chainlib.eth.settings import process_settings + logg = logging.getLogger() script_dir = os.path.dirname(os.path.realpath(__file__)) + +def process_config_local(config, arg, args, flags): + config.add(args.address, '_RECIPIENT', False) + return config + + arg_flags = ArgFlag() arg = Arg(arg_flags) flags = arg_flags.STD_READ @@ -55,25 +64,19 @@ logg.debug('flags {} {} {}'.format(flags, arg_flags.SEQ, flags & arg_flags.SEQ)) config = Config() config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) -wallet = chainlib.eth.cli.Wallet() -wallet.from_config(config) -holder_address = args.address -if wallet.get_signer_address() == None and holder_address != None: - holder_address = wallet.from_address(holder_address) +settings = ChainSettings() +settings = process_settings(settings, config) -rpc = chainlib.eth.cli.Rpc() -conn = rpc.connect_by_config(config) - -chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) def main(): r = None decimals = 18 - o = balance(holder_address, id_generator=rpc.id_generator) - r = conn.do(o) + o = balance(settings.get('RECIPIENT'), id_generator=settings.get('RPC_ID_GENERATOR')) + r = settings.get('CONN').do(o) hx = strip_0x(r) balance_value = int(hx, 16) diff --git a/chainlib/eth/settings.py b/chainlib/eth/settings.py new file mode 100644 index 0000000..caeb591 --- /dev/null +++ b/chainlib/eth/settings.py @@ -0,0 +1,42 @@ +# external imports +from chainlib.settings import process_settings as base_process_settings + +# local imports +import chainlib.eth.cli + + +def process_settings_rpc(settings, config): + rpc = chainlib.eth.cli.Rpc() + conn = rpc.connect_by_config(config) + + try: + settings.set('SIGNER', rpc.get_signer()) + except AttributeError: + pass + + settings.set('CONN', conn) + settings.set('RPC_ID_GENERATOR', rpc.id_generator) + return settings + + +def process_settings_wallet(settings, config): + wallet = chainlib.eth.cli.Wallet() + wallet.from_config(config) + + try: + recipient = config.get('_RECIPIENT') + except KeyError: + return settings + + if wallet.get_signer_address() == None and recipient != None: + recipient = wallet.from_address(recipient) + + settings.set('RECIPIENT', recipient) + return settings + + +def process_settings(settings, config): + settings = base_process_settings(settings, config) + settings = process_settings_wallet(settings, config) + settings = process_settings_rpc(settings, config) + return settings