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

difftreelog

source

pallets/fungible/src/common.rs7.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, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{CustomDataLimit, Property};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(_amount: u32) -> Weight {37		Self::create_item()38	}3940	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41		match data {42			CreateItemExData::Fungible(f) => {43				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44			}45			_ => 0,46		}47	}4849	fn burn_item() -> Weight {50		<SelfWeightOf<T>>::burn_item()51	}5253	fn set_property() -> Weight {54		<SelfWeightOf<T>>::set_property()55	}5657	fn transfer() -> Weight {58		<SelfWeightOf<T>>::transfer()59	}6061	fn approve() -> Weight {62		<SelfWeightOf<T>>::approve()63	}6465	fn transfer_from() -> Weight {66		<SelfWeightOf<T>>::transfer_from()67	}6869	fn burn_from() -> Weight {70		<SelfWeightOf<T>>::burn_from()71	}7273	fn set_variable_metadata(_bytes: u32) -> Weight {74		// Error75		076	}77}7879impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {80	fn create_item(81		&self,82		sender: T::CrossAccountId,83		to: T::CrossAccountId,84		data: up_data_structs::CreateItemData,85		nesting_budget: &dyn Budget,86	) -> DispatchResultWithPostInfo {87		match data {88			up_data_structs::CreateItemData::Fungible(data) => with_weight(89				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),90				<CommonWeights<T>>::create_item(),91			),92			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),93		}94	}9596	fn create_multiple_items(97		&self,98		sender: T::CrossAccountId,99		to: T::CrossAccountId,100		data: Vec<up_data_structs::CreateItemData>,101		nesting_budget: &dyn Budget,102	) -> DispatchResultWithPostInfo {103		let mut sum: u128 = 0;104		for data in data {105			match data {106				up_data_structs::CreateItemData::Fungible(data) => {107					sum = sum108						.checked_add(data.value)109						.ok_or(ArithmeticError::Overflow)?;110				}111				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),112			}113		}114115		with_weight(116			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),117			<CommonWeights<T>>::create_item(),118		)119	}120121	fn create_multiple_items_ex(122		&self,123		sender: <T>::CrossAccountId,124		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,125		nesting_budget: &dyn Budget,126	) -> DispatchResultWithPostInfo {127		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);128		let data = match data {129			up_data_structs::CreateItemExData::Fungible(f) => f,130			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),131		};132133		with_weight(134			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),135			weight,136		)137	}138139	fn burn_item(140		&self,141		sender: T::CrossAccountId,142		token: TokenId,143		amount: u128,144	) -> DispatchResultWithPostInfo {145		ensure!(146			token == TokenId::default(),147			<Error<T>>::FungibleItemsHaveNoId148		);149150		with_weight(151			<Pallet<T>>::burn(self, &sender, amount),152			<CommonWeights<T>>::burn_item(),153		)154	}155156	fn transfer(157		&self,158		from: T::CrossAccountId,159		to: T::CrossAccountId,160		token: TokenId,161		amount: u128,162		nesting_budget: &dyn Budget,163	) -> DispatchResultWithPostInfo {164		ensure!(165			token == TokenId::default(),166			<Error<T>>::FungibleItemsHaveNoId167		);168169		with_weight(170			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),171			<CommonWeights<T>>::transfer(),172		)173	}174175	fn approve(176		&self,177		sender: T::CrossAccountId,178		spender: T::CrossAccountId,179		token: TokenId,180		amount: u128,181	) -> DispatchResultWithPostInfo {182		ensure!(183			token == TokenId::default(),184			<Error<T>>::FungibleItemsHaveNoId185		);186187		with_weight(188			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),189			<CommonWeights<T>>::approve(),190		)191	}192193	fn transfer_from(194		&self,195		sender: T::CrossAccountId,196		from: T::CrossAccountId,197		to: T::CrossAccountId,198		token: TokenId,199		amount: u128,200		nesting_budget: &dyn Budget,201	) -> DispatchResultWithPostInfo {202		ensure!(203			token == TokenId::default(),204			<Error<T>>::FungibleItemsHaveNoId205		);206207		with_weight(208			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),209			<CommonWeights<T>>::transfer_from(),210		)211	}212213	fn burn_from(214		&self,215		sender: T::CrossAccountId,216		from: T::CrossAccountId,217		token: TokenId,218		amount: u128,219		nesting_budget: &dyn Budget,220	) -> DispatchResultWithPostInfo {221		ensure!(222			token == TokenId::default(),223			<Error<T>>::FungibleItemsHaveNoId224		);225226		with_weight(227			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),228			<CommonWeights<T>>::burn_from(),229		)230	}231232	fn change_collection_property(233		&self,234		_sender: T::CrossAccountId,235		_property: Property,236	) -> DispatchResultWithPostInfo {237		fail!(<Error<T>>::PropertiesNotAllowed)238	}239240	fn change_token_property(241		&self,242		_sender: T::CrossAccountId,243		_token_id: TokenId,244		_property: Property,245	) -> DispatchResultWithPostInfo {246		fail!(<Error<T>>::PropertiesNotAllowed)247	}248249	fn set_variable_metadata(250		&self,251		_sender: T::CrossAccountId,252		_token: TokenId,253		_data: BoundedVec<u8, CustomDataLimit>,254	) -> DispatchResultWithPostInfo {255		fail!(<Error<T>>::FungibleItemsDontHaveData)256	}257258	fn check_nesting(259		&self,260		_sender: <T>::CrossAccountId,261		_from: (CollectionId, TokenId),262		_under: TokenId,263		_budget: &dyn Budget,264	) -> sp_runtime::DispatchResult {265		fail!(<Error<T>>::FungibleDisallowsNesting)266	}267268	fn collection_tokens(&self) -> Vec<TokenId> {269		vec![TokenId::default()]270	}271272	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {273		if <Balance<T>>::get((self.id, account)) != 0 {274			vec![TokenId::default()]275		} else {276			vec![]277		}278	}279280	fn token_exists(&self, token: TokenId) -> bool {281		token == TokenId::default()282	}283284	fn last_token_id(&self) -> TokenId {285		TokenId::default()286	}287288	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {289		None290	}291	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {292		Vec::new()293	}294	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {295		Vec::new()296	}297298	fn total_supply(&self) -> u32 {299		1300	}301302	fn account_balance(&self, account: T::CrossAccountId) -> u32 {303		if <Balance<T>>::get((self.id, account)) != 0 {304			1305		} else {306			0307		}308	}309310	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {311		if token != TokenId::default() {312			return 0;313		}314		<Balance<T>>::get((self.id, account))315	}316317	fn allowance(318		&self,319		sender: T::CrossAccountId,320		spender: T::CrossAccountId,321		token: TokenId,322	) -> u128 {323		if token != TokenId::default() {324			return 0;325		}326		<Allowance<T>>::get((self.id, sender, spender))327	}328}