Initial commit; receive all eth code from chainlib package
This commit is contained in:
0
chainlib/eth/runnable/__init__.py
Normal file
0
chainlib/eth/runnable/__init__.py
Normal file
102
chainlib/eth/runnable/balance.py
Normal file
102
chainlib/eth/runnable/balance.py
Normal file
@@ -0,0 +1,102 @@
|
||||
#!python3
|
||||
|
||||
"""Token balance query script
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# standard imports
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
even,
|
||||
)
|
||||
import sha3
|
||||
from eth_abi import encode_single
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import to_checksum
|
||||
from chainlib.jsonrpc import (
|
||||
jsonrpc_result,
|
||||
IntSequenceGenerator,
|
||||
)
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.gas import (
|
||||
OverrideGasOracle,
|
||||
balance,
|
||||
)
|
||||
from chainlib.chain import ChainSpec
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
default_eth_provider = os.environ.get('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('address', type=str, help='Account address')
|
||||
args = argparser.parse_args()
|
||||
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
conn = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
gas_oracle = OverrideGasOracle(conn)
|
||||
|
||||
address = to_checksum(args.address)
|
||||
if not args.u and address != add_0x(args.address):
|
||||
raise ValueError('invalid checksum address')
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
def main():
|
||||
r = None
|
||||
decimals = 18
|
||||
|
||||
o = balance(address, id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
|
||||
hx = strip_0x(r)
|
||||
balance_value = int(hx, 16)
|
||||
logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
|
||||
|
||||
balance_str = str(balance_value)
|
||||
balance_len = len(balance_str)
|
||||
if balance_len < decimals + 1:
|
||||
print('0.{}'.format(balance_str.zfill(decimals)))
|
||||
else:
|
||||
offset = balance_len-decimals
|
||||
print('{}.{}'.format(balance_str[:offset],balance_str[offset:]))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
33
chainlib/eth/runnable/checksum.py
Normal file
33
chainlib/eth/runnable/checksum.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# standard imports
|
||||
import sys
|
||||
import select
|
||||
|
||||
# external imports
|
||||
from hexathon import strip_0x
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
|
||||
v = None
|
||||
if len(sys.argv) > 1:
|
||||
v = sys.argv[1]
|
||||
else:
|
||||
h = select.select([sys.stdin], [], [], 0)
|
||||
if len(h[0]) > 0:
|
||||
v = h[0][0].read()
|
||||
v = v.rstrip()
|
||||
|
||||
if v == None:
|
||||
sys.stderr.write('input missing\n')
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
try:
|
||||
print(to_checksum_address(strip_0x(v)))
|
||||
except ValueError as e:
|
||||
sys.stderr.write('invalid input: {}\n'.format(e))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
90
chainlib/eth/runnable/count.py
Normal file
90
chainlib/eth/runnable/count.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# standard imports
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
import select
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import to_checksum
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.tx import count
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.jsonrpc import IntSequenceGenerator
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
|
||||
from hexathon import add_0x
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
default_eth_provider = os.environ.get('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
def stdin_arg():
|
||||
h = select.select([sys.stdin], [], [], 0)
|
||||
if len(h[0]) > 0:
|
||||
v = h[0][0].read()
|
||||
return v.rstrip()
|
||||
return None
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
|
||||
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('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('address', nargs='?', type=str, default=stdin_arg(), help='Ethereum address of recipient')
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.address == None:
|
||||
argparser.error('need first positional argument or value from stdin')
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
|
||||
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, passphrase)
|
||||
logg.debug('now have key for signer address {}'.format(signer_address))
|
||||
signer = EIP155Signer(keystore)
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
rpc = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
def main():
|
||||
recipient = to_checksum(args.address)
|
||||
if not args.u and recipient != add_0x(args.address):
|
||||
raise ValueError('invalid checksum address')
|
||||
|
||||
o = count(recipient, id_generator=rpc_id_generator)
|
||||
r = rpc.do(o)
|
||||
count_result = None
|
||||
try:
|
||||
count_result = int(r, 16)
|
||||
except ValueError:
|
||||
count_result = int(r, 10)
|
||||
print(count_result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
65
chainlib/eth/runnable/decode.py
Normal file
65
chainlib/eth/runnable/decode.py
Normal file
@@ -0,0 +1,65 @@
|
||||
#!python3
|
||||
|
||||
"""Decode raw transaction
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# standard imports
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
import select
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.tx import unpack
|
||||
from chainlib.chain import ChainSpec
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.runnable.util import decode_for_puny_humans
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
def stdin_arg(t=0):
|
||||
h = select.select([sys.stdin], [], [], t)
|
||||
if len(h[0]) > 0:
|
||||
v = h[0][0].read()
|
||||
return v.rstrip()
|
||||
return None
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-i', '--chain-id', dest='i', default='evm:ethereum:1', type=str, help='Numeric network id')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('tx', type=str, nargs='?', default=stdin_arg(), help='hex-encoded signed raw transaction')
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
argp = args.tx
|
||||
logg.debug('txxxx {}'.format(args.tx))
|
||||
if argp == None:
|
||||
argp = stdin_arg(t=3)
|
||||
if argp == None:
|
||||
argparser.error('need first positional argument or value from stdin')
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
|
||||
def main():
|
||||
tx_raw = argp
|
||||
decode_for_puny_humans(tx_raw, chain_spec, sys.stdout)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
180
chainlib/eth/runnable/gas.py
Normal file
180
chainlib/eth/runnable/gas.py
Normal file
@@ -0,0 +1,180 @@
|
||||
#!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 (
|
||||
JSONRPCRequest,
|
||||
IntSequenceGenerator,
|
||||
)
|
||||
from chainlib.eth.nonce import (
|
||||
RPCNonceOracle,
|
||||
OverrideNonceOracle,
|
||||
)
|
||||
from chainlib.eth.gas import (
|
||||
RPCGasOracle,
|
||||
OverrideGasOracle,
|
||||
Gas,
|
||||
)
|
||||
from chainlib.eth.gas import balance as gas_balance
|
||||
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('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, 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', 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('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
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='gas value in wei')
|
||||
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)
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
conn = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
nonce_oracle = None
|
||||
if args.nonce != None:
|
||||
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce, id_generator=rpc_id_generator)
|
||||
else:
|
||||
nonce_oracle = RPCNonceOracle(signer_address, conn, id_generator=rpc_id_generator)
|
||||
|
||||
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, id_generator=rpc_id_generator)
|
||||
else:
|
||||
gas_oracle = RPCGasOracle(conn, id_generator=rpc_id_generator)
|
||||
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
value = args.amount
|
||||
|
||||
send = args.s
|
||||
|
||||
g = Gas(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
|
||||
|
||||
|
||||
def balance(address, id_generator):
|
||||
o = gas_balance(address, id_generator=id_generator)
|
||||
r = conn.do(o)
|
||||
hx = strip_0x(r)
|
||||
return int(hx, 16)
|
||||
|
||||
|
||||
def main():
|
||||
recipient = to_checksum(args.recipient)
|
||||
if not args.u and recipient != add_0x(args.recipient):
|
||||
raise ValueError('invalid checksum address')
|
||||
|
||||
logg.info('gas transfer from {} to {} value {}'.format(signer_address, recipient, value))
|
||||
if logg.isEnabledFor(logging.DEBUG):
|
||||
try:
|
||||
sender_balance = balance(signer_address, rpc_id_generator)
|
||||
recipient_balance = balance(recipient, rpc_id_generator)
|
||||
logg.debug('sender {} balance before: {}'.format(signer_address, sender_balance))
|
||||
logg.debug('recipient {} balance before: {}'.format(recipient, recipient_balance))
|
||||
except urllib.error.URLError:
|
||||
pass
|
||||
|
||||
(tx_hash_hex, o) = g.create(signer_address, recipient, value, id_generator=rpc_id_generator)
|
||||
|
||||
if send:
|
||||
conn.do(o)
|
||||
if block_last:
|
||||
r = conn.wait(tx_hash_hex)
|
||||
if logg.isEnabledFor(logging.DEBUG):
|
||||
sender_balance = balance(signer_address, rpc_id_generator)
|
||||
recipient_balance = balance(recipient, rpc_id_generator)
|
||||
logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
|
||||
logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
|
||||
if r['status'] == 0:
|
||||
logg.critical('VM revert. Wish I could tell you more')
|
||||
sys.exit(1)
|
||||
print(tx_hash_hex)
|
||||
else:
|
||||
if logg.isEnabledFor(logging.INFO):
|
||||
io_str = io.StringIO()
|
||||
decode_for_puny_humans(o['params'][0], chain_spec, io_str)
|
||||
print(io_str.getvalue())
|
||||
else:
|
||||
print(o['params'][0])
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
158
chainlib/eth/runnable/get.py
Normal file
158
chainlib/eth/runnable/get.py
Normal file
@@ -0,0 +1,158 @@
|
||||
#!python3
|
||||
|
||||
"""Data retrieval script
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# 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
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
import sha3
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import to_checksum
|
||||
from chainlib.jsonrpc import (
|
||||
JSONRPCRequest,
|
||||
jsonrpc_result,
|
||||
IntSequenceGenerator,
|
||||
)
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.tx import (
|
||||
Tx,
|
||||
pack,
|
||||
)
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
from chainlib.eth.block import Block
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.status import Status
|
||||
from chainlib.eth.runnable.util import decode_for_puny_humans
|
||||
|
||||
logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s')
|
||||
logg = logging.getLogger()
|
||||
|
||||
default_eth_provider = os.environ.get('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
def stdin_arg(t=0):
|
||||
h = select.select([sys.stdin], [], [], t)
|
||||
if len(h[0]) > 0:
|
||||
v = h[0][0].read()
|
||||
return v.rstrip()
|
||||
return None
|
||||
|
||||
argparser = argparse.ArgumentParser('eth-get', description='display information about an Ethereum address or transaction', epilog='address/transaction can be provided as an argument or from standard input')
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('--rlp', action='store_true', help='Display transaction as raw rlp')
|
||||
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
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('item', nargs='?', default=stdin_arg(), type=str, help='Item to get information for (address og transaction)')
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
argp = args.item
|
||||
if argp == None:
|
||||
argp = stdin_arg(None)
|
||||
if argsp == None:
|
||||
argparser.error('need first positional argument or value from stdin')
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
conn = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
item = add_0x(args.item)
|
||||
as_rlp = bool(args.rlp)
|
||||
|
||||
|
||||
def get_transaction(conn, tx_hash, id_generator):
|
||||
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)
|
||||
|
||||
if as_rlp:
|
||||
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)
|
||||
tx.generate_wire(chain_spec)
|
||||
return tx
|
||||
|
||||
|
||||
def get_address(conn, address, id_generator):
|
||||
j = JSONRPCRequest(id_generator=id_generator)
|
||||
o = j.template()
|
||||
o['method'] = 'eth_getCode'
|
||||
o['params'].append(address)
|
||||
o['params'].append('latest')
|
||||
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():
|
||||
r = None
|
||||
if len(item) > 42:
|
||||
r = get_transaction(conn, item, rpc_id_generator).to_human()
|
||||
elif args.u or to_checksum_address(item):
|
||||
r = get_address(conn, item, rpc_id_generator)
|
||||
print(r)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
170
chainlib/eth/runnable/info.py
Normal file
170
chainlib/eth/runnable/info.py
Normal file
@@ -0,0 +1,170 @@
|
||||
#!python3
|
||||
|
||||
"""Token balance query script
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# standard imports
|
||||
import datetime
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
even,
|
||||
)
|
||||
import sha3
|
||||
from eth_abi import encode_single
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import (
|
||||
to_checksum_address,
|
||||
is_checksum_address,
|
||||
)
|
||||
from chainlib.eth.chain import network_id
|
||||
from chainlib.eth.block import (
|
||||
block_latest,
|
||||
block_by_number,
|
||||
Block,
|
||||
)
|
||||
from chainlib.eth.tx import count
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.gas import (
|
||||
OverrideGasOracle,
|
||||
balance,
|
||||
price,
|
||||
)
|
||||
from chainlib.jsonrpc import (
|
||||
IntSequenceGenerator,
|
||||
)
|
||||
from chainlib.chain import ChainSpec
|
||||
|
||||
BLOCK_SAMPLES = 10
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
default_eth_provider = os.environ.get('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('-H', '--human', dest='human', action='store_true', help='Use human-friendly formatting')
|
||||
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
argparser.add_argument('-l', '--long', dest='l', action='store_true', help='Calculate averages through sampling of blocks and txs')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Include summary for keyfile')
|
||||
argparser.add_argument('address', nargs='?', type=str, help='Include summary for address (conflicts with -y)')
|
||||
args = argparser.parse_args()
|
||||
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
signer = None
|
||||
holder_address = None
|
||||
if args.address != None:
|
||||
if not args.u and not is_checksum_address(args.address):
|
||||
raise ValueError('invalid checksum address {}'.format(args.address))
|
||||
holder_address = add_0x(args.address)
|
||||
elif args.y != None:
|
||||
f = open(args.y, 'r')
|
||||
o = json.load(f)
|
||||
f.close()
|
||||
holder_address = add_0x(to_checksum_address(o['address']))
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
conn = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
gas_oracle = OverrideGasOracle(conn)
|
||||
|
||||
token_symbol = 'eth'
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
human = args.human
|
||||
|
||||
longmode = args.l
|
||||
|
||||
def main():
|
||||
o = network_id(id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
#if human:
|
||||
# n = format(n, ',')
|
||||
sys.stdout.write('Network id: {}\n'.format(r))
|
||||
|
||||
o = block_latest(id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
n = int(r, 16)
|
||||
first_block_number = n
|
||||
if human:
|
||||
n = format(n, ',')
|
||||
sys.stdout.write('Block: {}\n'.format(n))
|
||||
|
||||
o = block_by_number(first_block_number, False, id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
last_block = Block(r)
|
||||
last_timestamp = last_block.timestamp
|
||||
|
||||
if longmode:
|
||||
aggr_time = 0.0
|
||||
aggr_gas = 0
|
||||
for i in range(BLOCK_SAMPLES):
|
||||
o = block_by_number(first_block_number-i, False, id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
block = Block(r)
|
||||
aggr_time += last_block.timestamp - block.timestamp
|
||||
|
||||
gas_limit = int(r['gasLimit'], 16)
|
||||
aggr_gas += gas_limit
|
||||
|
||||
last_block = block
|
||||
last_timestamp = block.timestamp
|
||||
|
||||
n = int(aggr_gas / BLOCK_SAMPLES)
|
||||
if human:
|
||||
n = format(n, ',')
|
||||
|
||||
sys.stdout.write('Gaslimit: {}\n'.format(n))
|
||||
sys.stdout.write('Blocktime: {}\n'.format(aggr_time / BLOCK_SAMPLES))
|
||||
|
||||
o = price(id_generator=rpc_id_generator)
|
||||
r = conn.do(o)
|
||||
n = int(r, 16)
|
||||
if human:
|
||||
n = format(n, ',')
|
||||
sys.stdout.write('Gasprice: {}\n'.format(n))
|
||||
|
||||
if holder_address != None:
|
||||
o = count(holder_address)
|
||||
r = conn.do(o)
|
||||
n = int(r, 16)
|
||||
sys.stdout.write('Address: {}\n'.format(holder_address))
|
||||
sys.stdout.write('Nonce: {}\n'.format(n))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
189
chainlib/eth/runnable/raw.py
Normal file
189
chainlib/eth/runnable/raw.py
Normal file
@@ -0,0 +1,189 @@
|
||||
#!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 (
|
||||
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.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('RPC_PROVIDER')
|
||||
if default_eth_provider == None:
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, 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', dest='y', type=str, help='Ethereum keystore file to use for signing')
|
||||
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
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('--seq', action='store_true', help='Use sequential rpc ids')
|
||||
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('-l', '--local', dest='l', action='store_true', help='Local contract call')
|
||||
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)
|
||||
|
||||
rpc_id_generator = None
|
||||
if args.seq:
|
||||
rpc_id_generator = IntSequenceGenerator()
|
||||
|
||||
auth = None
|
||||
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
|
||||
from chainlib.auth import BasicAuth
|
||||
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
|
||||
conn = EthHTTPConnection(args.p, auth=auth)
|
||||
|
||||
send = args.s
|
||||
|
||||
local = args.l
|
||||
if local:
|
||||
send = False
|
||||
|
||||
nonce_oracle = None
|
||||
gas_oracle = None
|
||||
if signer_address != None and not local:
|
||||
if args.nonce != None:
|
||||
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
|
||||
else:
|
||||
nonce_oracle = RPCNonceOracle(signer_address, conn)
|
||||
|
||||
if args.gas_price or args.gas_limit != None:
|
||||
gas_oracle = OverrideGasOracle(price=args.gas_price, limit=args.gas_limit, conn=conn, id_generator=rpc_id_generator)
|
||||
else:
|
||||
gas_oracle = RPCGasOracle(conn, id_generator=rpc_id_generator)
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
|
||||
value = args.value
|
||||
|
||||
|
||||
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')
|
||||
|
||||
if local:
|
||||
j = JSONRPCRequest(id_generator=rpc_id_generator)
|
||||
o = j.template()
|
||||
o['method'] = 'eth_call'
|
||||
o['params'].append({
|
||||
'to': recipient,
|
||||
'from': signer_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(args.data),
|
||||
})
|
||||
o['params'].append('latest')
|
||||
o = j.finalize(o)
|
||||
r = conn.do(o)
|
||||
print(strip_0x(r))
|
||||
return
|
||||
|
||||
elif signer_address != None:
|
||||
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, id_generator=rpc_id_generator)
|
||||
|
||||
if send:
|
||||
r = conn.do(o)
|
||||
print(r)
|
||||
else:
|
||||
print(o)
|
||||
print(tx_hash_hex)
|
||||
|
||||
else:
|
||||
o = raw(args.data, id_generator=rpc_id_generator)
|
||||
if send:
|
||||
r = conn.do(o)
|
||||
print(r)
|
||||
else:
|
||||
print(o)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
21
chainlib/eth/runnable/subscribe.py
Normal file
21
chainlib/eth/runnable/subscribe.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import json
|
||||
|
||||
import websocket
|
||||
|
||||
ws = websocket.create_connection('ws://localhost:8545')
|
||||
|
||||
o = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "eth_subscribe",
|
||||
"params": [
|
||||
"newHeads",
|
||||
],
|
||||
"id": 0,
|
||||
}
|
||||
|
||||
ws.send(json.dumps(o).encode('utf-8'))
|
||||
|
||||
while True:
|
||||
print(ws.recv())
|
||||
|
||||
ws.close()
|
||||
31
chainlib/eth/runnable/util.py
Normal file
31
chainlib/eth/runnable/util.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# local imports
|
||||
from chainlib.eth.tx import unpack
|
||||
from hexathon import (
|
||||
strip_0x,
|
||||
add_0x,
|
||||
)
|
||||
|
||||
def decode_out(tx, writer, skip_keys=[]):
|
||||
for k in tx.keys():
|
||||
if k in skip_keys:
|
||||
continue
|
||||
x = None
|
||||
if k == 'value':
|
||||
x = '{:.18f} eth'.format(tx[k] / (10**18))
|
||||
elif k == 'gasPrice':
|
||||
x = '{} gwei'.format(int(tx[k] / (10**9)))
|
||||
elif k == 'value':
|
||||
k = 'gas-value'
|
||||
if x != None:
|
||||
writer.write('{}: {} ({})\n'.format(k, tx[k], x))
|
||||
else:
|
||||
writer.write('{}: {}\n'.format(k, tx[k]))
|
||||
|
||||
|
||||
def decode_for_puny_humans(tx_raw, chain_spec, writer, skip_keys=[]):
|
||||
tx_raw = strip_0x(tx_raw)
|
||||
tx_raw_bytes = bytes.fromhex(tx_raw)
|
||||
tx = unpack(tx_raw_bytes, chain_spec)
|
||||
decode_out(tx, writer, skip_keys=skip_keys)
|
||||
writer.write('src: {}\n'.format(add_0x(tx_raw)))
|
||||
|
||||
Reference in New Issue
Block a user