2021-06-28 07:48:36 +02:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
# standard imports
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import enum
|
|
|
|
import select
|
|
|
|
|
|
|
|
# external imports
|
2021-08-21 09:27:40 +02:00
|
|
|
from potaahto.symbols import snake_and_camel
|
2021-06-28 07:48:36 +02:00
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
|
|
|
import sha3
|
|
|
|
from chainlib.jsonrpc import (
|
|
|
|
JSONRPCRequest,
|
|
|
|
jsonrpc_result,
|
|
|
|
IntSequenceGenerator,
|
|
|
|
)
|
2021-08-21 09:27:40 +02:00
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
from chainlib.status import Status
|
|
|
|
|
|
|
|
# local imports
|
2021-06-28 07:48:36 +02:00
|
|
|
from chainlib.eth.connection import EthHTTPConnection
|
|
|
|
from chainlib.eth.tx import (
|
|
|
|
Tx,
|
|
|
|
pack,
|
|
|
|
)
|
2021-08-21 09:27:40 +02:00
|
|
|
from chainlib.eth.address import (
|
|
|
|
to_checksum_address,
|
|
|
|
is_checksum_address,
|
|
|
|
)
|
|
|
|
from chainlib.eth.block import (
|
|
|
|
Block,
|
|
|
|
block_by_hash,
|
|
|
|
)
|
2021-06-28 07:48:36 +02:00
|
|
|
from chainlib.eth.runnable.util import decode_for_puny_humans
|
2021-08-21 09:27:40 +02:00
|
|
|
from chainlib.eth.jsonrpc import to_blockheight_param
|
|
|
|
import chainlib.eth.cli
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s')
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
config_dir = os.path.join(script_dir, '..', 'data', 'config')
|
2021-06-28 07:48:36 +02:00
|
|
|
|
2022-02-24 11:36:15 +01:00
|
|
|
arg_flags = chainlib.eth.cli.argflag_std_base_read
|
|
|
|
arg_flags = chainlib.eth.cli.argflag_reset(arg_flags, chainlib.eth.cli.Flag.CHAIN_SPEC)
|
2021-08-21 09:27:40 +02:00
|
|
|
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
|
|
|
argparser.add_positional('item', type=str, help='Address or transaction to retrieve data for')
|
|
|
|
args = argparser.parse_args()
|
|
|
|
config = chainlib.eth.cli.Config.from_args(args, arg_flags, default_config_dir=config_dir)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
rpc = chainlib.eth.cli.Rpc()
|
|
|
|
conn = rpc.connect_by_config(config)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
item = add_0x(args.item)
|
|
|
|
|
|
|
|
|
|
|
|
def get_transaction(conn, tx_hash, id_generator):
|
2021-10-30 16:49:36 +02:00
|
|
|
tx_hash = add_0x(tx_hash)
|
2021-06-28 07:48:36 +02:00
|
|
|
j = JSONRPCRequest(id_generator=id_generator)
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_getTransactionByHash'
|
|
|
|
o['params'].append(tx_hash)
|
|
|
|
o = j.finalize(o)
|
|
|
|
tx_src = conn.do(o)
|
|
|
|
if tx_src == None:
|
|
|
|
logg.error('Transaction {} not found'.format(tx_hash))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
if config.true('_RAW'):
|
2021-06-28 07:48:36 +02:00
|
|
|
tx_src = Tx.src_normalize(tx_src)
|
|
|
|
return pack(tx_src, chain_spec).hex()
|
|
|
|
|
|
|
|
tx = None
|
|
|
|
status = -1
|
|
|
|
rcpt = None
|
|
|
|
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_getTransactionReceipt'
|
|
|
|
o['params'].append(tx_hash)
|
|
|
|
o = j.finalize(o)
|
|
|
|
rcpt = conn.do(o)
|
|
|
|
#status = int(strip_0x(rcpt['status']), 16)
|
|
|
|
|
|
|
|
if tx == None:
|
|
|
|
tx = Tx(tx_src)
|
|
|
|
if rcpt != None:
|
|
|
|
tx.apply_receipt(rcpt)
|
2021-08-21 09:27:40 +02:00
|
|
|
rcpt = snake_and_camel(rcpt)
|
|
|
|
o = block_by_hash(rcpt['block_hash'])
|
|
|
|
r = conn.do(o)
|
|
|
|
block = Block(r)
|
|
|
|
tx.apply_block(block)
|
2021-06-28 07:48:36 +02:00
|
|
|
tx.generate_wire(chain_spec)
|
|
|
|
return tx
|
2021-08-21 09:27:40 +02:00
|
|
|
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
def get_address(conn, address, id_generator, height):
|
2021-10-30 16:49:36 +02:00
|
|
|
address = add_0x(address)
|
2021-06-28 07:48:36 +02:00
|
|
|
j = JSONRPCRequest(id_generator=id_generator)
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_getCode'
|
|
|
|
o['params'].append(address)
|
2021-08-21 09:27:40 +02:00
|
|
|
height = to_blockheight_param(height)
|
|
|
|
o['params'].append(height)
|
2021-06-28 07:48:36 +02:00
|
|
|
o = j.finalize(o)
|
|
|
|
code = conn.do(o)
|
|
|
|
|
|
|
|
content = strip_0x(code, allow_empty=True)
|
|
|
|
if len(content) == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-08-21 09:27:40 +02:00
|
|
|
address = item
|
2021-06-28 07:48:36 +02:00
|
|
|
r = None
|
2021-08-21 09:27:40 +02:00
|
|
|
if len(address) > 42:
|
|
|
|
r = get_transaction(conn, address, rpc.id_generator)
|
|
|
|
if not config.true('_RAW'):
|
|
|
|
r = r.to_human()
|
|
|
|
else:
|
|
|
|
if config.get('_UNSAFE'):
|
|
|
|
address = to_checksum_address(address)
|
|
|
|
elif not is_checksum_address(address):
|
|
|
|
raise ValueError('invalid checksum address: {}'.format(address))
|
|
|
|
r = get_address(conn, address, rpc.id_generator, config.get('_HEIGHT'))
|
2021-09-01 09:38:09 +02:00
|
|
|
if r != None:
|
|
|
|
print(r)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|