1234567891011121314151617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, CreateItemExData};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::CustomDataLimit;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 transfer() -> Weight {54 <SelfWeightOf<T>>::transfer()55 }5657 fn approve() -> Weight {58 <SelfWeightOf<T>>::approve()59 }6061 fn transfer_from() -> Weight {62 <SelfWeightOf<T>>::transfer_from()63 }6465 fn burn_from() -> Weight {66 <SelfWeightOf<T>>::burn_from()67 }6869 fn set_variable_metadata(_bytes: u32) -> Weight {70 71 072 }73}7475impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {76 fn create_item(77 &self,78 sender: T::CrossAccountId,79 to: T::CrossAccountId,80 data: up_data_structs::CreateItemData,81 ) -> DispatchResultWithPostInfo {82 match data {83 up_data_structs::CreateItemData::Fungible(data) => with_weight(84 <Pallet<T>>::create_item(self, &sender, (to, data.value)),85 <CommonWeights<T>>::create_item(),86 ),87 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),88 }89 }9091 fn create_multiple_items(92 &self,93 sender: T::CrossAccountId,94 to: T::CrossAccountId,95 data: Vec<up_data_structs::CreateItemData>,96 ) -> DispatchResultWithPostInfo {97 let mut sum: u128 = 0;98 for data in data {99 match data {100 up_data_structs::CreateItemData::Fungible(data) => {101 sum = sum102 .checked_add(data.value)103 .ok_or(ArithmeticError::Overflow)?;104 }105 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),106 }107 }108109 with_weight(110 <Pallet<T>>::create_item(self, &sender, (to, sum)),111 <CommonWeights<T>>::create_item(),112 )113 }114115 fn create_multiple_items_ex(116 &self,117 sender: <T>::CrossAccountId,118 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,119 ) -> DispatchResultWithPostInfo {120 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);121 let data = match data {122 up_data_structs::CreateItemExData::Fungible(f) => f,123 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),124 };125126 with_weight(127 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),128 weight,129 )130 }131132 fn burn_item(133 &self,134 sender: T::CrossAccountId,135 token: TokenId,136 amount: u128,137 ) -> DispatchResultWithPostInfo {138 ensure!(139 token == TokenId::default(),140 <Error<T>>::FungibleItemsHaveNoId141 );142143 with_weight(144 <Pallet<T>>::burn(self, &sender, amount),145 <CommonWeights<T>>::burn_item(),146 )147 }148149 fn transfer(150 &self,151 from: T::CrossAccountId,152 to: T::CrossAccountId,153 token: TokenId,154 amount: u128,155 ) -> DispatchResultWithPostInfo {156 ensure!(157 token == TokenId::default(),158 <Error<T>>::FungibleItemsHaveNoId159 );160161 with_weight(162 <Pallet<T>>::transfer(self, &from, &to, amount),163 <CommonWeights<T>>::transfer(),164 )165 }166167 fn approve(168 &self,169 sender: T::CrossAccountId,170 spender: T::CrossAccountId,171 token: TokenId,172 amount: u128,173 ) -> DispatchResultWithPostInfo {174 ensure!(175 token == TokenId::default(),176 <Error<T>>::FungibleItemsHaveNoId177 );178179 with_weight(180 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),181 <CommonWeights<T>>::approve(),182 )183 }184185 fn transfer_from(186 &self,187 sender: T::CrossAccountId,188 from: T::CrossAccountId,189 to: T::CrossAccountId,190 token: TokenId,191 amount: u128,192 ) -> DispatchResultWithPostInfo {193 ensure!(194 token == TokenId::default(),195 <Error<T>>::FungibleItemsHaveNoId196 );197198 with_weight(199 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount),200 <CommonWeights<T>>::transfer_from(),201 )202 }203204 fn burn_from(205 &self,206 sender: T::CrossAccountId,207 from: T::CrossAccountId,208 token: TokenId,209 amount: u128,210 ) -> DispatchResultWithPostInfo {211 ensure!(212 token == TokenId::default(),213 <Error<T>>::FungibleItemsHaveNoId214 );215216 with_weight(217 <Pallet<T>>::burn_from(self, &sender, &from, amount),218 <CommonWeights<T>>::burn_from(),219 )220 }221222 fn set_variable_metadata(223 &self,224 _sender: T::CrossAccountId,225 _token: TokenId,226 _data: BoundedVec<u8, CustomDataLimit>,227 ) -> DispatchResultWithPostInfo {228 fail!(<Error<T>>::FungibleItemsDontHaveData)229 }230231 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {232 if <Balance<T>>::get((self.id, account)) != 0 {233 vec![TokenId::default()]234 } else {235 vec![]236 }237 }238239 fn token_exists(&self, token: TokenId) -> bool {240 token == TokenId::default()241 }242243 fn last_token_id(&self) -> TokenId {244 TokenId::default()245 }246247 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {248 None249 }250 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {251 Vec::new()252 }253 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {254 Vec::new()255 }256257 fn collection_tokens(&self) -> u32 {258 1259 }260261 fn account_balance(&self, account: T::CrossAccountId) -> u32 {262 if <Balance<T>>::get((self.id, account)) != 0 {263 1264 } else {265 0266 }267 }268269 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {270 if token != TokenId::default() {271 return 0;272 }273 <Balance<T>>::get((self.id, account))274 }275276 fn allowance(277 &self,278 sender: T::CrossAccountId,279 spender: T::CrossAccountId,280 token: TokenId,281 ) -> u128 {282 if token != TokenId::default() {283 return 0;284 }285 <Allowance<T>>::get((self.id, sender, spender))286 }287}