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

difftreelog

source

pallets/fungible/src/common.rs5.1 KiBsourcehistory
1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};4use nft_data_structs::TokenId;5use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};6use sp_runtime::ArithmeticError;7use sp_std::{vec::Vec, vec};89use crate::{10	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,11};1213pub struct CommonWeights<T: Config>(PhantomData<T>);14impl<T: Config> CommonWeightInfo for CommonWeights<T> {15	fn create_item() -> Weight {16		<SelfWeightOf<T>>::create_item()17	}1819	fn create_multiple_items(_amount: u32) -> Weight {20		Self::create_item()21	}2223	fn burn_item() -> Weight {24		<SelfWeightOf<T>>::burn_item()25	}2627	fn transfer() -> Weight {28		<SelfWeightOf<T>>::transfer()29	}3031	fn approve() -> Weight {32		<SelfWeightOf<T>>::approve()33	}3435	fn transfer_from() -> Weight {36		<SelfWeightOf<T>>::transfer_from()37	}3839	fn burn_from() -> Weight {40		<SelfWeightOf<T>>::burn_from()41	}4243	fn set_variable_metadata(_bytes: u32) -> Weight {44		// Error45		046	}47}4849impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {50	fn create_item(51		&self,52		sender: T::CrossAccountId,53		to: T::CrossAccountId,54		data: nft_data_structs::CreateItemData,55	) -> DispatchResultWithPostInfo {56		match data {57			nft_data_structs::CreateItemData::Fungible(data) => with_weight(58				<Pallet<T>>::create_item(self, &sender, (to, data.value)),59				<CommonWeights<T>>::create_item(),60			),61			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),62		}63	}6465	fn create_multiple_items(66		&self,67		sender: T::CrossAccountId,68		to: T::CrossAccountId,69		data: Vec<nft_data_structs::CreateItemData>,70	) -> DispatchResultWithPostInfo {71		let mut sum: u128 = 0;72		for data in data {73			match data {74				nft_data_structs::CreateItemData::Fungible(data) => {75					sum = sum76						.checked_add(data.value)77						.ok_or(ArithmeticError::Overflow)?;78				}79				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),80			}81		}8283		with_weight(84			<Pallet<T>>::create_item(self, &sender, (to, sum)),85			<CommonWeights<T>>::create_item(),86		)87	}8889	fn burn_item(90		&self,91		sender: T::CrossAccountId,92		token: TokenId,93		amount: u128,94	) -> DispatchResultWithPostInfo {95		ensure!(96			token == TokenId::default(),97			<Error<T>>::FungibleItemsHaveNoId98		);99100		with_weight(101			<Pallet<T>>::burn(self, &sender, amount),102			<CommonWeights<T>>::burn_item(),103		)104	}105106	fn transfer(107		&self,108		from: T::CrossAccountId,109		to: T::CrossAccountId,110		token: TokenId,111		amount: u128,112	) -> DispatchResultWithPostInfo {113		ensure!(114			token == TokenId::default(),115			<Error<T>>::FungibleItemsHaveNoId116		);117118		with_weight(119			<Pallet<T>>::transfer(&self, &from, &to, amount),120			<CommonWeights<T>>::transfer(),121		)122	}123124	fn approve(125		&self,126		sender: T::CrossAccountId,127		spender: T::CrossAccountId,128		token: TokenId,129		amount: u128,130	) -> DispatchResultWithPostInfo {131		ensure!(132			token == TokenId::default(),133			<Error<T>>::FungibleItemsHaveNoId134		);135136		with_weight(137			<Pallet<T>>::set_allowance(&self, &sender, &spender, amount),138			<CommonWeights<T>>::approve(),139		)140	}141142	fn transfer_from(143		&self,144		sender: T::CrossAccountId,145		from: T::CrossAccountId,146		to: T::CrossAccountId,147		token: TokenId,148		amount: u128,149	) -> DispatchResultWithPostInfo {150		ensure!(151			token == TokenId::default(),152			<Error<T>>::FungibleItemsHaveNoId153		);154155		with_weight(156			<Pallet<T>>::transfer_from(&self, &sender, &from, &to, amount),157			<CommonWeights<T>>::transfer_from(),158		)159	}160161	fn burn_from(162		&self,163		sender: T::CrossAccountId,164		from: T::CrossAccountId,165		token: TokenId,166		amount: u128,167	) -> DispatchResultWithPostInfo {168		ensure!(169			token == TokenId::default(),170			<Error<T>>::FungibleItemsHaveNoId171		);172173		with_weight(174			<Pallet<T>>::burn_from(&self, &sender, &from, amount),175			<CommonWeights<T>>::burn_from(),176		)177	}178179	fn set_variable_metadata(180		&self,181		_sender: T::CrossAccountId,182		_token: TokenId,183		_data: Vec<u8>,184	) -> DispatchResultWithPostInfo {185		fail!(<Error<T>>::FungibleItemsHaveData)186	}187188	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {189		if <Balance<T>>::get((self.id, account)) != 0 {190			vec![TokenId::default()]191		} else {192			vec![]193		}194	}195196	fn token_exists(&self, token: TokenId) -> bool {197		token == TokenId::default()198	}199200	fn last_token_id(&self) -> TokenId {201		TokenId::default()202	}203204	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {205		T::CrossAccountId::default()206	}207	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {208		Vec::new()209	}210	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {211		Vec::new()212	}213214	fn collection_tokens(&self) -> u32 {215		1216	}217218	fn account_balance(&self, account: T::CrossAccountId) -> u32 {219		if <Balance<T>>::get((self.id, account)) != 0 {220			1221		} else {222			0223		}224	}225226	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {227		if token != TokenId::default() {228			return 0;229		}230		<Balance<T>>::get((self.id, account))231	}232233	fn allowance(234		&self,235		sender: T::CrossAccountId,236		spender: T::CrossAccountId,237		token: TokenId,238	) -> u128 {239		if token != TokenId::default() {240			return 0;241		}242		<Allowance<T>>::get((self.id, sender, spender))243	}244}