Add docstrings to cli rpc util

This commit is contained in:
nolash 2021-08-23 08:47:01 +02:00
parent 96e0a97c3b
commit dcf03b2708
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -9,7 +9,7 @@ from chainlib.eth.nonce import (
RPCNonceOracle, RPCNonceOracle,
OverrideNonceOracle, OverrideNonceOracle,
) )
from chainlib.eth.gas import ( from chainlib.eth.fee import (
RPCGasOracle, RPCGasOracle,
OverrideGasOracle, OverrideGasOracle,
) )
@ -19,6 +19,13 @@ logg = logging.getLogger(__name__)
class Rpc: class Rpc:
"""Convenience wrapper to build rpc connection from processed configuration values.
:param cls: RPC connection class to instantiate
:type cls: chainlib.connection.RPCConnection implementation
:param wallet: Add wallet backend to instance
:type wallet: chainlib.cli.wallet.Wallet
"""
def __init__(self, cls, wallet=None): def __init__(self, cls, wallet=None):
self.constructor = cls self.constructor = cls
@ -27,10 +34,25 @@ class Rpc:
self.chain_spec = None self.chain_spec = None
self.wallet = wallet self.wallet = wallet
self.nonce_oracle = None self.nonce_oracle = None
self.gas_oracle = None self.fee_oracle = None
def connect_by_config(self, config): def connect_by_config(self, config):
"""Create a connection using the provided configuration, as rendered by chainlib.cli.config.Config.
The connection url string is fetched from the "RPC_HTTP_PROVIDER" configuration key. Currently only HTTP connection is supported. Basic HTTP auth is supported using the "RPC_HTTP_USERNAME" and "RPC_HTTP_PASSWORD" keys together with "RPC_HTTP_AUTHENTICATION" set to "basic".
The "CHAIN_SPEC" value is used for the chain context of the connection.
If the sequence flag was set in the confiruation (which generates the configuration key "_SEQ"), a sequential integer generator will be used for rpc ids. Otherwise uuids will be used.
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.
:param config: Processed configuration
:type config: confini.Config
:rtype: chainlib.connection.RPCConnection
:returns: An established rpc connection
"""
auth = None auth = None
if config.get('RPC_HTTP_AUTHENTICATION') == 'basic': if config.get('RPC_HTTP_AUTHENTICATION') == 'basic':
from chainlib.auth import BasicAuth from chainlib.auth import BasicAuth
@ -53,30 +75,57 @@ class Rpc:
fee_price = config.get('_FEE_PRICE') fee_price = config.get('_FEE_PRICE')
fee_limit = config.get('_FEE_LIMIT') fee_limit = config.get('_FEE_LIMIT')
if fee_price != None or fee_limit != None: if fee_price != None or fee_limit != None:
self.gas_oracle = OverrideGasOracle(price=fee_price, limit=fee_limit, conn=self.conn, id_generator=self.id_generator) self.fee_oracle = OverrideGasOracle(price=fee_price, limit=fee_limit, conn=self.conn, id_generator=self.id_generator)
else: else:
self.gas_oracle = RPCGasOracle(self.conn, id_generator=self.id_generator) self.fee_oracle = RPCGasOracle(self.conn, id_generator=self.id_generator)
return self.conn return self.conn
def get_nonce_oracle(self): def get_nonce_oracle(self):
"""Nonce oracle getter.
:rtype: chainlib.nonce.NonceOracle
:returns: Nonce oracle
"""
return self.nonce_oracle return self.nonce_oracle
def get_gas_oracle(self): def get_fee_oracle(self):
return self.gas_oracle """Fee oracle getter.
:rtype: chainlib.fee.FeeOracle
:returns: Fee oracle
"""
return self.fee_oracle
def can_sign(self): def can_sign(self):
"""Check if instance has signer capability.
:rtype: bool
:returns: True if signing is possible
"""
return self.wallet != None and self.wallet.signer != None return self.wallet != None and self.wallet.signer != None
def get_signer(self): def get_signer(self):
"""Signer getter.
:raises chainlib.error.SignerMissingException: Instance has no signer defined
:rtype: Signer implementation (todo: define base interface class)
:returns: Signer
"""
if self.wallet.signer == None: if self.wallet.signer == None:
raise SignerMissingException() raise SignerMissingException()
return self.wallet.signer return self.wallet.signer
def get_sender_address(self): def get_sender_address(self):
"""Wallet address getter.
:raises AttributeError: Instance has no signed defined
:rtype: str
:returns: Wallet address in canonical string representation
"""
return self.wallet.signer_address return self.wallet.signer_address