1234567891011121314151617use core::marker::PhantomData;1819use sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight};21use up_data_structs::{22 CollectionId, TokenId, CreateItemExData, CreateRefungibleExData, budget::Budget, Property,23 PropertyKey, PropertyValue, PropertyKeyPermission, CreateItemData,24};25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};26use sp_runtime::DispatchError;27use sp_std::{vec::Vec, vec};2829use crate::{30 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,31 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,32};3334macro_rules! max_weight_of {35 ($($method:ident ($($args:tt)*)),*) => {36 037 $(38 .max(<SelfWeightOf<T>>::$method($($args)*))39 )*40 };41}4243pub struct CommonWeights<T: Config>(PhantomData<T>);44impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {45 fn create_item() -> Weight {46 <SelfWeightOf<T>>::create_item()47 }4849 fn create_multiple_items(data: &[CreateItemData]) -> Weight {50 <SelfWeightOf<T>>::create_multiple_items(data.len() as u32)51 }5253 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {54 match call {55 CreateItemExData::RefungibleMultipleOwners(i) => {56 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)57 }58 CreateItemExData::RefungibleMultipleItems(i) => {59 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)60 }61 _ => 0,62 }63 }6465 fn burn_item() -> Weight {66 max_weight_of!(burn_item_partial(), burn_item_fully())67 }6869 fn set_collection_properties(_amount: u32) -> Weight {70 71 072 }7374 fn delete_collection_properties(_amount: u32) -> Weight {75 76 077 }7879 fn set_token_properties(amount: u32) -> Weight {80 <SelfWeightOf<T>>::set_token_properties(amount)81 }8283 fn delete_token_properties(amount: u32) -> Weight {84 <SelfWeightOf<T>>::delete_token_properties(amount)85 }8687 fn set_property_permissions(amount: u32) -> Weight {88 <SelfWeightOf<T>>::set_property_permissions(amount)89 }9091 fn transfer() -> Weight {92 max_weight_of!(93 transfer_normal(),94 transfer_creating(),95 transfer_removing(),96 transfer_creating_removing()97 )98 }99100 fn approve() -> Weight {101 <SelfWeightOf<T>>::approve()102 }103104 fn transfer_from() -> Weight {105 max_weight_of!(106 transfer_from_normal(),107 transfer_from_creating(),108 transfer_from_removing(),109 transfer_from_creating_removing()110 )111 }112113 fn burn_from() -> Weight {114 <SelfWeightOf<T>>::burn_from()115 }116}117118fn map_create_data<T: Config>(119 data: up_data_structs::CreateItemData,120 to: &T::CrossAccountId,121) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {122 match data {123 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {124 const_data: data.const_data,125 users: {126 let mut out = BTreeMap::new();127 out.insert(to.clone(), data.pieces);128 out.try_into().expect("limit > 0")129 },130 }),131 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),132 }133}134135impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {136 fn create_item(137 &self,138 sender: T::CrossAccountId,139 to: T::CrossAccountId,140 data: up_data_structs::CreateItemData,141 nesting_budget: &dyn Budget,142 ) -> DispatchResultWithPostInfo {143 with_weight(144 <Pallet<T>>::create_item(145 self,146 &sender,147 map_create_data::<T>(data, &to)?,148 nesting_budget,149 ),150 <CommonWeights<T>>::create_item(),151 )152 }153154 fn create_multiple_items(155 &self,156 sender: T::CrossAccountId,157 to: T::CrossAccountId,158 data: Vec<up_data_structs::CreateItemData>,159 nesting_budget: &dyn Budget,160 ) -> DispatchResultWithPostInfo {161 let weight = <CommonWeights<T>>::create_multiple_items(&data);162 let data = data163 .into_iter()164 .map(|d| map_create_data::<T>(d, &to))165 .collect::<Result<Vec<_>, DispatchError>>()?;166167 with_weight(168 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),169 weight,170 )171 }172173 fn create_multiple_items_ex(174 &self,175 sender: <T>::CrossAccountId,176 data: CreateItemExData<T::CrossAccountId>,177 nesting_budget: &dyn Budget,178 ) -> DispatchResultWithPostInfo {179 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);180 let data = match data {181 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],182 CreateItemExData::RefungibleMultipleItems(r)183 if r.iter().all(|i| i.users.len() == 1) =>184 {185 r.into_inner()186 }187 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),188 };189190 with_weight(191 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),192 weight,193 )194 }195196 fn burn_item(197 &self,198 sender: T::CrossAccountId,199 token: TokenId,200 amount: u128,201 ) -> DispatchResultWithPostInfo {202 with_weight(203 <Pallet<T>>::burn(self, &sender, token, amount),204 <CommonWeights<T>>::burn_item(),205 )206 }207208 fn transfer(209 &self,210 from: T::CrossAccountId,211 to: T::CrossAccountId,212 token: TokenId,213 amount: u128,214 nesting_budget: &dyn Budget,215 ) -> DispatchResultWithPostInfo {216 with_weight(217 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),218 <CommonWeights<T>>::transfer(),219 )220 }221222 fn approve(223 &self,224 sender: T::CrossAccountId,225 spender: T::CrossAccountId,226 token: TokenId,227 amount: u128,228 ) -> DispatchResultWithPostInfo {229 with_weight(230 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),231 <CommonWeights<T>>::approve(),232 )233 }234235 fn transfer_from(236 &self,237 sender: T::CrossAccountId,238 from: T::CrossAccountId,239 to: T::CrossAccountId,240 token: TokenId,241 amount: u128,242 nesting_budget: &dyn Budget,243 ) -> DispatchResultWithPostInfo {244 with_weight(245 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),246 <CommonWeights<T>>::transfer_from(),247 )248 }249250 fn burn_from(251 &self,252 sender: T::CrossAccountId,253 from: T::CrossAccountId,254 token: TokenId,255 amount: u128,256 nesting_budget: &dyn Budget,257 ) -> DispatchResultWithPostInfo {258 with_weight(259 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),260 <CommonWeights<T>>::burn_from(),261 )262 }263264 fn set_collection_properties(265 &self,266 _sender: T::CrossAccountId,267 _property: Vec<Property>,268 ) -> DispatchResultWithPostInfo {269 fail!(<Error<T>>::SettingPropertiesNotAllowed)270 }271272 fn delete_collection_properties(273 &self,274 _sender: &T::CrossAccountId,275 _property_keys: Vec<PropertyKey>,276 ) -> DispatchResultWithPostInfo {277 fail!(<Error<T>>::SettingPropertiesNotAllowed)278 }279280 fn set_token_properties(281 &self,282 _sender: T::CrossAccountId,283 _token_id: TokenId,284 _property: Vec<Property>,285 ) -> DispatchResultWithPostInfo {286 fail!(<Error<T>>::SettingPropertiesNotAllowed)287 }288289 fn set_property_permissions(290 &self,291 _sender: &T::CrossAccountId,292 _property_permissions: Vec<PropertyKeyPermission>,293 ) -> DispatchResultWithPostInfo {294 fail!(<Error<T>>::SettingPropertiesNotAllowed)295 }296297 fn delete_token_properties(298 &self,299 _sender: T::CrossAccountId,300 _token_id: TokenId,301 _property_keys: Vec<PropertyKey>,302 ) -> DispatchResultWithPostInfo {303 fail!(<Error<T>>::SettingPropertiesNotAllowed)304 }305306 fn check_nesting(307 &self,308 _sender: <T>::CrossAccountId,309 _from: (CollectionId, TokenId),310 _under: TokenId,311 _budget: &dyn Budget,312 ) -> sp_runtime::DispatchResult {313 fail!(<Error<T>>::RefungibleDisallowsNesting)314 }315316 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {317 <Owned<T>>::iter_prefix((self.id, account))318 .map(|(id, _)| id)319 .collect()320 }321322 fn collection_tokens(&self) -> Vec<TokenId> {323 <TokenData<T>>::iter_prefix((self.id,))324 .map(|(id, _)| id)325 .collect()326 }327328 fn token_exists(&self, token: TokenId) -> bool {329 <Pallet<T>>::token_exists(self, token)330 }331332 fn last_token_id(&self) -> TokenId {333 TokenId(<TokensMinted<T>>::get(self.id))334 }335336 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {337 None338 }339 fn const_metadata(&self, token: TokenId) -> Vec<u8> {340 <TokenData<T>>::get((self.id, token))341 .const_data342 .into_inner()343 }344345 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {346 None347 }348349 fn token_properties(350 &self,351 _token_id: TokenId,352 _keys: Option<Vec<PropertyKey>>,353 ) -> Vec<Property> {354 Vec::new()355 }356357 fn total_supply(&self) -> u32 {358 <Pallet<T>>::total_supply(self)359 }360361 fn account_balance(&self, account: T::CrossAccountId) -> u32 {362 <AccountBalance<T>>::get((self.id, account))363 }364365 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {366 <Balance<T>>::get((self.id, token, account))367 }368369 fn allowance(370 &self,371 sender: T::CrossAccountId,372 spender: T::CrossAccountId,373 token: TokenId,374 ) -> u128 {375 <Allowance<T>>::get((self.id, token, sender, spender))376 }377}