git.delta.rocks / unique-network / refs/commits / 6773058243b9

difftreelog

fix use rmrk type in proxy

Daniel Shiposha2022-05-19parent: #3703ed8.patch.diff
in: master

4 files changed

modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -16,9 +16,9 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
-use frame_support::{pallet_prelude::*, transactional, BoundedVec, traits::ConstU32, dispatch::DispatchResult};
+use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};
 use frame_system::{pallet_prelude::*, ensure_signed};
-use sp_runtime::DispatchError;
+use sp_runtime::{DispatchError, traits::StaticLookup};
 use up_data_structs::*;
 use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};
 use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};
@@ -54,15 +54,20 @@
 	pub enum Event<T: Config> {
         CollectionCreated {
 			issuer: T::AccountId,
-			collection_id: CollectionId,
+			collection_id: RmrkCollectionId,
 		},
         CollectionDestroyed {
 			issuer: T::AccountId,
-			collection_id: CollectionId,
+			collection_id: RmrkCollectionId,
+		},
+        IssuerChanged {
+			old_issuer: T::AccountId,
+			new_issuer: T::AccountId,
+			collection_id: RmrkCollectionId,
 		},
         CollectionLocked {
 			issuer: T::AccountId,
-			collection_id: CollectionId,
+			collection_id: RmrkCollectionId,
 		},
 	}
 
@@ -71,7 +76,8 @@
         /* Unique-specific events */
         CorruptedCollectionType,
         NotRmrkCollection,
-        RmrkPropertyIsTooLong,
+        RmrkPropertyKeyIsTooLong,
+        RmrkPropertyValueIsTooLong,
 
         /* RMRK compatible events */
         CollectionNotEmpty,
@@ -85,9 +91,9 @@
 		#[transactional]
 		pub fn create_collection(
 			origin: OriginFor<T>,
-			metadata: PropertyValue,
+			metadata: RmrkString,
 			max: Option<u32>,
-			symbol: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,
+			symbol: RmrkCollectionSymbol,
 		) -> DispatchResult {
             let sender = ensure_signed(origin)?;
 
@@ -98,7 +104,9 @@
 
             let data = CreateCollectionData {
                 limits,
-                token_prefix: symbol,
+                token_prefix: symbol.into_inner()
+                    .try_into()
+                    .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
                 ..Default::default()
             };
 
@@ -121,7 +129,10 @@
                 ].into_iter()
             )?;
 
-            Self::deposit_event(Event::CollectionCreated { issuer: sender, collection_id });
+            Self::deposit_event(Event::CollectionCreated {
+                issuer: sender,
+                collection_id: collection_id.0
+            });
 
             Ok(())
         }
@@ -130,14 +141,16 @@
 		#[transactional]
 		pub fn destroy_collection(
 			origin: OriginFor<T>,
-			collection_id: CollectionId,
+			collection_id: RmrkCollectionId,
 		) -> DispatchResult {
             let sender = ensure_signed(origin)?;
             let cross_sender = T::CrossAccountId::from_sub(sender.clone());
 
-            let collection = Self::get_nft_collection(collection_id)?;
+            let unique_collection_id = collection_id.into();
+
+            let collection = Self::get_nft_collection(unique_collection_id)?;
 
-            Self::check_collection_type(collection_id, CollectionType::Regular)?;
+            Self::check_collection_type(unique_collection_id, CollectionType::Regular)?;
 
             ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
 
@@ -152,18 +165,26 @@
 		#[transactional]
 		pub fn change_collection_issuer(
 			origin: OriginFor<T>,
-			collection_id: CollectionId,
-			new_issuer: T::AccountId,
+			collection_id: RmrkCollectionId,
+			new_issuer: <T::Lookup as StaticLookup>::Source,
 		) -> DispatchResult {
             let sender = ensure_signed(origin)?;
 
+            let new_issuer = T::Lookup::lookup(new_issuer)?;
+
             Self::change_collection_owner(
-                collection_id,
+                collection_id.into(),
                 CollectionType::Regular,
-                sender,
-                new_issuer
+                sender.clone(),
+                new_issuer.clone()
             )?;
 
+            Self::deposit_event(Event::IssuerChanged {
+				old_issuer: sender,
+				new_issuer,
+				collection_id,
+			});
+
             Ok(())
         }
 
@@ -171,12 +192,12 @@
 		#[transactional]
 		pub fn lock_collection(
 			origin: OriginFor<T>,
-			collection_id: CollectionId,
+			collection_id: RmrkCollectionId,
 		) -> DispatchResult {
             let sender = ensure_signed(origin)?;
             let cross_sender = T::CrossAccountId::from_sub(sender.clone());
 
-            let collection = Self::get_nft_collection(collection_id)?;
+            let collection = Self::get_nft_collection(collection_id.into())?;
             collection.check_is_owner(&cross_sender)?;
 
             let token_count = collection.total_supply();
modifiedpallets/proxy-rmrk-core/src/misc.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/misc.rs
+++ b/pallets/proxy-rmrk-core/src/misc.rs
@@ -4,9 +4,11 @@
 
 macro_rules! impl_rmrk_value {
     ($enum_name:path, decode_error: $error:ident) => {
-        impl From<$enum_name> for PropertyValue {
-            fn from(e: $enum_name) -> Self {
-                e.encode().try_into().unwrap()
+        impl IntoPropertyValue for $enum_name {
+            fn into_property_value(self) -> Result<PropertyValue, MiscError> {
+                self.encode()
+                    .try_into()
+                    .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)
             }
         }
 
@@ -25,12 +27,14 @@
 }
 
 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,
         }
     }
@@ -49,6 +53,18 @@
     }
 }
 
