chainlib-eth

Ethereum implementation of the chainlib interface
Log | Files | Refs | README | LICENSE

commit 85e5359e7ce3a167e400bb0300c809df0f97dfe1
parent 72f3b8d34657a7f0147da2f96020535d2971b58e
Author: lash <dev@holbrook.no>
Date:   Thu, 12 May 2022 13:49:42 +0000

Add settings module, implement for balance tool

Diffstat:
Mchainlib/eth/cli/wallet.py | 3---
Mchainlib/eth/runnable/balance.py | 37++++++++++++++++++++-----------------
Achainlib/eth/settings.py | 42++++++++++++++++++++++++++++++++++++++++++
3 files changed, 62 insertions(+), 20 deletions(-)

diff --git 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 @@ -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) - -rpc = chainlib.eth.cli.Rpc() -conn = rpc.connect_by_config(config) +settings = ChainSettings() +settings = process_settings(settings, 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 @@ -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