difftreelog
fix use rmrk type in proxy
in: master
4 files changed
pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::{DispatchError, traits::StaticLookup};22use up_data_structs::*;23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};24use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};25use pallet_evm::account::CrossAccountId;2627pub use pallet::*;2829pub mod misc;30pub mod property;3132use misc::*;33pub use property::*;3435#[frame_support::pallet]36pub mod pallet {37 use super::*;38 use pallet_evm::account;3940 #[pallet::config]41 pub trait Config: frame_system::Config42 + pallet_common::Config43 + pallet_nonfungible::Config44 + account::Config {45 type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;46 }4748 #[pallet::pallet]49 #[pallet::generate_store(pub(super) trait Store)]50 pub struct Pallet<T>(_);5152 #[pallet::event]53 #[pallet::generate_deposit(pub(super) fn deposit_event)]54 pub enum Event<T: Config> {55 CollectionCreated {56 issuer: T::AccountId,57 collection_id: RmrkCollectionId,58 },59 CollectionDestroyed {60 issuer: T::AccountId,61 collection_id: RmrkCollectionId,62 },63 IssuerChanged {64 old_issuer: T::AccountId,65 new_issuer: T::AccountId,66 collection_id: RmrkCollectionId,67 },68 CollectionLocked {69 issuer: T::AccountId,70 collection_id: RmrkCollectionId,71 },72 }7374 #[pallet::error]75 pub enum Error<T> {76 /* Unique-specific events */77 CorruptedCollectionType,78 NotRmrkCollection,79 RmrkPropertyKeyIsTooLong,80 RmrkPropertyValueIsTooLong,8182 /* RMRK compatible events */83 CollectionNotEmpty,84 NoAvailableCollectionId,85 CollectionUnknown,86 }8788 #[pallet::call]89 impl<T: Config> Pallet<T> {90 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]91 #[transactional]92 pub fn create_collection(93 origin: OriginFor<T>,94 metadata: RmrkString,95 max: Option<u32>,96 symbol: RmrkCollectionSymbol,97 ) -> DispatchResult {98 let sender = ensure_signed(origin)?;99100 let limits = max.map(|max| CollectionLimits {101 token_limit: Some(max),102 ..Default::default()103 });104105 let data = CreateCollectionData {106 limits,107 token_prefix: symbol.into_inner()108 .try_into()109 .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,110 ..Default::default()111 };112113 let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);114115 if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {116 return Err(<Error<T>>::NoAvailableCollectionId.into());117 }118119 let collection_id = collection_id_res?;120121 let collection = Self::get_nft_collection(collection_id)?.into_inner();122123 <PalletCommon<T>>::set_scoped_collection_properties(124 &collection,125 PropertyScope::Rmrk,126 [127 rmrk_property!(Config=T, Metadata: metadata)?,128 rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?,129 ].into_iter()130 )?;131132 Self::deposit_event(Event::CollectionCreated {133 issuer: sender,134 collection_id: collection_id.0135 });136137 Ok(())138 }139140 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]141 #[transactional]142 pub fn destroy_collection(143 origin: OriginFor<T>,144 collection_id: RmrkCollectionId,145 ) -> DispatchResult {146 let sender = ensure_signed(origin)?;147 let cross_sender = T::CrossAccountId::from_sub(sender.clone());148149 let unique_collection_id = collection_id.into();150151 let collection = Self::get_nft_collection(unique_collection_id)?;152153 Self::check_collection_type(unique_collection_id, CollectionType::Regular)?;154155 ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);156157 <PalletNft<T>>::destroy_collection(collection, &cross_sender)?;158159 Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });160161 Ok(())162 }163164 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]165 #[transactional]166 pub fn change_collection_issuer(167 origin: OriginFor<T>,168 collection_id: RmrkCollectionId,169 new_issuer: <T::Lookup as StaticLookup>::Source,170 ) -> DispatchResult {171 let sender = ensure_signed(origin)?;172173 let new_issuer = T::Lookup::lookup(new_issuer)?;174175 Self::change_collection_owner(176 collection_id.into(),177 CollectionType::Regular,178 sender.clone(),179 new_issuer.clone()180 )?;181182 Self::deposit_event(Event::IssuerChanged {183 old_issuer: sender,184 new_issuer,185 collection_id,186 });187188 Ok(())189 }190191 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]192 #[transactional]193 pub fn lock_collection(194 origin: OriginFor<T>,195 collection_id: RmrkCollectionId,196 ) -> DispatchResult {197 let sender = ensure_signed(origin)?;198 let cross_sender = T::CrossAccountId::from_sub(sender.clone());199200 let collection = Self::get_nft_collection(collection_id.into())?;201 collection.check_is_owner(&cross_sender)?;202203 let token_count = collection.total_supply();204205 let mut collection = collection.into_inner();206 collection.limits.token_limit = Some(token_count);207 collection.save()?;208209 Self::deposit_event(Event::CollectionLocked { issuer: sender, collection_id });210211 Ok(())212 }213 }214}215216impl<T: Config> Pallet<T> {217 fn change_collection_owner(218 collection_id: CollectionId,219 collection_type: CollectionType,220 sender: T::AccountId,221 new_owner: T::AccountId,222 ) -> DispatchResult {223 let mut collection = Self::get_nft_collection(collection_id)?.into_inner();224 collection.check_is_owner(&T::CrossAccountId::from_sub(sender))?;225226 Self::check_collection_type(collection_id, collection_type)?;227228 collection.owner = new_owner;229 collection.save()230 }231232 fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {233 let collection = <CollectionHandle<T>>::try_get(collection_id)234 .map_err(|_| <Error<T>>::CollectionUnknown)?235 .into_nft_collection()?;236237 Ok(collection)238 }239240 fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {241 let collection_type: CollectionType = <PalletCommon<T>>::collection_properties(collection_id)242 .get(&rmrk_property!(Config=T, CollectionType)?)243 .ok_or(<Error<T>>::NotRmrkCollection)?244 .try_into()245 .map_err(<Error<T>>::from)?;246247 Ok(collection_type)248 }249250 fn check_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {251 let actual_type = Self::get_collection_type(collection_id)?;252 ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);253254 Ok(())255 }256}pallets/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,
pallets/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)
};
}
primitives/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>,