git.delta.rocks / unique-network / refs/commits / 9ca985e04f6a

difftreelog

decoupling of unique and refungible pallets

Grigoriy Simonov2022-06-28parent: #af3427e.patch.diff
in: master

16 files changed

modified.gitignorediffbeforeafterboth
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@
 
 /.idea/
 /.cargo/
+/.vscode/
 
 tests/.vscode
 cumulus-parachain/
modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6608,7 +6608,6 @@
  "pallet-evm",
  "pallet-evm-coder-substrate",
  "pallet-nonfungible",
- "pallet-refungible",
  "parity-scale-codec 3.1.5",
  "scale-info",
  "serde",
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1258,6 +1258,10 @@
 	}
 }
 
+pub trait RefungibleExtensionsWeightInfo {
+	fn repartition() -> Weight;
+}
+
 pub trait CommonCollectionOperations<T: Config> {
 	fn create_item(
 		&self,
@@ -1384,6 +1388,19 @@
 		spender: T::CrossAccountId,
 		token: TokenId,
 	) -> u128;
+	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>>;
+}
+
+pub trait RefungibleExtensions<T>
+where
+	T: Config,
+{
+	fn repartition(
+		&self,
+		owner: &T::CrossAccountId,
+		token: TokenId,
+		amount: u128,
+	) -> DispatchResultWithPostInfo;
 }
 
 // Flexible enough for implementing CommonCollectionOperations
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -18,7 +18,7 @@
 
 use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, traits::Get};
 use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};
-use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
+use pallet_common::{CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight};
 use pallet_structure::Error as StructureError;
 use sp_runtime::ArithmeticError;
 use sp_std::{vec::Vec, vec};
@@ -399,4 +399,8 @@
 		}
 		<Allowance<T>>::get((self.id, sender, spender))
 	}
+
+	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {
+		None
+	}
 }
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -22,7 +22,8 @@
 	PropertyKeyPermission, PropertyValue,
 };
 use pallet_common::{
-	CommonCollectionOperations, CommonWeightInfo, with_weight, weights::WeightInfo as _,
+	CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,
+	weights::WeightInfo as _,
 };
 use sp_runtime::DispatchError;
 use sp_std::vec::Vec;
@@ -467,4 +468,8 @@
 			0
 		}
 	}
+
+	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {
+		None
+	}
 }
modifiedpallets/refungible/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/refungible/src/benchmarking.rs
+++ b/pallets/refungible/src/benchmarking.rs
@@ -203,4 +203,12 @@
 		let item = create_max_item(&collection, &owner, [(sender.clone(), 200)])?;
 		<Pallet<T>>::set_allowance(&collection, &sender, &burner, item, 200)?;
 	}: {<Pallet<T>>::burn_from(&collection, &burner, &sender, item, 200, &Unlimited)?}
+
+	repartition_item {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			sender: cross_from_sub(owner); owner: cross_sub;
+		};
+		let item = create_max_item(&collection, &sender, [(owner.clone(), 100)])?;
+	}: {<Pallet<T>>::repartition(&collection, &owner, item, 200)?}
 }
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -22,9 +22,9 @@
 	CollectionId, TokenId, CreateItemExData, CreateRefungibleExData, budget::Budget, Property,
 	PropertyKey, PropertyValue, PropertyKeyPermission, CreateItemData,
 };
-use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
+use pallet_common::{CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight};
 use pallet_structure::Error as StructureError;
-use sp_runtime::DispatchError;
+use sp_runtime::{DispatchError};
 use sp_std::{vec::Vec, vec};
 
 use crate::{
@@ -405,4 +405,22 @@
 	) -> u128 {
 		<Allowance<T>>::get((self.id, token, sender, spender))
 	}
