difftreelog
refactor create collection with optional payer
in: master
5 files changed
pallets/common/src/dispatch.rsdiffbeforeafterboth--- a/pallets/common/src/dispatch.rs
+++ b/pallets/common/src/dispatch.rs
@@ -72,20 +72,11 @@
/// Create a 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` - The user who pays the collection creation fee.
+ /// * `payer` - If set, the user who pays the collection creation deposit.
/// * `data` - Description of the created collection.
fn create(
sender: T::CrossAccountId,
- payer: T::CrossAccountId,
- data: CreateCollectionData<T::CrossAccountId>,
- ) -> Result<CollectionId, DispatchError>;
-
- /// Create a foreign 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.
- /// * `data` - Description of the created collection.
- fn create_foreign(
- sender: T::CrossAccountId,
+ payer: Option<T::CrossAccountId>,
data: CreateCollectionData<T::CrossAccountId>,
) -> Result<CollectionId, DispatchError>;
pallets/foreign-assets/src/lib.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -152,8 +152,10 @@
.try_into()
.expect("description length < max description length; qed");
- let collection_id = T::CollectionDispatch::create_foreign(
+ let payer = None;
+ let collection_id = T::CollectionDispatch::create(
foreign_collection_owner,
+ payer,
CreateCollectionData {
name,
description,
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -110,8 +110,9 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id = T::CollectionDispatch::create(caller, collection_helpers_address, data)
- .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+ let collection_id =
+ T::CollectionDispatch::create(caller, Some(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)
}
@@ -240,8 +241,9 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id = T::CollectionDispatch::create(caller, collection_helpers_address, data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id =
+ T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
+ .map_err(dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
@@ -274,8 +276,9 @@
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, collection_helpers_address, data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id =
+ T::CollectionDispatch::create(caller, Some(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
@@ -401,7 +401,7 @@
// =========
let sender = T::CrossAccountId::from_sub(sender);
- let _id = T::CollectionDispatch::create(sender.clone(), sender, data)?;
+ let _id = T::CollectionDispatch::create(sender.clone(), Some(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, unsupported, 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(70 sender: T::CrossAccountId,71 payer: T::CrossAccountId,72 data: CreateCollectionData<T::CrossAccountId>,73 ) -> Result<CollectionId, DispatchError> {74 match data.mode {75 CollectionMode::Fungible(decimal_points) => {76 // check params77 ensure!(78 decimal_points <= MAX_DECIMAL_POINTS,79 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded80 );81 }8283 #[cfg(not(feature = "refungible"))]84 CollectionMode::ReFungible => return unsupported!(T),8586 _ => {}87 };8889 <PalletCommon<T>>::init_collection(sender, Some(payer), data)90 }9192 fn create_foreign(93 sender: <T>::CrossAccountId,94 data: CreateCollectionData<<T>::CrossAccountId>,95 ) -> Result<CollectionId, DispatchError> {96 match data.mode {97 CollectionMode::Fungible(decimal_points) => {98 // check params99 ensure!(100 decimal_points <= MAX_DECIMAL_POINTS,101 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded102 );103 }104105 CollectionMode::ReFungible => return unsupported!(T),106 _ => {}107 };108109 let payer = None;110 <PalletCommon<T>>::init_collection(sender, payer, data)111 }112113 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {114 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {115 fail!(<pallet_common::Error<T>>::UnsupportedOperation);116 }117118 let collection = <CollectionHandle<T>>::try_get(collection_id)?;119120 match collection.mode {121 CollectionMode::ReFungible => {122 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?123 }124 CollectionMode::Fungible(_) => {125 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?126 }127 CollectionMode::NFT => {128 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?129 }130 }131 Ok(())132 }133134 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {135 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {136 return Ok(Self::NativeFungible(137 NativeFungibleHandle::new_with_gas_limit(u64::MAX),138 ));139 }140141 let handle = <CollectionHandle<T>>::try_get(collection_id)?;142 Ok(match handle.mode {143 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),144 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),145 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),146 })147 }148149 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {150 match self {151 Self::Fungible(h) => h,152 Self::Nonfungible(h) => h,153 Self::Refungible(h) => h,154 Self::NativeFungible(h) => h,155 }156 }157}158159impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>160where161 T: pallet_common::Config162 + pallet_unique::Config163 + pallet_fungible::Config164 + pallet_nonfungible::Config165 + pallet_refungible::Config166 + pallet_balances_adapter::Config,167 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,168{169 fn is_reserved(target: &H160) -> bool {170 map_eth_to_id(target).is_some()171 }172 fn is_used(target: &H160) -> bool {173 map_eth_to_id(target)174 .map(<CollectionById<T>>::contains_key)175 .unwrap_or(false)176 }177 fn get_code(target: &H160) -> Option<Vec<u8>> {178 if let Some(collection_id) = map_eth_to_id(target) {179 let collection = <CollectionById<T>>::get(collection_id)?;180 Some(181 match collection.mode {182 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,183 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,184 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,185 }186 .to_owned(),187 )188 } else if let Some((collection_id, _token_id)) =189 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)190 {191 let collection = <CollectionById<T>>::get(collection_id)?;192 if collection.mode != CollectionMode::ReFungible {193 return None;194 }195 // TODO: check token existence196 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())197 } else {198 None199 }200 }201 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {202 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {203 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {204 <NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)205 } else {206 let collection = <CollectionHandle<T>>::new_with_gas_limit(207 collection_id,208 handle.remaining_gas(),209 )?;210211 match collection.mode {212 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),213 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),214 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),215 }216 }217 } else if let Some((collection_id, token_id)) =218 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(219 &handle.code_address(),220 ) {221 let collection =222 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;223 if collection.mode != CollectionMode::ReFungible {224 return None;225 }226227 let h = RefungibleHandle::cast(collection);228 // TODO: check token existence229 RefungibleTokenHandle(h, token_id).call(handle)230 } else {231 None232 }233 }234}