From 6773058243b9cd5906d34c0a6f50e2d7f97039e7 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 19 May 2022 16:58:29 +0000 Subject: [PATCH] fix: use rmrk type in proxy --- --- 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 { 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, - metadata: PropertyValue, + metadata: RmrkString, max: Option, - symbol: BoundedVec>, + 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(|_| >::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, - 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, >::CollectionNotEmpty); @@ -152,18 +165,26 @@ #[transactional] pub fn change_collection_issuer( origin: OriginFor, - collection_id: CollectionId, - new_issuer: T::AccountId, + collection_id: RmrkCollectionId, + new_issuer: ::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, - 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(); --- 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 { + self.encode() + .try_into() + .map_err(|_| MiscError::RmrkPropertyValueIsTooLong) } } @@ -25,12 +27,14 @@ } pub enum MiscError { + RmrkPropertyValueIsTooLong, CorruptedCollectionType, } impl From for Error { 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; +} + +impl> IntoPropertyValue for BoundedVec { + fn into_property_value(self) -> Result { + self.into_inner() + .try_into() + .map_err(|_| MiscError::RmrkPropertyValueIsTooLong) + } +} + #[derive(Encode, Decode, PartialEq, Eq)] pub enum CollectionType { Regular, --- 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(|_| >::RmrkPropertyIsTooLong) + .map_err(|_| >::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) }; } --- 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 = - CollectionInfo, AccountId>; +impl From for CollectionId { + fn from(id: RmrkCollectionId) -> Self { + Self(id) + } +} + +impl From for TokenId { + fn from(id: RmrkNftId) -> Self { + Self(id) + } +} + +pub type RmrkCollectionSymbol = BoundedVec; +pub type RmrkCollectionInfo = CollectionInfo; pub type RmrkInstanceInfo = NftInfo; pub type RmrkResourceInfo = ResourceInfo< BoundedVec, -- gitstuff