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

difftreelog

refactor rmrk proxy, add add_theme rmrk proxy

Daniel Shiposha2022-05-25parent: #4186437.patch.diff
in: master

8 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -30,7 +30,7 @@
 // RMRK
 use rmrk_rpc::RmrkApi as RmrkRuntimeApi;
 use up_data_structs::{
-	RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName, RmrkPropertyKey,
+	RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName,
 	RmrkResourceId,
 };
 
@@ -248,7 +248,7 @@
 		fn collection_properties(
 			&self,
 			collection_id: RmrkCollectionId,
-			filter_keys: Option<Vec<RmrkPropertyKey>>, //String
+			filter_keys: Option<Vec<String>>,
 			at: Option<BlockHash>,
 		) -> Result<Vec<PropertyInfo>>;
 
@@ -258,7 +258,7 @@
 			&self,
 			collection_id: RmrkCollectionId,
 			nft_id: RmrkNftId,
-			filter_keys: Option<Vec<RmrkPropertyKey>>,
+			filter_keys: Option<Vec<String>>,
 			at: Option<BlockHash>,
 		) -> Result<Vec<PropertyInfo>>;
 
@@ -299,8 +299,8 @@
 		fn theme(
 			&self,
 			base_id: RmrkBaseId,
-			theme_name: RmrkThemeName, // String
-			filter_keys: Option<Vec<RmrkPropertyKey>>,
+			theme_name: String,
+			filter_keys: Option<Vec<String>>,
 			at: Option<BlockHash>,
 		) -> Result<Option<Theme>>;
 	}
@@ -523,11 +523,22 @@
 	pass_method!(account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Vec<RmrkNftId>, rmrk_api);
 	pass_method!(nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkNftChild>, rmrk_api);
 	pass_method!(
-		collection_properties(collection_id: RmrkCollectionId, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Vec<PropertyInfo>,
+		collection_properties(
+			collection_id: RmrkCollectionId,
+
+			#[map(|keys| string_keys_to_bytes_keys(keys))]
+			filter_keys: Option<Vec<String>>
+		) -> Vec<PropertyInfo>,
 		rmrk_api
 	);
 	pass_method!(
-		nft_properties(collection_id: RmrkCollectionId, nft_id: RmrkNftId, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Vec<PropertyInfo>,
+		nft_properties(
+			collection_id: RmrkCollectionId,
+			nft_id: RmrkNftId,
+
+			#[map(|keys| string_keys_to_bytes_keys(keys))]
+			filter_keys: Option<Vec<String>>
+		) -> Vec<PropertyInfo>,
 		rmrk_api
 	);
 	pass_method!(nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<ResourceInfo>, rmrk_api);
@@ -535,7 +546,16 @@
 	pass_method!(base(base_id: RmrkBaseId) -> Option<BaseInfo>, rmrk_api);
 	pass_method!(base_parts(base_id: RmrkBaseId) -> Vec<PartType>, rmrk_api);
 	pass_method!(theme_names(base_id: RmrkBaseId) -> Vec<RmrkThemeName>, rmrk_api);
-	pass_method!(theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Option<Theme>, rmrk_api);
+	pass_method!(
+		theme(
+			base_id: RmrkBaseId,
+
+			#[map(|n| n.into_bytes())]
+			theme_name: String,
+
+			#[map(|keys| string_keys_to_bytes_keys(keys))]
+			filter_keys: Option<Vec<String>>
+		) -> Option<Theme>, rmrk_api);
 }
 
 fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -795,11 +795,11 @@
 	}
 
 	pub fn set_scoped_collection_property(
-		collection: &CollectionHandle<T>,
+		collection_id: CollectionId,
 		scope: PropertyScope,
 		property: Property,
 	) -> DispatchResult {
-		CollectionProperties::<T>::try_mutate(collection.id, |properties| {
+		CollectionProperties::<T>::try_mutate(collection_id, |properties| {
 			properties.try_scoped_set(scope, property.key, property.value)
 		})
 		.map_err(<Error<T>>::from)?;
@@ -807,13 +807,12 @@
 		Ok(())
 	}
 
-	#[transactional]
 	pub fn set_scoped_collection_properties(
-		collection: &CollectionHandle<T>,
+		collection_id: CollectionId,
 		scope: PropertyScope,
 		properties: impl Iterator<Item = Property>,
 	) -> DispatchResult {
-		CollectionProperties::<T>::try_mutate(collection.id, |stored_properties| {
+		CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {
 			stored_properties.try_scoped_set_from_iter(scope, properties)
 		})
 		.map_err(<Error<T>>::from)?;
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -195,12 +195,12 @@
 	}
 
 	pub fn set_scoped_token_property(
-		collection: &CollectionHandle<T>,
+		collection_id: CollectionId,
 		token_id: TokenId,
 		scope: PropertyScope,
 		property: Property,
 	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection.id, token_id), |properties| {
+		TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {
 			properties.try_scoped_set(scope, property.key, property.value)
 		})
 		.map_err(<CommonError<T>>::from)?;
@@ -209,12 +209,12 @@
 	}
 
 	pub fn set_scoped_token_properties(
-		collection: &CollectionHandle<T>,
+		collection_id: CollectionId,
 		token_id: TokenId,
 		scope: PropertyScope,
 		properties: impl Iterator<Item=Property>,
 	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection.id, token_id), |stored_properties| {
+		TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
 			stored_properties.try_scoped_set_from_iter(scope, properties)
 		})
 		.map_err(<CommonError<T>>::from)?;
@@ -222,8 +222,8 @@
 		Ok(())
 	}
 
