git.delta.rocks / unique-network / refs/commits / 2dd986e349cb

difftreelog

minor: Changed tokenURI retrieving logic

Trubnikov Sergey2022-07-18parent: #fd07d81.patch.diff
in: master

2 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- 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<string> {
-		let key = token_uri_key();
-		if !has_token_permission::<T>(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 = <TokenProperties<T>>::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<T: Config>(collection: &CollectionHandle<T>, token_id: u32, key: &up_data_structs::PropertyKey) -> Result<string> {
+	collection.consume_store_reads(1)?;
+	let properties = <TokenProperties<T>>::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<T: Config>(
 	collection_id: CollectionId,
 	key: &PropertyKey,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
91 base_uri_value: PropertyValue,91 base_uri_value: PropertyValue,
92 add_properties: bool,92 add_properties: bool,
93) -> Result<CreateCollectionData<T::AccountId>> {93) -> Result<CreateCollectionData<T::AccountId>> {
94 let mut collection_properties = up_data_structs::CollectionPropertiesVec::default();94 let mut properties = up_data_structs::CollectionPropertiesVec::default();
95 let mut token_property_permissions =95 let mut token_property_permissions =
96 up_data_structs::CollectionPropertiesPermissionsVec::default();96 up_data_structs::CollectionPropertiesPermissionsVec::default();
9797
129 })129 })
130 .map_err(|e| Error::Revert(format!("{:?}", e)))?;130 .map_err(|e| Error::Revert(format!("{:?}", e)))?;
131131
132 collection_properties132 properties
133 .try_push(up_data_structs::Property {133 .try_push(up_data_structs::Property {
134 key: schema_name_key(),134 key: schema_name_key(),
135 value: erc721_value(),135 value: erc721_value(),
136 })136 })
137 .map_err(|e| Error::Revert(format!("{:?}", e)))?;137 .map_err(|e| Error::Revert(format!("{:?}", e)))?;
138138
139 if !base_uri_value.is_empty() {139 if !base_uri_value.is_empty() {
140 collection_properties140 properties
141 .try_push(up_data_structs::Property {141 .try_push(up_data_structs::Property {
142 key: base_uri_key(),142 key: base_uri_key(),
143 value: base_uri_value,143 value: base_uri_value,
152 description,152 description,
153 token_prefix,153 token_prefix,
154 token_property_permissions,154 token_property_permissions,
155 properties,
155 ..Default::default()156 ..Default::default()
156 };157 };
157 Ok(data)158 Ok(data)