From 2dd986e349cb4da6b609f0473db082d308127ab8 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Mon, 18 Jul 2022 06:48:17 +0000 Subject: [PATCH] minor: Changed tokenURI retrieving logic --- --- a/pallets/nonfungible/src/erc.rs +++ b/pallets/nonfungible/src/erc.rs @@ -39,6 +39,7 @@ use pallet_evm::{account::CrossAccountId, PrecompileHandle}; use pallet_evm_coder_substrate::call; use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _}; +use alloc::string::ToString; use crate::{ AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted, @@ -218,21 +219,35 @@ /// @return token's const_metadata #[solidity(rename_selector = "tokenURI")] fn token_uri(&self, token_id: uint256) -> Result { - let key = token_uri_key(); - if !has_token_permission::(self.id, &key) { - return Err("No tokenURI permission".into()); + let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?; + + if let Ok(shema_name) = get_token_property(self, token_id, &schema_name_key()) { + if shema_name != "ERC721" { + return Ok("".into()); + } + } else { + return Ok("".into()); } - self.consume_store_reads(1)?; - let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?; + if let Ok(url) = get_token_property(self, token_id, &u_key()) { + if !url.is_empty() { + return Ok(url); + } + } + + if let Ok(base_uri) = get_token_property(self, token_id, &base_uri_key()) { + if !base_uri.is_empty() { + if let Ok(suffix) = get_token_property(self, token_id, &s_key()) { + if !suffix.is_empty() { + return Ok(base_uri + suffix.as_str()); + } + } - let properties = >::try_get((self.id, token_id)) - .map_err(|_| Error::Revert("Token properties not found".into()))?; - if let Some(property) = properties.get(&key) { - return Ok(string::from_utf8_lossy(property).into()); + return Ok(base_uri + token_id.to_string().as_str()); + } } - Err("Property tokenURI not found".into()) + Ok("".into()) } } @@ -520,6 +535,17 @@ } } +fn get_token_property(collection: &CollectionHandle, token_id: u32, key: &up_data_structs::PropertyKey) -> Result { + collection.consume_store_reads(1)?; + let properties = >::try_get((collection.id, token_id)) + .map_err(|_| Error::Revert("Token properties not found".into()))?; + if let Some(property) = properties.get(key) { + return Ok(string::from_utf8_lossy(property).into()); + } + + Err("Property tokenURI not found".into()) +} + fn get_token_permission( collection_id: CollectionId, key: &PropertyKey, --- a/pallets/unique/src/eth/mod.rs +++ b/pallets/unique/src/eth/mod.rs @@ -91,7 +91,7 @@ base_uri_value: PropertyValue, add_properties: bool, ) -> Result> { - let mut collection_properties = up_data_structs::CollectionPropertiesVec::default(); + let mut properties = up_data_structs::CollectionPropertiesVec::default(); let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default(); @@ -129,7 +129,7 @@ }) .map_err(|e| Error::Revert(format!("{:?}", e)))?; - collection_properties + properties .try_push(up_data_structs::Property { key: schema_name_key(), value: erc721_value(), @@ -137,7 +137,7 @@ .map_err(|e| Error::Revert(format!("{:?}", e)))?; if !base_uri_value.is_empty() { - collection_properties + properties .try_push(up_data_structs::Property { key: base_uri_key(), value: base_uri_value, @@ -152,6 +152,7 @@ description, token_prefix, token_property_permissions, + properties, ..Default::default() }; Ok(data) -- gitstuff