difftreelog
fix use CollectionIssuer enum for collection creation
in: master
6 files changed
pallets/common/src/dispatch.rsdiffbeforeafterboth--- a/pallets/common/src/dispatch.rs
+++ b/pallets/common/src/dispatch.rs
@@ -11,7 +11,7 @@
use sp_weights::Weight;
use up_data_structs::{CollectionId, CreateCollectionData};
-use crate::{pallet::Config, CommonCollectionOperations};
+use crate::{pallet::Config, CollectionIssuer, CommonCollectionOperations};
// TODO: move to benchmarking
/// Price of [`dispatch_tx`] call with noop `call` argument
@@ -72,26 +72,11 @@
/// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).
///
/// * `sender` - The user who will become the owner of the collection.
- /// * `payer` - If set, the user who pays the collection creation deposit.
+ /// * `issuer` - An entity that creates the collection.
/// * `data` - Description of the created collection.
fn create(
sender: T::CrossAccountId,
- payer: Option<T::CrossAccountId>,
- data: CreateCollectionData<T::CrossAccountId>,
- ) -> Result<CollectionId, DispatchError> {
- Self::create_raw(sender, payer, false, data)
- }
-
- /// Function for creating regular and special collections.
- ///
- /// * `sender` - The user who will become the owner of the collection.
- /// * `payer` - If set, the user who pays the collection creation deposit.
- /// * `data` - Description of the created collection.
- /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
- fn create_raw(
- sender: T::CrossAccountId,
- payer: Option<T::CrossAccountId>,
- is_special_collection: bool,
+ issuer: CollectionIssuer<T::CrossAccountId>,
data: CreateCollectionData<T::CrossAccountId>,
) -> Result<CollectionId, DispatchError>;
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -944,6 +944,15 @@
}
}
+/// An issuer of a collection.
+pub enum CollectionIssuer<CrossAccountId> {
+ /// A user who creates the collection.
+ User(CrossAccountId),
+
+ /// The internal mechanisms are creating the collection.
+ Internals,
+}
+
fn check_token_permissions<T: Config>(
collection_admin_permitted: bool,
token_owner_permitted: bool,
@@ -1128,32 +1137,34 @@
/// Create new collection.
///
/// * `owner` - The owner of the collection.
- /// * `payer` - If set, the user that will pay a deposit for the collection creation.
- /// * `data` - Description of the created collection.
+ /// * `issuer` - An entity that creates the collection.
/// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
pub fn init_collection(
owner: T::CrossAccountId,
- payer: Option<T::CrossAccountId>,
- is_special_collection: bool,
+ issuer: CollectionIssuer<T::CrossAccountId>,
data: CreateCollectionData<T::CrossAccountId>,
) -> Result<CollectionId, DispatchError> {
- if !is_special_collection {
- ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
- }
+ match issuer {
+ CollectionIssuer::User(payer) => {
+ ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
- // Take a (non-refundable) deposit of collection creation
- if let Some(payer) = payer {
- let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
- imbalance.subsume(<T as Config>::Currency::deposit(
- &T::TreasuryAccountId::get(),
- T::CollectionCreationPrice::get(),
- Precision::Exact,
- )?);
- let credit =
- <T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)
- .map_err(|_| Error::<T>::NotSufficientFounds)?;
+ // Take a (non-refundable) deposit of collection creation
+ let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
+ imbalance.subsume(<T as Config>::Currency::deposit(
+ &T::TreasuryAccountId::get(),
+ T::CollectionCreationPrice::get(),
+ Precision::Exact,
+ )?);
+ let credit = <T as Config>::Currency::settle(
+ payer.as_sub(),
+ imbalance,
+ Preservation::Preserve,
+ )
+ .map_err(|_| Error::<T>::NotSufficientFounds)?;
- debug_assert!(credit.peek().is_zero())
+ debug_assert!(credit.peek().is_zero());
+ }
+ CollectionIssuer::Internals => {}
}
{
pallets/foreign-assets/src/lib.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -56,6 +56,7 @@
#[frame_support::pallet]
pub mod module {
+ use pallet_common::CollectionIssuer;
use up_data_structs::{
CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,
};
@@ -169,12 +170,9 @@
.try_into()
.expect("description length < max description length; qed");
- let payer = None;
- let is_special_collection = true;
- let collection_id = T::CollectionDispatch::create_raw(
+ let collection_id = T::CollectionDispatch::create(
foreign_collection_owner,
- payer,
- is_special_collection,
+ CollectionIssuer::Internals,
CreateCollectionData {
name,
token_prefix,
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -26,7 +26,7 @@
dispatch::CollectionDispatch,
erc::{static_property::key, CollectionHelpersEvents},
eth::{self, collection_id_to_address, map_eth_to_id},
- CollectionById, CollectionHandle, Pallet as PalletCommon,
+ CollectionById, CollectionHandle, CollectionIssuer, Pallet as PalletCommon,
};
use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
use pallet_evm_coder_substrate::{
@@ -110,9 +110,12 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
}
@@ -241,9 +244,12 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
@@ -276,9 +282,12 @@
check_sent_amount_equals_collection_creation_price::<T>(value)?;
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -93,7 +93,8 @@
use frame_system::{ensure_root, ensure_signed};
use pallet_common::{
dispatch::{dispatch_tx, CollectionDispatch},
- CollectionHandle, CommonWeightInfo, Pallet as PalletCommon, RefungibleExtensionsWeightInfo,
+ CollectionHandle, CollectionIssuer, CommonWeightInfo, Pallet as PalletCommon,
+ RefungibleExtensionsWeightInfo,
};
use pallet_evm::account::CrossAccountId;
use pallet_structure::weights::WeightInfo as StructureWeightInfo;
@@ -401,7 +402,11 @@
// =========
let sender = T::CrossAccountId::from_sub(sender);
- let _id = T::CollectionDispatch::create(sender.clone(), Some(sender), data)?;
+ let _id = T::CollectionDispatch::create(
+ sender.clone(),
+ CollectionIssuer::User(sender),
+ data,
+ )?;
Ok(())
}
runtime/common/dispatch.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/>.1617use frame_support::{dispatch::DispatchResult, ensure, fail};18use pallet_balances_adapter::NativeFungibleHandle;19pub use pallet_common::dispatch::CollectionDispatch;20use pallet_common::{21 erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,22 CommonCollectionOperations, Pallet as PalletCommon,23};24use pallet_evm::{PrecompileHandle, PrecompileResult};25use pallet_fungible::{FungibleHandle, Pallet as PalletFungible};26use pallet_nonfungible::{NonfungibleHandle, Pallet as PalletNonfungible};27use pallet_refungible::{28 erc_token::RefungibleTokenHandle, Pallet as PalletRefungible, RefungibleHandle,29};30use sp_core::H160;31use sp_runtime::DispatchError;32use sp_std::{borrow::ToOwned, vec::Vec};33use up_data_structs::{34 mapping::TokenAddressMapping, CollectionId, CollectionMode, CreateCollectionData,35 MAX_DECIMAL_POINTS,36};3738pub enum CollectionDispatchT<T>39where40 T: pallet_fungible::Config41 + pallet_nonfungible::Config42 + pallet_refungible::Config43 + pallet_balances_adapter::Config,44{45 Fungible(FungibleHandle<T>),46 Nonfungible(NonfungibleHandle<T>),47 Refungible(RefungibleHandle<T>),48 NativeFungible(NativeFungibleHandle<T>),49}5051impl<T> CollectionDispatch<T> for CollectionDispatchT<T>52where53 T: pallet_common::Config54 + pallet_unique::Config55 + pallet_fungible::Config56 + pallet_nonfungible::Config57 + pallet_refungible::Config58 + pallet_balances_adapter::Config,59{60 fn check_is_internal(&self) -> DispatchResult {61 match self {62 Self::Fungible(h) => h.check_is_internal(),63 Self::Nonfungible(h) => h.check_is_internal(),64 Self::Refungible(h) => h.check_is_internal(),65 Self::NativeFungible(h) => h.check_is_internal(),66 }67 }6869 fn create_raw(70 sender: T::CrossAccountId,71 payer: Option<T::CrossAccountId>,72 is_special_collection: bool,73 data: CreateCollectionData<T::CrossAccountId>,74 ) -> Result<CollectionId, DispatchError> {75 match data.mode {76 CollectionMode::Fungible(decimal_points) => {77 // check params78 ensure!(79 decimal_points <= MAX_DECIMAL_POINTS,80 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded81 );82 }8384 #[cfg(not(feature = "refungible"))]85 CollectionMode::ReFungible => return unsupported!(T),8687 _ => {}88 };8990 <PalletCommon<T>>::init_collection(sender, payer, is_special_collection, data)91 }9293 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {94 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {95 fail!(<pallet_common::Error<T>>::UnsupportedOperation);96 }9798 let collection = <CollectionHandle<T>>::try_get(collection_id)?;99100 match collection.mode {101 CollectionMode::ReFungible => {102 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?103 }104 CollectionMode::Fungible(_) => {105 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?106 }107 CollectionMode::NFT => {108 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?109 }110 }111 Ok(())112 }113114 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {115 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {116 return Ok(Self::NativeFungible(117 NativeFungibleHandle::new_with_gas_limit(u64::MAX),118 ));119 }120121 let handle = <CollectionHandle<T>>::try_get(collection_id)?;122 Ok(match handle.mode {123 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),124 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),125 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),126 })127 }128129 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {130 match self {131 Self::Fungible(h) => h,132 Self::Nonfungible(h) => h,133 Self::Refungible(h) => h,134 Self::NativeFungible(h) => h,135 }136 }137}138139impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>140where141 T: pallet_common::Config142 + pallet_unique::Config143 + pallet_fungible::Config144 + pallet_nonfungible::Config145 + pallet_refungible::Config146 + pallet_balances_adapter::Config,147 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,148{149 fn is_reserved(target: &H160) -> bool {150 map_eth_to_id(target).is_some()151 }152 fn is_used(target: &H160) -> bool {153 map_eth_to_id(target)154 .map(<CollectionById<T>>::contains_key)155 .unwrap_or(false)156 }157 fn get_code(target: &H160) -> Option<Vec<u8>> {158 if let Some(collection_id) = map_eth_to_id(target) {159 let collection = <CollectionById<T>>::get(collection_id)?;160 Some(161 match collection.mode {162 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,163 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,164 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,165 }166 .to_owned(),167 )168 } else if let Some((collection_id, _token_id)) =169 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)170 {171 let collection = <CollectionById<T>>::get(collection_id)?;172 if collection.mode != CollectionMode::ReFungible {173 return None;174 }175 // TODO: check token existence176 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())177 } else {178 None179 }180 }181 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {182 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {183 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {184 <NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)185 } else {186 let collection = <CollectionHandle<T>>::new_with_gas_limit(187 collection_id,188 handle.remaining_gas(),189 )?;190191 match collection.mode {192 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),193 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),194 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),195 }196 }197 } else if let Some((collection_id, token_id)) =198 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(199 &handle.code_address(),200 ) {201 let collection =202 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;203 if collection.mode != CollectionMode::ReFungible {204 return None;205 }206207 let h = RefungibleHandle::cast(collection);208 // TODO: check token existence209 RefungibleTokenHandle(h, token_id).call(handle)210 } else {211 None212 }213 }214}