From 960c1089697d2401949db63a99186e47d488f23b Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Fri, 17 Jun 2022 14:35:21 +0000 Subject: [PATCH] refactor: move RMRK rpc impl to proxy pallets --- --- a/pallets/proxy-rmrk-core/src/lib.rs +++ b/pallets/proxy-rmrk-core/src/lib.rs @@ -35,6 +35,7 @@ pub mod benchmarking; pub mod misc; pub mod property; +pub mod rpc; pub mod weights; pub type SelfWeightOf = ::WeightInfo; --- /dev/null +++ b/pallets/proxy-rmrk-core/src/rpc.rs @@ -0,0 +1,265 @@ +use super::*; + +pub fn last_collection_idx() -> Result { + Ok(>::last_collection_idx()) +} + +pub fn collection_by_id( + collection_id: RmrkCollectionId, +) -> Result>, DispatchError> { + let (collection, collection_id) = match >::get_typed_nft_collection_mapped( + collection_id, + misc::CollectionType::Regular, + ) { + Ok(c) => c, + Err(_) => return Ok(None), + }; + + let nfts_count = collection.total_supply(); + + Ok(Some(RmrkCollectionInfo { + issuer: collection.owner.clone(), + metadata: >::get_collection_property_decoded( + collection_id, + RmrkProperty::Metadata, + )?, + max: collection.limits.token_limit, + symbol: >::rebind(&collection.token_prefix)?, + nfts_count, + })) +} + +pub fn nft_by_id( + collection_id: RmrkCollectionId, + nft_by_id: RmrkNftId, +) -> Result>, DispatchError> { + let (collection, collection_id) = match >::get_typed_nft_collection_mapped( + collection_id, + misc::CollectionType::Regular, + ) { + Ok(c) => c, + Err(_) => return Ok(None), + }; + + let nft_id = TokenId(nft_by_id); + if !>::nft_exists(collection_id, nft_id) { + return Ok(None); + } + + let owner = match collection.token_owner(nft_id) { + Some(owner) => match T::CrossTokenAddressMapping::address_to_token(&owner) { + Some((col, tok)) => { + let rmrk_collection = >::rmrk_collection_id(col)?; + + RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(rmrk_collection, tok.0) + } + None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone()), + }, + None => return Ok(None), + }; + + Ok(Some(RmrkInstanceInfo { + owner: owner, + royalty: >::get_nft_property_decoded( + collection_id, + nft_id, + RmrkProperty::RoyaltyInfo, + )?, + metadata: >::get_nft_property_decoded( + collection_id, + nft_id, + RmrkProperty::Metadata, + )?, + equipped: >::get_nft_property_decoded( + collection_id, + nft_id, + RmrkProperty::Equipped, + )?, + pending: >::get_nft_property_decoded( + collection_id, + nft_id, + RmrkProperty::PendingNftAccept, + )?, + })) +} + +pub fn account_tokens( + account_id: T::AccountId, + collection_id: RmrkCollectionId, +) -> Result, DispatchError> { + let cross_account_id = CrossAccountId::from_sub(account_id); + + let (collection, collection_id) = match >::get_typed_nft_collection_mapped( + collection_id, + misc::CollectionType::Regular, + ) { + Ok(c) => c, + Err(_) => return Ok(Vec::new()), + }; + + let tokens = collection + .account_tokens(cross_account_id) + .into_iter() + .filter(|token| { + let is_pending = >::get_nft_property_decoded( + collection_id, + *token, + RmrkProperty::PendingNftAccept, + ) + .unwrap_or(true); + + !is_pending + }) + .map(|token| token.0) + .collect(); + + Ok(tokens) +} + +pub fn nft_children( + collection_id: RmrkCollectionId, + nft_id: RmrkNftId, +) -> Result, DispatchError> { + let collection_id = match >::unique_collection_id(collection_id) { + Ok(id) => id, + Err(_) => return Ok(Vec::new()), + }; + let nft_id = TokenId(nft_id); + if !>::nft_exists(collection_id, nft_id) { + return Ok(Vec::new()); + } + + Ok( + pallet_nonfungible::TokenChildren::::iter_prefix((collection_id, nft_id)) + .filter_map(|((child_collection, child_token), _)| { + let is_pending = >::get_nft_property_decoded( + child_collection, + child_token, + RmrkProperty::PendingNftAccept, + ) + .ok()?; + + if is_pending { + return None; + } + + let rmrk_child_collection = + >::rmrk_collection_id(child_collection).ok()?; + + Some(RmrkNftChild { + collection_id: rmrk_child_collection, + nft_id: child_token.0, + }) + }) + .collect(), + ) +} + +pub fn collection_properties( + collection_id: RmrkCollectionId, + filter_keys: Option>, +) -> Result, DispatchError> { + let collection_id = match >::unique_collection_id(collection_id) { + Ok(id) => id, + Err(_) => return Ok(Vec::new()), + }; + if >::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() { + return Ok(Vec::new()); + } + + let properties = >::filter_user_properties( + collection_id, + /* token_id = */ None, + filter_keys, + |key, value| RmrkPropertyInfo { key, value }, + )?; + + Ok(properties) +} + +pub fn nft_properties( + collection_id: RmrkCollectionId, + nft_id: RmrkNftId, + filter_keys: Option>, +) -> Result, DispatchError> { + let collection_id = match >::unique_collection_id(collection_id) { + Ok(id) => id, + Err(_) => return Ok(Vec::new()), + }; + let token_id = TokenId(nft_id); + + if >::ensure_nft_type(collection_id, token_id, NftType::Regular).is_err() { + return Ok(Vec::new()); + } + + let properties = >::filter_user_properties( + collection_id, + Some(token_id), + filter_keys, + |key, value| RmrkPropertyInfo { key, value }, + )?; + + Ok(properties) +} + +pub fn nft_resources( + collection_id: RmrkCollectionId, + nft_id: RmrkNftId, +) -> Result, DispatchError> { + let collection_id = match >::unique_collection_id(collection_id) { + Ok(id) => id, + Err(_) => return Ok(Vec::new()), + }; + if >::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() { + return Ok(Vec::new()); + } + + let nft_id = TokenId(nft_id); + if >::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { + return Ok(Vec::new()); + } + + let resources = >::iterate_token_aux_properties( + collection_id, + nft_id, + PropertyScope::Rmrk, + ) + .filter_map(|(_, value)| { + let resource_info: RmrkResourceInfo = >::decode_property(&value).ok()?; + + Some(resource_info) + }) + .collect(); + + Ok(resources) +} + +pub fn nft_resource_priority( + collection_id: RmrkCollectionId, + nft_id: RmrkNftId, + resource_id: RmrkResourceId, +) -> Result, DispatchError> { + let collection_id = match >::unique_collection_id(collection_id) { + Ok(id) => id, + Err(_) => return Ok(None), + }; + if >::ensure_collection_type(collection_id, misc::CollectionType::Regular).is_err() { + return Ok(None); + } + + let nft_id = TokenId(nft_id); + if >::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { + return Ok(None); + } + + let priorities: Vec<_> = >::get_nft_property_decoded( + collection_id, + nft_id, + RmrkProperty::ResourcePriorities, + )?; + Ok(priorities + .into_iter() + .enumerate() + .find(|(_, id)| *id == resource_id) + .map(|(priority, _): (usize, RmrkResourceId)| priority as u32)) +} --- a/pallets/proxy-rmrk-equip/src/lib.rs +++ b/pallets/proxy-rmrk-equip/src/lib.rs @@ -34,6 +34,7 @@ #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; +pub mod rpc; pub mod weights; pub type SelfWeightOf = ::WeightInfo; --- /dev/null +++ b/pallets/proxy-rmrk-equip/src/rpc.rs @@ -0,0 +1,186 @@ +use super::*; +use pallet_rmrk_core::{misc, property::*}; +use sp_std::vec::Vec; + +pub fn base( + base_id: RmrkBaseId, +) -> Result>, DispatchError> { + let (collection, collection_id) = + match >::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base) + { + Ok(c) => c, + Err(_) => return Ok(None), + }; + + Ok(Some(RmrkBaseInfo { + issuer: collection.owner.clone(), + base_type: >::get_collection_property_decoded( + collection_id, + RmrkProperty::BaseType, + )?, + symbol: >::rebind(&collection.token_prefix)?, + })) +} + +pub fn base_parts(base_id: RmrkBaseId) -> Result, DispatchError> { + use pallet_common::CommonCollectionOperations; + + let (collection, collection_id) = + match >::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base) + { + Ok(c) => c, + Err(_) => return Ok(Vec::new()), + }; + + let parts = collection + .collection_tokens() + .into_iter() + .filter_map(|token_id| { + let nft_type = >::get_nft_type(collection_id, token_id).ok()?; + + match nft_type { + NftType::FixedPart => Some(RmrkPartType::FixedPart(RmrkFixedPart { + id: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::ExternalPartId, + ) + .ok()?, + src: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::Src, + ) + .ok()?, + z: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::ZIndex, + ) + .ok()?, + })), + NftType::SlotPart => Some(RmrkPartType::SlotPart(RmrkSlotPart { + id: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::ExternalPartId, + ) + .ok()?, + src: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::Src, + ) + .ok()?, + z: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::ZIndex, + ) + .ok()?, + equippable: >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::EquippableList, + ) + .ok()?, + })), + _ => None, + } + }) + .collect(); + + Ok(parts) +} + +pub fn theme_names(base_id: RmrkBaseId) -> Result, DispatchError> { + use pallet_common::CommonCollectionOperations; + + let (collection, collection_id) = + match >::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base) + { + Ok(c) => c, + Err(_) => return Ok(Vec::new()), + }; + + let theme_names = collection + .collection_tokens() + .iter() + .filter_map(|token_id| { + let nft_type = >::get_nft_type(collection_id, *token_id).ok()?; + + match nft_type { + NftType::Theme => >::get_nft_property_decoded( + collection_id, + *token_id, + RmrkProperty::ThemeName, + ) + .ok(), + _ => None, + } + }) + .collect(); + + Ok(theme_names) +} + +pub fn theme( + base_id: RmrkBaseId, + theme_name: RmrkThemeName, + filter_keys: Option>, +) -> Result, DispatchError> { + use pallet_common::CommonCollectionOperations; + + let (collection, collection_id) = + match >::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base) + { + Ok(c) => c, + Err(_) => return Ok(None), + }; + + let theme_info = collection + .collection_tokens() + .into_iter() + .find_map(|token_id| { + >::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?; + + let name: RmrkString = >::get_nft_property_decoded( + collection_id, + token_id, + RmrkProperty::ThemeName, + ) + .ok()?; + + if name == theme_name { + Some((name, token_id)) + } else { + None + } + }); + + let (name, theme_id) = match theme_info { + Some((name, theme_id)) => (name, theme_id), + None => return Ok(None), + }; + + let properties = >::filter_user_properties( + collection_id, + Some(theme_id), + filter_keys, + |key, value| RmrkThemeProperty { key, value }, + )?; + + let inherit = >::get_nft_property_decoded( + collection_id, + theme_id, + RmrkProperty::ThemeInherit, + )?; + + let theme = RmrkTheme { + name, + properties, + inherit, + }; + + Ok(Some(theme)) +} --- a/runtime/opal/src/lib.rs +++ b/runtime/opal/src/lib.rs @@ -74,9 +74,8 @@ CollectionStats, RpcCollection, mapping::{EvmTokenAddressMapping, CrossTokenAddressMapping}, TokenChild, RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, - RmrkBaseInfo, RmrkPartType, RmrkTheme, RmrkThemeName, RmrkThemeProperty, RmrkCollectionId, - RmrkNftId, RmrkAccountIdOrCollectionNftTuple, RmrkNftChild, RmrkPropertyKey, RmrkResourceId, - RmrkBaseId, RmrkFixedPart, RmrkSlotPart, RmrkString, + RmrkBaseInfo, RmrkPartType, RmrkTheme, RmrkThemeName, RmrkCollectionId, RmrkNftId, + RmrkNftChild, RmrkPropertyKey, RmrkResourceId, RmrkBaseId, }; // use pallet_contracts::weights::WeightInfo; @@ -1330,352 +1329,55 @@ RmrkTheme > for Runtime { fn last_collection_idx() -> Result { - Ok(RmrkCore::last_collection_idx()) + pallet_proxy_rmrk_core::rpc::last_collection_idx::() } fn collection_by_id(collection_id: RmrkCollectionId) -> Result>, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let nfts_count = collection.total_supply(); - - Ok(Some(RmrkCollectionInfo { - issuer: collection.owner.clone(), - metadata: RmrkCore::get_collection_property_decoded(collection_id, RmrkProperty::Metadata)?, - max: collection.limits.token_limit, - symbol: RmrkCore::rebind(&collection.token_prefix)?, - nfts_count - })) + pallet_proxy_rmrk_core::rpc::collection_by_id::(collection_id) } fn nft_by_id(collection_id: RmrkCollectionId, nft_by_id: RmrkNftId) -> Result>, DispatchError> { - use up_data_structs::mapping::TokenAddressMapping; - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let nft_id = TokenId(nft_by_id); - if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(None); } - - let owner = match collection.token_owner(nft_id) { - Some(owner) => match ::CrossTokenAddressMapping::address_to_token(&owner) { - Some((col, tok)) => { - let rmrk_collection = RmrkCore::rmrk_collection_id(col)?; - - RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(rmrk_collection, tok.0) - } - None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone()) - }, - None => return Ok(None) - }; - - Ok(Some(RmrkInstanceInfo { - owner: owner, - royalty: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::RoyaltyInfo)?, - metadata: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Metadata)?, - equipped: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Equipped)?, - pending: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::PendingNftAccept)?, - })) + pallet_proxy_rmrk_core::rpc::nft_by_id::(collection_id, nft_by_id) } fn account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let cross_account_id = CrossAccountId::from_sub(account_id); - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let tokens = collection.account_tokens(cross_account_id) - .into_iter() - .filter(|token| { - let is_pending = RmrkCore::get_nft_property_decoded( - collection_id, - *token, - RmrkProperty::PendingNftAccept - ).unwrap_or(true); - - !is_pending - }) - .map(|token| token.0) - .collect(); - - Ok(tokens) + pallet_proxy_rmrk_core::rpc::account_tokens::(account_id, collection_id) } fn nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::RmrkProperty; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - let nft_id = TokenId(nft_id); - if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(Vec::new()); } - - Ok( - pallet_nonfungible::TokenChildren::::iter_prefix((collection_id, nft_id)) - .filter_map(|((child_collection, child_token), _)| { - let is_pending = RmrkCore::get_nft_property_decoded( - child_collection, - child_token, - RmrkProperty::PendingNftAccept - ).ok()?; - - if is_pending { - return None; - } - - let rmrk_child_collection = RmrkCore::rmrk_collection_id( - child_collection - ).ok()?; - - Some(RmrkNftChild { - collection_id: rmrk_child_collection, - nft_id: child_token.0, - }) - }).collect() - ) + pallet_proxy_rmrk_core::rpc::nft_children::(collection_id, nft_id) } fn collection_properties(collection_id: RmrkCollectionId, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::CollectionType; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { - return Ok(Vec::new()); - } - - let properties = RmrkCore::filter_user_properties( - collection_id, - /* token_id = */ None, - filter_keys, - |key, value| RmrkPropertyInfo { - key, - value - } - )?; - - Ok(properties) + pallet_proxy_rmrk_core::rpc::collection_properties::(collection_id, filter_keys) } fn nft_properties(collection_id: RmrkCollectionId, nft_id: RmrkNftId, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::NftType; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - let token_id = TokenId(nft_id); - - if RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Regular).is_err() { - return Ok(Vec::new()); - } - - let properties = RmrkCore::filter_user_properties( - collection_id, - Some(token_id), - filter_keys, - |key, value| RmrkPropertyInfo { - key, - value - } - )?; - - Ok(properties) + pallet_proxy_rmrk_core::rpc::nft_properties::(collection_id, nft_id, filter_keys) } fn nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::{CollectionType, NftType}; - use up_data_structs::PropertyScope; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(Vec::new()); } - - let nft_id = TokenId(nft_id); - if RmrkCore::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { return Ok(Vec::new()); } - - let resources = >::iterate_token_aux_properties( - collection_id, nft_id, PropertyScope::Rmrk - ).filter_map(|(_, value)| { - let resource_info: RmrkResourceInfo = RmrkCore::decode_property(&value).ok()?; - - Some(resource_info) - }).collect(); - - Ok(resources) + pallet_proxy_rmrk_core::rpc::nft_resources::(collection_id, nft_id) } fn nft_resource_priority(collection_id: RmrkCollectionId, nft_id: RmrkNftId, resource_id: RmrkResourceId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}}; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(None) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(None); } - - let nft_id = TokenId(nft_id); - if RmrkCore::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { return Ok(None); } - - let priorities: Vec<_> = RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::ResourcePriorities)?; - Ok( - priorities.into_iter() - .enumerate() - .find(|(_, id)| *id == resource_id) - .map(|(priority, _): (usize, RmrkResourceId)| priority as u32) - ) + pallet_proxy_rmrk_core::rpc::nft_resource_priority::(collection_id, nft_id, resource_id) } fn base(base_id: RmrkBaseId) -> Result>, DispatchError> { - use pallet_proxy_rmrk_core::{ - RmrkProperty, misc::{CollectionType}, - }; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - Ok(Some(RmrkBaseInfo { - issuer: collection.owner.clone(), - base_type: RmrkCore::get_collection_property_decoded(collection_id, RmrkProperty::BaseType)?, - symbol: RmrkCore::rebind(&collection.token_prefix)?, - })) + pallet_proxy_rmrk_equip::rpc::base::(base_id) } fn base_parts(base_id: RmrkBaseId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let parts = collection.collection_tokens() - .into_iter() - .filter_map(|token_id| { - let nft_type = RmrkCore::get_nft_type(collection_id, token_id).ok()?; - - match nft_type { - NftType::FixedPart => Some(RmrkPartType::FixedPart(RmrkFixedPart { - id: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ExternalPartId).ok()?, - src: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::Src).ok()?, - z: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ZIndex).ok()?, - })), - NftType::SlotPart => Some(RmrkPartType::SlotPart(RmrkSlotPart { - id: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ExternalPartId).ok()?, - src: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::Src).ok()?, - z: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ZIndex).ok()?, - equippable: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::EquippableList).ok()?, - })), - _ => None - } - }) - .collect(); - - Ok(parts) + pallet_proxy_rmrk_equip::rpc::base_parts::(base_id) } fn theme_names(base_id: RmrkBaseId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{NftType, CollectionType}}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let theme_names = collection.collection_tokens() - .iter() - .filter_map(|token_id| { - let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).ok()?; - - match nft_type { - NftType::Theme => RmrkCore::get_nft_property_decoded(collection_id, *token_id, RmrkProperty::ThemeName).ok(), - _ => None - } - }) - .collect(); - - Ok(theme_names) + pallet_proxy_rmrk_equip::rpc::theme_names::(base_id) } fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{ - RmrkProperty, - misc::{CollectionType, NftType} - }; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let theme_info = collection.collection_tokens() - .into_iter() - .find_map(|token_id| { - RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?; - - let name: RmrkString = RmrkCore::get_nft_property_decoded( - collection_id, token_id, RmrkProperty::ThemeName - ).ok()?; - - if name == theme_name { - Some((name, token_id)) - } else { - None - } - }); - - let (name, theme_id) = match theme_info { - Some((name, theme_id)) => (name, theme_id), - None => return Ok(None) - }; - - let properties = RmrkCore::filter_user_properties( - collection_id, - Some(theme_id), - filter_keys, - |key, value| RmrkThemeProperty { - key, - value - } - )?; - - let inherit = RmrkCore::get_nft_property_decoded( - collection_id, - theme_id, - RmrkProperty::ThemeInherit - )?; - - let theme = RmrkTheme { - name, - properties, - inherit, - }; - - Ok(Some(theme)) + pallet_proxy_rmrk_equip::rpc::theme::(base_id, theme_name, filter_keys) } } } --- a/runtime/quartz/src/lib.rs +++ b/runtime/quartz/src/lib.rs @@ -74,9 +74,8 @@ CollectionStats, RpcCollection, mapping::{EvmTokenAddressMapping, CrossTokenAddressMapping}, TokenChild, RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, - RmrkBaseInfo, RmrkPartType, RmrkTheme, RmrkThemeName, RmrkThemeProperty, RmrkCollectionId, - RmrkNftId, RmrkAccountIdOrCollectionNftTuple, RmrkNftChild, RmrkPropertyKey, RmrkResourceId, - RmrkBaseId, RmrkFixedPart, RmrkSlotPart, RmrkString, + RmrkBaseInfo, RmrkPartType, RmrkTheme, RmrkThemeName, RmrkCollectionId, RmrkNftId, + RmrkNftChild, RmrkPropertyKey, RmrkResourceId, RmrkBaseId, }; // use pallet_contracts::weights::WeightInfo; @@ -1330,352 +1329,55 @@ RmrkTheme > for Runtime { fn last_collection_idx() -> Result { - Ok(RmrkCore::last_collection_idx()) + pallet_proxy_rmrk_core::rpc::last_collection_idx::() } fn collection_by_id(collection_id: RmrkCollectionId) -> Result>, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let nfts_count = collection.total_supply(); - - Ok(Some(RmrkCollectionInfo { - issuer: collection.owner.clone(), - metadata: RmrkCore::get_collection_property_decoded(collection_id, RmrkProperty::Metadata)?, - max: collection.limits.token_limit, - symbol: RmrkCore::rebind(&collection.token_prefix)?, - nfts_count - })) + pallet_proxy_rmrk_core::rpc::collection_by_id::(collection_id) } fn nft_by_id(collection_id: RmrkCollectionId, nft_by_id: RmrkNftId) -> Result>, DispatchError> { - use up_data_structs::mapping::TokenAddressMapping; - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let nft_id = TokenId(nft_by_id); - if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(None); } - - let owner = match collection.token_owner(nft_id) { - Some(owner) => match ::CrossTokenAddressMapping::address_to_token(&owner) { - Some((col, tok)) => { - let rmrk_collection = RmrkCore::rmrk_collection_id(col)?; - - RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(rmrk_collection, tok.0) - } - None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone()) - }, - None => return Ok(None) - }; - - Ok(Some(RmrkInstanceInfo { - owner: owner, - royalty: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::RoyaltyInfo)?, - metadata: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Metadata)?, - equipped: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Equipped)?, - pending: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::PendingNftAccept)?, - })) + pallet_proxy_rmrk_core::rpc::nft_by_id::(collection_id, nft_by_id) } fn account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType}; - use pallet_common::CommonCollectionOperations; - - let cross_account_id = CrossAccountId::from_sub(account_id); - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(collection_id, CollectionType::Regular) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let tokens = collection.account_tokens(cross_account_id) - .into_iter() - .filter(|token| { - let is_pending = RmrkCore::get_nft_property_decoded( - collection_id, - *token, - RmrkProperty::PendingNftAccept - ).unwrap_or(true); - - !is_pending - }) - .map(|token| token.0) - .collect(); - - Ok(tokens) + pallet_proxy_rmrk_core::rpc::account_tokens::(account_id, collection_id) } fn nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::RmrkProperty; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - let nft_id = TokenId(nft_id); - if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(Vec::new()); } - - Ok( - pallet_nonfungible::TokenChildren::::iter_prefix((collection_id, nft_id)) - .filter_map(|((child_collection, child_token), _)| { - let is_pending = RmrkCore::get_nft_property_decoded( - child_collection, - child_token, - RmrkProperty::PendingNftAccept - ).ok()?; - - if is_pending { - return None; - } - - let rmrk_child_collection = RmrkCore::rmrk_collection_id( - child_collection - ).ok()?; - - Some(RmrkNftChild { - collection_id: rmrk_child_collection, - nft_id: child_token.0, - }) - }).collect() - ) + pallet_proxy_rmrk_core::rpc::nft_children::(collection_id, nft_id) } fn collection_properties(collection_id: RmrkCollectionId, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::CollectionType; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { - return Ok(Vec::new()); - } - - let properties = RmrkCore::filter_user_properties( - collection_id, - /* token_id = */ None, - filter_keys, - |key, value| RmrkPropertyInfo { - key, - value - } - )?; - - Ok(properties) + pallet_proxy_rmrk_core::rpc::collection_properties::(collection_id, filter_keys) } fn nft_properties(collection_id: RmrkCollectionId, nft_id: RmrkNftId, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::NftType; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - let token_id = TokenId(nft_id); - - if RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Regular).is_err() { - return Ok(Vec::new()); - } - - let properties = RmrkCore::filter_user_properties( - collection_id, - Some(token_id), - filter_keys, - |key, value| RmrkPropertyInfo { - key, - value - } - )?; - - Ok(properties) + pallet_proxy_rmrk_core::rpc::nft_properties::(collection_id, nft_id, filter_keys) } fn nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::misc::{CollectionType, NftType}; - use up_data_structs::PropertyScope; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(Vec::new()) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(Vec::new()); } - - let nft_id = TokenId(nft_id); - if RmrkCore::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { return Ok(Vec::new()); } - - let resources = >::iterate_token_aux_properties( - collection_id, nft_id, PropertyScope::Rmrk - ).filter_map(|(_, value)| { - let resource_info: RmrkResourceInfo = RmrkCore::decode_property(&value).ok()?; - - Some(resource_info) - }).collect(); - - Ok(resources) + pallet_proxy_rmrk_core::rpc::nft_resources::(collection_id, nft_id) } fn nft_resource_priority(collection_id: RmrkCollectionId, nft_id: RmrkNftId, resource_id: RmrkResourceId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}}; - - let collection_id = match RmrkCore::unique_collection_id(collection_id) { - Ok(id) => id, - Err(_) => return Ok(None) - }; - if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(None); } - - let nft_id = TokenId(nft_id); - if RmrkCore::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { return Ok(None); } - - let priorities: Vec<_> = RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::ResourcePriorities)?; - Ok( - priorities.into_iter() - .enumerate() - .find(|(_, id)| *id == resource_id) - .map(|(priority, _): (usize, RmrkResourceId)| priority as u32) - ) + pallet_proxy_rmrk_core::rpc::nft_resource_priority::(collection_id, nft_id, resource_id) } fn base(base_id: RmrkBaseId) -> Result>, DispatchError> { - use pallet_proxy_rmrk_core::{ - RmrkProperty, misc::{CollectionType}, - }; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - Ok(Some(RmrkBaseInfo { - issuer: collection.owner.clone(), - base_type: RmrkCore::get_collection_property_decoded(collection_id, RmrkProperty::BaseType)?, - symbol: RmrkCore::rebind(&collection.token_prefix)?, - })) + pallet_proxy_rmrk_equip::rpc::base::(base_id) } fn base_parts(base_id: RmrkBaseId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let parts = collection.collection_tokens() - .into_iter() - .filter_map(|token_id| { - let nft_type = RmrkCore::get_nft_type(collection_id, token_id).ok()?; - - match nft_type { - NftType::FixedPart => Some(RmrkPartType::FixedPart(RmrkFixedPart { - id: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ExternalPartId).ok()?, - src: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::Src).ok()?, - z: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ZIndex).ok()?, - })), - NftType::SlotPart => Some(RmrkPartType::SlotPart(RmrkSlotPart { - id: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ExternalPartId).ok()?, - src: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::Src).ok()?, - z: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::ZIndex).ok()?, - equippable: RmrkCore::get_nft_property_decoded(collection_id, token_id, RmrkProperty::EquippableList).ok()?, - })), - _ => None - } - }) - .collect(); - - Ok(parts) + pallet_proxy_rmrk_equip::rpc::base_parts::(base_id) } fn theme_names(base_id: RmrkBaseId) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{RmrkProperty, misc::{NftType, CollectionType}}; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(Vec::new()), - }; - - let theme_names = collection.collection_tokens() - .iter() - .filter_map(|token_id| { - let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).ok()?; - - match nft_type { - NftType::Theme => RmrkCore::get_nft_property_decoded(collection_id, *token_id, RmrkProperty::ThemeName).ok(), - _ => None - } - }) - .collect(); - - Ok(theme_names) + pallet_proxy_rmrk_equip::rpc::theme_names::(base_id) } fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option>) -> Result, DispatchError> { - use pallet_proxy_rmrk_core::{ - RmrkProperty, - misc::{CollectionType, NftType} - }; - use pallet_common::CommonCollectionOperations; - - let (collection, collection_id) = match RmrkCore::get_typed_nft_collection_mapped(base_id, CollectionType::Base) { - Ok(c) => c, - Err(_) => return Ok(None), - }; - - let theme_info = collection.collection_tokens() - .into_iter() - .find_map(|token_id| { - RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?; - - let name: RmrkString = RmrkCore::get_nft_property_decoded( - collection_id, token_id, RmrkProperty::ThemeName - ).ok()?; - - if name == theme_name { - Some((name, token_id)) - } else { - None - } - }); - - let (name, theme_id) = match theme_info { - Some((name, theme_id)) => (name, theme_id), - None => return Ok(None) - }; - - let properties = RmrkCore::filter_user_properties( - collection_id, - Some(theme_id), - filter_keys, - |key, value| RmrkThemeProperty { - key, - value - } - )?; - - let inherit = RmrkCore::get_nft_property_decoded( - collection_id, - theme_id, - RmrkProperty::ThemeInherit - )?; - - let theme = RmrkTheme { - name, - properties, - inherit, - }; - - Ok(Some(theme)) + pallet_proxy_rmrk_equip::rpc::theme::(base_id, theme_name, filter_keys) } } } -- gitstuff