+pub trait IntoPropertyValue {
+    fn into_property_value(self) -> Result<PropertyValue, MiscError>;
+}
+
+impl<L: Get<u32>> IntoPropertyValue for BoundedVec<u8, L> {
+    fn into_property_value(self) -> Result<PropertyValue, MiscError> {
+        self.into_inner()
+            .try_into()
+            .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)
+    }
+}
+
 #[derive(Encode, Decode, PartialEq, Eq)]
 pub enum CollectionType {
     Regular,
modifiedpallets/proxy-rmrk-core/src/property.rsdiffbeforeafterboth
before · pallets/proxy-rmrk-core/src/property.rs
1use super::*;2use core::convert::AsRef;34pub enum RmrkProperty {5    Metadata,6    CollectionType,7    Recipient,8    Royalty,9    Equipped,10    Pending,11    ResourceCollection,12    ResourcePriorities,13    PendingRemoval,14    Parts,15    Base,16    Src,17    Slot,18    License,19    Thumb,20    EquippedNft,21    BaseType,22    // // RmrkPartId(/* Id type? */)23    EquippableList,24    ZIndex,25    ThemeName,26    ThemeProperty(RmrkString),27    ThemeInherit,28}2930impl RmrkProperty {31    pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {32        fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {33            container.as_ref()34        }3536        macro_rules! key {37            ($($component:expr),+) => {38                PropertyKey::try_from([$(key!(@ &$component)),+].concat())39                    .map_err(|_| <Error<T>>::RmrkPropertyIsTooLong)40            };4142            (@ $key:expr) => {43                get_bytes($key)44            };45        }4647        match self {48            Self::Metadata => key!("metadata"),49            Self::CollectionType => key!("collection-type"),50            Self::Recipient => key!("recipient"),51            Self::Royalty => key!("royalty"),52            Self::Equipped => key!("equipped"),53            Self::Pending => key!("pending"),54            Self::ResourceCollection => key!("resource-collection"),55            Self::ResourcePriorities => key!("resource-priorities"),56            Self::PendingRemoval => key!("pending-removal"),57            Self::Parts => key!("parts"),58            Self::Base => key!("base"),59            Self::Src => key!("src"),60            Self::Slot => key!("slot"),61            Self::License => key!("license"),62            Self::Thumb => key!("thumb"),63            Self::EquippedNft => key!("equipped-nft"),64            Self::BaseType => key!("base-type"),65            // RmrkResourceId(/* Id type? */)66            // RmrkPartId(/* Id type? */)67            Self::EquippableList => key!("equippable-list"),68            Self::ZIndex => key!("z-index"),69            Self::ThemeName => key!("theme-name"),70            Self::ThemeProperty(name) => key!("theme-property-", name),71            Self::ThemeInherit => key!("theme-inherit"),72        }73    }74}7576#[macro_export]77macro_rules! rmrk_property {78    (Config=$cfg:ty, $key:ident: $value:expr) => {79        rmrk_property!(@$cfg, $key).map(|key| Property {80            key,81            value: $value.into()82        })83    };8485    (@$cfg:ty, $key:ident) => {86        $crate::RmrkProperty::$key.to_key::<$cfg>()87    };8889    (Config=$cfg:ty, $key:ident) => {90        PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key)?)91            .map_err(|_| <$crate::Error<$cfg>>::RmrkPropertyIsTooLong)92    };93}
after · pallets/proxy-rmrk-core/src/property.rs
1use super::*;2use core::convert::AsRef;34pub enum RmrkProperty {5    Metadata,6    CollectionType,7    Recipient,8    Royalty,9    Equipped,10    Pending,11    ResourceCollection,12    ResourcePriorities,13    PendingRemoval,14    Parts,15    Base,16    Src,17    Slot,18    License,19    Thumb,20    EquippedNft,21    BaseType,22    // // RmrkPartId(/* Id type? */)23    EquippableList,24    ZIndex,25    ThemeName,26    ThemeProperty(RmrkString),27    ThemeInherit,28}2930impl RmrkProperty {31    pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {32        fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {33            container.as_ref()34        }3536        macro_rules! key {37            ($($component:expr),+) => {38                PropertyKey::try_from([$(key!(@ &$component)),+].concat())39                    .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)40            };4142            (@ $key:expr) => {43                get_bytes($key)44            };45        }4647        match self {48            Self::Metadata => key!("metadata"),49            Self::CollectionType => key!("collection-type"),50            Self::Recipient => key!("recipient"),51            Self::Royalty => key!("royalty"),52            Self::Equipped => key!("equipped"),53            Self::Pending => key!("pending"),54            Self::ResourceCollection => key!("resource-collection"),55            Self::ResourcePriorities => key!("resource-priorities"),56            Self::PendingRemoval => key!("pending-removal"),57            Self::Parts => key!("parts"),58            Self::Base => key!("base"),59            Self::Src => key!("src"),60            Self::Slot => key!("slot"),61            Self::License => key!("license"),62            Self::Thumb => key!("thumb"),63            Self::EquippedNft => key!("equipped-nft"),64            Self::BaseType => key!("base-type"),65            // RmrkResourceId(/* Id type? */)66            // RmrkPartId(/* Id type? */)67            Self::EquippableList => key!("equippable-list"),68            Self::ZIndex => key!("z-index"),69            Self::ThemeName => key!("theme-name"),70            Self::ThemeProperty(name) => key!("theme-property-", name),71            Self::ThemeInherit => key!("theme-inherit"),72        }73    }74}7576#[macro_export]77macro_rules! rmrk_property {78    (Config=$cfg:ty, $key:ident: $value:expr) => {{79        let key = rmrk_property!(@$cfg, $key)?;8081        let value = $value.into_property_value()82            .map_err(<$crate::Error<$cfg>>::from)?;8384        Ok::<_, $crate::Error<$cfg>>(Property {85            key,86            value,87        })88    }};8990    (@$cfg:ty, $key:ident) => {91        $crate::RmrkProperty::$key.to_key::<$cfg>()92    };9394    (Config=$cfg:ty, $key:ident) => {95        PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key)?)96            .map_err(|_| <$crate::Error<$cfg>>::RmrkPropertyKeyIsTooLong)97    };98}
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -905,8 +905,20 @@
 	pub const RmrkPartsLimit: u32 = 3;
 }
 
-pub type RmrkCollectionInfo<AccountId> =
-	CollectionInfo<RmrkString, BoundedVec<u8, RmrkCollectionSymbolLimit>, AccountId>;
+impl From<RmrkCollectionId> for CollectionId {
+	fn from(id: RmrkCollectionId) -> Self {
+		Self(id)
+	}
+}
+
+impl From<RmrkNftId> for TokenId {
+	fn from(id: RmrkNftId) -> Self {
+		Self(id)
+	}
+}
+
+pub type RmrkCollectionSymbol = BoundedVec<u8, RmrkCollectionSymbolLimit>;
+pub type RmrkCollectionInfo<AccountId> = CollectionInfo<RmrkString, RmrkCollectionSymbol, AccountId>;
 pub type RmrkInstanceInfo<AccountId> = NftInfo<AccountId, Permill, RmrkString>;
 pub type RmrkResourceInfo = ResourceInfo<
 	BoundedVec<u8, RmrkResourceSymbolLimit>,