From 9356ef53b22cacc6bac26675f270c8202a901ad4 Mon Sep 17 00:00:00 2001 From: nolash Date: Sat, 17 Apr 2021 13:01:26 +0200 Subject: [PATCH] Add missing jsonrpc module, add address code retrieve to get cli util --- .gitignore | 6 +++++ Makefile | 11 +++++++++ chainlib/eth/jsonrpc.py | 16 ++++++++++++++ chainlib/eth/runnable/get.py | 43 ++++++++++++++++++++++++++---------- setup.cfg | 2 +- 5 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 chainlib/eth/jsonrpc.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c556a5f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +__pycache__ +gmon.out +*.pyc +dist/ +build/ +*.egg-info diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40833ea --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +PACKAGE=chainlib + +build: + python setup.py bdist_wheel + +.PHONY clean: + rm -rf build + rm -rf dist + rm -rf $(PACKAGE).egg-info + +.PHONY dist: clean build diff --git a/chainlib/eth/jsonrpc.py b/chainlib/eth/jsonrpc.py new file mode 100644 index 0000000..03db2c4 --- /dev/null +++ b/chainlib/eth/jsonrpc.py @@ -0,0 +1,16 @@ +# proposed custom errors +# source: https://eth.wiki/json-rpc/json-rpc-error-codes-improvement-proposal + +#1 Unauthorized Should be used when some action is not authorized, e.g. sending from a locked account. +#2 Action not allowed Should be used when some action is not allowed, e.g. preventing an action, while another depending action is processing on, like sending again when a confirmation popup is shown to the user (?). +#3 Execution error Will contain a subset of custom errors in the data field. See below. + +#100 X doesn’t exist Should be used when something which should be there is not found. (Doesn’t apply to eth_getTransactionBy_ and eth_getBlock_. They return a success with value null) +#101 Requires ether Should be used for actions which require somethin else, e.g. gas or a value. +#102 Gas too low Should be used when a to low value of gas was given. +#103 Gas limit exceeded Should be used when a limit is exceeded, e.g. for the gas limit in a block. +#104 Rejected Should be used when an action was rejected, e.g. because of its content (too long contract code, containing wrong characters ?, should differ from -32602 - Invalid params). +#105 Ether too low Should be used when a to low value of Ether was given. + +#106 Timeout Should be used when an action timedout. +#107 Conflict Should be used when an action conflicts with another (ongoing?) action. diff --git a/chainlib/eth/runnable/get.py b/chainlib/eth/runnable/get.py index 090f6ab..8579a48 100644 --- a/chainlib/eth/runnable/get.py +++ b/chainlib/eth/runnable/get.py @@ -17,11 +17,10 @@ import argparse import logging import enum -# third-party imports +# external imports from hexathon import ( add_0x, strip_0x, - even, ) import sha3 @@ -33,8 +32,10 @@ from chainlib.jsonrpc import ( ) from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.tx import Tx +from chainlib.eth.address import to_checksum_address from chainlib.eth.block import Block from chainlib.chain import ChainSpec +from chainlib.status import Status logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -50,7 +51,7 @@ argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Au argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=default_abi_dir, help='Directory containing bytecode and abi (default {})'.format(default_abi_dir)) argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') -argparser.add_argument('tx_hash', type=str, help='Transaction hash') +argparser.add_argument('item', type=str, help='Item to get information for (address og transaction)') args = argparser.parse_args() if args.vv: @@ -60,16 +61,11 @@ elif args.v: conn = EthHTTPConnection(args.p) -tx_hash = add_0x(args.tx_hash) +#tx_hash = add_0x(args.tx_hash) +item = add_0x(args.item) -class Status(enum.Enum): - UNCONFIRMED = -1 - REVERTED = 0 - SUCCESS = 1 - - -def main(): +def get_transaction(conn, tx_hash): o = jsonrpc_template() o['method'] = 'eth_getTransactionByHash' o['params'].append(tx_hash) @@ -92,7 +88,30 @@ def main(): tx = Tx(tx_src) if rcpt != None: tx.apply_receipt(rcpt) - print(tx) + return tx + + +def get_address(conn, address): + o = jsonrpc_template() + o['method'] = 'eth_getCode' + o['params'].append(address) + o['params'].append('latest') + code = conn.do(o) + + content = strip_0x(code, allow_empty=True) + if len(content) == 0: + return None + + return content + + +def main(): + r = None + if len(item) > 42: + r = get_transaction(conn, item) + elif args.u or to_checksum_address(item): + r = get_address(conn, item) + print(r) if __name__ == '__main__': diff --git a/setup.cfg b/setup.cfg index 49477e3..cc166e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib -version = 0.0.2a15 +version = 0.0.2a16 description = Generic blockchain access library and tooling author = Louis Holbrook author_email = dev@holbrook.no