git.delta.rocks / unique-network / refs/commits / 6cd8e0c2387a

difftreelog

source

pallets/refungible/src/common.rs5.6 KiBsourcehistory
1use core::marker::PhantomData;23use sp_std::collections::btree_map::BTreeMap;4use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight};5use nft_data_structs::TokenId;6use pallet_common::{7	CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,8};9use sp_runtime::DispatchError;10use sp_std::vec::Vec;1112use crate::{13	AccountBalance, Allowance, Balance, Config, CreateItemData, Error, Owned, Pallet,14	RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,15};1617macro_rules! max_weight_of {18	($($method:ident ($($args:tt)*)),*) => {19		020		$(21			.max(<SelfWeightOf<T>>::$method($($args)*))22		)*23	};24}2526pub struct CommonWeights<T: Config>(PhantomData<T>);27impl<T: Config> CommonWeightInfo for CommonWeights<T> {28	fn create_item() -> Weight {29		<SelfWeightOf<T>>::create_item()30	}3132	fn create_multiple_items(amount: u32) -> Weight {33		<SelfWeightOf<T>>::create_multiple_items(amount)34	}3536	fn burn_item() -> Weight {37		max_weight_of!(burn_item_partial(), burn_item_fully())38	}3940	fn transfer() -> Weight {41		max_weight_of!(42			transfer_normal(),43			transfer_creating(),44			transfer_removing(),45			transfer_creating_removing()46		)47	}4849	fn approve() -> Weight {50		<SelfWeightOf<T>>::approve()51	}5253	fn transfer_from() -> Weight {54		max_weight_of!(55			transfer_from_normal(),56			transfer_from_creating(),57			transfer_from_removing(),58			transfer_from_creating_removing()59		)60	}6162	fn burn_from() -> Weight {63		064	}6566	fn set_variable_metadata(bytes: u32) -> Weight {67		<SelfWeightOf<T>>::set_variable_metadata(bytes)68	}69}7071fn map_create_data<T: Config>(72	data: nft_data_structs::CreateItemData,73	to: &T::CrossAccountId,74) -> Result<CreateItemData<T>, DispatchError> {75	match data {76		nft_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData {77			const_data: data.const_data,78			variable_data: data.variable_data,79			users: {80				let mut out = BTreeMap::new();81				out.insert(to.clone(), data.pieces);82				out83			},84		}),85		_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),86	}87}8889impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {90	fn create_item(91		&self,92		sender: T::CrossAccountId,93		to: T::CrossAccountId,94		data: nft_data_structs::CreateItemData,95	) -> DispatchResultWithPostInfo {96		with_weight(97			<Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),98			<CommonWeights<T>>::create_item(),99		)100	}101102	fn create_multiple_items(103		&self,104		sender: T::CrossAccountId,105		to: T::CrossAccountId,106		data: Vec<nft_data_structs::CreateItemData>,107	) -> DispatchResultWithPostInfo {108		let data = data109			.into_iter()110			.map(|d| map_create_data::<T>(d, &to))111			.collect::<Result<Vec<_>, DispatchError>>()?;112113		let amount = data.len();114		with_weight(115			<Pallet<T>>::create_multiple_items(self, &sender, data),116			<CommonWeights<T>>::create_multiple_items(amount as u32),117		)118	}119120	fn burn_item(121		&self,122		sender: T::CrossAccountId,123		token: TokenId,124		amount: u128,125	) -> DispatchResultWithPostInfo {126		with_weight(127			<Pallet<T>>::burn(self, &sender, token, amount),128			<CommonWeights<T>>::burn_item(),129		)130	}131132	fn transfer(133		&self,134		from: T::CrossAccountId,135		to: T::CrossAccountId,136		token: TokenId,137		amount: u128,138	) -> DispatchResultWithPostInfo {139		with_weight(140			<Pallet<T>>::transfer(&self, &from, &to, token, amount),141			<CommonWeights<T>>::transfer(),142		)143	}144145	fn approve(146		&self,147		sender: T::CrossAccountId,148		spender: T::CrossAccountId,149		token: TokenId,150		amount: u128,151	) -> DispatchResultWithPostInfo {152		with_weight(153			<Pallet<T>>::set_allowance(&self, &sender, &spender, token, amount),154			<CommonWeights<T>>::approve(),155		)156	}157158	fn transfer_from(159		&self,160		sender: T::CrossAccountId,161		from: T::CrossAccountId,162		to: T::CrossAccountId,163		token: TokenId,164		amount: u128,165	) -> DispatchResultWithPostInfo {166		with_weight(167			<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),168			<CommonWeights<T>>::transfer_from(),169		)170	}171172	fn burn_from(173		&self,174		sender: T::CrossAccountId,175		from: T::CrossAccountId,176		token: TokenId,177		amount: u128,178	) -> DispatchResultWithPostInfo {179		with_weight(180			<Pallet<T>>::burn_from(&self, &sender, &from, token, amount),181			<CommonWeights<T>>::burn_from(),182		)183	}184185	fn set_variable_metadata(186		&self,187		sender: T::CrossAccountId,188		token: TokenId,189		data: Vec<u8>,190	) -> DispatchResultWithPostInfo {191		let len = data.len();192		with_weight(193			<Pallet<T>>::set_variable_metadata(&self, &sender, token, data),194			<CommonWeights<T>>::set_variable_metadata(len as u32),195		)196	}197198	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {199		<Owned<T>>::iter_prefix((self.id, account.as_sub()))200			.map(|(id, _)| id)201			.collect()202	}203204	fn token_exists(&self, token: TokenId) -> bool {205		<Pallet<T>>::token_exists(self, token)206	}207208	fn last_token_id(&self) -> TokenId {209		TokenId(<TokensMinted<T>>::get(self.id))210	}211212	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {213		T::CrossAccountId::default()214	}215	fn const_metadata(&self, token: TokenId) -> Vec<u8> {216		<TokenData<T>>::get((self.id, token)).const_data217	}218	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {219		<TokenData<T>>::get((self.id, token)).variable_data220	}221222	fn collection_tokens(&self) -> u32 {223		<Pallet<T>>::total_supply(self)224	}225226	fn account_balance(&self, account: T::CrossAccountId) -> u32 {227		<AccountBalance<T>>::get((self.id, account.as_sub()))228	}229230	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {231		<Balance<T>>::get((self.id, token, account.as_sub()))232	}233234	fn allowance(235		&self,236		sender: T::CrossAccountId,237		spender: T::CrossAccountId,238		token: TokenId,239	) -> u128 {240		<Allowance<T>>::get((self.id, token, sender.as_sub(), spender))241	}242}