1234567891011121314151617use core::marker::PhantomData;1819use sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};21use up_data_structs::{22 CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,23 budget::Budget, Property, PropertyKey, PropertyKeyPermission,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(amount: u32) -> Weight {50 <SelfWeightOf<T>>::create_multiple_items(amount)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 <SelfWeightOf<T>>::set_collection_properties(amount)71 }7273 fn set_token_properties(amount: u32) -> Weight {74 <SelfWeightOf<T>>::set_token_properties(amount)75 }7677 fn delete_token_properties(amount: u32) -> Weight {78 <SelfWeightOf<T>>::delete_token_properties(amount)79 }8081 fn set_property_permissions(amount: u32) -> Weight {82 <SelfWeightOf<T>>::set_property_permissions(amount)83 }8485 fn transfer() -> Weight {86 max_weight_of!(87 transfer_normal(),88 transfer_creating(),89 transfer_removing(),90 transfer_creating_removing()91 )92 }9394 fn approve() -> Weight {95 <SelfWeightOf<T>>::approve()96 }9798 fn transfer_from() -> Weight {99 max_weight_of!(100 transfer_from_normal(),101 transfer_from_creating(),102 transfer_from_removing(),103 transfer_from_creating_removing()104 )105 }106107 fn burn_from() -> Weight {108 <SelfWeightOf<T>>::burn_from()109 }110111 fn set_variable_metadata(bytes: u32) -> Weight {112 <SelfWeightOf<T>>::set_variable_metadata(bytes)113 }114}115116fn map_create_data<T: Config>(117 data: up_data_structs::CreateItemData,118 to: &T::CrossAccountId,119) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {120 match data {121 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {122 const_data: data.const_data,123 variable_data: data.variable_data,124 users: {125 let mut out = BTreeMap::new();126 out.insert(to.clone(), data.pieces);127 out.try_into().expect("limit > 0")128 },129 }),130 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),131 }132}133134impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {135 fn create_item(136 &self,137 sender: T::CrossAccountId,138 to: T::CrossAccountId,139 data: up_data_structs::CreateItemData,140 nesting_budget: &dyn Budget,141 ) -> DispatchResultWithPostInfo {142 with_weight(143 <Pallet<T>>::create_item(144 self,145 &sender,146 map_create_data::<T>(data, &to)?,147 nesting_budget,148 ),149 <CommonWeights<T>>::create_item(),150 )151 }152153 fn create_multiple_items(154 &self,155 sender: T::CrossAccountId,156 to: T::CrossAccountId,157 data: Vec<up_data_structs::CreateItemData>,158 nesting_budget: &dyn Budget,159 ) -> DispatchResultWithPostInfo {160 let data = data161 .into_iter()162 .map(|d| map_create_data::<T>(d, &to))163 .collect::<Result<Vec<_>, DispatchError>>()?;164165 let amount = data.len();166 with_weight(167 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),168 <CommonWeights<T>>::create_multiple_items(amount as u32),169 )170 }171172 fn create_multiple_items_ex(173 &self,174 sender: <T>::CrossAccountId,175 data: CreateItemExData<T::CrossAccountId>,176 nesting_budget: &dyn Budget,177 ) -> DispatchResultWithPostInfo {178 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);179 let data = match data {180 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],181 CreateItemExData::RefungibleMultipleItems(r)182 if r.iter().all(|i| i.users.len() == 1) =>183 {184 r.into_inner()185 }186 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),187 };188189 with_weight(190 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),191 weight,192 )193 }194195 fn burn_item(196 &self,197 sender: T::CrossAccountId,198 token: TokenId,199 amount: u128,200 ) -> DispatchResultWithPostInfo {201 with_weight(202 <Pallet<T>>::burn(self, &sender, token, amount),203 <CommonWeights<T>>::burn_item(),204 )205 }206207 fn transfer(208 &self,209 from: T::CrossAccountId,210 to: T::CrossAccountId,211 token: TokenId,212 amount: u128,213 nesting_budget: &dyn Budget,214 ) -> DispatchResultWithPostInfo {215 with_weight(216 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),217 <CommonWeights<T>>::transfer(),218 )219 }220221 fn approve(222 &self,223 sender: T::CrossAccountId,224 spender: T::CrossAccountId,225 token: TokenId,226 amount: u128,227 ) -> DispatchResultWithPostInfo {228 with_weight(229 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),230 <CommonWeights<T>>::approve(),231 )232 }233234 fn transfer_from(235 &self,236 sender: T::CrossAccountId,237 from: T::CrossAccountId,238 to: T::CrossAccountId,239 token: TokenId,240 amount: u128,241 nesting_budget: &dyn Budget,242 ) -> DispatchResultWithPostInfo {243 with_weight(244 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),245 <CommonWeights<T>>::transfer_from(),246 )247 }248249 fn burn_from(250 &self,251 sender: T::CrossAccountId,252 from: T::CrossAccountId,253 token: TokenId,254 amount: u128,255 nesting_budget: &dyn Budget,256 ) -> DispatchResultWithPostInfo {257 with_weight(258 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),259 <CommonWeights<T>>::burn_from(),260 )261 }262263 fn set_collection_properties(264 &self,265 _sender: T::CrossAccountId,266 _property: Vec<Property>,267 ) -> DispatchResultWithPostInfo {268 fail!(<Error<T>>::PropertiesNotAllowed)269 }270271 fn set_token_properties(272 &self,273 _sender: T::CrossAccountId,274 _token_id: TokenId,275 _property: Vec<Property>,276 ) -> DispatchResultWithPostInfo {277 fail!(<Error<T>>::PropertiesNotAllowed)278 }279280 fn set_property_permissions(281 &self,282 _sender: &T::CrossAccountId,283 _property_permissions: Vec<PropertyKeyPermission>,284 ) -> DispatchResultWithPostInfo {285 fail!(<Error<T>>::PropertiesNotAllowed)286 }287288 fn delete_token_properties(289 &self,290 _sender: T::CrossAccountId,291 _token_id: TokenId,292 _property_keys: Vec<PropertyKey>,293 ) -> DispatchResultWithPostInfo {294 fail!(<Error<T>>::PropertiesNotAllowed)295 }296297 fn set_variable_metadata(298 &self,299 sender: T::CrossAccountId,300 token: TokenId,301 data: BoundedVec<u8, CustomDataLimit>,302 ) -> DispatchResultWithPostInfo {303 let len = data.len();304 with_weight(305 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),306 <CommonWeights<T>>::set_variable_metadata(len as u32),307 )308 }309310 fn check_nesting(311 &self,312 _sender: <T>::CrossAccountId,313 _from: (CollectionId, TokenId),314 _under: TokenId,315 _budget: &dyn Budget,316 ) -> sp_runtime::DispatchResult {317 fail!(<Error<T>>::RefungibleDisallowsNesting)318 }319320 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {321 <Owned<T>>::iter_prefix((self.id, account))322 .map(|(id, _)| id)323 .collect()324 }325326 fn collection_tokens(&self) -> Vec<TokenId> {327 <TokenData<T>>::iter_prefix((self.id,))328 .map(|(id, _)| id)329 .collect()330 }331332 fn token_exists(&self, token: TokenId) -> bool {333 <Pallet<T>>::token_exists(self, token)334 }335336 fn last_token_id(&self) -> TokenId {337 TokenId(<TokensMinted<T>>::get(self.id))338 }339340 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {341 None342 }343 fn const_metadata(&self, token: TokenId) -> Vec<u8> {344 <TokenData<T>>::get((self.id, token))345 .const_data346 .into_inner()347 }348 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {349 <TokenData<T>>::get((self.id, token))350 .variable_data351 .into_inner()352 }353354 fn total_supply(&self) -> u32 {355 <Pallet<T>>::total_supply(self)356 }357358 fn account_balance(&self, account: T::CrossAccountId) -> u32 {359 <AccountBalance<T>>::get((self.id, account))360 }361362 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {363 <Balance<T>>::get((self.id, token, account))364 }365366 fn allowance(367 &self,368 sender: T::CrossAccountId,369 spender: T::CrossAccountId,370 token: TokenId,371 ) -> u128 {372 <Allowance<T>>::get((self.id, token, sender, spender))373 }374}