1use super::*;23pub fn last_collection_idx<T: Config>() -> Result<RmrkCollectionId, DispatchError> {4 Ok(<Pallet<T>>::last_collection_idx())5}67pub fn collection_by_id<T: Config>(8 collection_id: RmrkCollectionId,9) -> Result<Option<RmrkCollectionInfo<T::AccountId>>, DispatchError> {10 let (collection, collection_id) = match <Pallet<T>>::get_typed_nft_collection_mapped(11 collection_id,12 misc::CollectionType::Regular,13 ) {14 Ok(c) => c,15 Err(_) => return Ok(None),16 };1718 let nfts_count = collection.total_supply();1920 Ok(Some(RmrkCollectionInfo {21 issuer: collection.owner.clone(),22 metadata: <Pallet<T>>::get_collection_property_decoded(23 collection_id,24 RmrkProperty::Metadata,25 )?,26 max: collection.limits.token_limit,27 symbol: <Pallet<T>>::rebind(&collection.token_prefix)?,28 nfts_count,29 }))30}3132pub fn nft_by_id<T: Config>(33 collection_id: RmrkCollectionId,34 nft_by_id: RmrkNftId,35) -> Result<Option<RmrkInstanceInfo<T::AccountId>>, DispatchError> {36 let (collection, collection_id) = match <Pallet<T>>::get_typed_nft_collection_mapped(37 collection_id,38 misc::CollectionType::Regular,39 ) {40 Ok(c) => c,41 Err(_) => return Ok(None),42 };4344 let nft_id = TokenId(nft_by_id);45 if !<Pallet<T>>::nft_exists(collection_id, nft_id) {46 return Ok(None);47 }4849 let owner = match collection.token_owner(nft_id) {50 Some(owner) => match T::CrossTokenAddressMapping::address_to_token(&owner) {51 Some((col, tok)) => {52 let rmrk_collection = <Pallet<T>>::rmrk_collection_id(col)?;5354 RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(rmrk_collection, tok.0)55 }56 None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone()),57 },58 None => return Ok(None),59 };6061 Ok(Some(RmrkInstanceInfo {62 owner: owner,63 royalty: <Pallet<T>>::get_nft_property_decoded(64 collection_id,65 nft_id,66 RmrkProperty::RoyaltyInfo,67 )?,68 metadata: <Pallet<T>>::get_nft_property_decoded(69 collection_id,70 nft_id,71 RmrkProperty::Metadata,72 )?,73 equipped: <Pallet<T>>::get_nft_property_decoded(74 collection_id,75 nft_id,76 RmrkProperty::Equipped,77 )?,78 pending: <Pallet<T>>::get_nft_property_decoded(79 collection_id,80 nft_id,81 RmrkProperty::PendingNftAccept,82 )?,83 }))84}8586pub fn account_tokens<T: Config>(87 account_id: T::AccountId,88 collection_id: RmrkCollectionId,89) -> Result<Vec<RmrkNftId>, DispatchError> {90 let cross_account_id = CrossAccountId::from_sub(account_id);9192 let (collection, collection_id) = match <Pallet<T>>::get_typed_nft_collection_mapped(93 collection_id,94 misc::CollectionType::Regular,95 ) {96 Ok(c) => c,97 Err(_) => return Ok(Vec::new()),98 };99100 let tokens = collection101 .account_tokens(cross_account_id)102 .into_iter()103 .filter(|token| {104 let is_pending = <Pallet<T>>::get_nft_property_decoded(105 collection_id,106 *token,107 RmrkProperty::PendingNftAccept,108 )109 .unwrap_or(true);110111 !is_pending112 })113 .map(|token| token.0)114 .collect();115116 Ok(tokens)117}118119pub fn nft_children<T: Config>(120 collection_id: RmrkCollectionId,121 nft_id: RmrkNftId,122) -> Result<Vec<RmrkNftChild>, DispatchError> {123 let collection_id = match <Pallet<T>>::unique_collection_id(collection_id) {124 Ok(id) => id,125 Err(_) => return Ok(Vec::new()),126 };127 let nft_id = TokenId(nft_id);128 if !<Pallet<T>>::nft_exists(collection_id, nft_id) {129 return Ok(Vec::new());130 }131132 Ok(133 pallet_nonfungible::TokenChildren::<T>::iter_prefix((collection_id, nft_id))134 .filter_map(|((child_collection, child_token), _)| {135 let is_pending = <Pallet<T>>::get_nft_property_decoded(136 child_collection,137 child_token,138 RmrkProperty::PendingNftAccept,139 )140 .ok()?;141142 if is_pending {143 return None;144 }145146 let rmrk_child_collection =147 <Pallet<T>>::rmrk_collection_id(child_collection).ok()?;148149 Some(RmrkNftChild {150 collection_id: rmrk_child_collection,151 nft_id: child_token.0,152 })153 })154 .collect(),155 )156}157158pub fn collection_properties<T: Config>(159 collection_id: RmrkCollectionId,160 filter_keys: Option<Vec<RmrkPropertyKey>>,161) -> Result<Vec<RmrkPropertyInfo>, DispatchError> {162 let collection_id = match <Pallet<T>>::unique_collection_id(collection_id) {163 Ok(id) => id,164 Err(_) => return Ok(Vec::new()),165 };166 if <Pallet<T>>::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() {167 return Ok(Vec::new());168 }169170 let properties = <Pallet<T>>::filter_user_properties(171 collection_id,172 None,173 filter_keys,174 |key, value| RmrkPropertyInfo { key, value },175 )?;176177 Ok(properties)178}179180pub fn nft_properties<T: Config>(181 collection_id: RmrkCollectionId,182 nft_id: RmrkNftId,183 filter_keys: Option<Vec<RmrkPropertyKey>>,184) -> Result<Vec<RmrkPropertyInfo>, DispatchError> {185 let collection_id = match <Pallet<T>>::unique_collection_id(collection_id) {186 Ok(id) => id,187 Err(_) => return Ok(Vec::new()),188 };189 let token_id = TokenId(nft_id);190191 if <Pallet<T>>::ensure_nft_type(collection_id, token_id, NftType::Regular).is_err() {192 return Ok(Vec::new());193 }194195 let properties = <Pallet<T>>::filter_user_properties(196 collection_id,197 Some(token_id),198 filter_keys,199 |key, value| RmrkPropertyInfo { key, value },200 )?;201202 Ok(properties)203}204205pub fn nft_resources<T: Config>(206 collection_id: RmrkCollectionId,207 nft_id: RmrkNftId,208) -> Result<Vec<RmrkResourceInfo>, DispatchError> {209 let collection_id = match <Pallet<T>>::unique_collection_id(collection_id) {210 Ok(id) => id,211 Err(_) => return Ok(Vec::new()),212 };213 if <Pallet<T>>::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() {214 return Ok(Vec::new());215 }216217 let nft_id = TokenId(nft_id);218 if <Pallet<T>>::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() {219 return Ok(Vec::new());220 }221222 let resources = <pallet_nonfungible::Pallet<T>>::iterate_token_aux_properties(223 collection_id,224 nft_id,225 PropertyScope::Rmrk,226 )227 .filter_map(|(_, value)| {228 let resource_info: RmrkResourceInfo = <Pallet<T>>::decode_property(&value).ok()?;229230 Some(resource_info)231 })232 .collect();233234 Ok(resources)235}236237pub fn nft_resource_priority<T: Config>(238 collection_id: RmrkCollectionId,239 nft_id: RmrkNftId,240 resource_id: RmrkResourceId,241) -> Result<Option<u32>, DispatchError> {242 let collection_id = match <Pallet<T>>::unique_collection_id(collection_id) {243 Ok(id) => id,244 Err(_) => return Ok(None),245 };246 if <Pallet<T>>::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() {247 return Ok(None);248 }249250 let nft_id = TokenId(nft_id);251 if <Pallet<T>>::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() {252 return Ok(None);253 }254255 let priorities: Vec<_> = <Pallet<T>>::get_nft_property_decoded(256 collection_id,257 nft_id,258 RmrkProperty::ResourcePriorities,259 )?;260 Ok(priorities261 .into_iter()262 .enumerate()263 .find(|(_, id)| *id == resource_id)264 .map(|(priority, _): (usize, RmrkResourceId)| priority as u32))265}