chainlib-eth/chainlib/eth/runnable/raw.py

164 lines
5.0 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import io
import sys
import os
import json
import argparse
import logging
import urllib
# external imports
from chainlib.settings import ChainSettings
2021-10-18 14:23:54 +02:00
from funga.eth.signer import EIP155Signer
from funga.eth.keystore.dict import DictKeystore
from hexathon import (
add_0x,
strip_0x,
)
from chainlib.error import SignerMissingException
from chainlib.chain import ChainSpec
# local imports
from chainlib.eth.address import to_checksum
from chainlib.eth.connection import EthHTTPConnection
from chainlib.jsonrpc import (
JSONRPCRequest,
IntSequenceGenerator,
)
from chainlib.eth.nonce import (
RPCNonceOracle,
OverrideNonceOracle,
)
from chainlib.eth.gas import (
RPCGasOracle,
OverrideGasOracle,
)
from chainlib.eth.tx import (
TxFactory,
raw,
)
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
2022-05-12 14:58:24 +02:00
import chainlib.eth.cli
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
2022-05-12 14:58:24 +02:00
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')
def process_config_local(config, arg, args, flags):
config.add(args.deploy, '_DEPLOY', False)
config.add(args.mode, '_MODE', False)
config.add(args.data, '_DATA', False)
return config
2022-05-12 14:58:24 +02:00
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.EXEC
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
argparser.add_argument('--deploy', action='store_true', help='Deploy data as contract')
2022-02-25 12:07:18 +01:00
argparser.add_argument('--mode', choices=['tx', 'call'], type=str, help='Mode of operation')
2022-05-12 14:58:24 +02:00
argparser.add_argument('data', type=str, help='Transaction data')
2021-08-21 09:27:40 +02:00
args = argparser.parse_args()
2022-05-12 14:58:24 +02:00
logg = process_log(args, logg)
config = Config()
config = process_config(config, arg, args, flags)
config = process_config_local(config, arg, args, flags)
2022-05-12 14:58:24 +02:00
logg.debug('config loaded:\n{}'.format(config))
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
2021-08-21 09:27:40 +02:00
def main():
if config.get('_EXEC_ADDRESS') != None or config.true('_DEPLOY'):
if not args.u and exec_address != exec_address:
raise ValueError('invalid checksum address')
if settings.get('SENDER_ADDRESS'):
j = JSONRPCRequest(id_generator=settings.get('RPC_ID_GENERATOR'))
o = j.template()
o['method'] = 'eth_call'
o['params'].append({
'to': settings.get('EXEC'),
'from': settings.get('SENDER_ADDRESS'),
'value': '0x00',
'gas': add_0x(int.to_bytes(8000000, 8, byteorder='big').hex()), # TODO: better get of network gas limit
'gasPrice': '0x01',
'data': add_0x(config.get('_DATA')),
})
height = to_blockheight_param(config.get('_HEIGHT'))
o['params'].append(height)
o = j.finalize(o)
r = settings.get('CONN').do(o)
try:
print(strip_0x(r))
except ValueError:
sys.stderr.write('query returned an empty value ({})\n'.format(r))
sys.exit(1)
else:
if settings.get('CHAIN_SPEC') == None:
2021-09-01 09:38:09 +02:00
raise ValueError('chain spec must be specified')
g = TxFactory(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('GAS_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
tx = g.template(
settings.get('SENDER_ADDRESS'),
settings.get('EXEC'),
use_nonce=True,
)
if config.get('_DATA') != None:
tx = g.set_code(tx, add_0x(config.get('_DATA')))
2021-09-01 09:38:09 +02:00
(tx_hash_hex, o) = g.finalize(tx, id_generator=rpc.id_generator)
if send:
r = settings.get('CONN').do(o)
2021-09-01 09:38:09 +02:00
print(r)
else:
if config.get('_RAW'):
o = strip_0x(o)
2021-09-01 09:38:09 +02:00
print(o)
else:
o = raw(config.get('_DATA'), id_generator=settings.get('RPC_ID_GENERATOR'))
if settings.get('RPC_SEND'):
r = settings.get('CONN').do(o)
2022-05-12 14:58:24 +02:00
if config.true('_WAIT'):
r = settings.get('CONN').wait(tx_hash_hex)
if r['status'] == 0:
logg.critical('VM revert for {}. Wish I could tell you more'.format(tx_hash_hex))
sys.exit(1)
print(r)
else:
print(o)
if __name__ == '__main__':
main()