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

difftreelog

source

pallets/refungible/src/common.rs8.6 KiBsourcehistory
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/>.1617use core::marker::PhantomData;1819use sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};21use up_data_structs::{22	CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,23	budget::Budget, Property,24};25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};26use sp_runtime::DispatchError;27use sp_std::{vec::Vec, vec};2829use crate::{30	AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,31	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,32};3334macro_rules! max_weight_of {35	($($method:ident ($($args:tt)*)),*) => {36		037		$(38			.max(<SelfWeightOf<T>>::$method($($args)*))39		)*40	};41}4243pub struct CommonWeights<T: Config>(PhantomData<T>);44impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {45	fn create_item() -> Weight {46		<SelfWeightOf<T>>::create_item()47	}4849	fn create_multiple_items(amount: u32) -> Weight {50		<SelfWeightOf<T>>::create_multiple_items(amount)51	}5253	fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {54		match call {55			CreateItemExData::RefungibleMultipleOwners(i) => {56				<SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)57			}58			CreateItemExData::RefungibleMultipleItems(i) => {59				<SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)60			}61			_ => 0,62		}63	}6465	fn burn_item() -> Weight {66		max_weight_of!(burn_item_partial(), burn_item_fully())67	}6869	fn set_property() -> Weight {70		<SelfWeightOf<T>>::set_property()71	}7273	fn transfer() -> Weight {74		max_weight_of!(75			transfer_normal(),76			transfer_creating(),77			transfer_removing(),78			transfer_creating_removing()79		)80	}8182	fn approve() -> Weight {83		<SelfWeightOf<T>>::approve()84	}8586	fn transfer_from() -> Weight {87		max_weight_of!(88			transfer_from_normal(),89			transfer_from_creating(),90			transfer_from_removing(),91			transfer_from_creating_removing()92		)93	}9495	fn burn_from() -> Weight {96		<SelfWeightOf<T>>::burn_from()97	}9899	fn set_variable_metadata(bytes: u32) -> Weight {100		<SelfWeightOf<T>>::set_variable_metadata(bytes)101	}102}103104fn map_create_data<T: Config>(105	data: up_data_structs::CreateItemData,106	to: &T::CrossAccountId,107) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {108	match data {109		up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {110			const_data: data.const_data,111			variable_data: data.variable_data,112			users: {113				let mut out = BTreeMap::new();114				out.insert(to.clone(), data.pieces);115				out.try_into().expect("limit > 0")116			},117		}),118		_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),119	}120}121122impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {123	fn create_item(124		&self,125		sender: T::CrossAccountId,126		to: T::CrossAccountId,127		data: up_data_structs::CreateItemData,128		nesting_budget: &dyn Budget,129	) -> DispatchResultWithPostInfo {130		with_weight(131			<Pallet<T>>::create_item(132				self,133				&sender,134				map_create_data::<T>(data, &to)?,135				nesting_budget,136			),137			<CommonWeights<T>>::create_item(),138		)139	}140141	fn create_multiple_items(142		&self,143		sender: T::CrossAccountId,144		to: T::CrossAccountId,145		data: Vec<up_data_structs::CreateItemData>,146		nesting_budget: &dyn Budget,147	) -> DispatchResultWithPostInfo {148		let data = data149			.into_iter()150			.map(|d| map_create_data::<T>(d, &to))151			.collect::<Result<Vec<_>, DispatchError>>()?;152153		let amount = data.len();154		with_weight(155			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),156			<CommonWeights<T>>::create_multiple_items(amount as u32),157		)158	}159160	fn create_multiple_items_ex(161		&self,162		sender: <T>::CrossAccountId,163		data: CreateItemExData<T::CrossAccountId>,164		nesting_budget: &dyn Budget,165	) -> DispatchResultWithPostInfo {166		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);167		let data = match data {168			CreateItemExData::RefungibleMultipleOwners(r) => vec![r],169			CreateItemExData::RefungibleMultipleItems(r)170				if r.iter().all(|i| i.users.len() == 1) =>171			{172				r.into_inner()173			}174			_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),175		};176177		with_weight(178			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),179			weight,180		)181	}182183	fn burn_item(184		&self,185		sender: T::CrossAccountId,186		token: TokenId,187		amount: u128,188	) -> DispatchResultWithPostInfo {189		with_weight(190			<Pallet<T>>::burn(self, &sender, token, amount),191			<CommonWeights<T>>::burn_item(),192		)193	}194195	fn transfer(196		&self,197		from: T::CrossAccountId,198		to: T::CrossAccountId,199		token: TokenId,200		amount: u128,201		nesting_budget: &dyn Budget,202	) -> DispatchResultWithPostInfo {203		with_weight(204			<Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),205			<CommonWeights<T>>::transfer(),206		)207	}208209	fn approve(210		&self,211		sender: T::CrossAccountId,212		spender: T::CrossAccountId,213		token: TokenId,214		amount: u128,215	) -> DispatchResultWithPostInfo {216		with_weight(217			<Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),218			<CommonWeights<T>>::approve(),219		)220	}221222	fn transfer_from(223		&self,224		sender: T::CrossAccountId,225		from: T::CrossAccountId,226		to: T::CrossAccountId,227		token: TokenId,228		amount: u128,229		nesting_budget: &dyn Budget,230	) -> DispatchResultWithPostInfo {231		with_weight(232			<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),233			<CommonWeights<T>>::transfer_from(),234		)235	}236237	fn burn_from(238		&self,239		sender: T::CrossAccountId,240		from: T::CrossAccountId,241		token: TokenId,242		amount: u128,243		nesting_budget: &dyn Budget,244	) -> DispatchResultWithPostInfo {245		with_weight(246			<Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),247			<CommonWeights<T>>::burn_from(),248		)249	}250251	fn change_collection_property(252		&self,253		_sender: T::CrossAccountId,254		_property: Property,255	) -> DispatchResultWithPostInfo {256		fail!(<Error<T>>::PropertiesNotAllowed)257	}258259	fn change_token_property(260		&self,261		_sender: T::CrossAccountId,262		_token_id: TokenId,263		_property: Property,264	) -> DispatchResultWithPostInfo {265		fail!(<Error<T>>::PropertiesNotAllowed)266	}267268	fn set_variable_metadata(269		&self,270		sender: T::CrossAccountId,271		token: TokenId,272		data: BoundedVec<u8, CustomDataLimit>,273	) -> DispatchResultWithPostInfo {274		let len = data.len();275		with_weight(276			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),277			<CommonWeights<T>>::set_variable_metadata(len as u32),278		)279	}280281	fn check_nesting(282		&self,283		_sender: <T>::CrossAccountId,284		_from: (CollectionId, TokenId),285		_under: TokenId,286		_budget: &dyn Budget,287	) -> sp_runtime::DispatchResult {288		fail!(<Error<T>>::RefungibleDisallowsNesting)289	}290291	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {292		<Owned<T>>::iter_prefix((self.id, account))293			.map(|(id, _)| id)294			.collect()295	}296297	fn collection_tokens(&self) -> Vec<TokenId> {298		<TokenData<T>>::iter_prefix((self.id,))299			.map(|(id, _)| id)300			.collect()301	}302303	fn token_exists(&self, token: TokenId) -> bool {304		<Pallet<T>>::token_exists(self, token)305	}306307	fn last_token_id(&self) -> TokenId {308		TokenId(<TokensMinted<T>>::get(self.id))309	}310311	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {312		None313	}314	fn const_metadata(&self, token: TokenId) -> Vec<u8> {315		<TokenData<T>>::get((self.id, token))316			.const_data317			.into_inner()318	}319	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {320		<TokenData<T>>::get((self.id, token))321			.variable_data322			.into_inner()323	}324325	fn total_supply(&self) -> u32 {326		<Pallet<T>>::total_supply(self)327	}328329	fn account_balance(&self, account: T::CrossAccountId) -> u32 {330		<AccountBalance<T>>::get((self.id, account))331	}332333	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {334		<Balance<T>>::get((self.id, token, account))335	}336337	fn allowance(338		&self,339		sender: T::CrossAccountId,340		spender: T::CrossAccountId,341		token: TokenId,342	) -> u128 {343		<Allowance<T>>::get((self.id, token, sender, spender))344	}345}