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
1616
17#![cfg_attr(not(feature = "std"), no_std)]17#![cfg_attr(not(feature = "std"), no_std)]
1818
19use frame_support::{pallet_prelude::*, transactional, BoundedVec, traits::ConstU32, dispatch::DispatchResult};19use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};
20use frame_system::{pallet_prelude::*, ensure_signed};20use frame_system::{pallet_prelude::*, ensure_signed};
21use sp_runtime::DispatchError;21use sp_runtime::{DispatchError, traits::StaticLookup};
22use up_data_structs::*;22use up_data_structs::*;
23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};
24use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};24use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};
54 pub enum Event<T: Config> {54 pub enum Event<T: Config> {
55 CollectionCreated {55 CollectionCreated {
56 issuer: T::AccountId,56 issuer: T::AccountId,
57 collection_id: CollectionId,57 collection_id: RmrkCollectionId,
58 },58 },
59 CollectionDestroyed {59 CollectionDestroyed {
60 issuer: T::AccountId,60 issuer: T::AccountId,
61 collection_id: CollectionId,61 collection_id: RmrkCollectionId,
62 },62 },
63 IssuerChanged {
64 old_issuer: T::AccountId,
65 new_issuer: T::AccountId,
66 collection_id: RmrkCollectionId,
67 },
63 CollectionLocked {68 CollectionLocked {
64 issuer: T::AccountId,69 issuer: T::AccountId,
65 collection_id: CollectionId,70 collection_id: RmrkCollectionId,
66 },71 },
67 }72 }
6873
71 /* Unique-specific events */76 /* Unique-specific events */
72 CorruptedCollectionType,77 CorruptedCollectionType,
73 NotRmrkCollection,78 NotRmrkCollection,
74 RmrkPropertyIsTooLong,79 RmrkPropertyKeyIsTooLong,
80 RmrkPropertyValueIsTooLong,
7581
76 /* RMRK compatible events */82 /* RMRK compatible events */
77 CollectionNotEmpty,83 CollectionNotEmpty,
85 #[transactional]91 #[transactional]
86 pub fn create_collection(92 pub fn create_collection(
87 origin: OriginFor<T>,93 origin: OriginFor<T>,
88 metadata: PropertyValue,94 metadata: RmrkString,
89 max: Option<u32>,95 max: Option<u32>,
90 symbol: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,96 symbol: RmrkCollectionSymbol,
91 ) -> DispatchResult {97 ) -> DispatchResult {
92 let sender = ensure_signed(origin)?;98 let sender = ensure_signed(origin)?;
9399
98104
99 let data = CreateCollectionData {105 let data = CreateCollectionData {
100 limits,106 limits,
101 token_prefix: symbol,107 token_prefix: symbol.into_inner()
108 .try_into()
109 .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
102 ..Default::default()110 ..Default::default()
103 };111 };
104112
123131
124 Self::deposit_event(Event::CollectionCreated { issuer: sender, collection_id });132 Self::deposit_event(Event::CollectionCreated {
133 issuer: sender,
134 collection_id: collection_id.0
135 });
125136
126 Ok(())137 Ok(())
130 #[transactional]141 #[transactional]
131 pub fn destroy_collection(142 pub fn destroy_collection(
132 origin: OriginFor<T>,143 origin: OriginFor<T>,
133 collection_id: CollectionId,144 collection_id: RmrkCollectionId,
134 ) -> DispatchResult {145 ) -> DispatchResult {
135 let sender = ensure_signed(origin)?;146 let sender = ensure_signed(origin)?;
136 let cross_sender = T::CrossAccountId::from_sub(sender.clone());147 let cross_sender = T::CrossAccountId::from_sub(sender.clone());
148
149 let unique_collection_id = collection_id.into();
137150
138 let collection = Self::get_nft_collection(collection_id)?;151 let collection = Self::get_nft_collection(unique_collection_id)?;
139152
140 Self::check_collection_type(collection_id, CollectionType::Regular)?;153 Self::check_collection_type(unique_collection_id, CollectionType::Regular)?;
141154
142 ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);155 ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
143156
152 #[transactional]165 #[transactional]
153 pub fn change_collection_issuer(166 pub fn change_collection_issuer(
154 origin: OriginFor<T>,167 origin: OriginFor<T>,
155 collection_id: CollectionId,168 collection_id: RmrkCollectionId,
156 new_issuer: T::AccountId,169 new_issuer: <T::Lookup as StaticLookup>::Source,
157 ) -> DispatchResult {170 ) -> DispatchResult {
158 let sender = ensure_signed(origin)?;171 let sender = ensure_signed(origin)?;
172
173 let new_issuer = T::Lookup::lookup(new_issuer)?;
159174
160 Self::change_collection_owner(175 Self::change_collection_owner(
161 collection_id,176 collection_id.into(),
162 CollectionType::Regular,177 CollectionType::Regular,
163 sender,178 sender.clone(),
164 new_issuer179 new_issuer.clone()
165 )?;180 )?;
181
182 Self::deposit_event(Event::IssuerChanged {
183 old_issuer: sender,
184 new_issuer,
185 collection_id,
186 });
166187
167 Ok(())188 Ok(())
168 }189 }
171 #[transactional]192 #[transactional]
172 pub fn lock_collection(193 pub fn lock_collection(
173 origin: OriginFor<T>,194 origin: OriginFor<T>,
174 collection_id: CollectionId,195 collection_id: RmrkCollectionId,
175 ) -> DispatchResult {196 ) -> DispatchResult {
176 let sender = ensure_signed(origin)?;197 let sender = ensure_signed(origin)?;
177 let cross_sender = T::CrossAccountId::from_sub(sender.clone());198 let cross_sender = T::CrossAccountId::from_sub(sender.clone());
178199
179 let collection = Self::get_nft_collection(collection_id)?;200 let collection = Self::get_nft_collection(collection_id.into())?;
180 collection.check_is_owner(&cross_sender)?;201 collection.check_is_owner(&cross_sender)?;
181202
182 let token_count = collection.total_supply();203 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
--- a/pallets/proxy-rmrk-core/src/property.rs
+++ b/pallets/proxy-rmrk-core/src/property.rs
@@ -36,7 +36,7 @@
         macro_rules! key {
             ($($component:expr),+) => {
                 PropertyKey::try_from([$(key!(@ &$component)),+].concat())
-                    .map_err(|_| <Error<T>>::RmrkPropertyIsTooLong)
+                    .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)
             };
 
             (@ $key:expr) => {
@@ -75,12 +75,17 @@
 
 #[macro_export]
 macro_rules! rmrk_property {
-    (Config=$cfg:ty, $key:ident: $value:expr) => {
-        rmrk_property!(@$cfg, $key).map(|key| Property {
+    (Config=$cfg:ty, $key:ident: $value:expr) => {{
+        let key = rmrk_property!(@$cfg, $key)?;
+
+        let value = $value.into_property_value()
+            .map_err(<$crate::Error<$cfg>>::from)?;
+
+        Ok::<_, $crate::Error<$cfg>>(Property {
             key,
-            value: $value.into()
+            value,
         })
-    };
+    }};
 
     (@$cfg:ty, $key:ident) => {
         $crate::RmrkProperty::$key.to_key::<$cfg>()
@@ -88,6 +93,6 @@
 
     (Config=$cfg:ty, $key:ident) => {
         PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key)?)
-            .map_err(|_| <$crate::Error<$cfg>>::RmrkPropertyIsTooLong)
+            .map_err(|_| <$crate::Error<$cfg>>::RmrkPropertyKeyIsTooLong)
     };
 }
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>,