From c000c1d41bc42c084fb8a0945f18cf298ea3078c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83lina=20Cenan?= Date: Wed, 4 Jan 2023 11:03:47 +0200 Subject: [PATCH] Adds a correct default token uri. (#1225) * Adds a correct default token uri. * Adds default token uri function pending DID. --- ocean_lib/models/data_nft.py | 16 +++++++++++++++- ocean_lib/models/test/test_data_nft.py | 11 ++++++++++- .../web3_internal/test/test_contract_base.py | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ocean_lib/models/data_nft.py b/ocean_lib/models/data_nft.py index a89a56482..2f60e8756 100644 --- a/ocean_lib/models/data_nft.py +++ b/ocean_lib/models/data_nft.py @@ -2,7 +2,9 @@ # Copyright 2022 Ocean Protocol Foundation # SPDX-License-Identifier: Apache-2.0 # +import json import warnings +from base64 import b64encode from enum import IntEnum, IntFlag from typing import Optional @@ -80,10 +82,22 @@ def __init__( additional_datatoken_deployer or ZERO_ADDRESS ) self.additional_metadata_updater = additional_metadata_updater or ZERO_ADDRESS - self.uri = uri or "https://oceanprotocol.com/nft/" + self.uri = uri or self.get_default_token_uri() self.transferable = transferable or True self.owner = owner + def get_default_token_uri(self): + data = { + "name": self.name, + "symbol": self.symbol, + "background_color": "141414", + "image_data": "data:image/svg+xml,%3Csvg viewBox='0 0 99 99' fill='undefined' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%23ff409277' d='M0,99L0,29C9,24 19,19 31,19C42,18 55,23 67,25C78,26 88,23 99,21L99,99Z'/%3E%3Cpath fill='%23ff4092bb' d='M0,99L0,43C9,45 18,47 30,48C41,48 54,46 66,45C77,43 88,43 99,43L99,99Z'%3E%3C/path%3E%3Cpath fill='%23ff4092ff' d='M0,99L0,78C10,75 20,72 31,71C41,69 53,69 65,70C76,70 87,72 99,74L99,99Z'%3E%3C/path%3E%3C/svg%3E", + } + + return b"data:application/json;base64," + b64encode( + json.dumps(data, separators=(",", ":")).encode("utf-8") + ) + def deploy_contract(self, config_dict, tx_dict) -> DataNFT: from ocean_lib.models.data_nft_factory import ( # isort:skip DataNFTFactoryContract, diff --git a/ocean_lib/models/test/test_data_nft.py b/ocean_lib/models/test/test_data_nft.py index 36d36aa1e..0c4e52c91 100644 --- a/ocean_lib/models/test/test_data_nft.py +++ b/ocean_lib/models/test/test_data_nft.py @@ -2,6 +2,9 @@ # Copyright 2022 Ocean Protocol Foundation # SPDX-License-Identifier: Apache-2.0 # +import json +from base64 import b64decode + import pytest from web3 import Web3 @@ -32,7 +35,13 @@ def test_permissions( data_nft.addManager(consumer_wallet.address, {"from": publisher_wallet}) assert data_nft.getPermissions(consumer_wallet.address)[DataNFTPermissions.MANAGER] - assert data_nft.tokenURI(1) == "https://oceanprotocol.com/nft/" + token_uri = data_nft.tokenURI(1).replace("data:application/json;base64,", "") + decoded_token_uri = json.loads(b64decode(token_uri)) + + assert decoded_token_uri["name"] == "NFT" + assert decoded_token_uri["symbol"] == "NFTSYMBOL" + assert decoded_token_uri["background_color"] == "141414" + assert decoded_token_uri["image_data"].startswith("data:image/svg+xm") # Tests failing clearing permissions with pytest.raises(Exception, match="not NFTOwner"): diff --git a/ocean_lib/web3_internal/test/test_contract_base.py b/ocean_lib/web3_internal/test/test_contract_base.py index b6b14078f..e5ffaf899 100644 --- a/ocean_lib/web3_internal/test/test_contract_base.py +++ b/ocean_lib/web3_internal/test/test_contract_base.py @@ -34,7 +34,7 @@ def test_main(config): 1, ZERO_ADDRESS, ZERO_ADDRESS, - "https://oceanprotocol.com/nft/", + "http://someurl", True, alice_wallet.address, {"from": alice_wallet},