chainlib-eth/chainlib/eth/cli.py

124 lines
4.3 KiB
Python
Raw Normal View History

2021-08-21 09:27:40 +02:00
# standard imports
import os
2021-09-01 09:38:09 +02:00
import logging
2021-08-21 09:27:40 +02:00
# external imports
from chainlib.cli import (
ArgumentParser,
argflag_std_read,
argflag_std_write,
argflag_std_base,
Config as BaseConfig,
Wallet as BaseWallet,
2021-09-13 16:35:45 +02:00
Rpc as BaseRpc, Flag,
2021-08-21 09:27:40 +02:00
)
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
# local imports
from chainlib.eth.address import AddressChecksum
from chainlib.eth.connection import EthHTTPConnection
2021-08-24 17:55:01 +02:00
from chainlib.eth.gas import (
OverrideGasOracle,
RPCGasOracle,
)
from chainlib.eth.nonce import (
OverrideNonceOracle,
RPCNonceOracle,
)
2021-09-01 09:38:09 +02:00
logg = logging.getLogger(__name__)
2021-08-21 09:27:40 +02:00
script_dir = os.path.dirname(os.path.realpath(__file__))
2021-08-24 17:55:01 +02:00
2021-08-21 09:27:40 +02:00
class Wallet(BaseWallet):
"""Convenience constructor to set Ethereum defaults for chainlib cli Wallet object
:param checksummer: Address checksummer object
:type checksummer: Implementation of chainlib.eth.address.AddressChecksum
"""
def __init__(self, checksummer=AddressChecksum):
super(Wallet, self).__init__(EIP155Signer, checksummer=checksummer)
2021-10-11 21:12:59 +02:00
# TODO: how is the keystore implemented in rpc here?
2021-08-21 09:27:40 +02:00
class Rpc(BaseRpc):
"""Convenience constructor to set Ethereum defaults for chainlib cli Rpc object
2021-08-24 17:55:01 +02:00
2021-08-21 09:27:40 +02:00
"""
def __init__(self, wallet=None):
super(Rpc, self).__init__(EthHTTPConnection, wallet=wallet)
2021-08-24 17:55:01 +02:00
def connect_by_config(self, config):
"""
If the standard arguments for nonce and fee price/price have been defined (which generate the configuration keys "_NONCE", "_FEE_PRICE" and "_FEE_LIMIT" respectively) , the corresponding overrides for fee and nonce generators will be defined.
"""
super(Rpc, self).connect_by_config(config)
if self.can_sign():
nonce = None
try:
nonce = config.get('_NONCE')
except KeyError:
pass
2021-08-24 17:55:01 +02:00
if nonce != None:
self.nonce_oracle = OverrideNonceOracle(self.get_sender_address(), nonce, id_generator=self.id_generator)
else:
self.nonce_oracle = RPCNonceOracle(self.get_sender_address(), self.conn, id_generator=self.id_generator)
2021-09-01 09:38:09 +02:00
fee_price = None
fee_limit = None
try:
fee_price = config.get('_FEE_PRICE')
except KeyError:
pass
try:
fee_limit = config.get('_FEE_LIMIT')
except KeyError:
pass
if fee_price != None or fee_limit != None:
self.fee_oracle = OverrideGasOracle(price=fee_price, limit=fee_limit, conn=self.conn, id_generator=self.id_generator)
else:
self.fee_oracle = RPCGasOracle(self.conn, id_generator=self.id_generator)
2021-08-24 17:55:01 +02:00
error_parser = None
if config.get('RPC_DIALECT') == 'openethereum':
from chainlib.eth.dialect.openethereum import DialectErrorParser
self.error_parser = DialectErrorParser()
2021-08-24 17:55:01 +02:00
return self.conn
def get_gas_oracle(self):
return self.get_fee_oracle()
2021-08-21 09:27:40 +02:00
class Config(BaseConfig):
"""Convenience constructor to set Ethereum defaults for the chainlib cli config object
"""
default_base_config_dir = os.path.join(script_dir, 'data', 'config')
default_fee_limit = 21000
@classmethod
def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=None):
2021-09-13 16:35:45 +02:00
super(Config, cls).override_defaults(base_dir=cls.default_base_config_dir)
2021-08-28 10:24:46 +02:00
if default_fee_limit == None:
default_fee_limit = cls.default_fee_limit
config = BaseConfig.from_args(args, arg_flags=arg_flags, env=env, extra_args=extra_args, base_config_dir=base_config_dir, default_config_dir=default_config_dir, user_config_dir=user_config_dir, default_fee_limit=default_fee_limit, logger=logger, load_callback=load_callback)
if not config.get('RPC_DIALECT'):
config.add('default', 'RPC_DIALECT', exists_ok=True)
elif config.get('RPC_DIALECT') not in [
'openethereum',
'default',
]:
raise ValueError('unknown rpc dialect {}'.format(config.get('RPC_DIALECT')))
return config