-	pub fn current_token_id(collection: &CollectionHandle<T>) -> TokenId {
-		TokenId(<TokensMinted<T>>::get(collection.id))
+	pub fn current_token_id(collection_id: CollectionId) -> TokenId {
+		TokenId(<TokensMinted<T>>::get(collection_id))
 	}
 }
 
modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -33,6 +33,8 @@
 use misc::*;
 pub use property::*;
 
+use RmrkProperty::*;
+
 #[frame_support::pallet]
 pub mod pallet {
     use super::*;
@@ -135,15 +137,13 @@
             }
 
             let collection_id = collection_id_res?;
-
-            let collection = Self::get_nft_collection(collection_id)?.into_inner();
 
             <PalletCommon<T>>::set_scoped_collection_properties(
-                &collection,
+                collection_id,
                 PropertyScope::Rmrk,
                 [
-                    rmrk_property!(Config=T, Metadata: metadata)?,
-                    rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?,
+                    Self::rmrk_property(Metadata, &metadata)?,
+                    Self::rmrk_property(CollectionType, &misc::CollectionType::Regular)?,
                 ].into_iter()
             )?;
 
@@ -168,7 +168,7 @@
 
             let unique_collection_id = collection_id.into();
 
-            let collection = Self::get_typed_nft_collection(unique_collection_id, CollectionType::Regular)?;
+            let collection = Self::get_typed_nft_collection(unique_collection_id, misc::CollectionType::Regular)?;
 
             ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
 
@@ -193,7 +193,7 @@
 
             Self::change_collection_owner(
                 collection_id.into(),
-                CollectionType::Regular,
+                misc::CollectionType::Regular,
                 sender.clone(),
                 new_issuer.clone()
             )?;
@@ -218,7 +218,7 @@
 
             let collection = Self::get_typed_nft_collection(
                 collection_id.into(),
-                CollectionType::Regular
+                misc::CollectionType::Regular
             )?;
 
             Self::check_collection_owner(&collection, &cross_sender)?;
@@ -253,20 +253,27 @@
                 amount
             });
 
