chainlib-eth

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

commit 3dd67ddab5762a790f0243fab243385375b1e286
parent 3a145d60c6f46004a42f17dbf41d594b0d9e4b52
Author: lash <dev@holbrook.no>
Date:   Sun,  6 Nov 2022 10:28:36 +0000

Unify human output, toggle fields in decode, get

Diffstat:
MCHANGELOG | 3+++
Achainlib/eth/cli/decode.py | 18++++++++++++++++++
Mchainlib/eth/runnable/block.py | 1-
Mchainlib/eth/runnable/decode.py | 6++++--
Mchainlib/eth/runnable/encode.py | 1-
Mchainlib/eth/runnable/gas.py | 14++++++++++----
Mchainlib/eth/runnable/raw.py | 1-
Dchainlib/eth/runnable/util.py | 31-------------------------------
Mchainlib/eth/runnable/wait.py | 1-
Mchainlib/eth/tx.py | 11+++++++----
Msetup.cfg | 2+-
11 files changed, 43 insertions(+), 46 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,6 @@ +- 0.4.3 + * Enable raw output for raw cli tool + * Unify human readable transaction rendering - 0.4.2 * Get latest block for block cli command when no argument. - 0.4.1 diff --git a/chainlib/eth/cli/decode.py b/chainlib/eth/cli/decode.py @@ -0,0 +1,18 @@ +# external imports +from hexathon import ( + strip_0x, + add_0x, + ) + +# local imports +from chainlib.eth.tx import unpack +from chainlib.eth.tx import Tx + + +def decode_for_puny_humans(tx_raw, chain_spec, writer, fields=None, skip_keys=False): + tx_raw = strip_0x(tx_raw) + tx_raw_bytes = bytes.fromhex(tx_raw) + tx_src = unpack(tx_raw_bytes, chain_spec) + tx = Tx.from_src(tx_src, chain_spec=chain_spec) + writer.write(tx.to_human(fields=fields, skip_keys=skip_keys)) + writer.write('\n') diff --git a/chainlib/eth/runnable/block.py b/chainlib/eth/runnable/block.py @@ -41,7 +41,6 @@ from chainlib.eth.block import ( block_by_number, block_latest, ) -from chainlib.eth.runnable.util import decode_for_puny_humans from chainlib.eth.jsonrpc import to_blockheight_param import chainlib.eth.cli from chainlib.eth.cli.arg import ( diff --git a/chainlib/eth/runnable/decode.py b/chainlib/eth/runnable/decode.py @@ -19,7 +19,7 @@ from hexathon import ( # local imports import chainlib.eth.cli -from chainlib.eth.runnable.util import decode_for_puny_humans +from chainlib.eth.cli.decode import decode_for_puny_humans from chainlib.eth.cli.arg import ( Arg, ArgFlag, @@ -57,7 +57,7 @@ def process_config_local(config, arg, args, flags): arg_flags = ArgFlag() arg = Arg(arg_flags) -flags = arg_flags.VERBOSE | arg_flags.CHAIN_SPEC | arg_flags.RAW | arg_flags.ENV | arg_flags.SEQ +flags = arg_flags.VERBOSE | arg_flags.CHAIN_SPEC | arg_flags.RAW | arg_flags.ENV | arg_flags.SEQ | arg_flags.TAB argparser = chainlib.eth.cli.ArgumentParser() argparser = process_args(argparser, arg, flags) @@ -81,6 +81,8 @@ def main(): config.get('_TX_DATA'), settings.get('CHAIN_SPEC'), sys.stdout, + fields=config.get('_OUTARG'), + skip_keys=config.true('_RAW'), ) if __name__ == '__main__': diff --git a/chainlib/eth/runnable/encode.py b/chainlib/eth/runnable/encode.py @@ -48,7 +48,6 @@ from chainlib.eth.tx import ( ) from chainlib.error import SignerMissingException from chainlib.chain import ChainSpec -from chainlib.eth.runnable.util import decode_for_puny_humans from chainlib.eth.jsonrpc import to_blockheight_param from chainlib.eth.address import to_checksum_address diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -26,7 +26,7 @@ from chainlib.eth.address import to_checksum_address from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.gas import Gas from chainlib.eth.gas import balance as gas_balance -from chainlib.eth.runnable.util import decode_for_puny_humans +from chainlib.eth.cli.decode import decode_for_puny_humans from chainlib.eth.address import ( is_same_address, is_checksum_address, @@ -83,7 +83,7 @@ def process_config_local(config, arg, args, flags): arg_flags = ArgFlag() arg = Arg(arg_flags) -flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.CREATE | arg_flags.VALUE +flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.CREATE | arg_flags.VALUE | arg_flags.TAB argparser = chainlib.eth.cli.ArgumentParser() argparser = process_args(argparser, arg, flags) @@ -179,8 +179,14 @@ def main(): print(o['params'][0]) else: io_str = io.StringIO() - decode_for_puny_humans(o['params'][0], settings.get('CHAIN_SPEC'), io_str) - print(io_str.getvalue()) + decode_for_puny_humans( + o['params'][0], + settings.get('CHAIN_SPEC'), + io_str, + fields=config.get('_OUTARG'), + skip_keys=config.true('_RAW'), + ) + sys.stdout.write(io_str.getvalue()) diff --git a/chainlib/eth/runnable/raw.py b/chainlib/eth/runnable/raw.py @@ -39,7 +39,6 @@ from chainlib.eth.tx import ( TxFactory, raw, ) -from chainlib.eth.runnable.util import decode_for_puny_humans from chainlib.eth.jsonrpc import to_blockheight_param import chainlib.eth.cli from chainlib.eth.cli.arg import ( diff --git a/chainlib/eth/runnable/util.py b/chainlib/eth/runnable/util.py @@ -1,31 +0,0 @@ -# local imports -from chainlib.eth.tx import unpack -from hexathon import ( - strip_0x, - add_0x, - ) - -def decode_out(tx, writer, skip_keys=[]): - for k in tx.keys(): - if k in skip_keys: - continue - x = None - if k == 'value': - x = '{:.18f} eth'.format(tx[k] / (10**18)) - elif k == 'gasPrice': - x = '{} gwei'.format(int(tx[k] / (10**9))) - elif k == 'value': - k = 'gas-value' - if x != None: - writer.write('{}: {} ({})\n'.format(k, tx[k], x)) - else: - writer.write('{}: {}\n'.format(k, tx[k])) - - -def decode_for_puny_humans(tx_raw, chain_spec, writer, skip_keys=[]): - tx_raw = strip_0x(tx_raw) - tx_raw_bytes = bytes.fromhex(tx_raw) - tx = unpack(tx_raw_bytes, chain_spec) - decode_out(tx, writer, skip_keys=skip_keys) - writer.write('src: {}\n'.format(add_0x(tx_raw))) - diff --git a/chainlib/eth/runnable/wait.py b/chainlib/eth/runnable/wait.py @@ -40,7 +40,6 @@ from chainlib.eth.tx import ( raw, ) from chainlib.eth.error import RevertEthException -from chainlib.eth.runnable.util import decode_for_puny_humans from chainlib.eth.jsonrpc import to_blockheight_param import chainlib.eth.cli from chainlib.eth.cli.arg import ( diff --git a/chainlib/eth/tx.py b/chainlib/eth/tx.py @@ -705,12 +705,16 @@ class Tx(BaseTx, Src): @staticmethod - def from_src(src, block=None, rcpt=None, strict=False): + def from_src(src, block=None, rcpt=None, strict=False, chain_spec=None): """Creates a new Tx object. Alias of constructor. """ - return Tx(src, block=block, rcpt=rcpt, strict=strict) + tx = Tx(src, block=block, rcpt=rcpt, strict=strict) + if chain_spec != None: + tx.generate_wire(chain_spec) + return tx + def __str__(self): @@ -770,9 +774,9 @@ class Tx(BaseTx, Src): outvals.append(self.result.fee_cost) if self.block != None: outkeys += [ + 'block_number', 'block_hash', 'tx_index', - 'src', ] outvals += [ self.block.number, @@ -780,7 +784,6 @@ class Tx(BaseTx, Src): self.result.tx_index, ] - if self.wire != None: outkeys.append('src') outvals.append(self.wire) diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib-eth -version = 0.4.2 +version = 0.4.3 description = Ethereum implementation of the chainlib interface author = Louis Holbrook author_email = dev@holbrook.no