git.delta.rocks / unique-network / refs/commits / 71e16e1f1a02

difftreelog

source

pallets/proxy-rmrk-equip/src/rpc.rs4.3 KiBsourcehistory
1use super::*;2use pallet_rmrk_core::{misc, property::*};3use sp_std::vec::Vec;45pub fn base<T: Config>(6	base_id: RmrkBaseId,7) -> Result<Option<RmrkBaseInfo<T::AccountId>>, DispatchError> {8	let (collection, collection_id) =9		match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)10		{11			Ok(c) => c,12			Err(_) => return Ok(None),13		};1415	Ok(Some(RmrkBaseInfo {16		issuer: collection.owner.clone(),17		base_type: <PalletCore<T>>::get_collection_property_decoded(18			collection_id,19			RmrkProperty::BaseType,20		)?,21		symbol: <PalletCore<T>>::rebind(&collection.token_prefix)?,22	}))23}2425pub fn base_parts<T: Config>(base_id: RmrkBaseId) -> Result<Vec<RmrkPartType>, DispatchError> {26	use pallet_common::CommonCollectionOperations;2728	let (collection, collection_id) =29		match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)30		{31			Ok(c) => c,32			Err(_) => return Ok(Vec::new()),33		};3435	let parts = collection36		.collection_tokens()37		.into_iter()38		.filter_map(|token_id| {39			let nft_type = <PalletCore<T>>::get_nft_type(collection_id, token_id).ok()?;4041			match nft_type {42				NftType::FixedPart => Some(RmrkPartType::FixedPart(RmrkFixedPart {43					id: <PalletCore<T>>::get_nft_property_decoded(44						collection_id,45						token_id,46						RmrkProperty::ExternalPartId,47					)48					.ok()?,49					src: <PalletCore<T>>::get_nft_property_decoded(50						collection_id,51						token_id,52						RmrkProperty::Src,53					)54					.ok()?,55					z: <PalletCore<T>>::get_nft_property_decoded(56						collection_id,57						token_id,58						RmrkProperty::ZIndex,59					)60					.ok()?,61				})),62				NftType::SlotPart => Some(RmrkPartType::SlotPart(RmrkSlotPart {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					equippable: <PalletCore<T>>::get_nft_property_decoded(82						collection_id,83						token_id,84						RmrkProperty::EquippableList,85					)86					.ok()?,87				})),88				_ => None,89			}90		})91		.collect();9293	Ok(parts)94}9596pub fn theme_names<T: Config>(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {97	use pallet_common::CommonCollectionOperations;9899	let (collection, collection_id) =100		match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)101		{102			Ok(c) => c,103			Err(_) => return Ok(Vec::new()),104		};105106	let theme_names = collection107		.collection_tokens()108		.iter()109		.filter_map(|token_id| {110			let nft_type = <PalletCore<T>>::get_nft_type(collection_id, *token_id).ok()?;111112			match nft_type {113				NftType::Theme => <PalletCore<T>>::get_nft_property_decoded(114					collection_id,115					*token_id,116					RmrkProperty::ThemeName,117				)118				.ok(),119				_ => None,120			}121		})122		.collect();123124	Ok(theme_names)125}126127pub fn theme<T: Config>(128	base_id: RmrkBaseId,129	theme_name: RmrkThemeName,130	filter_keys: Option<Vec<RmrkPropertyKey>>,131) -> Result<Option<RmrkTheme>, DispatchError> {132	use pallet_common::CommonCollectionOperations;133134	let (collection, collection_id) =135		match <PalletCore<T>>::get_typed_nft_collection_mapped(base_id, misc::CollectionType::Base)136		{137			Ok(c) => c,138			Err(_) => return Ok(None),139		};140141	let theme_info = collection142		.collection_tokens()143		.into_iter()144		.find_map(|token_id| {145			<PalletCore<T>>::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?;146147			let name: RmrkString = <PalletCore<T>>::get_nft_property_decoded(148				collection_id,149				token_id,150				RmrkProperty::ThemeName,151			)152			.ok()?;153154			if name == theme_name {155				Some((name, token_id))156			} else {157				None158			}159		});160161	let (name, theme_id) = match theme_info {162		Some((name, theme_id)) => (name, theme_id),163		None => return Ok(None),164	};165166	let properties = <PalletCore<T>>::filter_user_properties(167		collection_id,168		Some(theme_id),169		filter_keys,170		|key, value| RmrkThemeProperty { key, value },171	)?;172173	let inherit = <PalletCore<T>>::get_nft_property_decoded(174		collection_id,175		theme_id,176		RmrkProperty::ThemeInherit,177	)?;178179	let theme = RmrkTheme {180		name,181		properties,182		inherit,183	};184185	Ok(Some(theme))186}