def dummy_contract(request): directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = ComponentConfiguration.load(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) # load into sys modules Contract.from_config(configuration) # load into registry path = Path(configuration.directory, configuration.path_to_contract_interface) with open(path, "r") as interface_file: contract_interface = json.load(interface_file) contract_registry.register( id_=str(configuration.public_id), entry_point= f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={"contract_interface": contract_interface}, contract_config=configuration, ) contract = contract_registry.make(str(configuration.public_id)) yield contract contract_registry.specs.pop(str(configuration.public_id))
def _populate_contract_registry(self): """Populate contract registry.""" for configuration in self._package_dependency_manager.get_components_by_type( ComponentType.CONTRACT ).values(): configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: logger.warning( f"Skipping registration of contract {configuration.public_id} since already registered." ) continue logger.debug( # pragma: nocover f"Registering contract {configuration.public_id}" ) try: # pragma: nocover contract_registry.register( id_=str(configuration.public_id), entry_point=f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={ "contract_interface": configuration.contract_interfaces }, contract_config=configuration, # TODO: resolve configuration being applied globally ) except AEAException as e: # pragma: nocover if "Cannot re-register id:" in str(e): logger.warning( "Already registered: {}".format(configuration.class_name) ) else: raise e
def erc1155_contract(): """ Instantiate an ERC1155 contract instance. As a side effect, register it to the registry, if not already registered. """ directory = Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155") configuration = ComponentConfiguration.load(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) not in contract_registry.specs: # load contract into sys modules Contract.from_config(configuration) path = Path(configuration.directory, configuration.path_to_contract_interface) with open(path, "r") as interface_file: contract_interface = json.load(interface_file) contract_registry.register( id_=str(configuration.public_id), entry_point= f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={"contract_interface": contract_interface}, contract_config=configuration, ) contract = contract_registry.make(str(configuration.public_id)) yield contract
def test_from_config_and_registration(): """Tests the from config method and contract registry registration.""" directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = ComponentConfiguration.load(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) contract = Contract.from_config(configuration) assert contract is not None assert contract.contract_interface is None assert contract.configuration == configuration assert contract.id == configuration.public_id contract_registry.register( id_=str(configuration.public_id), entry_point=f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={"contract_interface": configuration.contract_interfaces}, contract_config=configuration, ) contract = contract_registry.make(str(configuration.public_id)) assert contract is not None assert contract.configuration == configuration assert contract.contract_interface is not None