difftreelog
minor: Changed tokenURI retrieving logic
in: master
2 files changed
pallets/nonfungible/src/erc.rsdiffbeforeafterboth39use 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;424343use 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_metadata219 #[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 }225226 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")?; 228223229 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 }231232 if let Ok(url) = get_token_property(self, token_id, &u_key()) {233 if !url.is_empty() {234 return Ok(url);235 }236 }237231 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 }245232 return Ok(string::from_utf8_lossy(property).into());246 return Ok(base_uri + token_id.to_string().as_str());247 }233 }248 }234249235 Err("Property tokenURI not found".into())250 Ok("".into())236 }251 }237}252}238253520 }535 }521}536}537538fn 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 }545546 Err("Property tokenURI not found".into())547}522548523fn get_token_permission<T: Config>(549fn get_token_permission<T: Config>(524 collection_id: CollectionId,550 collection_id: CollectionId,pallets/unique/src/eth/mod.rsdiffbeforeafterboth91 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();9797129 })129 })130 .map_err(|e| Error::Revert(format!("{:?}", e)))?;130 .map_err(|e| Error::Revert(format!("{:?}", e)))?;131131132 collection_properties132 properties133 .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)))?;138138139 if !base_uri_value.is_empty() {139 if !base_uri_value.is_empty() {140 collection_properties140 properties141 .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)