chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit df4d62e3e7dcfb2cb398bd69d56f95c6f3338041
parent 113b90e5e3438c9460651af61ffd3d040844b064
Author: nolash <dev@holbrook.no>
Date:   Tue, 13 Apr 2021 07:14:19 +0200

Add raw tx send cli

Diffstat:
Mchainlib/eth/runnable/gas.py | 4++--
Achainlib/eth/runnable/raw.py | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msetup.cfg | 1+
3 files changed, 138 insertions(+), 2 deletions(-)

diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -63,8 +63,8 @@ argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Au argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-s', '--send', dest='s', action='store_true', help='Send to network') -argparser.add_argument('recipient', type=str, help='Ethereum address of recipient') -argparser.add_argument('amount', type=int, help='Amount of tokens to mint and gift') +argparser.add_argument('recipient', type=str, help='ethereum address of recipient') +argparser.add_argument('amount', type=int, help='gas value in wei') args = argparser.parse_args() diff --git a/chainlib/eth/runnable/raw.py b/chainlib/eth/runnable/raw.py @@ -0,0 +1,135 @@ +#!python3 + +"""Gas transfer script + +.. moduleauthor:: Louis Holbrook <dev@holbrook.no> +.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 + +""" + +# 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 crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer +from crypto_dev_signer.keystore.dict import DictKeystore +from hexathon import ( + add_0x, + strip_0x, + ) + +# local imports +from chainlib.eth.address import to_checksum +from chainlib.eth.connection import EthHTTPConnection +from chainlib.jsonrpc import jsonrpc_template +from chainlib.eth.nonce import ( + RPCNonceOracle, + OverrideNonceOracle, + ) +from chainlib.eth.gas import ( + RPCGasOracle, + OverrideGasOracle, + ) +from chainlib.eth.tx import TxFactory +from chainlib.chain import ChainSpec +from chainlib.eth.runnable.util import decode_for_puny_humans + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger() + + +default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545') + +argparser = argparse.ArgumentParser() +argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)') +argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') +argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') +argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') +argparser.add_argument('-y', '--key-file', required=True, dest='y', type=str, help='Ethereum keystore file to use for signing') +argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') +argparser.add_argument('--nonce', type=int, help='override nonce') +argparser.add_argument('--gas-price', dest='gas_price', type=int, help='override gas price') +argparser.add_argument('--gas-limit', dest='gas_limit', type=int, help='override gas limit') +argparser.add_argument('-a', '--recipient', dest='a', type=str, help='recipient address (None for contract creation)') +argparser.add_argument('-value', type=int, help='gas value of transaction in wei') +argparser.add_argument('-v', action='store_true', help='Be verbose') +argparser.add_argument('-vv', action='store_true', help='Be more verbose') +argparser.add_argument('-s', '--send', dest='s', action='store_true', help='Send to network') +argparser.add_argument('data', nargs='?', type=str, help='Transaction data') +args = argparser.parse_args() + + +if args.vv: + logg.setLevel(logging.DEBUG) +elif args.v: + logg.setLevel(logging.INFO) + +block_all = args.ww +block_last = args.w or block_all + +passphrase_env = 'ETH_PASSPHRASE' +if args.env_prefix != None: + passphrase_env = args.env_prefix + '_' + passphrase_env +passphrase = os.environ.get(passphrase_env) +if passphrase == None: + logg.warning('no passphrase given') + passphrase='' + +signer_address = None +keystore = DictKeystore() +if args.y != None: + logg.debug('loading keystore file {}'.format(args.y)) + signer_address = keystore.import_keystore_file(args.y, password=passphrase) + logg.debug('now have key for signer address {}'.format(signer_address)) +signer = EIP155Signer(keystore) + +conn = EthHTTPConnection(args.p) + +nonce_oracle = None +if args.nonce != None: + nonce_oracle = OverrideNonceOracle(signer_address, args.nonce) +else: + nonce_oracle = RPCNonceOracle(signer_address, conn) + +gas_oracle = None +if args.gas_price or args.gas_limit != None: + gas_oracle = OverrideGasOracle(price=args.gas_price, limit=args.gas_limit, conn=conn) +else: + gas_oracle = RPCGasOracle(conn) + + +chain_spec = ChainSpec.from_chain_str(args.i) + +value = args.value + +send = args.s + +g = TxFactory(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) + +def main(): + recipient = None + if args.a != None: + recipient = add_0x(to_checksum(args.a)) + if not args.u and recipient != add_0x(recipient): + raise ValueError('invalid checksum address') + + tx = g.template(signer_address, recipient, use_nonce=True) + if args.data != None: + tx = g.set_code(tx, add_0x(args.data)) + + (tx_hash_hex, o) = g.finalize(tx) + + print(o) + print(tx_hash_hex) + + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg @@ -37,6 +37,7 @@ console_scripts = eth-balance = chainlib.eth.runnable.balance:main eth-checksum = chainlib.eth.runnable.checksum:main eth-gas = chainlib.eth.runnable.gas:main + eth-raw = chainlib.eth.runnable.raw:main eth-transfer = chainlib.eth.runnable.transfer:main eth-get = chainlib.eth.runnable.get:main eth-decode = chainlib.eth.runnable.decode:main