git.delta.rocks / unique-network / refs/commits / f8774b896e5b

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::{CommonCollectionOperations, CommonWeightInfo, with_weight};7use sp_runtime::DispatchError;8use sp_std::vec::Vec;910use crate::{11	AccountBalance, Allowance, Balance, Config, CreateItemData, Error, Owned, Pallet,12	RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,13};1415macro_rules! max_weight_of {16	($($method:ident ($($args:tt)*)),*) => {17		018		$(19			.max(<SelfWeightOf<T>>::$method($($args)*))20		)*21	};22}2324pub struct CommonWeights<T: Config>(PhantomData<T>);25impl<T: Config> CommonWeightInfo for CommonWeights<T> {26	fn create_item() -> Weight {27		<SelfWeightOf<T>>::create_item()28	}2930	fn create_multiple_items(amount: u32) -> Weight {31		<SelfWeightOf<T>>::create_multiple_items(amount)32	}3334	fn burn_item() -> Weight {35		max_weight_of!(burn_item_partial(), burn_item_fully())36	}3738	fn transfer() -> Weight {39		max_weight_of!(40			transfer_normal(),41			transfer_creating(),42			transfer_removing(),43			transfer_creating_removing()44		)45	}4647	fn approve() -> Weight {48		<SelfWeightOf<T>>::approve()49	}5051	fn transfer_from() -> Weight {52		max_weight_of!(53			transfer_from_normal(),54			transfer_from_creating(),55			transfer_from_removing(),56			transfer_from_creating_removing()57		)58	}5960	fn burn_from() -> Weight {61		062	}6364	fn set_variable_metadata(bytes: u32) -> Weight {65		<SelfWeightOf<T>>::set_variable_metadata(bytes)66	}67}6869fn map_create_data<T: Config>(70	data: nft_data_structs::CreateItemData,71	to: &T::CrossAccountId,72) -> Result<CreateItemData<T>, DispatchError> {73	match data {74		nft_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData {75			const_data: data.const_data,76			variable_data: data.variable_data,77			users: {78				let mut out = BTreeMap::new();79				out.insert(to.clone(), data.pieces);80				out81			},82		}),83		_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),84	}85}8687impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {88	fn create_item(89		&self,90		sender: T::CrossAccountId,91		to: T::CrossAccountId,92		data: nft_data_structs::CreateItemData,93	) -> DispatchResultWithPostInfo {94		with_weight(95			<Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),96			<CommonWeights<T>>::create_item(),97		)98	}99100	fn create_multiple_items(101		&self,102		sender: T::CrossAccountId,103		to: T::CrossAccountId,104		data: Vec<nft_data_structs::CreateItemData>,105	) -> DispatchResultWithPostInfo {106		let data = data107			.into_iter()108			.map(|d| map_create_data::<T>(d, &to))109			.collect::<Result<Vec<_>, DispatchError>>()?;110111		let amount = data.len();112		with_weight(113			<Pallet<T>>::create_multiple_items(self, &sender, data),114			<CommonWeights<T>>::create_multiple_items(amount as u32),115		)116	}117118	fn burn_item(119		&self,120		sender: T::CrossAccountId,121		token: TokenId,122		amount: u128,123	) -> DispatchResultWithPostInfo {124		with_weight(125			<Pallet<T>>::burn(self, &sender, token, amount),126			<CommonWeights<T>>::burn_item(),127		)128	}129130	fn transfer(131		&self,132		from: T::CrossAccountId,133		to: T::CrossAccountId,134		token: TokenId,135		amount: u128,136	) -> DispatchResultWithPostInfo {137		with_weight(138			<Pallet<T>>::transfer(&self, &from, &to, token, amount),139			<CommonWeights<T>>::transfer(),140		)141	}142143	fn approve(144		&self,145		sender: T::CrossAccountId,146		spender: T::CrossAccountId,147		token: TokenId,148		amount: u128,149	) -> DispatchResultWithPostInfo {150		with_weight(151			<Pallet<T>>::set_allowance(&self, &sender, &spender, token, amount),152			<CommonWeights<T>>::approve(),153		)154	}155156	fn transfer_from(157		&self,158		sender: T::CrossAccountId,159		from: T::CrossAccountId,160		to: T::CrossAccountId,161		token: TokenId,162		amount: u128,163	) -> DispatchResultWithPostInfo {164		with_weight(165			<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),166			<CommonWeights<T>>::transfer_from(),167		)168	}169170	fn burn_from(171		&self,172		sender: T::CrossAccountId,173		from: T::CrossAccountId,174		token: TokenId,175		amount: u128,176	) -> DispatchResultWithPostInfo {177		with_weight(178			<Pallet<T>>::burn_from(&self, &sender, &from, token, amount),179			<CommonWeights<T>>::burn_from(),180		)181	}182183	fn set_variable_metadata(184		&self,185		sender: T::CrossAccountId,186		token: TokenId,187		data: Vec<u8>,188	) -> DispatchResultWithPostInfo {189		let len = data.len();190		with_weight(191			<Pallet<T>>::set_variable_metadata(&self, &sender, token, data),192			<CommonWeights<T>>::set_variable_metadata(len as u32),193		)194	}195196	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {197		<Owned<T>>::iter_prefix((self.id, account))198			.map(|(id, _)| id)199			.collect()200	}201202	fn token_exists(&self, token: TokenId) -> bool {203		<Pallet<T>>::token_exists(self, token)204	}205206	fn last_token_id(&self) -> TokenId {207		TokenId(<TokensMinted<T>>::get(self.id))208	}209210	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {211		T::CrossAccountId::default()212	}213	fn const_metadata(&self, token: TokenId) -> Vec<u8> {214		<TokenData<T>>::get((self.id, token)).const_data215	}216	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {217		<TokenData<T>>::get((self.id, token)).variable_data218	}219220	fn collection_tokens(&self) -> u32 {221		<Pallet<T>>::total_supply(self)222	}223224	fn account_balance(&self, account: T::CrossAccountId) -> u32 {225		<AccountBalance<T>>::get((self.id, account))226	}227228	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {229		<Balance<T>>::get((self.id, token, account))230	}231232	fn allowance(233		&self,234		sender: T::CrossAccountId,235		spender: T::CrossAccountId,236		token: TokenId,237	) -> u128 {238		<Allowance<T>>::get((self.id, token, sender, spender))239	}240}