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 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 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