1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};4use up_data_structs::TokenId;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 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 burn_item() -> Weight {25 <SelfWeightOf<T>>::burn_item()26 }2728 fn transfer() -> Weight {29 <SelfWeightOf<T>>::transfer()30 }3132 fn approve() -> Weight {33 <SelfWeightOf<T>>::approve()34 }3536 fn transfer_from() -> Weight {37 <SelfWeightOf<T>>::transfer_from()38 }3940 fn burn_from() -> Weight {41 <SelfWeightOf<T>>::burn_from()42 }4344 fn set_variable_metadata(_bytes: u32) -> Weight {45 46 047 }48}4950impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {51 fn create_item(52 &self,53 sender: T::CrossAccountId,54 to: T::CrossAccountId,55 data: up_data_structs::CreateItemData,56 ) -> DispatchResultWithPostInfo {57 match data {58 up_data_structs::CreateItemData::Fungible(data) => with_weight(59 <Pallet<T>>::create_item(self, &sender, (to, data.value)),60 <CommonWeights<T>>::create_item(),61 ),62 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),63 }64 }6566 fn create_multiple_items(67 &self,68 sender: T::CrossAccountId,69 to: T::CrossAccountId,70 data: Vec<up_data_structs::CreateItemData>,71 ) -> DispatchResultWithPostInfo {72 let mut sum: u128 = 0;73 for data in data {74 match data {75 up_data_structs::CreateItemData::Fungible(data) => {76 sum = sum77 .checked_add(data.value)78 .ok_or(ArithmeticError::Overflow)?;79 }80 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),81 }82 }8384 with_weight(85 <Pallet<T>>::create_item(self, &sender, (to, sum)),86 <CommonWeights<T>>::create_item(),87 )88 }8990 fn burn_item(91 &self,92 sender: T::CrossAccountId,93 token: TokenId,94 amount: u128,95 ) -> DispatchResultWithPostInfo {96 ensure!(97 token == TokenId::default(),98 <Error<T>>::FungibleItemsHaveNoId99 );100101 with_weight(102 <Pallet<T>>::burn(self, &sender, amount),103 <CommonWeights<T>>::burn_item(),104 )105 }106107 fn transfer(108 &self,109 from: T::CrossAccountId,110 to: T::CrossAccountId,111 token: TokenId,112 amount: u128,113 ) -> DispatchResultWithPostInfo {114 ensure!(115 token == TokenId::default(),116 <Error<T>>::FungibleItemsHaveNoId117 );118119 with_weight(120 <Pallet<T>>::transfer(self, &from, &to, amount),121 <CommonWeights<T>>::transfer(),122 )123 }124125 fn approve(126 &self,127 sender: T::CrossAccountId,128 spender: T::CrossAccountId,129 token: TokenId,130 amount: u128,131 ) -> DispatchResultWithPostInfo {132 ensure!(133 token == TokenId::default(),134 <Error<T>>::FungibleItemsHaveNoId135 );136137 with_weight(138 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),139 <CommonWeights<T>>::approve(),140 )141 }142143 fn transfer_from(144 &self,145 sender: T::CrossAccountId,146 from: T::CrossAccountId,147 to: T::CrossAccountId,148 token: TokenId,149 amount: u128,150 ) -> DispatchResultWithPostInfo {151 ensure!(152 token == TokenId::default(),153 <Error<T>>::FungibleItemsHaveNoId154 );155156 with_weight(157 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount),158 <CommonWeights<T>>::transfer_from(),159 )160 }161162 fn burn_from(163 &self,164 sender: T::CrossAccountId,165 from: T::CrossAccountId,166 token: TokenId,167 amount: u128,168 ) -> DispatchResultWithPostInfo {169 ensure!(170 token == TokenId::default(),171 <Error<T>>::FungibleItemsHaveNoId172 );173174 with_weight(175 <Pallet<T>>::burn_from(self, &sender, &from, amount),176 <CommonWeights<T>>::burn_from(),177 )178 }179180 fn set_variable_metadata(181 &self,182 _sender: T::CrossAccountId,183 _token: TokenId,184 _data: BoundedVec<u8, CustomDataLimit>,185 ) -> DispatchResultWithPostInfo {186 fail!(<Error<T>>::FungibleItemsDontHaveData)187 }188189 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {190 if <Balance<T>>::get((self.id, account)) != 0 {191 vec![TokenId::default()]192 } else {193 vec![]194 }195 }196197 fn token_exists(&self, token: TokenId) -> bool {198 token == TokenId::default()199 }200201 fn last_token_id(&self) -> TokenId {202 TokenId::default()203 }204205 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {206 None207 }208 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {209 Vec::new()210 }211 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {212 Vec::new()213 }214215 fn collection_tokens(&self) -> u32 {216 1217 }218219 fn account_balance(&self, account: T::CrossAccountId) -> u32 {220 if <Balance<T>>::get((self.id, account)) != 0 {221 1222 } else {223 0224 }225 }226227 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {228 if token != TokenId::default() {229 return 0;230 }231 <Balance<T>>::get((self.id, account))232 }233234 fn allowance(235 &self,236 sender: T::CrossAccountId,237 spender: T::CrossAccountId,238 token: TokenId,239 ) -> u128 {240 if token != TokenId::default() {241 return 0;242 }243 <Allowance<T>>::get((self.id, sender, spender))244 }245}