1234567891011121314151617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, 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;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 nesting_budget: &dyn Budget,193 ) -> DispatchResultWithPostInfo {194 ensure!(195 token == TokenId::default(),196 <Error<T>>::FungibleItemsHaveNoId197 );198199 with_weight(200 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),201 <CommonWeights<T>>::transfer_from(),202 )203 }204205 fn burn_from(206 &self,207 sender: T::CrossAccountId,208 from: T::CrossAccountId,209 token: TokenId,210 amount: u128,211 nesting_budget: &dyn Budget,212 ) -> DispatchResultWithPostInfo {213 ensure!(214 token == TokenId::default(),215 <Error<T>>::FungibleItemsHaveNoId216 );217218 with_weight(219 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),220 <CommonWeights<T>>::burn_from(),221 )222 }223224 fn set_variable_metadata(225 &self,226 _sender: T::CrossAccountId,227 _token: TokenId,228 _data: BoundedVec<u8, CustomDataLimit>,229 ) -> DispatchResultWithPostInfo {230 fail!(<Error<T>>::FungibleItemsDontHaveData)231 }232233 fn nest_token(234 &self,235 _sender: <T>::CrossAccountId,236 _from: (up_data_structs::CollectionId, TokenId),237 _under: TokenId,238 ) -> sp_runtime::DispatchResult {239 fail!(<Error<T>>::FungibleDisallowsNesting)240 }241242 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {243 if <Balance<T>>::get((self.id, account)) != 0 {244 vec![TokenId::default()]245 } else {246 vec![]247 }248 }249250 fn token_exists(&self, token: TokenId) -> bool {251 token == TokenId::default()252 }253254 fn last_token_id(&self) -> TokenId {255 TokenId::default()256 }257258 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {259 None260 }261 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {262 Vec::new()263 }264 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {265 Vec::new()266 }267268 fn collection_tokens(&self) -> u32 {269 1270 }271272 fn account_balance(&self, account: T::CrossAccountId) -> u32 {273 if <Balance<T>>::get((self.id, account)) != 0 {274 1275 } else {276 0277 }278 }279280 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {281 if token != TokenId::default() {282 return 0;283 }284 <Balance<T>>::get((self.id, account))285 }286287 fn allowance(288 &self,289 sender: T::CrossAccountId,290 spender: T::CrossAccountId,291 token: TokenId,292 ) -> u128 {293 if token != TokenId::default() {294 return 0;295 }296 <Allowance<T>>::get((self.id, sender, spender))297 }298}