Add get_contract_data method to find contracts on the Registrar chain via address or name

Fix syntax error
pull/224/merge^2
tuxxy 2018-04-12 17:38:35 -06:00
parent 0fb2d03707
commit fccdfc2a16
1 changed files with 25 additions and 4 deletions

View File

@ -48,7 +48,7 @@ class Registrar:
self.__registrar_filepath = registrar_filepath or self.__DEFAULT_REGISTRAR_FILEPATH
@classmethod
def get_chains(cls, registrar_filepath: str=None):
def get_chains(cls, registrar_filepath: str=None) -> dict:
"""
Returns a dict of Registrar objects where the key is the chain name and
the value is the Registrar object for that chain.
@ -57,7 +57,7 @@ class Registrar:
filepath = registrar_filepath or self.__DEFAULT_REGISTRAR_FILEPATH
instance = cls(registrar_filepath=filepath)
registrar_data = instance._read_registrar_file(filepath)
registrar_data = _read_registrar_file(filepath)
chain_names = registrar_data.keys()
chains = dict()
@ -66,7 +66,7 @@ class Registrar:
registrar_filepath=filepath)
return chains
def enroll(self, contract_name: str, contract_address: str, contract_abi: list):
def enroll(self, contract_name: str, contract_address: str, contract_abi: list) -> None:
"""
Enrolls a contract to the chain registrar by writing the abi information
to the filesystem as JSON. This can also be used to update the info
@ -89,9 +89,11 @@ class Registrar:
__write_registrar_file(registrar_data, self.__registrar_filepath)
def get_chain_data(self):
def get_chain_data(self) -> dict:
"""
Returns all data from the current registrar chain as a dict.
If no data exists for the current registrar chain, then it will raise
KeyError.
If you haven't specified the chain name, it's probably the tester chain.
"""
registrar_data = __read_registrar_file(self.__registrar_filepath)
@ -100,3 +102,22 @@ class Registrar:
except KeyError:
raise KeyError("Data does not exist for chain '{}'".format(self._chain_name))
return chain_data
def get_contract_data(self, identifier: str=None) -> dict:
"""
Returns contract data on the chain as a dict given an `identifier`.
It first attempts to use identifier as a contract name. If no name is
found, it will attempt to use identifier as an address.
If no contract is found, it will raise NoKnownContract.
"""
chain_data = self.get_chain_data()
if identifier in chain_data:
contract_data = chain_data[identifier]
return contract_data
else:
for contract_name, contract_data in chain_data.items():
if contract_data['addr'] == identifier:
return contract_data
raise self.NoKnownContract(
"Could not identify a contract name or address with {}".format(identifier)
)