git.delta.rocks / unique-network / refs/commits / 77688fbffa89

difftreelog

source

pallets/fungible/src/common.rs8.7 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 frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};2526use crate::{27	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,28};2930pub struct CommonWeights<T: Config>(PhantomData<T>);31impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {32	fn create_item() -> Weight {33		<SelfWeightOf<T>>::create_item()34	}3536	fn create_multiple_items(_data: &[CreateItemData]) -> Weight {37		// All items minted for the same user, so it works same as create_item38		Self::create_item()39	}4041	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {42		match data {43			CreateItemExData::Fungible(f) => {44				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)45			}46			_ => 0,47		}48	}4950	fn burn_item() -> Weight {51		<SelfWeightOf<T>>::burn_item()52	}5354	fn set_collection_properties(_amount: u32) -> Weight {55		// Error56		057	}5859	fn delete_collection_properties(_amount: u32) -> Weight {60		// Error61		062	}6364	fn set_token_properties(_amount: u32) -> Weight {65		// Error66		067	}6869	fn delete_token_properties(_amount: u32) -> Weight {70		// Error71		072	}7374	fn set_property_permissions(_amount: u32) -> Weight {75		// Error76		077	}7879	fn transfer() -> Weight {80		<SelfWeightOf<T>>::transfer()81	}8283	fn approve() -> Weight {84		<SelfWeightOf<T>>::approve()85	}8687	fn transfer_from() -> Weight {88		<SelfWeightOf<T>>::transfer_from()89	}9091	fn burn_from() -> Weight {92		<SelfWeightOf<T>>::burn_from()93	}94}9596impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {97	fn create_item(98		&self,99		sender: T::CrossAccountId,100		to: T::CrossAccountId,101		data: up_data_structs::CreateItemData,102		nesting_budget: &dyn Budget,103	) -> DispatchResultWithPostInfo {104		match data {105			up_data_structs::CreateItemData::Fungible(data) => with_weight(106				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),107				<CommonWeights<T>>::create_item(),108			),109			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),110		}111	}112113	fn create_multiple_items(114		&self,115		sender: T::CrossAccountId,116		to: T::CrossAccountId,117		data: Vec<up_data_structs::CreateItemData>,118		nesting_budget: &dyn Budget,119	) -> DispatchResultWithPostInfo {120		let mut sum: u128 = 0;121		for data in data {122			match data {123				up_data_structs::CreateItemData::Fungible(data) => {124					sum = sum125						.checked_add(data.value)126						.ok_or(ArithmeticError::Overflow)?;127				}128				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),129			}130		}131132		with_weight(133			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),134			<CommonWeights<T>>::create_item(),135		)136	}137138	fn create_multiple_items_ex(139		&self,140		sender: <T>::CrossAccountId,141		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,142		nesting_budget: &dyn Budget,143	) -> DispatchResultWithPostInfo {144		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);145		let data = match data {146			up_data_structs::CreateItemExData::Fungible(f) => f,147			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),148		};149150		with_weight(151			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),152			weight,153		)154	}155156	fn burn_item(157		&self,158		sender: T::CrossAccountId,159		token: TokenId,160		amount: u128,161	) -> DispatchResultWithPostInfo {162		ensure!(163			token == TokenId::default(),164			<Error<T>>::FungibleItemsHaveNoId165		);166167		with_weight(168			<Pallet<T>>::burn(self, &sender, amount),169			<CommonWeights<T>>::burn_item(),170		)171	}172173	fn burn_item_unchecked(174		&self,175		owner: &T::CrossAccountId,176		_token: TokenId,177		amount: u128,178	) -> sp_runtime::DispatchResult {179		<Pallet<T>>::burn_item_unchecked(self, owner, amount)?;180181		Ok(())182	}183184	fn transfer(185		&self,186		from: T::CrossAccountId,187		to: T::CrossAccountId,188		token: TokenId,189		amount: u128,190		nesting_budget: &dyn Budget,191	) -> DispatchResultWithPostInfo {192		ensure!(193			token == TokenId::default(),194			<Error<T>>::FungibleItemsHaveNoId195		);196197		with_weight(198			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),199			<CommonWeights<T>>::transfer(),200		)201	}202203	fn approve(204		&self,205		sender: T::CrossAccountId,206		spender: T::CrossAccountId,207		token: TokenId,208		amount: u128,209	) -> DispatchResultWithPostInfo {210		ensure!(211			token == TokenId::default(),212			<Error<T>>::FungibleItemsHaveNoId213		);214215		with_weight(216			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),217			<CommonWeights<T>>::approve(),218		)219	}220221	fn transfer_from(222		&self,223		sender: T::CrossAccountId,224		from: T::CrossAccountId,225		to: T::CrossAccountId,226		token: TokenId,227		amount: u128,228		nesting_budget: &dyn Budget,229	) -> DispatchResultWithPostInfo {230		ensure!(231			token == TokenId::default(),232			<Error<T>>::FungibleItemsHaveNoId233		);234235		with_weight(236			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),237			<CommonWeights<T>>::transfer_from(),238		)239	}240241	fn burn_from(242		&self,243		sender: T::CrossAccountId,244		from: T::CrossAccountId,245		token: TokenId,246		amount: u128,247		nesting_budget: &dyn Budget,248	) -> DispatchResultWithPostInfo {249		ensure!(250			token == TokenId::default(),251			<Error<T>>::FungibleItemsHaveNoId252		);253254		with_weight(255			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),256			<CommonWeights<T>>::burn_from(),257		)258	}259260	fn set_collection_properties(261		&self,262		_sender: T::CrossAccountId,263		_property: Vec<Property>,264	) -> DispatchResultWithPostInfo {265		fail!(<Error<T>>::SettingPropertiesNotAllowed)266	}267268	fn delete_collection_properties(269		&self,270		_sender: &T::CrossAccountId,271		_property_keys: Vec<PropertyKey>,272	) -> DispatchResultWithPostInfo {273		fail!(<Error<T>>::SettingPropertiesNotAllowed)274	}275276	fn set_token_properties(277		&self,278		_sender: T::CrossAccountId,279		_token_id: TokenId,280		_property: Vec<Property>,281	) -> DispatchResultWithPostInfo {282		fail!(<Error<T>>::SettingPropertiesNotAllowed)283	}284285	fn set_property_permissions(286		&self,287		_sender: &T::CrossAccountId,288		_property_permissions: Vec<PropertyKeyPermission>,289	) -> DispatchResultWithPostInfo {290		fail!(<Error<T>>::SettingPropertiesNotAllowed)291	}292293	fn delete_token_properties(294		&self,295		_sender: T::CrossAccountId,296		_token_id: TokenId,297		_property_keys: Vec<PropertyKey>,298	) -> DispatchResultWithPostInfo {299		fail!(<Error<T>>::SettingPropertiesNotAllowed)300	}301302	fn check_nesting(303		&self,304		_sender: <T>::CrossAccountId,305		_from: (CollectionId, TokenId),306		_under: TokenId,307		_budget: &dyn Budget,308	) -> sp_runtime::DispatchResult {309		fail!(<Error<T>>::FungibleDisallowsNesting)310	}311312	fn collection_tokens(&self) -> Vec<TokenId> {313		vec![TokenId::default()]314	}315316	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {317		if <Balance<T>>::get((self.id, account)) != 0 {318			vec![TokenId::default()]319		} else {320			vec![]321		}322	}323324	fn token_exists(&self, token: TokenId) -> bool {325		token == TokenId::default()326	}327328	fn last_token_id(&self) -> TokenId {329		TokenId::default()330	}331332	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {333		None334	}335336	fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {337		None338	}339340	fn token_properties(341		&self,342		_token_id: TokenId,343		_keys: Option<Vec<PropertyKey>>,344	) -> Vec<Property> {345		Vec::new()346	}347348	fn total_supply(&self) -> u32 {349		1350	}351352	fn account_balance(&self, account: T::CrossAccountId) -> u32 {353		if <Balance<T>>::get((self.id, account)) != 0 {354			1355		} else {356			0357		}358	}359360	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {361		if token != TokenId::default() {362			return 0;363		}364		<Balance<T>>::get((self.id, account))365	}366367	fn allowance(368		&self,369		sender: T::CrossAccountId,370		spender: T::CrossAccountId,371		token: TokenId,372	) -> u128 {373		if token != TokenId::default() {374			return 0;375		}376		<Allowance<T>>::get((self.id, sender, spender))377	}378}