chainlib-eth

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

commit 8edcb2364057b9dadaa9928dc69560b2a10d268d
parent 16e5876af31ae2d4aa04df8eec64bb370d8ee90e
Author: lash <dev@holbrook.no>
Date:   Thu,  3 Nov 2022 19:31:12 +0000

Add flag to set value as total including fee

Diffstat:
MCHANGELOG | 5+++++
Mchainlib/eth/cli/arg.py | 12++++++++++--
Mchainlib/eth/cli/config.py | 4++--
Mchainlib/eth/runnable/encode.py | 3++-
Mchainlib/eth/runnable/gas.py | 4++--
Mchainlib/eth/settings.py | 40+++++++++++++++++++++++++++++++++++++++-
Mrequirements.txt | 2+-
7 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,8 @@ +- 0.3.4 + * option for net amount in eth-gas (deduct fee from desired spend) + * implement height for eth-balance + * data input from file in gas command + * get address from keyfile in balance call - 0.3.3 * Update flags bit value cli command to use new cli flags generator - 0.3.2 diff --git a/chainlib/eth/cli/arg.py b/chainlib/eth/cli/arg.py @@ -1,14 +1,22 @@ from chainlib.cli.arg import ( ArgumentParser, Arg as BaseArg, - ArgFlag, + ArgFlag as BaseArgFlag, process_args, stdin_arg, ) +class ArgFlag(BaseArgFlag): + + def __init__(self): + super(ArgFlag, self).__init__() + + self.add('create') + + class Arg(BaseArg): def __init__(self, flags): super(Arg, self).__init__(flags) - self.add('z', 'wallet', typ=bool, help='Send to null address (contract creation)') + self.add_long('null', 'create', typ=bool, help='Send to null-address (contract creation). Same as -a null.') diff --git a/chainlib/eth/cli/config.py b/chainlib/eth/cli/config.py @@ -31,8 +31,8 @@ def process_config(config, arg, args, flags, pos_arg_name=None): ]: raise ValueError('unknown rpc dialect {}'.format(config.get('RPC_DIALECT'))) - if arg.match('wallet', flags): - config.add(getattr(args, 'z'), '_Z') + if arg.match('create', flags): + config.add(getattr(args, 'null'), '_NULL') return config diff --git a/chainlib/eth/runnable/encode.py b/chainlib/eth/runnable/encode.py @@ -64,7 +64,8 @@ def process_config_local(config, arg, args, flags): arg_flags = ArgFlag() arg = Arg(arg_flags) -flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.FEE | arg_flags.FMT_HUMAN | arg_flags.FMT_WIRE | arg_flags.FMT_RPC +flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.FEE | arg_flags.FMT_HUMAN | arg_flags.FMT_WIRE | arg_flags.FMT_RPC +flags |= arg_flags.CREATE argparser = chainlib.eth.cli.ArgumentParser() argparser = process_args(argparser, arg, flags) diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -82,7 +82,7 @@ def process_config_local(config, arg, args, flags): arg_flags = ArgFlag() arg = Arg(arg_flags) -flags = arg_flags.STD_WRITE | arg_flags.WALLET +flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.CREATE | arg_flags.VALUE argparser = chainlib.eth.cli.ArgumentParser() argparser = process_args(argparser, arg, flags) @@ -117,7 +117,7 @@ def main(): g = Gas( settings.get('CHAIN_SPEC'), signer=settings.get('SIGNER'), - gas_oracle=settings.get('GAS_ORACLE'), + gas_oracle=settings.get('FEE_ORACLE'), nonce_oracle=settings.get('NONCE_ORACLE'), ) diff --git a/chainlib/eth/settings.py b/chainlib/eth/settings.py @@ -1,3 +1,6 @@ +# standard imports +import logging + # external imports from chainlib.settings import process_settings as base_process_settings from chainlib.error import SignerMissingException @@ -12,6 +15,8 @@ import chainlib.eth.cli from chainlib.eth.address import to_checksum_address from chainlib.eth.constant import ZERO_ADDRESS +logg = logging.getLogger(__name__) + def process_settings_rpc(settings, config): rpc = chainlib.eth.cli.Rpc(settings.get('WALLET')) @@ -23,6 +28,7 @@ def process_settings_rpc(settings, config): gas_oracle = rpc.get_gas_oracle() settings.set('GAS_ORACLE', gas_oracle) + settings.set('FEE_ORACLE', gas_oracle) try: settings.set('SIGNER', rpc.get_signer()) @@ -79,6 +85,25 @@ def __try_zero_address(config, address): return add_0x(recipient) +def __calculate_net_amount(settings, config): + price = settings.get('FEE_PRICE') + if price == None: + fee_oracle = settings.get('FEE_ORACLE') + # TODO: pass on code and input + r = fee_oracle.get_fee() + price = r[0] + + value = settings.get('VALUE') + if config.true('_TOTAL'): + delta = config.get('_FEE_LIMIT') * price + value -= delta + if value < 0: + raise ValueError('Resulting value is negative') + logg.info('total switch set, adjusting sent amount {} by {} to {}'.format(settings.get('VALUE'), delta, value)) + + return value + + def process_settings_wallet(settings, config): wallet = chainlib.eth.cli.Wallet() wallet.from_config(config) @@ -86,7 +111,7 @@ def process_settings_wallet(settings, config): settings.set('WALLET', wallet) try: - if config.get('_Z'): + if config.get('_NULL'): settings.set('RECIPIENT', None) return settings except KeyError: @@ -100,6 +125,9 @@ def process_settings_wallet(settings, config): if recipient_in == None: return settings + elif recipient_in == 'null': + settings.set('RECIPIENT', None) + return settings if wallet.get_signer_address() == None and recipient_in != None: recipient_in = wallet.from_address(recipient_in) @@ -108,6 +136,7 @@ def process_settings_wallet(settings, config): recipient = __try_zero_address(config, recipient_in) settings.set('RECIPIENT', recipient) + return settings @@ -147,6 +176,14 @@ def process_settings_data(settings, config): return settings +def process_settings_value(settings, config): + updated_value = __calculate_net_amount(settings, config) + settings.set('VALUE', updated_value) + + + return settings + + def process_settings_hash(settings, config): hshs = None try: @@ -178,5 +215,6 @@ def process_settings(settings, config): settings = process_settings_blockspec(settings, config) settings = process_settings_data(settings, config) settings = process_settings_hash(settings, config) + settings = process_settings_value(settings, config) settings = process_settings_contract(settings, config) return settings diff --git a/requirements.txt b/requirements.txt @@ -3,4 +3,4 @@ pysha3==1.0.2 hexathon~=0.1.7 websocket-client==0.57.0 potaahto~=0.1.1 -chainlib~=0.3.2 +chainlib~=0.3.3