difftreelog
refactor(rmrk-rpc) replace dispatch_unique_runtime with direct calls to collections + cleaning
in: master
1 file changed
runtime/common/src/runtime_apis.rsdiffbeforeafterboth144 }144 }145145146 fn collection_by_id(collection_id: RmrkCollectionId) -> Result<Option<RmrkCollectionInfo<AccountId>>, DispatchError> {146 fn collection_by_id(collection_id: RmrkCollectionId) -> Result<Option<RmrkCollectionInfo<AccountId>>, DispatchError> {147 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType}};147 use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType};148 use pallet_common::CommonCollectionOperations;148149149 let collection_id = RmrkCore::unique_collection_id(collection_id)?;150 let collection_id = RmrkCore::unique_collection_id(collection_id)?;150 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Regular) {151 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Regular) {151 Ok(c) => c,152 Ok(c) => c,152 Err(_) => return Ok(None),153 Err(_) => return Ok(None),153 };154 };154155155 // todo replace dispatch... calls with calls to rmrkcore and NFT collection. There's no point trying non-NFT collections156 let nfts_count = dispatch_unique_runtime!(collection_id.total_supply())?;156 let nfts_count = collection.total_supply();157157158 Ok(Some(RmrkCollectionInfo {158 Ok(Some(RmrkCollectionInfo {159 issuer: collection.owner.clone(),159 issuer: collection.owner.clone(),166166167 fn nft_by_id(collection_id: RmrkCollectionId, nft_by_id: RmrkNftId) -> Result<Option<RmrkInstanceInfo<AccountId>>, DispatchError> {167 fn nft_by_id(collection_id: RmrkCollectionId, nft_by_id: RmrkNftId) -> Result<Option<RmrkInstanceInfo<AccountId>>, DispatchError> {168 use up_data_structs::mapping::TokenAddressMapping;168 use up_data_structs::mapping::TokenAddressMapping;169 use pallet_proxy_rmrk_core::{RmrkProperty, misc::RmrkDecode};169 use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType};170 use pallet_common::CommonCollectionOperations;170171171 let collection_id = RmrkCore::unique_collection_id(collection_id)?;172 let collection_id = RmrkCore::unique_collection_id(collection_id)?;173 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Regular) {174 Ok(c) => c,175 Err(_) => return Ok(None),176 };177172 let nft_id = TokenId(nft_by_id);178 let nft_id = TokenId(nft_by_id);173 if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(None); }179 if !RmrkCore::nft_exists(collection_id, nft_id) { return Ok(None); }174180175 // todo replace dispatch with collection176 let owner = match dispatch_unique_runtime!(collection_id.token_owner(nft_id))? {181 let owner = match collection.token_owner(nft_id) {177 Some(owner) => match <Runtime as pallet_common::Config>::CrossTokenAddressMapping::address_to_token(&owner) {182 Some(owner) => match <Runtime as pallet_common::Config>::CrossTokenAddressMapping::address_to_token(&owner) {178 Some((col, tok)) => RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(col.0, tok.0),183 Some((col, tok)) => RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(col.0, tok.0),179 None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone())184 None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone())194199195 fn account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Result<Vec<RmrkNftId>, DispatchError> {200 fn account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Result<Vec<RmrkNftId>, DispatchError> {196 use pallet_proxy_rmrk_core::misc::CollectionType;201 use pallet_proxy_rmrk_core::misc::CollectionType;202 use pallet_common::CommonCollectionOperations;197203198 let cross_account_id = CrossAccountId::from_sub(account_id);204 let cross_account_id = CrossAccountId::from_sub(account_id);199 let collection_id = RmrkCore::unique_collection_id(collection_id)?;205 let collection_id = RmrkCore::unique_collection_id(collection_id)?;200 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(Vec::new()); }206 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Regular) {207 Ok(c) => c,208 Err(_) => return Ok(Vec::new()),209 };201210202 Ok(211 Ok(203 dispatch_unique_runtime!(collection_id.account_tokens(cross_account_id))?212 collection.account_tokens(cross_account_id)204 .into_iter()213 .into_iter()205 .map(|token| token.0)214 .map(|token| token.0)206 .collect()215 .collect()271 }280 }272281273 fn nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result<Vec<RmrkResourceInfo>, DispatchError> {282 fn nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result<Vec<RmrkResourceInfo>, DispatchError> {274 use frame_support::BoundedVec;275 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, ResourceType, RmrkDecode}};283 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, ResourceType}};276 use pallet_common::CommonCollectionOperations;284 use pallet_common::CommonCollectionOperations;277285278 let collection_id = RmrkCore::unique_collection_id(collection_id)?;286 let collection_id = RmrkCore::unique_collection_id(collection_id)?;322 }330 }323331324 fn nft_resource_priorities(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result<Vec<RmrkResourceId>, DispatchError> {332 fn nft_resource_priorities(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result<Vec<RmrkResourceId>, DispatchError> {325 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, RmrkDecode}};333 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}};326334327 let collection_id = RmrkCore::unique_collection_id(collection_id)?;335 let collection_id = RmrkCore::unique_collection_id(collection_id)?;328 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(Vec::new()); }336 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Regular).is_err() { return Ok(Vec::new()); }366 }374 }367375368 fn base_parts(base_id: RmrkBaseId) -> Result<Vec<RmrkPartType>, DispatchError> {376 fn base_parts(base_id: RmrkBaseId) -> Result<Vec<RmrkPartType>, DispatchError> {369 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, RmrkDecode}};377 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType}};378 use pallet_common::CommonCollectionOperations;370379371 let collection_id = RmrkCore::unique_collection_id(base_id)?;380 let collection_id = RmrkCore::unique_collection_id(base_id)?;372 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() { return Ok(Vec::new()); }381 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Base) {382 Ok(c) => c,383 Err(_) => return Ok(Vec::new()),384 };385373386374 let parts = dispatch_unique_runtime!(collection_id.collection_tokens())?387 let parts = collection.collection_tokens()375 .into_iter()388 .into_iter()376 .filter_map(|token_id| {389 .filter_map(|token_id| {377 let nft_type = RmrkCore::get_nft_type(collection_id, token_id).ok()?;390 let nft_type = RmrkCore::get_nft_type(collection_id, token_id).ok()?;397 }410 }398411399 fn theme_names(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {412 fn theme_names(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {400 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, RmrkDecode}};413 use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType};414 use pallet_common::CommonCollectionOperations;401415402 let collection_id = RmrkCore::unique_collection_id(base_id)?;416 let collection_id = RmrkCore::unique_collection_id(base_id)?;403 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() {417 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Base) {418 Ok(c) => c,404 return Ok(Vec::new());419 Err(_) => return Ok(Vec::new()),405 }420 };421406422407 let theme_names = dispatch_unique_runtime!(collection_id.collection_tokens())?423 let theme_names = collection.collection_tokens()408 .iter()424 .iter()409 .filter_map(|token_id| {425 .filter_map(|token_id| {410 let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).unwrap();426 let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).unwrap();424 fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Result<Option<RmrkTheme>, DispatchError> {440 fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Result<Option<RmrkTheme>, DispatchError> {425 use pallet_proxy_rmrk_core::{441 use pallet_proxy_rmrk_core::{426 RmrkProperty,442 RmrkProperty,427 misc::{CollectionType, NftType, RmrkDecode}443 misc::{CollectionType, NftType}428 };444 };445 use pallet_common::CommonCollectionOperations;429446430 let collection_id = RmrkCore::unique_collection_id(base_id)?;447 let collection_id = RmrkCore::unique_collection_id(base_id)?;431 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() {448 let collection = match RmrkCore::get_typed_nft_collection(collection_id, CollectionType::Base) {449 Ok(c) => c,432 return Ok(None);450 Err(_) => return Ok(None),433 }451 };434452435 let theme_info = dispatch_unique_runtime!(collection_id.collection_tokens())?453 let theme_info = collection.collection_tokens()436 .into_iter()454 .into_iter()437 .find_map(|token_id| {455 .find_map(|token_id| {438 RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?;456 RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?;