difftreelog
feat add proxy pallet
in: master
3 files changed
pallets/rmrk-proxy/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/pallets/rmrk-proxy/Cargo.toml
@@ -0,0 +1,49 @@
+[package]
+name = "pallet-rmrk-proxy"
+version = "0.1.0"
+license = "GPLv3"
+edition = "2021"
+
+[dependencies.codec]
+default-features = false
+features = ['derive']
+package = 'parity-scale-codec'
+version = '3.1.2'
+
+[dependencies]
+frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+pallet-common = { default-features = false, path = '../common' }
+pallet-nonfungible = { default-features = false, path = "../../pallets/nonfungible" }
+# pallet-structure = { default-features = false, path = '../structure' }
+up-data-structs = { default-features = false, path = '../../primitives/data-structs' }
+# evm-coder = { default-features = false, path = '../../crates/evm-coder' }
+# pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.21-logs" }
+frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
+
+[features]
+default = ["std"]
+std = [
+ "frame-support/std",
+ "frame-system/std",
+ "sp-runtime/std",
+ "sp-std/std",
+ "up-data-structs/std",
+ "pallet-common/std",
+ "pallet-nonfungible/std",
+ "pallet-evm/std",
+ # "pallet-structure/std",
+ # "evm-coder/std",
+ # "pallet-evm-coder-substrate/std",
+ 'frame-benchmarking/std',
+]
+runtime-benchmarks = [
+ 'frame-benchmarking',
+ 'frame-support/runtime-benchmarks',
+ 'frame-system/runtime-benchmarks',
+]
pallets/rmrk-proxy/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, traits::ConstU32, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::DispatchError;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;3031use misc::*;3233#[frame_support::pallet]34pub mod pallet {35 use super::*;36 use pallet_evm::account;3738 #[pallet::config]39 pub trait Config: frame_system::Config40 + pallet_common::Config41 + pallet_nonfungible::Config42 + account::Config {43 type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;44 }4546 #[pallet::pallet]47 #[pallet::generate_store(pub(super) trait Store)]48 pub struct Pallet<T>(_);4950 #[pallet::event]51 #[pallet::generate_deposit(pub(super) fn deposit_event)]52 pub enum Event<T: Config> {53 CollectionCreated {54 issuer: T::AccountId,55 collection_id: CollectionId,56 },57 CollectionDestroyed {58 issuer: T::AccountId,59 collection_id: CollectionId,60 },61 CollectionLocked {62 issuer: T::AccountId,63 collection_id: CollectionId,64 },65 }6667 #[pallet::error]68 pub enum Error<T> {69 CorruptedCollectionType,70 NotRmrkCollection,71 CollectionNotEmpty,72 NoAvailableCollectionId,73 CollectionUnknown,74 }7576 #[pallet::call]77 impl<T: Config> Pallet<T> {78 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]79 #[transactional]80 pub fn create_collection(81 origin: OriginFor<T>,82 metadata: PropertyValue,83 max: Option<u32>,84 symbol: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,85 ) -> DispatchResult {86 let sender = ensure_signed(origin)?;8788 let limits = max.map(|max| CollectionLimits {89 token_limit: Some(max),90 ..Default::default()91 });9293 let data = CreateCollectionData {94 limits,95 token_prefix: symbol,96 ..Default::default()97 };9899 let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);100101 if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {102 return Err(<Error<T>>::NoAvailableCollectionId.into());103 }104105 let collection_id = collection_id_res?;106107 let collection = Self::get_nft_collection(collection_id)?.into_inner();108109 <PalletCommon<T>>::set_scoped_collection_properties(110 &collection,111 PropertyScope::Rmrk,112 [113 rmrk_property!(Metadata, metadata),114 rmrk_property!(CollectionType, CollectionType::Regular),115 ].into_iter()116 )?;117118 Self::deposit_event(Event::CollectionCreated { issuer: sender, collection_id });119120 Ok(())121 }122123 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]124 #[transactional]125 pub fn destroy_collection(126 origin: OriginFor<T>,127 collection_id: CollectionId,128 ) -> DispatchResult {129 let sender = ensure_signed(origin)?;130 let cross_sender = T::CrossAccountId::from_sub(sender.clone());131132 let collection = Self::get_nft_collection(collection_id)?;133134 Self::check_collection_type(collection_id, CollectionType::Regular)?;135136 ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);137138 <PalletNft<T>>::destroy_collection(collection, &cross_sender)?;139140 Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });141142 Ok(())143 }144145 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]146 #[transactional]147 pub fn change_collection_issuer(148 origin: OriginFor<T>,149 collection_id: CollectionId,150 new_issuer: T::AccountId,151 ) -> DispatchResult {152 let sender = ensure_signed(origin)?;153154 Self::change_collection_owner(155 collection_id,156 CollectionType::Regular,157 sender,158 new_issuer159 )?;160161 Ok(())162 }163164 #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]165 #[transactional]166 pub fn lock_collection(167 origin: OriginFor<T>,168 collection_id: CollectionId,169 ) -> DispatchResult {170 let sender = ensure_signed(origin)?;171 let cross_sender = T::CrossAccountId::from_sub(sender.clone());172173 let collection = Self::get_nft_collection(collection_id)?;174 collection.check_is_owner(&cross_sender)?;175176 let token_count = collection.total_supply();177178 let mut collection = collection.into_inner();179 collection.limits.token_limit = Some(token_count);180 collection.save()?;181182 Self::deposit_event(Event::CollectionLocked { issuer: sender, collection_id });183184 Ok(())185 }186 }187}188189impl<T: Config> Pallet<T> {190 fn change_collection_owner(191 collection_id: CollectionId,192 collection_type: CollectionType,193 sender: T::AccountId,194 new_owner: T::AccountId,195 ) -> DispatchResult {196 let mut collection = Self::get_nft_collection(collection_id)?.into_inner();197 collection.check_is_owner(&T::CrossAccountId::from_sub(sender))?;198199 Self::check_collection_type(collection_id, collection_type)?;200201 collection.owner = new_owner;202 collection.save()203 }204205 fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {206 let collection = <CollectionHandle<T>>::try_get(collection_id)207 .map_err(|_| <Error<T>>::CollectionUnknown)?208 .into_nft_collection()?;209210 Ok(collection)211 }212213 fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {214 let collection_type: CollectionType = <PalletCommon<T>>::collection_properties(collection_id)215 .get(&rmrk_property!(CollectionType))216 .ok_or(<Error<T>>::NotRmrkCollection)?217 .try_into()218 .map_err(<Error<T>>::from)?;219220 Ok(collection_type)221 }222223 fn check_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {224 let actual_type = Self::get_collection_type(collection_id)?;225 ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);226227 Ok(())228 }229}pallets/rmrk-proxy/src/misc.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/rmrk-proxy/src/misc.rs
@@ -0,0 +1,93 @@
+use super::*;
+use codec::{Encode, Decode};
+use pallet_nonfungible::NonfungibleHandle;
+
+#[macro_export]
+macro_rules! rmrk_property {
+ ($key:ident, $value:expr) => {
+ Property {
+ key: rmrk_property!($key),
+ value: $value.into()
+ }
+ };
+
+ ($key:ident) => {
+ RmrkProperty::$key.to_key()
+ };
+}
+
+macro_rules! impl_rmrk_value {
+ ($enum_name:path, decode_error: $error:ident) => {
+ impl Into<PropertyValue> for $enum_name {
+ fn into(self) -> PropertyValue {
+ self.encode().try_into().unwrap()
+ }
+ }
+
+ 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)
+ }
+ }
+
+ };
+}
+
+pub enum MiscError {
+ CorruptedCollectionType,
+}
+
+impl<T: Config> From<MiscError> for Error<T> {
+ fn from(error: MiscError) -> Self {
+ match error {
+ 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>>::NotRmrkCollection)
+ }
+ }
+}
+
+pub enum RmrkProperty {
+ Metadata,
+ CollectionType,
+}
+
+impl RmrkProperty {
+ pub fn to_key(self) -> PropertyKey {
+ let key = |str_key: &str| {
+ PropertyKey::try_from(
+ str_key.bytes().collect::<Vec<_>>()
+ ).unwrap()
+ };
+
+ match self {
+ Self::Metadata => key("metadata"),
+ Self::CollectionType => key("collection-type"),
+ }
+ }
+}
+
+#[derive(Encode, Decode, PartialEq, Eq)]
+pub enum CollectionType {
+ Regular,
+ Resource,
+ Base,
+}
+
+impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);