git.delta.rocks / unique-network / refs/commits / e617e73ed3a8

difftreelog

refactor(rmrk-rpc) replace dispatch_unique_runtime with direct calls to collections + cleaning

Fahrrader2022-06-02parent: #c2beece.patch.diff
in: master

1 file changed

modifiedruntime/common/src/runtime_apis.rsdiffbeforeafterboth
144 }144 }
145145
146 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;
148149
149 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 };
154155
155 // todo replace dispatch... calls with calls to rmrkcore and NFT collection. There's no point trying non-NFT collections
156 let nfts_count = dispatch_unique_runtime!(collection_id.total_supply())?;156 let nfts_count = collection.total_supply();
157157
158 Ok(Some(RmrkCollectionInfo {158 Ok(Some(RmrkCollectionInfo {
159 issuer: collection.owner.clone(),159 issuer: collection.owner.clone(),
166166
167 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;
170171
171 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 };
177
172 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); }
174180
175 // todo replace dispatch with collection
176 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())
194199
195 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;
197203
198 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 };
201210
202 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 }
272281
273 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;
277285
278 let collection_id = RmrkCore::unique_collection_id(collection_id)?;286 let collection_id = RmrkCore::unique_collection_id(collection_id)?;
322 }330 }
323331
324 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}};
326334
327 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 }
367375
368 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;
370379
371 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 };
385
373386
374 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 }
398411
399 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;
401415
402 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 };
421
406422
407 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;
429446
430 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 };
434452
435 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()?;