+            let collection = Self::get_typed_nft_collection(
+                collection_id.into(),
+                misc::CollectionType::Regular,
+            )?;
+
             let nft_id = Self::create_nft(
                 &sender,
                 &cross_owner,
-                collection_id.into(),
-                CollectionType::Regular,
+                &collection,
                 NftType::Regular,
                 [
-                    rmrk_property!(Config=T, RoyaltyInfo: royalty_info)?,
-                    rmrk_property!(Config=T, Metadata: metadata)?,
-                    rmrk_property!(Config=T, Equipped: false)?,
-                    rmrk_property!(Config=T, ResourceCollection: None::<CollectionId>)?,
-                    rmrk_property!(Config=T, ResourcePriorities: <Vec<u8>>::new())?,
+                    Self::rmrk_property(RoyaltyInfo, &royalty_info)?,
+                    Self::rmrk_property(Metadata, &metadata)?,
+                    Self::rmrk_property(Equipped, &false)?,
+                    Self::rmrk_property(ResourceCollection, &None::<CollectionId>)?,
+                    Self::rmrk_property(ResourcePriorities, &<Vec<u8>>::new())?,
                 ].into_iter()
-            )?;
+            ).map_err(|err| match err {
+                DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),
+                err => Self::map_common_err_to_proxy(err)
+            })?;
 
             Self::deposit_event(Event::NftMinted {
                 owner,
@@ -290,7 +297,7 @@
             Self::destroy_nft(
                 cross_sender,
                 collection_id.into(),
-                CollectionType::Regular,
+                misc::CollectionType::Regular,
                 nft_id.into()
             )?;
 
@@ -302,19 +309,37 @@
 }
 
 impl<T: Config> Pallet<T> {
+    pub fn rmrk_property_key(rmrk_key: RmrkProperty) -> Result<PropertyKey, DispatchError> {
+        let key = rmrk_key.to_key::<T>()?;
+
+        let scoped_key = PropertyScope::Rmrk.apply(key)
+            .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)?;
+
+        Ok(scoped_key)
+    }
+
+    pub fn rmrk_property<E: Encode>(rmrk_key: RmrkProperty, value: &E) -> Result<Property, DispatchError> {
+        let key = rmrk_key.to_key::<T>()?;
+
+        let value = value.encode()
+            .try_into()
+            .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong)?;
+
+        let property = Property {
+            key,
+            value,
+        };
+
+        Ok(property)
+    }
+
     pub fn create_nft(
         sender: &T::CrossAccountId,
         owner: &T::CrossAccountId,
-        collection_id: CollectionId,
-        collection_type: CollectionType,
+        collection: &NonfungibleHandle<T>,
         nft_type: NftType,
         properties: impl Iterator<Item=Property>
     ) -> Result<TokenId, DispatchError> {
-        let collection = Self::get_typed_nft_collection(
-            collection_id,
-            collection_type
-        )?;
-
         let data = CreateNftExData {
             const_data: nft_type.encode()
                 .try_into()
@@ -326,16 +351,16 @@
         let budget = budget::Value::new(2);
 
         <PalletNft<T>>::create_item(
-            &collection,
+            collection,
             sender,
             data,
             &budget,
-        ).map_err(Self::map_common_err_to_proxy)?;
+        )?;
 
-        let nft_id = <PalletNft<T>>::current_token_id(&collection);
+        let nft_id = <PalletNft<T>>::current_token_id(collection.id);
 
         <PalletNft<T>>::set_scoped_token_properties(
-            &collection,
+            collection.id,
             nft_id,
             PropertyScope::Rmrk,
             properties
@@ -347,7 +372,7 @@
     fn destroy_nft(
         sender: T::CrossAccountId,
         collection_id: CollectionId,
-        collection_type: CollectionType,
+        collection_type: misc::CollectionType,
         token_id: TokenId
     ) -> DispatchResult {
         let collection = Self::get_typed_nft_collection(
@@ -363,7 +388,7 @@
 
     fn change_collection_owner(
         collection_id: CollectionId,
-        collection_type: CollectionType,
+        collection_type: misc::CollectionType,
         sender: T::AccountId,
         new_owner: T::AccountId,
     ) -> DispatchResult {
@@ -390,10 +415,12 @@
 
     pub fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {
         let collection = <CollectionHandle<T>>::try_get(collection_id)
-            .map_err(|_| <Error<T>>::CollectionUnknown)?
-            .into_nft_collection()?;
+            .map_err(|_| <Error<T>>::CollectionUnknown)?;
 
-        Ok(collection)
+        match collection.mode {
+            CollectionMode::NFT => Ok(NonfungibleHandle::cast(collection)),
+            _ => Err(<Error<T>>::CollectionUnknown.into())
+        }
     }
 
     // should this even be here, might displace it to common/nonfungible -- but they did not need it, only rmrk does
@@ -407,23 +434,23 @@
 
     pub fn get_collection_property(collection_id: CollectionId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {
         let collection_property = <PalletCommon<T>>::collection_properties(collection_id)
-            .get(&rmrk_property!(Config=T, key)?)
+            .get(&Self::rmrk_property_key(key)?)
             .ok_or(<Error<T>>::CollectionUnknown)?
             .clone();
 
         Ok(collection_property)
     }
 
-    pub fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {
-        let value = Self::get_collection_property(collection_id, RmrkProperty::CollectionType)?;
-        let collection_type: CollectionType = (&value)
-            .try_into()
-            .map_err(<Error<T>>::from)?;
+    pub fn get_collection_type(collection_id: CollectionId) -> Result<misc::CollectionType, DispatchError> {
+        let value = Self::get_collection_property(collection_id, CollectionType)?;
+
+        let mut value = value.as_slice();
 
-        Ok(collection_type)
+        misc::CollectionType::decode(&mut value)
+            .map_err(|_| <Error<T>>::CorruptedCollectionType.into())
     }
 
-    pub fn ensure_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {
+    pub fn ensure_collection_type(collection_id: CollectionId, collection_type: misc::CollectionType) -> DispatchResult {
         let actual_type = Self::get_collection_type(collection_id)?;
         ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);
 
@@ -432,7 +459,7 @@
 
     pub fn get_nft_property(collection_id: CollectionId, nft_id: TokenId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {
         let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))
-            .get(&rmrk_property!(Config=T, key)?)
+            .get(&Self::rmrk_property_key(key)?)
             .ok_or(<Error<T>>::NoAvailableNftId)?
             .clone();
 
@@ -440,10 +467,12 @@
     }
 
     pub fn get_nft_type(collection_id: CollectionId, token_id: TokenId) -> Result<NftType, DispatchError> {
-        <TokenData<T>>::get((collection_id, token_id))
-            .unwrap()
-            .rmrk_nft_type()
-            .ok_or_else(|| <Error<T>>::NoAvailableNftId.into())
+        let token_data = <TokenData<T>>::get((collection_id, token_id))
+            .ok_or(<Error<T>>::NoAvailableNftId)?;
+
+        let mut const_data = token_data.const_data.as_slice();
+
+        NftType::decode(&mut const_data).map_err(|_| <Error<T>>::NoAvailableNftId.into())
     }
 
     pub fn ensure_nft_type(collection_id: CollectionId, token_id: TokenId, nft_type: NftType) -> DispatchResult {
@@ -466,7 +495,7 @@
                     let value = Self::get_nft_property(
                         collection_id,
                         token_id,
-                        RmrkProperty::ThemeProperty(&key)
+                        ThemeProperty(&key)
                     ).ok()?.decode_or_default();
 
                     let property = RmrkThemeProperty {
@@ -491,7 +520,7 @@
         collection_id: CollectionId,
         token_id: TokenId
     ) -> Result<impl Iterator<Item=RmrkThemeProperty>, DispatchError> {
-        let key_prefix = rmrk_property!(Config=T, key: ThemeProperty(&RmrkString::default()))?;
+        let key_prefix = Self::rmrk_property_key(ThemeProperty(&RmrkString::default()))?;
 
         let properties = <PalletNft<T>>::token_properties((collection_id, token_id))
             .into_iter()
@@ -514,7 +543,7 @@
 
     pub fn get_typed_nft_collection(
         collection_id: CollectionId,
-        collection_type: CollectionType
+        collection_type: misc::CollectionType
     ) -> Result<NonfungibleHandle<T>, DispatchError> {
         Self::ensure_collection_type(collection_id, collection_type)?;
 
modifiedpallets/proxy-rmrk-core/src/misc.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/misc.rs
+++ b/pallets/proxy-rmrk-core/src/misc.rs
@@ -1,23 +1,6 @@
 use super::*;
 use codec::{Encode, Decode};
-use pallet_nonfungible::{NonfungibleHandle, ItemData};
-
-macro_rules! impl_rmrk_value {
-    ($enum_name:path, decode_error: $error:ident) => {
-        impl TryFrom<&PropertyValue> for $enum_name {
-            type Error = MiscError;
-
-            fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {
-                let mut value = value.as_slice();
 
-                <$enum_name>::decode(&mut value)
-                    .map_err(|_| MiscError::$error)
-            }
-        }
-
-    };
-}
-
 #[macro_export]
 macro_rules! map_common_err_to_proxy {
     (match $err:ident { $($common_err:ident => $proxy_err:ident),+ }) => {
@@ -29,59 +12,8 @@
             $err
         }
     };
-}
-
-pub enum MiscError {
-    RmrkPropertyValueIsTooLong,
-    CorruptedCollectionType,
-}
-
-impl<T: Config> From<MiscError> for Error<T> {
-    fn from(error: MiscError) -> Self {
-        match error {
-            MiscError::RmrkPropertyValueIsTooLong => Self::RmrkPropertyValueIsTooLong,
-            MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,
-        }
-    }
-}
-
-pub trait IntoNftCollection<T: Config> {
-    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;
 }
 
-impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {
-    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {
-        match self.mode {
-            CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),
-            _ => Err(<Error<T>>::CollectionUnknown)
-        }
-    }
-}
-
-pub trait IntoPropertyValue {
-    fn into_property_value(self) -> Result<PropertyValue, MiscError>;
-}
-
-impl<T: Encode> IntoPropertyValue for T {
-    fn into_property_value(self) -> Result<PropertyValue, MiscError> {
-        self.encode()
-            .try_into()
-            .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)
-    }
-}
-
-pub trait RmrkNft {
-    fn rmrk_nft_type(&self) -> Option<NftType>;
-}
-
-impl<CrossAccountId> RmrkNft for ItemData<CrossAccountId> {
-    fn rmrk_nft_type(&self) -> Option<NftType> {
-        let mut value = self.const_data.as_slice();
-
-        NftType::decode(&mut value).ok()
-    }
-}
-
 pub trait RmrkDecode<T: Decode + Default, S> {
     fn decode_or_default(&self) -> T;
 }
@@ -121,5 +53,3 @@
     SlotPart,
     Theme
 }
-
-impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);
modifiedpallets/proxy-rmrk-core/src/property.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/property.rs
+++ b/pallets/proxy-rmrk-core/src/property.rs
@@ -71,31 +71,3 @@
         }
     }
 }
-
-#[macro_export]
-macro_rules! rmrk_property {
-    (Config=$cfg:ty, key: $key:ident $(($key_ext:expr))?) => {
-        rmrk_property!(Config=$cfg, $crate::RmrkProperty::$key $(($key_ext))?)
-    };
-
-    (Config=$cfg:ty, $key:ident $(($key_ext:expr))?: $value:expr) => {{
-        let key = rmrk_property!(@$cfg, $crate::RmrkProperty::$key $(($key_ext))?)?;
-
-        let value = $value.into_property_value()
-            .map_err(<$crate::Error<$cfg>>::from)?;
-
-        Ok::<_, $crate::Error<$cfg>>(Property {
-            key,
-            value,
-        })
-    }};
-
-    (@$cfg:ty, $key_enum:expr) => {
-        $key_enum.to_key::<$cfg>()
-    };
-
-    (Config=$cfg:ty, $key_enum:expr) => {
-        PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key_enum)?)
-            .map_err(|_| <$crate::Error<$cfg>>::RmrkPropertyKeyIsTooLong)
-    };
-}
modifiedpallets/proxy-rmrk-equip/src/lib.rsdiffbeforeafterboth
20use frame_system::{pallet_prelude::*, ensure_signed};20use frame_system::{pallet_prelude::*, ensure_signed};
21use sp_runtime::DispatchError;21use sp_runtime::DispatchError;
22use up_data_structs::*;22use up_data_structs::*;
23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle};23use pallet_common::{Pallet as PalletCommon, Error as CommonError};
24use pallet_rmrk_core::{Pallet as PalletCore, rmrk_property, misc::*};24use pallet_rmrk_core::{Pallet as PalletCore, misc::{self, *}, property::RmrkProperty::*};
25use pallet_nonfungible::{Pallet as PalletNft};25use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};
26use pallet_evm::account::CrossAccountId;26use pallet_evm::account::CrossAccountId;
2727
28pub use pallet::*;28pub use pallet::*;
48 TokenId48 TokenId
49 >;49 >;
50
51 #[pallet::storage]
52 #[pallet::getter(fn base_has_default_theme)]
53 pub type BaseHasDefaultTheme<T: Config> = StorageMap<
54 _,
55 Twox64Concat,
56 CollectionId,
57 bool,
58 ValueQuery
59 >;
5060
51 #[pallet::pallet]61 #[pallet::pallet]
52 #[pallet::generate_store(pub(super) trait Store)]62 #[pallet::generate_store(pub(super) trait Store)]
6373
64 #[pallet::error]74 #[pallet::error]
65 pub enum Error<T> {75 pub enum Error<T> {
76 PermissionError,
66 NoAvailableBaseId,77 NoAvailableBaseId,
78 NoAvailablePartId,
79 BaseDoesntExist,
80 NeedsDefaultThemeFirst,
67 }81 }
6882
69 #[pallet::call]83 #[pallet::call]
95109
96 let collection_id = collection_id_res?;110 let collection_id = collection_id_res?;
97
98 let collection = <PalletCore<T>>::get_nft_collection(collection_id)?.into_inner();
99111
100 <PalletCommon<T>>::set_scoped_collection_properties(112 <PalletCommon<T>>::set_scoped_collection_properties(
101 &collection,113 collection_id,
102 PropertyScope::Rmrk,114 PropertyScope::Rmrk,
103 [115 [
104 rmrk_property!(Config=T, CollectionType: CollectionType::Base)?,116 <PalletCore<T>>::rmrk_property(CollectionType, &misc::CollectionType::Base)?,
105 rmrk_property!(Config=T, BaseType: base_type)?,117 <PalletCore<T>>::rmrk_property(BaseType, &base_type)?,
106 ].into_iter()118 ].into_iter()
107 )?;119 )?;
120
121 let collection = <PalletCore<T>>::get_nft_collection(collection_id)?;
108122
109 for part in parts {123 for part in parts {
110 let part_id = part.id();124 let part_id = part.id();
117 <InernalPartId<T>>::insert(collection_id, part_id, part_token_id);131 <InernalPartId<T>>::insert(collection_id, part_id, part_token_id);
118132
119 <PalletNft<T>>::set_scoped_token_property(133 <PalletNft<T>>::set_scoped_token_property(
120 &collection,134 collection_id,
121 part_token_id,135 part_token_id,
122 PropertyScope::Rmrk,136 PropertyScope::Rmrk,
123 rmrk_property!(Config=T, ExternalPartId: part_id)?137 <PalletCore<T>>::rmrk_property(ExternalPartId, &part_id)?
124 )?;138 )?;
125 }139 }
126140
129 Ok(())143 Ok(())
130 }144 }
145
146 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
147 #[transactional]
148 pub fn theme_add(
149 origin: OriginFor<T>,
150 base_id: RmrkBaseId,
151 theme: RmrkTheme,
152 ) -> DispatchResult {
153 let sender = ensure_signed(origin)?;
154
155 let sender = T::CrossAccountId::from_sub(sender);
156 let owner = &sender;
157
158 let collection_id: CollectionId = base_id.into();
159
160 let collection = <PalletCore<T>>::get_typed_nft_collection(
161 collection_id,
162 misc::CollectionType::Base
163 ).map_err(|_| <Error<T>>::BaseDoesntExist)?;
164
165 if theme.name.as_slice() == b"default" {
166 <BaseHasDefaultTheme<T>>::insert(collection_id, true);
167 } else if !Self::base_has_default_theme(collection_id) {
168 return Err(<Error<T>>::NeedsDefaultThemeFirst.into());
169 }
170
171 let token_id = <PalletCore<T>>::create_nft(
172 &sender,
173 owner,
174 &collection,
175 NftType::Theme,
176 [
177 <PalletCore<T>>::rmrk_property(ThemeName, &theme.name)?,
178 <PalletCore<T>>::rmrk_property(ThemeInherit, &theme.inherit)?
179 ].into_iter()
180 ).map_err(|_| <Error<T>>::PermissionError)?;
181
182 for property in theme.properties {
183 <PalletNft<T>>::set_scoped_token_property(
184 collection_id,
185 token_id,
186 PropertyScope::Rmrk,
187 <PalletCore<T>>::rmrk_property(
188 ThemeProperty(&property.key),
189 &property.value
190 )?
191 )?;
192 }
193
194 Ok(())
195 }
131 }196 }
132}197}
133198
134impl<T: Config> Pallet<T> {199impl<T: Config> Pallet<T> {
135 fn create_part(200 fn create_part(
136 sender: &T::CrossAccountId,201 sender: &T::CrossAccountId,
137 collection: &CollectionHandle<T>,202 collection: &NonfungibleHandle<T>,
138 part: RmrkPartType203 part: RmrkPartType
139 ) -> Result<TokenId, DispatchError> {204 ) -> Result<TokenId, DispatchError> {
140 let owner = sender;205 let owner = sender;
150 let token_id = <PalletCore<T>>::create_nft(215 let token_id = <PalletCore<T>>::create_nft(
151 sender,216 sender,
152 owner,217 owner,
153 collection.id,218 collection,
154 CollectionType::Base,
155 nft_type,219 nft_type,
156 [220 [
157 rmrk_property!(Config=T, Src: src)?,221 <PalletCore<T>>::rmrk_property(Src, &src)?,
158 rmrk_property!(Config=T, ZIndex: z_index)?222 <PalletCore<T>>::rmrk_property(ZIndex, &z_index)?
159 ].into_iter()223 ].into_iter()
160 )?;224 ).map_err(|err| match err {
225 DispatchError::Arithmetic(_) => <Error<T>>::NoAvailablePartId.into(),
226 err => err
227 })?;
161228
162 if let RmrkPartType::SlotPart(part) = part {229 if let RmrkPartType::SlotPart(part) = part {
163 <PalletNft<T>>::set_scoped_token_property(230 <PalletNft<T>>::set_scoped_token_property(
164 collection,231 collection.id,
165 token_id,232 token_id,
166 PropertyScope::Rmrk,233 PropertyScope::Rmrk,
167 rmrk_property!(Config=T, EquippableList: part.equippable)?234 <PalletCore<T>>::rmrk_property(EquippableList, &part.equippable)?
168 )?;235 )?;
169 }236 }
170237
modifiedruntime/common/src/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -347,7 +347,7 @@
 
                 fn base_parts(base_id: RmrkBaseId) -> Result<Vec<RmrkPartType>, DispatchError> {
                     use frame_support::BoundedVec;
-                    use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, RmrkNft, RmrkDecode}};
+                    use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, NftType, RmrkDecode}};
 
                     let collection_id = CollectionId(base_id);
                     if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() { return Ok(Vec::new()); }
@@ -379,7 +379,7 @@
 
                 fn theme_names(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {
                     use frame_support::BoundedVec;
-                    use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, RmrkNft, RmrkDecode}};
+                    use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, RmrkDecode}};
 
                     let collection_id = CollectionId(base_id);
                     if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() {
@@ -407,7 +407,7 @@
                     use frame_support::BoundedVec;
                     use pallet_proxy_rmrk_core::{
                         RmrkProperty,
-                        misc::{CollectionType, NftType, RmrkNft, RmrkDecode}
+                        misc::{CollectionType, NftType, RmrkDecode}
                     };
 
                     let collection_id = CollectionId(base_id);