diff --git a/READMEs/c2d-flow.md b/READMEs/c2d-flow.md index c72499d68..ae4579525 100644 --- a/READMEs/c2d-flow.md +++ b/READMEs/c2d-flow.md @@ -107,40 +107,10 @@ In the same Python console: ALGO_url = "https://raw.githubusercontent.com/oceanprotocol/c2d-examples/main/branin_and_gpr/gpr.py" name = "grp" -(ALGO_data_nft, ALGO_datatoken, ALGO_ddo) = ocean.assets.create_url_asset(name, ALGO_url, alice_wallet, wait_for_aqua=True) +(ALGO_data_nft, ALGO_datatoken, ALGO_ddo) = ocean.assets.create_algo_asset(name, ALGO_url, alice_wallet, wait_for_aqua=True) print(f"ALGO_data_nft address = '{ALGO_data_nft.address}'") print(f"ALGO_datatoken address = '{ALGO_datatoken.address}'") - -# Specify metadata and services, using the Branin test dataset -ALGO_date_created = "2021-12-28T10:55:11Z" -ALGO_metadata = { - "created": ALGO_date_created, - "updated": ALGO_date_created, - "description": "gpr", - "name": "gpr", - "type": "algorithm", - "author": "Trent", - "license": "CC0: PublicDomain", - "algorithm": { - "language": "python", - "format": "docker-image", - "version": "0.1", - "container": { - "entrypoint": "python $ALGO", - "image": "oceanprotocol/algo_dockers", - "tag": "python-branin", - "checksum": "sha256:8221d20c1c16491d7d56b9657ea09082c0ee4a8ab1a6621fa720da58b09580e4", - }, - } -} - -# update ALGO_Asset metadata -ALGO_ddo.metadata.update(ALGO_metadata) -ALGO_ddo = ocean.assets.update( - asset=ALGO_ddo, publisher_wallet=alice_wallet, provider_uri=config["PROVIDER_URL"]) - - print(f"ALGO_ddo did = '{ALGO_ddo.did}'") ``` diff --git a/ocean_lib/ocean/ocean_assets.py b/ocean_lib/ocean/ocean_assets.py index 1ce4e96bb..f74a17ce9 100644 --- a/ocean_lib/ocean/ocean_assets.py +++ b/ocean_lib/ocean/ocean_assets.py @@ -146,15 +146,46 @@ def _assert_ddo_metadata(metadata: dict): assert "name" in metadata, "Must have name in metadata." + @enforce_types + def create_algo_asset( + self, + name: str, + url: str, + publisher_wallet, + image: str = "oceanprotocol/algo_dockers", + tag: str = "python-branin", + checksum: str = "sha256:8221d20c1c16491d7d56b9657ea09082c0ee4a8ab1a6621fa720da58b09580e4", + wait_for_aqua: bool = True, + ) -> tuple: + """Create asset of type "algorithm", having UrlFiles, with good defaults""" + + if image == "oceanprotocol/algo_dockers" or tag == "python-branin": + assert image == "oceanprotocol/algo_dockers" and tag == "python-branin" + + metadata = self._default_metadata(name, publisher_wallet, "algorithm") + metadata["algorithm"] = { + "language": "python", + "format": "docker-image", + "version": "0.1", + "container": { + "entrypoint": "python $ALGO", + "image": image, + "tag": tag, + "checksum": checksum, + }, + } + + files = [UrlFile(url)] + return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua) + @enforce_types def create_url_asset( self, name: str, url: str, publisher_wallet, wait_for_aqua: bool = True ) -> tuple: - """Create an asset of type "UrlFile", with good defaults""" + """Create asset of type "data", having UrlFiles, with good defaults""" + metadata = self._default_metadata(name, publisher_wallet) files = [UrlFile(url)] - return self.create_with_default_metadata( - name, files, publisher_wallet, wait_for_aqua - ) + return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua) @enforce_types def create_graphql_asset( @@ -165,11 +196,10 @@ def create_graphql_asset( publisher_wallet, wait_for_aqua: bool = True, ) -> tuple: - """Create an asset of type "GraphqlQuery", with good defaults""" + """Create asset of type "data", having GraphqlQuery files, w good defaults""" + metadata = self._default_metadata(name, publisher_wallet) files = [GraphqlQuery(url, query)] - return self.create_with_default_metadata( - name, files, publisher_wallet, wait_for_aqua - ) + return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua) @enforce_types def create_onchain_asset( @@ -180,39 +210,31 @@ def create_onchain_asset( publisher_wallet, wait_for_aqua: bool = True, ) -> tuple: - """Create an asset of type "SmartContractCall", with good defaults""" + """Create asset of type "data", having SmartContractCall files, w defaults""" chain_id = self._chain_id onchain_data = SmartContractCall(contract_address, chain_id, contract_abi) files = [onchain_data] - return self.create_with_default_metadata( - name, files, publisher_wallet, wait_for_aqua - ) + metadata = self._default_metadata(name, publisher_wallet) + return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua) @enforce_types - def create_with_default_metadata( - self, - name: str, - files: list, - publisher_wallet, - wait_for_aqua: bool = True, - ) -> tuple: - """Thin wrapper for create(). Creates 1 datatoken, with good defaults. - - If wait_for_aqua, then attempt to update aquarius within time constraints. - - Returns (data_nft, datatoken, ddo) - """ + def _default_metadata(self, name: str, publisher_wallet, type="dataset") -> dict: date_created = datetime.now().isoformat() metadata = { "created": date_created, "updated": date_created, "description": name, "name": name, - "type": "dataset", + "type": type, "author": publisher_wallet.address[:7], "license": "CC0: PublicDomain", } + return metadata + @enforce_types + def _create_1dt(self, metadata, files, publisher_wallet, wait_for_aqua): + """Call create(), focusing on just one datatoken""" + name = metadata["name"] (data_nft, datatokens, ddo) = self.create( metadata, publisher_wallet, diff --git a/ocean_lib/ocean/test/test_ocean_assets.py b/ocean_lib/ocean/test/test_ocean_assets.py index e5df54b91..4dfd038dc 100644 --- a/ocean_lib/ocean/test/test_ocean_assets.py +++ b/ocean_lib/ocean/test/test_ocean_assets.py @@ -608,3 +608,17 @@ def test_asset_creation_errors(publisher_ocean, publisher_wallet, config): deployed_datatokens=[datatoken], encrypt_flag=True, ) + + +@pytest.mark.integration +def test_create_algo_asset(publisher_ocean, publisher_wallet): + ocean = publisher_ocean + + name = "Branin dataset" + url = "https://raw.githubusercontent.com/oceanprotocol/c2d-examples/main/branin_and_gpr/gpr.py" + (data_nft, datatoken, ddo) = ocean.assets.create_algo_asset( + name, url, publisher_wallet + ) + + assert ddo.nft["name"] == name # thorough testing is below, on create() directly + assert len(ddo.datatokens) == 1