Add settings module, implement for balance tool

This commit is contained in:
lash 2022-05-12 13:49:42 +00:00
parent 72f3b8d346
commit 85e5359e7c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 62 additions and 20 deletions

View File

@ -15,6 +15,3 @@ class Wallet(BaseWallet):
"""
def __init__(self, checksummer=AddressChecksum):
super(Wallet, self).__init__(EIP155Signer, checksummer=checksummer, keystore=DictKeystore())

View File

@ -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)

42
chainlib/eth/settings.py Normal file
View File

@ -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