Add chain id, network id accessor on chain

This commit is contained in:
nolash 2021-02-17 21:20:21 +01:00
parent aaee1b2cfc
commit 33e2509476
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
7 changed files with 98 additions and 20 deletions

View File

@ -51,6 +51,4 @@ class Block:
def __str__(self): def __str__(self):
return 'block {} {}'.format(self.number, self.hash) return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs))

View File

@ -6,9 +6,12 @@ from hexathon import (
from crypto_dev_signer.eth.transaction import EIP155Transaction from crypto_dev_signer.eth.transaction import EIP155Transaction
# local imports # local imports
from chainlib.hash import keccak256_hex_to_hex
from chainlib.eth.rpc import jsonrpc_template from chainlib.eth.rpc import jsonrpc_template
from chainlib.eth.tx import TxFactory from chainlib.eth.tx import TxFactory
from chainlib.hash import keccak256_hex_to_hex from chainlib.eth.constant import (
MINIMUM_FEE_UNITS,
)
def price(): def price():
@ -48,8 +51,20 @@ class DefaultGasOracle:
self.conn = conn self.conn = conn
def get(self): def get(self, code=None):
o = price() o = price()
r = self.conn.do(o) r = self.conn.do(o)
n = strip_0x(r) 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)

View File

@ -35,3 +35,15 @@ class DefaultNonceOracle:
n = self.nonce n = self.nonce
self.nonce += 1 self.nonce += 1
return n 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

View File

@ -44,6 +44,14 @@ def main():
tx_raw_bytes = bytes.fromhex(tx_raw) tx_raw_bytes = bytes.fromhex(tx_raw)
tx = unpack(tx_raw_bytes, int(chain_id)) tx = unpack(tx_raw_bytes, int(chain_id))
for k in tx.keys(): for k in tx.keys():
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])) print('{}: {}'.format(k, tx[k]))

View File

@ -28,9 +28,13 @@ from hexathon import (
from chainlib.eth.address import to_checksum from chainlib.eth.address import to_checksum
from chainlib.eth.connection import HTTPConnection from chainlib.eth.connection import HTTPConnection
from chainlib.eth.rpc import jsonrpc_template 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 ( from chainlib.eth.gas import (
DefaultGasOracle, DefaultGasOracle,
OverrideGasOracle,
GasTxFactory, GasTxFactory,
) )
from chainlib.eth.gas import balance as gas_balance 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('-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('-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('-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('-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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more 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('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('amount', type=int, help='Amount of tokens to mint and gift')
args = argparser.parse_args() args = argparser.parse_args()
@ -65,23 +75,46 @@ elif args.v:
block_all = args.ww block_all = args.ww
block_last = args.w or block_all 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 signer_address = None
keystore = DictKeystore() keystore = DictKeystore()
if args.y != None: if args.y != None:
logg.debug('loading keystore file {}'.format(args.y)) 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)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore) signer = EIP155Signer(keystore)
conn = HTTPConnection(args.p) conn = HTTPConnection(args.p)
nonce_oracle = None
if args.nonce != None:
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
else:
nonce_oracle = DefaultNonceOracle(signer_address, conn) 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) gas_oracle = DefaultGasOracle(conn)
chain_pair = args.i.split(':') chain_pair = args.i.split(':')
chain_id = int(chain_pair[1]) chain_id = int(chain_pair[1])
value = args.amount 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) g = GasTxFactory(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id)
@ -102,6 +135,9 @@ def main():
logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient))) logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient)))
(tx_hash_hex, o) = g.create(signer_address, recipient, value) (tx_hash_hex, o) = g.create(signer_address, recipient, value)
if out:
print(o['params'][0])
if send:
conn.do(o) conn.do(o)
if block_last: if block_last:
@ -109,7 +145,6 @@ def main():
logg.debug('sender {} balance after: {}'.format(signer_address, balance(signer_address))) logg.debug('sender {} balance after: {}'.format(signer_address, balance(signer_address)))
logg.debug('recipient {} balance after: {}'.format(recipient, balance(recipient))) logg.debug('recipient {} balance after: {}'.format(recipient, balance(recipient)))
print(tx_hash_hex) print(tx_hash_hex)

View File

@ -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('-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('-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('--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('-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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
@ -64,6 +65,14 @@ elif args.v:
block_all = args.ww block_all = args.ww
block_last = args.w or block_all 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 signer_address = None
keystore = DictKeystore() keystore = DictKeystore()
if args.y != None: if args.y != None:

View File

@ -130,9 +130,10 @@ class TxFactory:
def template(self, sender, recipient, use_nonce=False): def template(self, sender, recipient, use_nonce=False):
gas_price = MINIMUM_FEE_PRICE gas_price = MINIMUM_FEE_PRICE
gas_limit = MINIMUM_FEE_UNITS
if self.gas_oracle != None: if self.gas_oracle != None:
gas_price = self.gas_oracle.get() (gas_price, gas_limit) = self.gas_oracle.get()
logg.debug('using gas price {}'.format(gas_price)) logg.debug('using gas price {} limit {}'.format(gas_price, gas_limit))
nonce = 0 nonce = 0
o = { o = {
'from': sender, 'from': sender,
@ -140,7 +141,7 @@ class TxFactory:
'value': 0, 'value': 0,
'data': '0x', 'data': '0x',
'gasPrice': gas_price, 'gasPrice': gas_price,
'gas': MINIMUM_FEE_UNITS, 'gas': gas_limit,
'chainId': self.chain_id, 'chainId': self.chain_id,
} }
if self.nonce_oracle != None and use_nonce: if self.nonce_oracle != None and use_nonce: