chainlib

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

commit 33e25094762f1f329a6479e552ede7b8d87f872a
parent aaee1b2cfc5ac166bb8761490dc47c47d626ef3e
Author: nolash <dev@holbrook.no>
Date:   Wed, 17 Feb 2021 21:20:21 +0100

Add chain id, network id accessor on chain

Diffstat:
Mchainlib/eth/block.py | 4+---
Mchainlib/eth/gas.py | 21++++++++++++++++++---
Mchainlib/eth/nonce.py | 12++++++++++++
Mchainlib/eth/runnable/decode.py | 10+++++++++-
Mchainlib/eth/runnable/gas.py | 57++++++++++++++++++++++++++++++++++++++++++++++-----------
Mchainlib/eth/runnable/transfer.py | 9+++++++++
Mchainlib/eth/tx.py | 7++++---
7 files changed, 99 insertions(+), 21 deletions(-)

diff --git a/chainlib/eth/block.py b/chainlib/eth/block.py @@ -51,6 +51,4 @@ class Block: def __str__(self): - return 'block {} {}'.format(self.number, self.hash) - - + return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs)) diff --git a/chainlib/eth/gas.py b/chainlib/eth/gas.py @@ -6,9 +6,12 @@ from hexathon import ( from crypto_dev_signer.eth.transaction import EIP155Transaction # local imports +from chainlib.hash import keccak256_hex_to_hex from chainlib.eth.rpc import jsonrpc_template from chainlib.eth.tx import TxFactory -from chainlib.hash import keccak256_hex_to_hex +from chainlib.eth.constant import ( + MINIMUM_FEE_UNITS, + ) def price(): @@ -48,8 +51,20 @@ class DefaultGasOracle: self.conn = conn - def get(self): + def get(self, code=None): o = price() r = self.conn.do(o) n = strip_0x(r) - return int(n, 16) + return (int(n, 16), MINIMUM_FEE_UNITS) + + +class OverrideGasOracle: + + def __init__(self, price, limit=None): + if limit == None: + limit = MINIMUM_FEE_UNITS + self.limit = limit + self.price = price + + def get(self): + return (self.price, self.limit) diff --git a/chainlib/eth/nonce.py b/chainlib/eth/nonce.py @@ -35,3 +35,15 @@ class DefaultNonceOracle: n = self.nonce self.nonce += 1 return n + + +class OverrideNonceOracle(DefaultNonceOracle): + + + def __init__(self, address, nonce): + self.nonce = nonce + super(OverrideNonceOracle, self).__init__(address, None) + + + def get(self): + return self.nonce diff --git a/chainlib/eth/runnable/decode.py b/chainlib/eth/runnable/decode.py @@ -44,7 +44,15 @@ def main(): tx_raw_bytes = bytes.fromhex(tx_raw) tx = unpack(tx_raw_bytes, int(chain_id)) for k in tx.keys(): - print('{}: {}'.format(k, tx[k])) + x = None + if k == 'value': + x = '{:.18f} eth'.format(tx[k] / (10**18)) + elif k == 'gasPrice': + x = '{} gwei'.format(int(tx[k] / (10**12))) + if x != None: + print('{}: {} ({})'.format(k, tx[k], x)) + else: + print('{}: {}'.format(k, tx[k])) if __name__ == '__main__': diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -28,9 +28,13 @@ from hexathon import ( from chainlib.eth.address import to_checksum from chainlib.eth.connection import HTTPConnection from chainlib.eth.rpc import jsonrpc_template -from chainlib.eth.nonce import DefaultNonceOracle +from chainlib.eth.nonce import ( + DefaultNonceOracle, + OverrideNonceOracle, + ) from chainlib.eth.gas import ( DefaultGasOracle, + OverrideGasOracle, GasTxFactory, ) from chainlib.eth.gas import balance as gas_balance @@ -49,9 +53,15 @@ argparser.add_argument('-ww', action='store_true', help='Wait for every transact argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string') argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Signing address') argparser.add_argument('-y', '--key-file', 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('--price', type=int, help='override gas price') +argparser.add_argument('--gas', type=int, help='override gas limit') argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') +argparser.add_argument('-o', action='store_true', help='Print raw to to terminal') +argparser.add_argument('-n', action='store_true', help='Do not 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') args = argparser.parse_args() @@ -65,23 +75,46 @@ elif args.v: 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) + signer_address = keystore.import_keystore_file(args.y, passphrase) logg.debug('now have key for signer address {}'.format(signer_address)) signer = EIP155Signer(keystore) conn = HTTPConnection(args.p) -nonce_oracle = DefaultNonceOracle(signer_address, conn) -gas_oracle = DefaultGasOracle(conn) + +nonce_oracle = None +if args.nonce != None: + nonce_oracle = OverrideNonceOracle(signer_address, args.nonce) +else: + nonce_oracle = DefaultNonceOracle(signer_address, conn) + +gas_oracle = None +if args.price != None: + gas_oracle = OverrideGasOracle(args.price, args.gas) +else: + gas_oracle = DefaultGasOracle(conn) + chain_pair = args.i.split(':') chain_id = int(chain_pair[1]) value = args.amount +out = args.o + +send = not args.n + g = GasTxFactory(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id) @@ -102,14 +135,16 @@ def main(): logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient))) (tx_hash_hex, o) = g.create(signer_address, recipient, value) - conn.do(o) - - if block_last: - conn.wait(tx_hash_hex) - logg.debug('sender {} balance after: {}'.format(signer_address, balance(signer_address))) - logg.debug('recipient {} balance after: {}'.format(recipient, balance(recipient))) + if out: + print(o['params'][0]) + if send: + conn.do(o) + + if block_last: + conn.wait(tx_hash_hex) + logg.debug('sender {} balance after: {}'.format(signer_address, balance(signer_address))) + logg.debug('recipient {} balance after: {}'.format(recipient, balance(recipient))) - print(tx_hash_hex) diff --git a/chainlib/eth/runnable/transfer.py b/chainlib/eth/runnable/transfer.py @@ -48,6 +48,7 @@ argparser.add_argument('--token-address', required='True', dest='t', type=str, h argparser.add_argument('-a', '--sender-address', dest='s', type=str, help='Sender account address') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') 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('--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('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') @@ -64,6 +65,14 @@ elif args.v: 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) +logg.error('pass {}'.format(passphrase_env)) +if passphrase == None: + logg.warning('no passphrase given') + signer_address = None keystore = DictKeystore() if args.y != None: diff --git a/chainlib/eth/tx.py b/chainlib/eth/tx.py @@ -130,9 +130,10 @@ class TxFactory: def template(self, sender, recipient, use_nonce=False): gas_price = MINIMUM_FEE_PRICE + gas_limit = MINIMUM_FEE_UNITS if self.gas_oracle != None: - gas_price = self.gas_oracle.get() - logg.debug('using gas price {}'.format(gas_price)) + (gas_price, gas_limit) = self.gas_oracle.get() + logg.debug('using gas price {} limit {}'.format(gas_price, gas_limit)) nonce = 0 o = { 'from': sender, @@ -140,7 +141,7 @@ class TxFactory: 'value': 0, 'data': '0x', 'gasPrice': gas_price, - 'gas': MINIMUM_FEE_UNITS, + 'gas': gas_limit, 'chainId': self.chain_id, } if self.nonce_oracle != None and use_nonce: