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

difftreelog

source

pallets/fungible/src/common.rs5.9 KiBsourcehistory
1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};4use up_data_structs::{TokenId, CreateItemExData};5use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};6use sp_runtime::ArithmeticError;7use sp_std::{vec::Vec, vec};8use up_data_structs::CustomDataLimit;910use crate::{11	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,12};1314pub struct CommonWeights<T: Config>(PhantomData<T>);15impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {16	fn create_item() -> Weight {17		<SelfWeightOf<T>>::create_item()18	}1920	fn create_multiple_items(_amount: u32) -> Weight {21		Self::create_item()22	}2324	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {25		match data {26			CreateItemExData::Fungible(f) => {27				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)28			}29			_ => 0,30		}31	}3233	fn burn_item() -> Weight {34		<SelfWeightOf<T>>::burn_item()35	}3637	fn transfer() -> Weight {38		<SelfWeightOf<T>>::transfer()39	}4041	fn approve() -> Weight {42		<SelfWeightOf<T>>::approve()43	}4445	fn transfer_from() -> Weight {46		<SelfWeightOf<T>>::transfer_from()47	}4849	fn burn_from() -> Weight {50		<SelfWeightOf<T>>::burn_from()51	}5253	fn set_variable_metadata(_bytes: u32) -> Weight {54		// Error55		056	}57}5859impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {60	fn create_item(61		&self,62		sender: T::CrossAccountId,63		to: T::CrossAccountId,64		data: up_data_structs::CreateItemData,65	) -> DispatchResultWithPostInfo {66		match data {67			up_data_structs::CreateItemData::Fungible(data) => with_weight(68				<Pallet<T>>::create_item(self, &sender, (to, data.value)),69				<CommonWeights<T>>::create_item(),70			),71			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),72		}73	}7475	fn create_multiple_items(76		&self,77		sender: T::CrossAccountId,78		to: T::CrossAccountId,79		data: Vec<up_data_structs::CreateItemData>,80	) -> DispatchResultWithPostInfo {81		let mut sum: u128 = 0;82		for data in data {83			match data {84				up_data_structs::CreateItemData::Fungible(data) => {85					sum = sum86						.checked_add(data.value)87						.ok_or(ArithmeticError::Overflow)?;88				}89				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),90			}91		}9293		with_weight(94			<Pallet<T>>::create_item(self, &sender, (to, sum)),95			<CommonWeights<T>>::create_item(),96		)97	}9899	fn create_multiple_items_ex(100		&self,101		sender: <T>::CrossAccountId,102		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,103	) -> DispatchResultWithPostInfo {104		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);105		let data = match data {106			up_data_structs::CreateItemExData::Fungible(f) => f,107			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),108		};109110		with_weight(111			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),112			weight,113		)114	}115116	fn burn_item(117		&self,118		sender: T::CrossAccountId,119		token: TokenId,120		amount: u128,121	) -> DispatchResultWithPostInfo {122		ensure!(123			token == TokenId::default(),124			<Error<T>>::FungibleItemsHaveNoId125		);126127		with_weight(128			<Pallet<T>>::burn(self, &sender, amount),129			<CommonWeights<T>>::burn_item(),130		)131	}132133	fn transfer(134		&self,135		from: T::CrossAccountId,136		to: T::CrossAccountId,137		token: TokenId,138		amount: u128,139	) -> DispatchResultWithPostInfo {140		ensure!(141			token == TokenId::default(),142			<Error<T>>::FungibleItemsHaveNoId143		);144145		with_weight(146			<Pallet<T>>::transfer(self, &from, &to, amount),147			<CommonWeights<T>>::transfer(),148		)149	}150151	fn approve(152		&self,153		sender: T::CrossAccountId,154		spender: T::CrossAccountId,155		token: TokenId,156		amount: u128,157	) -> DispatchResultWithPostInfo {158		ensure!(159			token == TokenId::default(),160			<Error<T>>::FungibleItemsHaveNoId161		);162163		with_weight(164			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),165			<CommonWeights<T>>::approve(),166		)167	}168169	fn transfer_from(170		&self,171		sender: T::CrossAccountId,172		from: T::CrossAccountId,173		to: T::CrossAccountId,174		token: TokenId,175		amount: u128,176	) -> DispatchResultWithPostInfo {177		ensure!(178			token == TokenId::default(),179			<Error<T>>::FungibleItemsHaveNoId180		);181182		with_weight(183			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount),184			<CommonWeights<T>>::transfer_from(),185		)186	}187188	fn burn_from(189		&self,190		sender: T::CrossAccountId,191		from: T::CrossAccountId,192		token: TokenId,193		amount: u128,194	) -> DispatchResultWithPostInfo {195		ensure!(196			token == TokenId::default(),197			<Error<T>>::FungibleItemsHaveNoId198		);199200		with_weight(201			<Pallet<T>>::burn_from(self, &sender, &from, amount),202			<CommonWeights<T>>::burn_from(),203		)204	}205206	fn set_variable_metadata(207		&self,208		_sender: T::CrossAccountId,209		_token: TokenId,210		_data: BoundedVec<u8, CustomDataLimit>,211	) -> DispatchResultWithPostInfo {212		fail!(<Error<T>>::FungibleItemsDontHaveData)213	}214215	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {216		if <Balance<T>>::get((self.id, account)) != 0 {217			vec![TokenId::default()]218		} else {219			vec![]220		}221	}222223	fn token_exists(&self, token: TokenId) -> bool {224		token == TokenId::default()225	}226227	fn last_token_id(&self) -> TokenId {228		TokenId::default()229	}230231	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {232		None233	}234	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {235		Vec::new()236	}237	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {238		Vec::new()239	}240241	fn collection_tokens(&self) -> u32 {242		1243	}244245	fn account_balance(&self, account: T::CrossAccountId) -> u32 {246		if <Balance<T>>::get((self.id, account)) != 0 {247			1248		} else {249			0250		}251	}252253	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {254		if token != TokenId::default() {255			return 0;256		}257		<Balance<T>>::get((self.id, account))258	}259260	fn allowance(261		&self,262		sender: T::CrossAccountId,263		spender: T::CrossAccountId,264		token: TokenId,265	) -> u128 {266		if token != TokenId::default() {267			return 0;268		}269		<Allowance<T>>::get((self.id, sender, spender))270	}271}