12345678910111213141516171819use super::*;20use pallet_rmrk_core::{misc, property::*};21use sp_std::vec::Vec;222324pub fn base<T: Config>(25 base_id: RmrkBaseId,26) -> Result<Option<RmrkBaseInfo<T::AccountId>>, DispatchError> {27 let (collection, collection_id) =28 match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)29 {30 Ok(c) => c,31 Err(_) => return Ok(None),32 };3334 Ok(Some(RmrkBaseInfo {35 issuer: collection.owner.clone(),36 base_type: <PalletCore<T>>::get_collection_property_decoded(37 collection_id,38 RmrkProperty::BaseType,39 )?,40 symbol: <PalletCore<T>>::rebind(&collection.token_prefix)?,41 }))42}434445pub fn base_parts<T: Config>(base_id: RmrkBaseId) -> Result<Vec<RmrkPartType>, DispatchError> {46 use pallet_common::CommonCollectionOperations;4748 let (collection, collection_id) =49 match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)50 {51 Ok(c) => c,52 Err(_) => return Ok(Vec::new()),53 };5455 let parts = collection56 .collection_tokens()57 .into_iter()58 .filter_map(|token_id| {59 let nft_type = <PalletCore<T>>::get_nft_type(collection_id, token_id).ok()?;6061 match nft_type {62 NftType::FixedPart => Some(RmrkPartType::FixedPart(RmrkFixedPart {63 id: <PalletCore<T>>::get_nft_property_decoded(64 collection_id,65 token_id,66 RmrkProperty::ExternalPartId,67 )68 .ok()?,69 src: <PalletCore<T>>::get_nft_property_decoded(70 collection_id,71 token_id,72 RmrkProperty::Src,73 )74 .ok()?,75 z: <PalletCore<T>>::get_nft_property_decoded(76 collection_id,77 token_id,78 RmrkProperty::ZIndex,79 )80 .ok()?,81 })),82 NftType::SlotPart => Some(RmrkPartType::SlotPart(RmrkSlotPart {83 id: <PalletCore<T>>::get_nft_property_decoded(84 collection_id,85 token_id,86 RmrkProperty::ExternalPartId,87 )88 .ok()?,89 src: <PalletCore<T>>::get_nft_property_decoded(90 collection_id,91 token_id,92 RmrkProperty::Src,93 )94 .ok()?,95 z: <PalletCore<T>>::get_nft_property_decoded(96 collection_id,97 token_id,98 RmrkProperty::ZIndex,99 )100 .ok()?,101 equippable: <PalletCore<T>>::get_nft_property_decoded(102 collection_id,103 token_id,104 RmrkProperty::EquippableList,105 )106 .ok()?,107 })),108 _ => None,109 }110 })111 .collect();112113 Ok(parts)114}115116117pub fn theme_names<T: Config>(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {118 use pallet_common::CommonCollectionOperations;119120 let (collection, collection_id) =121 match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)122 {123 Ok(c) => c,124 Err(_) => return Ok(Vec::new()),125 };126127 let theme_names = collection128 .collection_tokens()129 .iter()130 .filter_map(|token_id| {131 let nft_type = <PalletCore<T>>::get_nft_type(collection_id, *token_id).ok()?;132133 match nft_type {134 NftType::Theme => <PalletCore<T>>::get_nft_property_decoded(135 collection_id,136 *token_id,137 RmrkProperty::ThemeName,138 )139 .ok(),140 _ => None,141 }142 })143 .collect();144145 Ok(theme_names)146}147148149pub fn theme<T: Config>(150 base_id: RmrkBaseId,151 theme_name: RmrkThemeName,152 filter_keys: Option<Vec<RmrkPropertyKey>>,153) -> Result<Option<RmrkTheme>, DispatchError> {154 use pallet_common::CommonCollectionOperations;155156 let (collection, collection_id) =157 match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)158 {159 Ok(c) => c,160 Err(_) => return Ok(None),161 };162163 let theme_info = collection164 .collection_tokens()165 .into_iter()166 .find_map(|token_id| {167 <PalletCore<T>>::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?;168169 let name: RmrkString = <PalletCore<T>>::get_nft_property_decoded(170 collection_id,171 token_id,172 RmrkProperty::ThemeName,173 )174 .ok()?;175176 if name == theme_name {177 Some((name, token_id))178 } else {179 None180 }181 });182183 let (name, theme_id) = match theme_info {184 Some((name, theme_id)) => (name, theme_id),185 None => return Ok(None),186 };187188 let properties = <PalletCore<T>>::filter_user_properties(189 collection_id,190 Some(theme_id),191 filter_keys,192 |key, value| RmrkThemeProperty { key, value },193 )?;194195 let inherit = <PalletCore<T>>::get_nft_property_decoded(196 collection_id,197 theme_id,198 RmrkProperty::ThemeInherit,199 )?;200201 let theme = RmrkTheme {202 name,203 properties,204 inherit,205 };206207 Ok(Some(theme))208}