+
+	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {
+		Some(self)
+	}
+}
+
+impl<T: Config> RefungibleExtensions<T> for RefungibleHandle<T> {
+	fn repartition(
+		&self,
+		owner: &T::CrossAccountId,
+		token: TokenId,
+		amount: u128,
+	) -> DispatchResultWithPostInfo {
+		with_weight(
+			<Pallet<T>>::repartition(self, owner, token, amount),
+			<SelfWeightOf<T>>::repartition_item(),
+		)
+	}
 }
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
before · pallets/refungible/src/lib.rs
1// 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::{ensure, BoundedVec};20use up_data_structs::{21	AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,22	CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,23};24use pallet_evm::account::CrossAccountId;25use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon};26use pallet_structure::Pallet as PalletStructure;27use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};28use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};29use core::ops::Deref;30use codec::{Encode, Decode, MaxEncodedLen};31use scale_info::TypeInfo;3233pub use pallet::*;34#[cfg(feature = "runtime-benchmarks")]35pub mod benchmarking;36pub mod common;37pub mod erc;38pub mod weights;39pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;4041#[struct_versioning::versioned(version = 2, upper)]42#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)]43pub struct ItemData {44	pub const_data: BoundedVec<u8, CustomDataLimit>,4546	#[version(..2)]47	pub variable_data: BoundedVec<u8, CustomDataLimit>,48}4950#[frame_support::pallet]51pub mod pallet {52	use super::*;53	use frame_support::{54		Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key,55		traits::StorageVersion,56	};57	use frame_system::pallet_prelude::*;58	use up_data_structs::{CollectionId, TokenId};59	use super::weights::WeightInfo;6061	#[pallet::error]62	pub enum Error<T> {63		/// Not Refungible item data used to mint in Refungible collection.64		NotRefungibleDataUsedToMintFungibleCollectionToken,65		/// Maximum refungibility exceeded66		WrongRefungiblePieces,67		/// Refungible token can't be repartitioned by user who isn't owns all pieces68		RepartitionWhileNotOwningAllPieces,69		/// Refungible token can't nest other tokens70		RefungibleDisallowsNesting,71		/// Setting item properties is not allowed72		SettingPropertiesNotAllowed,73	}7475	#[pallet::config]76	pub trait Config:77		frame_system::Config + pallet_common::Config + pallet_structure::Config78	{79		type WeightInfo: WeightInfo;80	}8182	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);8384	#[pallet::pallet]85	#[pallet::storage_version(STORAGE_VERSION)]86	#[pallet::generate_store(pub(super) trait Store)]87	pub struct Pallet<T>(_);8889	#[pallet::storage]90	pub type TokensMinted<T: Config> =91		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;92	#[pallet::storage]93	pub type TokensBurnt<T: Config> =94		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;9596	#[pallet::storage]97	pub type TokenData<T: Config> = StorageNMap<98		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),99		Value = ItemData,100		QueryKind = ValueQuery,101	>;102103	#[pallet::storage]104	pub type TotalSupply<T: Config> = StorageNMap<105		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),106		Value = u128,107		QueryKind = ValueQuery,108	>;109110	/// Used to enumerate tokens owned by account111	#[pallet::storage]112	pub type Owned<T: Config> = StorageNMap<113		Key = (114			Key<Twox64Concat, CollectionId>,115			Key<Blake2_128Concat, T::CrossAccountId>,116			Key<Twox64Concat, TokenId>,117		),118		Value = bool,119		QueryKind = ValueQuery,120	>;121122	#[pallet::storage]123	pub type AccountBalance<T: Config> = StorageNMap<124		Key = (125			Key<Twox64Concat, CollectionId>,126			// Owner127			Key<Blake2_128Concat, T::CrossAccountId>,128		),129		Value = u32,130		QueryKind = ValueQuery,131	>;132133	#[pallet::storage]134	pub type Balance<T: Config> = StorageNMap<135		Key = (136			Key<Twox64Concat, CollectionId>,137			Key<Twox64Concat, TokenId>,138			// Owner139			Key<Blake2_128Concat, T::CrossAccountId>,140		),141		Value = u128,142		QueryKind = ValueQuery,143	>;144145	#[pallet::storage]146	pub type Allowance<T: Config> = StorageNMap<147		Key = (148			Key<Twox64Concat, CollectionId>,149			Key<Twox64Concat, TokenId>,150			// Owner151			Key<Blake2_128, T::CrossAccountId>,152			// Spender153			Key<Blake2_128Concat, T::CrossAccountId>,154		),155		Value = u128,156		QueryKind = ValueQuery,157	>;158159	#[pallet::hooks]160	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {161		fn on_runtime_upgrade() -> Weight {162			if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {163				<TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {164					Some(<ItemDataVersion2>::from(v))165				})166			}167168			0169		}170	}171}172173pub struct RefungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);174impl<T: Config> RefungibleHandle<T> {175	pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {176		Self(inner)177	}178	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {179		self.0180	}181}182impl<T: Config> Deref for RefungibleHandle<T> {183	type Target = pallet_common::CollectionHandle<T>;184185	fn deref(&self) -> &Self::Target {186		&self.0187	}188}189190impl<T: Config> Pallet<T> {191	pub fn total_supply(collection: &RefungibleHandle<T>) -> u32 {192		<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)193	}194	pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {195		<TotalSupply<T>>::contains_key((collection.id, token))196	}197}198199// unchecked calls skips any permission checks200impl<T: Config> Pallet<T> {201	pub fn init_collection(202		owner: T::CrossAccountId,203		data: CreateCollectionData<T::AccountId>,204	) -> Result<CollectionId, DispatchError> {205		<PalletCommon<T>>::init_collection(owner, data, false)206	}207	pub fn destroy_collection(208		collection: RefungibleHandle<T>,209		sender: &T::CrossAccountId,210	) -> DispatchResult {211		let id = collection.id;212213		if Self::collection_has_tokens(id) {214			return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());215		}216217		// =========218219		PalletCommon::destroy_collection(collection.0, sender)?;220221		<TokensMinted<T>>::remove(id);222		<TokensBurnt<T>>::remove(id);223		<TokenData<T>>::remove_prefix((id,), None);224		<TotalSupply<T>>::remove_prefix((id,), None);225		<Balance<T>>::remove_prefix((id,), None);226		<Allowance<T>>::remove_prefix((id,), None);227		<Owned<T>>::remove_prefix((id,), None);228		<AccountBalance<T>>::remove_prefix((id,), None);229		Ok(())230	}231232	fn collection_has_tokens(collection_id: CollectionId) -> bool {233		<TokenData<T>>::iter_prefix((collection_id,))234			.next()235			.is_some()236	}237238	pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {239		let burnt = <TokensBurnt<T>>::get(collection.id)240			.checked_add(1)241			.ok_or(ArithmeticError::Overflow)?;242243		<TokensBurnt<T>>::insert(collection.id, burnt);244		<TokenData<T>>::remove((collection.id, token_id));245		<TotalSupply<T>>::remove((collection.id, token_id));246		<Balance<T>>::remove_prefix((collection.id, token_id), None);247		<Allowance<T>>::remove_prefix((collection.id, token_id), None);248		// TODO: ERC721 transfer event249		Ok(())250	}251252	pub fn burn(253		collection: &RefungibleHandle<T>,254		owner: &T::CrossAccountId,255		token: TokenId,256		amount: u128,257	) -> DispatchResult {258		let total_supply = <TotalSupply<T>>::get((collection.id, token))259			.checked_sub(amount)260			.ok_or(<CommonError<T>>::TokenValueTooLow)?;261262		// This was probally last owner of this token?263		if total_supply == 0 {264			// Ensure user actually owns this amount265			ensure!(266				<Balance<T>>::get((collection.id, token, owner)) == amount,267				<CommonError<T>>::TokenValueTooLow268			);269			let account_balance = <AccountBalance<T>>::get((collection.id, owner))270				.checked_sub(1)271				// Should not occur272				.ok_or(ArithmeticError::Underflow)?;273274			// =========275276			<Owned<T>>::remove((collection.id, owner, token));277			<PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);278			<AccountBalance<T>>::insert((collection.id, owner), account_balance);279			Self::burn_token(collection, token)?;280			<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(281				collection.id,282				token,283				owner.clone(),284				amount,285			));286			return Ok(());287		}288289		let balance = <Balance<T>>::get((collection.id, token, owner))290			.checked_sub(amount)291			.ok_or(<CommonError<T>>::TokenValueTooLow)?;292		let account_balance = if balance == 0 {293			<AccountBalance<T>>::get((collection.id, owner))294				.checked_sub(1)295				// Should not occur296				.ok_or(ArithmeticError::Underflow)?297		} else {298			0299		};300301		// =========302303		if balance == 0 {304			<Owned<T>>::remove((collection.id, owner, token));305			<PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);306			<Balance<T>>::remove((collection.id, token, owner));307			<AccountBalance<T>>::insert((collection.id, owner), account_balance);308		} else {309			<Balance<T>>::insert((collection.id, token, owner), balance);310		}311		<TotalSupply<T>>::insert((collection.id, token), total_supply);312		// TODO: ERC20 transfer event313		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(314			collection.id,315			token,316			owner.clone(),317			amount,318		));319		Ok(())320	}321322	pub fn transfer(323		collection: &RefungibleHandle<T>,324		from: &T::CrossAccountId,325		to: &T::CrossAccountId,326		token: TokenId,327		amount: u128,328		nesting_budget: &dyn Budget,329	) -> DispatchResult {330		ensure!(331			collection.limits.transfers_enabled(),332			<CommonError<T>>::TransferNotAllowed333		);334335		if collection.permissions.access() == AccessMode::AllowList {336			collection.check_allowlist(from)?;337			collection.check_allowlist(to)?;338		}339		<PalletCommon<T>>::ensure_correct_receiver(to)?;340341		let balance_from = <Balance<T>>::get((collection.id, token, from))342			.checked_sub(amount)343			.ok_or(<CommonError<T>>::TokenValueTooLow)?;344		let mut create_target = false;345		let from_to_differ = from != to;346		let balance_to = if from != to {347			let old_balance = <Balance<T>>::get((collection.id, token, to));348			if old_balance == 0 {349				create_target = true;350			}351			Some(352				old_balance353					.checked_add(amount)354					.ok_or(ArithmeticError::Overflow)?,355			)356		} else {357			None358		};359360		let account_balance_from = if balance_from == 0 {361			Some(362				<AccountBalance<T>>::get((collection.id, from))363					.checked_sub(1)364					// Should not occur365					.ok_or(ArithmeticError::Underflow)?,366			)367		} else {368			None369		};370		// Account data is created in token, AccountBalance should be increased371		// But only if from != to as we shouldn't check overflow in this case372		let account_balance_to = if create_target && from_to_differ {373			let account_balance_to = <AccountBalance<T>>::get((collection.id, to))374				.checked_add(1)375				.ok_or(ArithmeticError::Overflow)?;376			ensure!(377				account_balance_to < collection.limits.account_token_ownership_limit(),378				<CommonError<T>>::AccountTokenLimitExceeded,379			);380381			Some(account_balance_to)382		} else {383			None384		};385386		// =========387388		<PalletStructure<T>>::nest_if_sent_to_token(389			from.clone(),390			to,391			collection.id,392			token,393			nesting_budget,394		)?;395396		if let Some(balance_to) = balance_to {397			// from != to398			if balance_from == 0 {399				<Balance<T>>::remove((collection.id, token, from));400				<PalletStructure<T>>::unnest_if_nested(from, collection.id, token);401			} else {402				<Balance<T>>::insert((collection.id, token, from), balance_from);403			}404			<Balance<T>>::insert((collection.id, token, to), balance_to);405			if let Some(account_balance_from) = account_balance_from {406				<AccountBalance<T>>::insert((collection.id, from), account_balance_from);407				<Owned<T>>::remove((collection.id, from, token));408			}409			if let Some(account_balance_to) = account_balance_to {410				<AccountBalance<T>>::insert((collection.id, to), account_balance_to);411				<Owned<T>>::insert((collection.id, to, token), true);412			}413		}414415		// TODO: ERC20 transfer event416		<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(417			collection.id,418			token,419			from.clone(),420			to.clone(),421			amount,422		));423		Ok(())424	}425426	pub fn create_multiple_items(427		collection: &RefungibleHandle<T>,428		sender: &T::CrossAccountId,429		data: Vec<CreateRefungibleExData<T::CrossAccountId>>,430		nesting_budget: &dyn Budget,431	) -> DispatchResult {432		if !collection.is_owner_or_admin(sender) {433			ensure!(434				collection.permissions.mint_mode(),435				<CommonError<T>>::PublicMintingNotAllowed436			);437			collection.check_allowlist(sender)?;438439			for item in data.iter() {440				for user in item.users.keys() {441					collection.check_allowlist(user)?;442				}443			}444		}445446		for item in data.iter() {447			for (owner, _) in item.users.iter() {448				<PalletCommon<T>>::ensure_correct_receiver(owner)?;449			}450		}451452		// Total pieces per tokens453		let totals = data454			.iter()455			.map(|data| {456				Ok(data457					.users458					.iter()459					.map(|u| u.1)460					.try_fold(0u128, |acc, v| acc.checked_add(*v))461					.ok_or(ArithmeticError::Overflow)?)462			})463			.collect::<Result<Vec<_>, DispatchError>>()?;464		for total in &totals {465			ensure!(466				*total <= MAX_REFUNGIBLE_PIECES,467				<Error<T>>::WrongRefungiblePieces468			);469		}470471		let first_token_id = <TokensMinted<T>>::get(collection.id);472		let tokens_minted = first_token_id473			.checked_add(data.len() as u32)474			.ok_or(ArithmeticError::Overflow)?;475		ensure!(476			tokens_minted < collection.limits.token_limit(),477			<CommonError<T>>::CollectionTokenLimitExceeded478		);479480		let mut balances = BTreeMap::new();481		for data in &data {482			for owner in data.users.keys() {483				let balance = balances484					.entry(owner)485					.or_insert_with(|| <AccountBalance<T>>::get((collection.id, owner)));486				*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;487488				ensure!(489					*balance <= collection.limits.account_token_ownership_limit(),490					<CommonError<T>>::AccountTokenLimitExceeded,491				);492			}493		}494495		for (i, token) in data.iter().enumerate() {496			let token_id = TokenId(first_token_id + i as u32 + 1);497			for (to, _) in token.users.iter() {498				<PalletStructure<T>>::check_nesting(499					sender.clone(),500					to,501					collection.id,502					token_id,503					nesting_budget,504				)?;505			}506		}507508		// =========509510		<TokensMinted<T>>::insert(collection.id, tokens_minted);511		for (account, balance) in balances {512			<AccountBalance<T>>::insert((collection.id, account), balance);513		}514		for (i, token) in data.into_iter().enumerate() {515			let token_id = first_token_id + i as u32 + 1;516			<TotalSupply<T>>::insert((collection.id, token_id), totals[i]);517518			<TokenData<T>>::insert(519				(collection.id, token_id),520				ItemData {521					const_data: token.const_data,522				},523			);524525			for (user, amount) in token.users.into_iter() {526				if amount == 0 {527					continue;528				}529				<Balance<T>>::insert((collection.id, token_id, &user), amount);530				<Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);531				<PalletStructure<T>>::nest_if_sent_to_token_unchecked(532					&user,533					collection.id,534					TokenId(token_id),535				);536537				// TODO: ERC20 transfer event538				<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(539					collection.id,540					TokenId(token_id),541					user,542					amount,543				));544			}545		}546		Ok(())547	}548549	pub fn set_allowance_unchecked(550		collection: &RefungibleHandle<T>,551		sender: &T::CrossAccountId,552		spender: &T::CrossAccountId,553		token: TokenId,554		amount: u128,555	) {556		if amount == 0 {557			<Allowance<T>>::remove((collection.id, token, sender, spender));558		} else {559			<Allowance<T>>::insert((collection.id, token, sender, spender), amount);560		}561		// TODO: ERC20 approval event562		<PalletCommon<T>>::deposit_event(CommonEvent::Approved(563			collection.id,564			token,565			sender.clone(),566			spender.clone(),567			amount,568		))569	}570571	pub fn set_allowance(572		collection: &RefungibleHandle<T>,573		sender: &T::CrossAccountId,574		spender: &T::CrossAccountId,575		token: TokenId,576		amount: u128,577	) -> DispatchResult {578		if collection.permissions.access() == AccessMode::AllowList {579			collection.check_allowlist(sender)?;580			collection.check_allowlist(spender)?;581		}582583		<PalletCommon<T>>::ensure_correct_receiver(spender)?;584585		if <Balance<T>>::get((collection.id, token, sender)) < amount {586			ensure!(587				collection.ignores_owned_amount(sender) && Self::token_exists(collection, token),588				<CommonError<T>>::CantApproveMoreThanOwned589			);590		}591592		// =========593594		Self::set_allowance_unchecked(collection, sender, spender, token, amount);595		Ok(())596	}597598	/// Returns allowance, which should be set after transaction599	fn check_allowed(600		collection: &RefungibleHandle<T>,601		spender: &T::CrossAccountId,602		from: &T::CrossAccountId,603		token: TokenId,604		amount: u128,605		nesting_budget: &dyn Budget,606	) -> Result<Option<u128>, DispatchError> {607		if spender.conv_eq(from) {608			return Ok(None);609		}610		if collection.permissions.access() == AccessMode::AllowList {611			// `from`, `to` checked in [`transfer`]612			collection.check_allowlist(spender)?;613		}614		if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {615			// TODO: should collection owner be allowed to perform this transfer?616			ensure!(617				<PalletStructure<T>>::check_indirectly_owned(618					spender.clone(),619					source.0,620					source.1,621					None,622					nesting_budget623				)?,624				<CommonError<T>>::ApprovedValueTooLow,625			);626			return Ok(None);627		}628		let allowance =629			<Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);630		if allowance.is_none() {631			ensure!(632				collection.ignores_allowance(spender),633				<CommonError<T>>::ApprovedValueTooLow634			);635		}636		Ok(allowance)637	}638639	pub fn transfer_from(640		collection: &RefungibleHandle<T>,641		spender: &T::CrossAccountId,642		from: &T::CrossAccountId,643		to: &T::CrossAccountId,644		token: TokenId,645		amount: u128,646		nesting_budget: &dyn Budget,647	) -> DispatchResult {648		let allowance =649			Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;650651		// =========652653		Self::transfer(collection, from, to, token, amount, nesting_budget)?;654		if let Some(allowance) = allowance {655			Self::set_allowance_unchecked(collection, from, spender, token, allowance);656		}657		Ok(())658	}659660	pub fn burn_from(661		collection: &RefungibleHandle<T>,662		spender: &T::CrossAccountId,663		from: &T::CrossAccountId,664		token: TokenId,665		amount: u128,666		nesting_budget: &dyn Budget,667	) -> DispatchResult {668		let allowance =669			Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;670671		// =========672673		Self::burn(collection, from, token, amount)?;674		if let Some(allowance) = allowance {675			Self::set_allowance_unchecked(collection, from, spender, token, allowance);676		}677		Ok(())678	}679680	/// Delegated to `create_multiple_items`681	pub fn create_item(682		collection: &RefungibleHandle<T>,683		sender: &T::CrossAccountId,684		data: CreateRefungibleExData<T::CrossAccountId>,685		nesting_budget: &dyn Budget,686	) -> DispatchResult {687		Self::create_multiple_items(collection, sender, vec![data], nesting_budget)688	}689690	pub fn repartition(691		owner: &T::CrossAccountId,692		collection: &RefungibleHandle<T>,693		token: TokenId,694		amount: u128,695	) -> DispatchResult {696		ensure!(697			amount <= MAX_REFUNGIBLE_PIECES,698			<Error<T>>::WrongRefungiblePieces699		);700		ensure!(amount > 0, <CommonError<T>>::TokenValueTooLow);701		// Ensure user owns all pieces702		let total_supply = <TotalSupply<T>>::get((collection.id, token));703		let balance = <Balance<T>>::get((collection.id, token, owner));704		ensure!(705			total_supply == balance,706			<Error<T>>::RepartitionWhileNotOwningAllPieces707		);708709		<Balance<T>>::insert((collection.id, token, owner), amount);710		<TotalSupply<T>>::insert((collection.id, token), amount);711		Ok(())712	}713}
after · pallets/refungible/src/lib.rs
1// 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::{ensure, BoundedVec};20use up_data_structs::{21	AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,22	CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,23};24use pallet_evm::account::CrossAccountId;25use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon};26use pallet_structure::Pallet as PalletStructure;27use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};28use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};29use core::ops::Deref;30use codec::{Encode, Decode, MaxEncodedLen};31use scale_info::TypeInfo;3233pub use pallet::*;34#[cfg(feature = "runtime-benchmarks")]35pub mod benchmarking;36pub mod common;37pub mod erc;38pub mod weights;39pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;4041#[struct_versioning::versioned(version = 2, upper)]42#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)]43pub struct ItemData {44	pub const_data: BoundedVec<u8, CustomDataLimit>,4546	#[version(..2)]47	pub variable_data: BoundedVec<u8, CustomDataLimit>,48}4950#[frame_support::pallet]51pub mod pallet {52	use super::*;53	use frame_support::{54		Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key,55		traits::StorageVersion,56	};57	use frame_system::pallet_prelude::*;58	use up_data_structs::{CollectionId, TokenId};59	use super::weights::WeightInfo;6061	#[pallet::error]62	pub enum Error<T> {63		/// Not Refungible item data used to mint in Refungible collection.64		NotRefungibleDataUsedToMintFungibleCollectionToken,65		/// Maximum refungibility exceeded66		WrongRefungiblePieces,67		/// Refungible token can't be repartitioned by user who isn't owns all pieces68		RepartitionWhileNotOwningAllPieces,69		/// Refungible token can't nest other tokens70		RefungibleDisallowsNesting,71		/// Setting item properties is not allowed72		SettingPropertiesNotAllowed,73	}7475	#[pallet::config]76	pub trait Config:77		frame_system::Config + pallet_common::Config + pallet_structure::Config78	{79		type WeightInfo: WeightInfo;80	}8182	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);8384	#[pallet::pallet]85	#[pallet::storage_version(STORAGE_VERSION)]86	#[pallet::generate_store(pub(super) trait Store)]87	pub struct Pallet<T>(_);8889	#[pallet::storage]90	pub type TokensMinted<T: Config> =91		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;92	#[pallet::storage]93	pub type TokensBurnt<T: Config> =94		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;9596	#[pallet::storage]97	pub type TokenData<T: Config> = StorageNMap<98		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),99		Value = ItemData,100		QueryKind = ValueQuery,101	>;102103	#[pallet::storage]104	pub type TotalSupply<T: Config> = StorageNMap<105		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),106		Value = u128,107		QueryKind = ValueQuery,108	>;109110	/// Used to enumerate tokens owned by account111	#[pallet::storage]112	pub type Owned<T: Config> = StorageNMap<113		Key = (114			Key<Twox64Concat, CollectionId>,115			Key<Blake2_128Concat, T::CrossAccountId>,116			Key<Twox64Concat, TokenId>,117		),118		Value = bool,119		QueryKind = ValueQuery,120	>;121122	#[pallet::storage]123	pub type AccountBalance<T: Config> = StorageNMap<124		Key = (125			Key<Twox64Concat, CollectionId>,126			// Owner127			Key<Blake2_128Concat, T::CrossAccountId>,128		),129		Value = u32,130		QueryKind = ValueQuery,131	>;132133	#[pallet::storage]134	pub type Balance<T: Config> = StorageNMap<135		Key = (136			Key<Twox64Concat, CollectionId>,137			Key<Twox64Concat, TokenId>,138			// Owner139			Key<Blake2_128Concat, T::CrossAccountId>,140		),141		Value = u128,142		QueryKind = ValueQuery,143	>;144145	#[pallet::storage]146	pub type Allowance<T: Config> = StorageNMap<147		Key = (148			Key<Twox64Concat, CollectionId>,149			Key<Twox64Concat, TokenId>,150			// Owner151			Key<Blake2_128, T::CrossAccountId>,152			// Spender153			Key<Blake2_128Concat, T::CrossAccountId>,154		),155		Value = u128,156		QueryKind = ValueQuery,157	>;158159	#[pallet::hooks]160	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {161		fn on_runtime_upgrade() -> Weight {162			if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {163				<TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {164					Some(<ItemDataVersion2>::from(v))165				})166			}167168			0169		}170	}171}172173pub struct RefungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);174impl<T: Config> RefungibleHandle<T> {175	pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {176		Self(inner)177	}178	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {179		self.0180	}181}182impl<T: Config> Deref for RefungibleHandle<T> {183	type Target = pallet_common::CollectionHandle<T>;184185	fn deref(&self) -> &Self::Target {186		&self.0187	}188}189190impl<T: Config> Pallet<T> {191	pub fn total_supply(collection: &RefungibleHandle<T>) -> u32 {192		<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)193	}194	pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {195		<TotalSupply<T>>::contains_key((collection.id, token))196	}197}198199// unchecked calls skips any permission checks200impl<T: Config> Pallet<T> {201	pub fn init_collection(202		owner: T::CrossAccountId,203		data: CreateCollectionData<T::AccountId>,204	) -> Result<CollectionId, DispatchError> {205		<PalletCommon<T>>::init_collection(owner, data, false)206	}207	pub fn destroy_collection(208		collection: RefungibleHandle<T>,209		sender: &T::CrossAccountId,210	) -> DispatchResult {211		let id = collection.id;212213		if Self::collection_has_tokens(id) {214			return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());215		}216217		// =========218219		PalletCommon::destroy_collection(collection.0, sender)?;220221		<TokensMinted<T>>::remove(id);222		<TokensBurnt<T>>::remove(id);223		<TokenData<T>>::remove_prefix((id,), None);224		<TotalSupply<T>>::remove_prefix((id,), None);225		<Balance<T>>::remove_prefix((id,), None);226		<Allowance<T>>::remove_prefix((id,), None);227		<Owned<T>>::remove_prefix((id,), None);228		<AccountBalance<T>>::remove_prefix((id,), None);229		Ok(())230	}231232	fn collection_has_tokens(collection_id: CollectionId) -> bool {233		<TokenData<T>>::iter_prefix((collection_id,))234			.next()235			.is_some()236	}237238	pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {239		let burnt = <TokensBurnt<T>>::get(collection.id)240			.checked_add(1)241			.ok_or(ArithmeticError::Overflow)?;242243		<TokensBurnt<T>>::insert(collection.id, burnt);244		<TokenData<T>>::remove((collection.id, token_id));245		<TotalSupply<T>>::remove((collection.id, token_id));246		<Balance<T>>::remove_prefix((collection.id, token_id), None);247		<Allowance<T>>::remove_prefix((collection.id, token_id), None);248		// TODO: ERC721 transfer event249		Ok(())250	}251252	pub fn burn(253		collection: &RefungibleHandle<T>,254		owner: &T::CrossAccountId,255		token: TokenId,256		amount: u128,257	) -> DispatchResult {258		let total_supply = <TotalSupply<T>>::get((collection.id, token))259			.checked_sub(amount)260			.ok_or(<CommonError<T>>::TokenValueTooLow)?;261262		// This was probally last owner of this token?263		if total_supply == 0 {264			// Ensure user actually owns this amount265			ensure!(266				<Balance<T>>::get((collection.id, token, owner)) == amount,267				<CommonError<T>>::TokenValueTooLow268			);269			let account_balance = <AccountBalance<T>>::get((collection.id, owner))270				.checked_sub(1)271				// Should not occur272				.ok_or(ArithmeticError::Underflow)?;273274			// =========275276			<Owned<T>>::remove((collection.id, owner, token));277			<PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);278			<AccountBalance<T>>::insert((collection.id, owner), account_balance);279			Self::burn_token(collection, token)?;280			<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(281				collection.id,282				token,283				owner.clone(),284				amount,285			));286			return Ok(());287		}288289		let balance = <Balance<T>>::get((collection.id, token, owner))290			.checked_sub(amount)291			.ok_or(<CommonError<T>>::TokenValueTooLow)?;292		let account_balance = if balance == 0 {293			<AccountBalance<T>>::get((collection.id, owner))294				.checked_sub(1)295				// Should not occur296				.ok_or(ArithmeticError::Underflow)?297		} else {298			0299		};300301		// =========302303		if balance == 0 {304			<Owned<T>>::remove((collection.id, owner, token));305			<PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);306			<Balance<T>>::remove((collection.id, token, owner));307			<AccountBalance<T>>::insert((collection.id, owner), account_balance);308		} else {309			<Balance<T>>::insert((collection.id, token, owner), balance);310		}311		<TotalSupply<T>>::insert((collection.id, token), total_supply);312		// TODO: ERC20 transfer event313		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(314			collection.id,315			token,316			owner.clone(),317			amount,318		));319		Ok(())320	}321322	pub fn transfer(323		collection: &RefungibleHandle<T>,324		from: &T::CrossAccountId,325		to: &T::CrossAccountId,326		token: TokenId,327		amount: u128,328		nesting_budget: &dyn Budget,329	) -> DispatchResult {330		ensure!(331			collection.limits.transfers_enabled(),332			<CommonError<T>>::TransferNotAllowed333		);334335		if collection.permissions.access() == AccessMode::AllowList {336			collection.check_allowlist(from)?;337			collection.check_allowlist(to)?;338		}339		<PalletCommon<T>>::ensure_correct_receiver(to)?;340341		let balance_from = <Balance<T>>::get((collection.id, token, from))342			.checked_sub(amount)343			.ok_or(<CommonError<T>>::TokenValueTooLow)?;344		let mut create_target = false;345		let from_to_differ = from != to;346		let balance_to = if from != to {347			let old_balance = <Balance<T>>::get((collection.id, token, to));348			if old_balance == 0 {349				create_target = true;350			}351			Some(352				old_balance353					.checked_add(amount)354					.ok_or(ArithmeticError::Overflow)?,355			)356		} else {357			None358		};359360		let account_balance_from = if balance_from == 0 {361			Some(362				<AccountBalance<T>>::get((collection.id, from))363					.checked_sub(1)364					// Should not occur365					.ok_or(ArithmeticError::Underflow)?,366			)367		} else {368			None369		};370		// Account data is created in token, AccountBalance should be increased371		// But only if from != to as we shouldn't check overflow in this case372		let account_balance_to = if create_target && from_to_differ {373			let account_balance_to = <AccountBalance<T>>::get((collection.id, to))374				.checked_add(1)375				.ok_or(ArithmeticError::Overflow)?;376			ensure!(377				account_balance_to < collection.limits.account_token_ownership_limit(),378				<CommonError<T>>::AccountTokenLimitExceeded,379			);380381			Some(account_balance_to)382		} else {383			None384		};385386		// =========387388		<PalletStructure<T>>::nest_if_sent_to_token(389			from.clone(),390			to,391			collection.id,392			token,393			nesting_budget,394		)?;395396		if let Some(balance_to) = balance_to {397			// from != to398			if balance_from == 0 {399				<Balance<T>>::remove((collection.id, token, from));400				<PalletStructure<T>>::unnest_if_nested(from, collection.id, token);401			} else {402				<Balance<T>>::insert((collection.id, token, from), balance_from);403			}404			<Balance<T>>::insert((collection.id, token, to), balance_to);405			if let Some(account_balance_from) = account_balance_from {406				<AccountBalance<T>>::insert((collection.id, from), account_balance_from);407				<Owned<T>>::remove((collection.id, from, token));408			}409			if let Some(account_balance_to) = account_balance_to {410				<AccountBalance<T>>::insert((collection.id, to), account_balance_to);411				<Owned<T>>::insert((collection.id, to, token), true);412			}413		}414415		// TODO: ERC20 transfer event416		<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(417			collection.id,418			token,419			from.clone(),420			to.clone(),421			amount,422		));423		Ok(())424	}425426	pub fn create_multiple_items(427		collection: &RefungibleHandle<T>,428		sender: &T::CrossAccountId,429		data: Vec<CreateRefungibleExData<T::CrossAccountId>>,430		nesting_budget: &dyn Budget,431	) -> DispatchResult {432		if !collection.is_owner_or_admin(sender) {433			ensure!(434				collection.permissions.mint_mode(),435				<CommonError<T>>::PublicMintingNotAllowed436			);437			collection.check_allowlist(sender)?;438439			for item in data.iter() {440				for user in item.users.keys() {441					collection.check_allowlist(user)?;442				}443			}444		}445446		for item in data.iter() {447			for (owner, _) in item.users.iter() {448				<PalletCommon<T>>::ensure_correct_receiver(owner)?;449			}450		}451452		// Total pieces per tokens453		let totals = data454			.iter()455			.map(|data| {456				Ok(data457					.users458					.iter()459					.map(|u| u.1)460					.try_fold(0u128, |acc, v| acc.checked_add(*v))461					.ok_or(ArithmeticError::Overflow)?)462			})463			.collect::<Result<Vec<_>, DispatchError>>()?;464		for total in &totals {465			ensure!(466				*total <= MAX_REFUNGIBLE_PIECES,467				<Error<T>>::WrongRefungiblePieces468			);469		}470471		let first_token_id = <TokensMinted<T>>::get(collection.id);472		let tokens_minted = first_token_id473			.checked_add(data.len() as u32)474			.ok_or(ArithmeticError::Overflow)?;475		ensure!(476			tokens_minted < collection.limits.token_limit(),477			<CommonError<T>>::CollectionTokenLimitExceeded478		);479480		let mut balances = BTreeMap::new();481		for data in &data {482			for owner in data.users.keys() {483				let balance = balances484					.entry(owner)485					.or_insert_with(|| <AccountBalance<T>>::get((collection.id, owner)));486				*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;487488				ensure!(489					*balance <= collection.limits.account_token_ownership_limit(),490					<CommonError<T>>::AccountTokenLimitExceeded,491				);492			}493		}494495		for (i, token) in data.iter().enumerate() {496			let token_id = TokenId(first_token_id + i as u32 + 1);497			for (to, _) in token.users.iter() {498				<PalletStructure<T>>::check_nesting(499					sender.clone(),500					to,501					collection.id,502					token_id,503					nesting_budget,504				)?;505			}506		}507508		// =========509510		<TokensMinted<T>>::insert(collection.id, tokens_minted);511		for (account, balance) in balances {512			<AccountBalance<T>>::insert((collection.id, account), balance);513		}514		for (i, token) in data.into_iter().enumerate() {515			let token_id = first_token_id + i as u32 + 1;516			<TotalSupply<T>>::insert((collection.id, token_id), totals[i]);517518			<TokenData<T>>::insert(519				(collection.id, token_id),520				ItemData {521					const_data: token.const_data,522				},523			);524525			for (user, amount) in token.users.into_iter() {526				if amount == 0 {527					continue;528				}529				<Balance<T>>::insert((collection.id, token_id, &user), amount);530				<Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);531				<PalletStructure<T>>::nest_if_sent_to_token_unchecked(532					&user,533					collection.id,534					TokenId(token_id),535				);536537				// TODO: ERC20 transfer event538				<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(539					collection.id,540					TokenId(token_id),541					user,542					amount,543				));544			}545		}546		Ok(())547	}548549	pub fn set_allowance_unchecked(550		collection: &RefungibleHandle<T>,551		sender: &T::CrossAccountId,552		spender: &T::CrossAccountId,553		token: TokenId,554		amount: u128,555	) {556		if amount == 0 {557			<Allowance<T>>::remove((collection.id, token, sender, spender));558		} else {559			<Allowance<T>>::insert((collection.id, token, sender, spender), amount);560		}561		// TODO: ERC20 approval event562		<PalletCommon<T>>::deposit_event(CommonEvent::Approved(563			collection.id,564			token,565			sender.clone(),566			spender.clone(),567			amount,568		))569	}570571	pub fn set_allowance(572		collection: &RefungibleHandle<T>,573		sender: &T::CrossAccountId,574		spender: &T::CrossAccountId,575		token: TokenId,576		amount: u128,577	) -> DispatchResult {578		if collection.permissions.access() == AccessMode::AllowList {579			collection.check_allowlist(sender)?;580			collection.check_allowlist(spender)?;581		}582583		<PalletCommon<T>>::ensure_correct_receiver(spender)?;584585		if <Balance<T>>::get((collection.id, token, sender)) < amount {586			ensure!(587				collection.ignores_owned_amount(sender) && Self::token_exists(collection, token),588				<CommonError<T>>::CantApproveMoreThanOwned589			);590		}591592		// =========593594		Self::set_allowance_unchecked(collection, sender, spender, token, amount);595		Ok(())596	}597598	/// Returns allowance, which should be set after transaction599	fn check_allowed(600		collection: &RefungibleHandle<T>,601		spender: &T::CrossAccountId,602		from: &T::CrossAccountId,603		token: TokenId,604		amount: u128,605		nesting_budget: &dyn Budget,606	) -> Result<Option<u128>, DispatchError> {607		if spender.conv_eq(from) {608			return Ok(None);609		}610		if collection.permissions.access() == AccessMode::AllowList {611			// `from`, `to` checked in [`transfer`]612			collection.check_allowlist(spender)?;613		}614		if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {615			// TODO: should collection owner be allowed to perform this transfer?616			ensure!(617				<PalletStructure<T>>::check_indirectly_owned(618					spender.clone(),619					source.0,620					source.1,621					None,622					nesting_budget623				)?,624				<CommonError<T>>::ApprovedValueTooLow,625			);626			return Ok(None);627		}628		let allowance =629			<Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);630		if allowance.is_none() {631			ensure!(632				collection.ignores_allowance(spender),633				<CommonError<T>>::ApprovedValueTooLow634			);635		}636		Ok(allowance)637	}638639	pub fn transfer_from(640		collection: &RefungibleHandle<T>,641		spender: &T::CrossAccountId,642		from: &T::CrossAccountId,643		to: &T::CrossAccountId,644		token: TokenId,645		amount: u128,646		nesting_budget: &dyn Budget,647	) -> DispatchResult {648		let allowance =649			Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;650651		// =========652653		Self::transfer(collection, from, to, token, amount, nesting_budget)?;654		if let Some(allowance) = allowance {655			Self::set_allowance_unchecked(collection, from, spender, token, allowance);656		}657		Ok(())658	}659660	pub fn burn_from(661		collection: &RefungibleHandle<T>,662		spender: &T::CrossAccountId,663		from: &T::CrossAccountId,664		token: TokenId,665		amount: u128,666		nesting_budget: &dyn Budget,667	) -> DispatchResult {668		let allowance =669			Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;670671		// =========672673		Self::burn(collection, from, token, amount)?;674		if let Some(allowance) = allowance {675			Self::set_allowance_unchecked(collection, from, spender, token, allowance);676		}677		Ok(())678	}679680	/// Delegated to `create_multiple_items`681	pub fn create_item(682		collection: &RefungibleHandle<T>,683		sender: &T::CrossAccountId,684		data: CreateRefungibleExData<T::CrossAccountId>,685		nesting_budget: &dyn Budget,686	) -> DispatchResult {687		Self::create_multiple_items(collection, sender, vec![data], nesting_budget)688	}689690	pub fn repartition(691		collection: &RefungibleHandle<T>,692		owner: &T::CrossAccountId,693		token: TokenId,694		amount: u128,695	) -> DispatchResult {696		ensure!(697			amount <= MAX_REFUNGIBLE_PIECES,698			<Error<T>>::WrongRefungiblePieces699		);700		ensure!(amount > 0, <CommonError<T>>::TokenValueTooLow);701		// Ensure user owns all pieces702		let total_supply = <TotalSupply<T>>::get((collection.id, token));703		let balance = <Balance<T>>::get((collection.id, token, owner));704		ensure!(705			total_supply == balance,706			<Error<T>>::RepartitionWhileNotOwningAllPieces707		);708709		<Balance<T>>::insert((collection.id, token, owner), amount);710		<TotalSupply<T>>::insert((collection.id, token), amount);711		Ok(())712	}713}
modifiedpallets/refungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -3,7 +3,7 @@
 //! Autogenerated weights for pallet_refungible
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-06-15, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-06-27, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
 //! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
 
 // Executed Command:
