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
39use pallet_evm::{account::CrossAccountId, PrecompileHandle};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};
40use pallet_evm_coder_substrate::call;40use pallet_evm_coder_substrate::call;
41use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};41use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
42use alloc::string::ToString;
4243
43use crate::{44use crate::{
44 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,45 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,
218 /// @return token's const_metadata219 /// @return token's const_metadata
219 #[solidity(rename_selector = "tokenURI")]220 #[solidity(rename_selector = "tokenURI")]
220 fn token_uri(&self, token_id: uint256) -> Result<string> {221 fn token_uri(&self, token_id: uint256) -> Result<string> {
221 let key = token_uri_key();
222 if !has_token_permission::<T>(self.id, &key) {
223 return Err("No tokenURI permission".into());
224 }
225
226 self.consume_store_reads(1)?;
227 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;222 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
228223
229 let properties = <TokenProperties<T>>::try_get((self.id, token_id))224 if let Ok(shema_name) = get_token_property(self, token_id, &schema_name_key()) {
225 if shema_name != "ERC721" {
230 .map_err(|_| Error::Revert("Token properties not found".into()))?;226 return Ok("".into());
227 }
228 } else {
229 return Ok("".into());
230 }
231
232 if let Ok(url) = get_token_property(self, token_id, &u_key()) {
233 if !url.is_empty() {
234 return Ok(url);
235 }
236 }
237
231 if let Some(property) = properties.get(&key) {238 if let Ok(base_uri) = get_token_property(self, token_id, &base_uri_key()) {
239 if !base_uri.is_empty() {
240 if let Ok(suffix) = get_token_property(self, token_id, &s_key()) {
241 if !suffix.is_empty() {
242 return Ok(base_uri + suffix.as_str());
243 }
244 }
245
232 return Ok(string::from_utf8_lossy(property).into());246 return Ok(base_uri + token_id.to_string().as_str());
247 }
233 }248 }
234249
235 Err("Property tokenURI not found".into())250 Ok("".into())
236 }251 }
237}252}
238253
520 }535 }
521}536}
537
538fn get_token_property<T: Config>(collection: &CollectionHandle<T>, token_id: u32, key: &up_data_structs::PropertyKey) -> Result<string> {
539 collection.consume_store_reads(1)?;
540 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))
541 .map_err(|_| Error::Revert("Token properties not found".into()))?;
542 if let Some(property) = properties.get(key) {
543 return Ok(string::from_utf8_lossy(property).into());
544 }
545
546 Err("Property tokenURI not found".into())
547}
522548
523fn get_token_permission<T: Config>(549fn get_token_permission<T: Config>(
524 collection_id: CollectionId,550 collection_id: CollectionId,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- 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<CreateCollectionData<T::AccountId>> {
-	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)