@@ -49,6 +49,7 @@
 	fn transfer_from_removing() -> Weight;
 	fn transfer_from_creating_removing() -> Weight;
 	fn burn_from() -> Weight;
+	fn repartition_item() -> Weight;
 }
 
 /// Weights for pallet_refungible using the Substrate node and recommended hardware.
@@ -61,7 +62,7 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn create_item() -> Weight {
-		(21_321_000 as Weight)
+		(17_553_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
@@ -72,9 +73,9 @@
 	// Storage: Refungible TokenData (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items(b: u32, ) -> Weight {
-		(16_313_000 as Weight)
-			// Standard Error: 4_000
-			.saturating_add((5_464_000 as Weight).saturating_mul(b as Weight))
+		(10_654_000 as Weight)
+			// Standard Error: 1_000
+			.saturating_add((5_114_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
@@ -86,9 +87,9 @@
 	// Storage: Refungible TokenData (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items_ex_multiple_items(b: u32, ) -> Weight {
-		(15_631_000 as Weight)
-			// Standard Error: 5_000
-			.saturating_add((8_141_000 as Weight).saturating_mul(b as Weight))
+		(3_587_000 as Weight)
+			// Standard Error: 2_000
+			.saturating_add((7_931_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
@@ -101,9 +102,9 @@
 	// Storage: Refungible Balance (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight {
-		(11_191_000 as Weight)
-			// Standard Error: 4_000
-			.saturating_add((6_321_000 as Weight).saturating_mul(b as Weight))
+		(1_980_000 as Weight)
+			// Standard Error: 2_000
+			.saturating_add((6_305_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(T::DbWeight::get().writes(3 as Weight))
@@ -114,7 +115,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_item_partial() -> Weight {
-		(24_421_000 as Weight)
+		(21_010_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(4 as Weight))
 	}
@@ -125,13 +126,13 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_item_fully() -> Weight {
-		(32_900_000 as Weight)
+		(28_413_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_normal() -> Weight {
-		(20_215_000 as Weight)
+		(17_513_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
@@ -139,7 +140,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_creating() -> Weight {
-		(24_809_000 as Weight)
+		(20_469_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(4 as Weight))
 	}
@@ -147,7 +148,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_removing() -> Weight {
-		(26_704_000 as Weight)
+		(22_472_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(4 as Weight))
 	}
@@ -155,21 +156,21 @@
 	// Storage: Refungible AccountBalance (r:2 w:2)
 	// Storage: Refungible Owned (r:0 w:2)
 	fn transfer_creating_removing() -> Weight {
-		(28_728_000 as Weight)
+		(24_866_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Refungible Balance (r:1 w:0)
 	// Storage: Refungible Allowance (r:0 w:1)
 	fn approve() -> Weight {
-		(16_107_000 as Weight)
+		(13_475_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Refungible Allowance (r:1 w:1)
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_from_normal() -> Weight {
-		(28_765_000 as Weight)
+		(24_707_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(3 as Weight))
 	}
@@ -178,7 +179,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_from_creating() -> Weight {
-		(32_788_000 as Weight)
+		(27_812_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(5 as Weight))
 	}
@@ -187,7 +188,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_from_removing() -> Weight {
-		(34_523_000 as Weight)
+		(29_966_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(5 as Weight))
 	}
@@ -196,7 +197,7 @@
 	// Storage: Refungible AccountBalance (r:2 w:2)
 	// Storage: Refungible Owned (r:0 w:2)
 	fn transfer_from_creating_removing() -> Weight {
-		(36_749_000 as Weight)
+		(31_660_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(5 as Weight))
 			.saturating_add(T::DbWeight::get().writes(7 as Weight))
 	}
@@ -208,10 +209,17 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_from() -> Weight {
-		(42_259_000 as Weight)
+		(36_248_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(5 as Weight))
 			.saturating_add(T::DbWeight::get().writes(7 as Weight))
 	}
+	// Storage: Refungible TotalSupply (r:1 w:1)
+	// Storage: Refungible Balance (r:1 w:1)
+	fn repartition_item() -> Weight {
+		(8_226_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+	}
 }
 
 // For backwards compatibility and tests
@@ -223,7 +231,7 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn create_item() -> Weight {
-		(21_321_000 as Weight)
+		(17_553_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
@@ -234,9 +242,9 @@
 	// Storage: Refungible TokenData (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items(b: u32, ) -> Weight {
-		(16_313_000 as Weight)
-			// Standard Error: 4_000
-			.saturating_add((5_464_000 as Weight).saturating_mul(b as Weight))
+		(10_654_000 as Weight)
+			// Standard Error: 1_000
+			.saturating_add((5_114_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
@@ -248,9 +256,9 @@
 	// Storage: Refungible TokenData (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items_ex_multiple_items(b: u32, ) -> Weight {
-		(15_631_000 as Weight)
-			// Standard Error: 5_000
-			.saturating_add((8_141_000 as Weight).saturating_mul(b as Weight))
+		(3_587_000 as Weight)
+			// Standard Error: 2_000
+			.saturating_add((7_931_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
@@ -263,9 +271,9 @@
 	// Storage: Refungible Balance (r:0 w:4)
 	// Storage: Refungible Owned (r:0 w:4)
 	fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight {
-		(11_191_000 as Weight)
-			// Standard Error: 4_000
-			.saturating_add((6_321_000 as Weight).saturating_mul(b as Weight))
+		(1_980_000 as Weight)
+			// Standard Error: 2_000
+			.saturating_add((6_305_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
@@ -276,7 +284,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_item_partial() -> Weight {
-		(24_421_000 as Weight)
+		(21_010_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
 	}
@@ -287,13 +295,13 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_item_fully() -> Weight {
-		(32_900_000 as Weight)
+		(28_413_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_normal() -> Weight {
-		(20_215_000 as Weight)
+		(17_513_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
@@ -301,7 +309,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_creating() -> Weight {
-		(24_809_000 as Weight)
+		(20_469_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
 	}
@@ -309,7 +317,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_removing() -> Weight {
-		(26_704_000 as Weight)
+		(22_472_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
 	}
@@ -317,21 +325,21 @@
 	// Storage: Refungible AccountBalance (r:2 w:2)
 	// Storage: Refungible Owned (r:0 w:2)
 	fn transfer_creating_removing() -> Weight {
-		(28_728_000 as Weight)
+		(24_866_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Refungible Balance (r:1 w:0)
 	// Storage: Refungible Allowance (r:0 w:1)
 	fn approve() -> Weight {
-		(16_107_000 as Weight)
+		(13_475_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Refungible Allowance (r:1 w:1)
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_from_normal() -> Weight {
-		(28_765_000 as Weight)
+		(24_707_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
 	}
@@ -340,7 +348,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_from_creating() -> Weight {
-		(32_788_000 as Weight)
+		(27_812_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
 	}
@@ -349,7 +357,7 @@
 	// Storage: Refungible AccountBalance (r:1 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn transfer_from_removing() -> Weight {
-		(34_523_000 as Weight)
+		(29_966_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
 	}
@@ -358,7 +366,7 @@
 	// Storage: Refungible AccountBalance (r:2 w:2)
 	// Storage: Refungible Owned (r:0 w:2)
 	fn transfer_from_creating_removing() -> Weight {
-		(36_749_000 as Weight)
+		(31_660_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(5 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(7 as Weight))
 	}
@@ -370,8 +378,15 @@
 	// Storage: Refungible TokenData (r:0 w:1)
 	// Storage: Refungible Owned (r:0 w:1)
 	fn burn_from() -> Weight {
-		(42_259_000 as Weight)
+		(36_248_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(5 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(7 as Weight))
 	}
+	// Storage: Refungible TotalSupply (r:1 w:1)
+	// Storage: Refungible Balance (r:1 w:1)
+	fn repartition_item() -> Weight {
+		(8_226_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+	}
 }
modifiedpallets/unique/Cargo.tomldiffbeforeafterboth
--- a/pallets/unique/Cargo.toml
+++ b/pallets/unique/Cargo.toml
@@ -103,4 +103,3 @@
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
 pallet-nonfungible = { default-features = false, path = '../../pallets/nonfungible' }
-pallet-refungible = { default-features = false, path = '../../pallets/refungible' }
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -45,9 +45,8 @@
 use pallet_evm::account::CrossAccountId;
 use pallet_common::{
 	CollectionHandle, Pallet as PalletCommon, CommonWeightInfo, dispatch::dispatch_tx,
-	dispatch::CollectionDispatch,
+	dispatch::CollectionDispatch, RefungibleExtensionsWeightInfo,
 };
-use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle};
 pub mod eth;
 
 #[cfg(feature = "runtime-benchmarks")]
@@ -71,14 +70,13 @@
 	}
 }
 
-pub trait Config:
-	system::Config + pallet_common::Config + pallet_refungible::Config + Sized + TypeInfo
-{
+pub trait Config: system::Config + pallet_common::Config + Sized + TypeInfo {
 	type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
 
 	/// Weight information for extrinsics in this pallet.
 	type WeightInfo: WeightInfo;
 	type CommonWeightInfo: CommonWeightInfo<Self::CrossAccountId>;
+	type RefungibleExtensionsWeightInfo: RefungibleExtensionsWeightInfo;
 }
 
 decl_event! {
@@ -904,23 +902,22 @@
 			target_collection.save()
 		}
 
-		#[weight = <SelfWeightOf<T>>::set_collection_limits()]
+		#[weight = T::RefungibleExtensionsWeightInfo::repartition()]
 		#[transactional]
 		pub fn repartition(
 			origin,
 			collection_id: CollectionId,
 			token: TokenId,
 			amount: u128,
-		) -> DispatchResult {
+		) -> DispatchResultWithPostInfo {
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
-			let target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
-			target_collection.check_is_internal()?;
-			let refungible_collection = match target_collection.mode {
-				CollectionMode::ReFungible => RefungibleHandle::cast(target_collection),
-				_ => fail!(<Error<T>>::RepartitionCalledOnNonRefungibleCollection),
-			};
-			<PalletRefungible<T>>::repartition(&sender, &refungible_collection, token, amount)?;
-			Ok(())
+			dispatch_tx::<T, _>(collection_id, |d| {
+				if let Some(refungible_extensions) = d.refungible_extensions() {
+					refungible_extensions.repartition(&sender, token, amount)
+				} else {
+					fail!(<Error<T>>::RepartitionCalledOnNonRefungibleCollection)
+				}
+			})
 		}
 	}
 }
modifiedpallets/unique/src/weights.rsdiffbeforeafterboth
--- a/pallets/unique/src/weights.rs
+++ b/pallets/unique/src/weights.rs
@@ -3,7 +3,7 @@
 //! Autogenerated weights for pallet_unique
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-06-15, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-06-28, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
 //! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
 
 // Executed Command:
@@ -57,7 +57,7 @@
 	// Storage: Common CollectionProperties (r:0 w:1)
 	// Storage: Common CollectionById (r:0 w:1)
 	fn create_collection() -> Weight {
-		(39_427_000 as Weight)
+		(53_511_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
@@ -69,27 +69,27 @@
 	// Storage: Common AdminAmount (r:0 w:1)
 	// Storage: Common CollectionProperties (r:0 w:1)
 	fn destroy_collection() -> Weight {
-		(48_339_000 as Weight)
+		(69_481_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn add_to_allow_list() -> Weight {
-		(17_379_000 as Weight)
+		(22_892_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn remove_from_allow_list() -> Weight {
-		(17_490_000 as Weight)
+		(22_973_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn change_collection_owner() -> Weight {
-		(17_701_000 as Weight)
+		(22_392_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
@@ -97,7 +97,7 @@
 	// Storage: Common IsAdmin (r:1 w:1)
 	// Storage: Common AdminAmount (r:1 w:1)
 	fn add_collection_admin() -> Weight {
-		(23_301_000 as Weight)
+		(30_298_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
@@ -105,37 +105,37 @@
 	// Storage: Common IsAdmin (r:1 w:1)
 	// Storage: Common AdminAmount (r:1 w:1)
 	fn remove_collection_admin() -> Weight {
-		(24_859_000 as Weight)
+		(32_842_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_collection_sponsor() -> Weight {
-		(17_795_000 as Weight)
+		(22_613_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn confirm_sponsorship() -> Weight {
-		(17_297_000 as Weight)
+		(22_462_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn remove_collection_sponsor() -> Weight {
-		(17_079_000 as Weight)
+		(21_730_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_transfers_enabled_flag() -> Weight {
-		(9_734_000 as Weight)
+		(10_941_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_collection_limits() -> Weight {
-		(17_998_000 as Weight)
+		(22_363_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
@@ -150,7 +150,7 @@
 	// Storage: Common CollectionProperties (r:0 w:1)
 	// Storage: Common CollectionById (r:0 w:1)
 	fn create_collection() -> Weight {
-		(39_427_000 as Weight)
+		(53_511_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
@@ -162,27 +162,27 @@
 	// Storage: Common AdminAmount (r:0 w:1)
 	// Storage: Common CollectionProperties (r:0 w:1)
 	fn destroy_collection() -> Weight {
-		(48_339_000 as Weight)
+		(69_481_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn add_to_allow_list() -> Weight {
-		(17_379_000 as Weight)
+		(22_892_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn remove_from_allow_list() -> Weight {
-		(17_490_000 as Weight)
+		(22_973_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn change_collection_owner() -> Weight {
-		(17_701_000 as Weight)
+		(22_392_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
@@ -190,7 +190,7 @@
 	// Storage: Common IsAdmin (r:1 w:1)
 	// Storage: Common AdminAmount (r:1 w:1)
 	fn add_collection_admin() -> Weight {
-		(23_301_000 as Weight)
+		(30_298_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
@@ -198,37 +198,37 @@
 	// Storage: Common IsAdmin (r:1 w:1)
 	// Storage: Common AdminAmount (r:1 w:1)
 	fn remove_collection_admin() -> Weight {
-		(24_859_000 as Weight)
+		(32_842_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_collection_sponsor() -> Weight {
-		(17_795_000 as Weight)
+		(22_613_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn confirm_sponsorship() -> Weight {
-		(17_297_000 as Weight)
+		(22_462_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn remove_collection_sponsor() -> Weight {
-		(17_079_000 as Weight)
+		(21_730_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_transfers_enabled_flag() -> Weight {
-		(9_734_000 as Weight)
+		(10_941_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Common CollectionById (r:1 w:1)
 	fn set_collection_limits() -> Weight {
-		(17_998_000 as Weight)
+		(22_363_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
modifiedruntime/common/src/weights.rsdiffbeforeafterboth
--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -16,11 +16,13 @@
 
 use core::marker::PhantomData;
 use frame_support::{weights::Weight};
-use pallet_common::{CommonWeightInfo, dispatch::dispatch_weight};
+use pallet_common::{CommonWeightInfo, dispatch::dispatch_weight, RefungibleExtensionsWeightInfo};
 
 use pallet_fungible::{Config as FungibleConfig, common::CommonWeights as FungibleWeights};
 use pallet_nonfungible::{Config as NonfungibleConfig, common::CommonWeights as NonfungibleWeights};
-use pallet_refungible::{Config as RefungibleConfig, common::CommonWeights as RefungibleWeights};
+use pallet_refungible::{
+	Config as RefungibleConfig, weights::WeightInfo, common::CommonWeights as RefungibleWeights,
+};
 use up_data_structs::{CreateItemExData, CreateItemData};
 
 macro_rules! max_weight_of {
@@ -98,3 +100,12 @@
 		max_weight_of!(burn_recursively_breadth_raw(amount))
 	}
 }
+
+impl<T> RefungibleExtensionsWeightInfo for CommonWeights<T>
+where
+	T: FungibleConfig + NonfungibleConfig + RefungibleConfig,
+{
+	fn repartition() -> Weight {
+		dispatch_weight::<T>() + <<T as RefungibleConfig>::WeightInfo>::repartition_item()
+	}
+}
modifiedruntime/opal/src/lib.rsdiffbeforeafterboth
--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -925,6 +925,7 @@
 	type Event = Event;
 	type WeightInfo = pallet_unique::weights::SubstrateWeight<Self>;
 	type CommonWeightInfo = CommonWeights<Self>;
+	type RefungibleExtensionsWeightInfo = CommonWeights<Self>;
 }
 
 parameter_types! {
modifiedruntime/tests/src/lib.rsdiffbeforeafterboth
--- a/runtime/tests/src/lib.rs
+++ b/runtime/tests/src/lib.rs
@@ -255,6 +255,7 @@
 	type Event = ();
 	type WeightInfo = ();
 	type CommonWeightInfo = CommonWeights<Self>;
+	type RefungibleExtensionsWeightInfo = CommonWeights<Self>;
 }
 
 // Build genesis storage according to the mock runtime.
modifiedruntime/tests/src/tests.rsdiffbeforeafterboth
--- a/runtime/tests/src/tests.rs
+++ b/runtime/tests/src/tests.rs
@@ -21,7 +21,7 @@
 	CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, TokenId,
 	MAX_TOKEN_OWNERSHIP, CreateCollectionData, CollectionMode, AccessMode, CollectionPermissions,
 	PropertyKeyPermission, PropertyPermission, Property, CollectionPropertiesVec,
-	CollectionPropertiesPermissionsVec, TokenChild,
+	CollectionPropertiesPermissionsVec,
 };
 use frame_support::{assert_noop, assert_ok, assert_err};
 use sp_std::convert::TryInto;