1use alloc::{vec, vec::Vec};2use core::marker::PhantomData;34use frame_support::{5 ensure, fail,6 traits::tokens::{fungible::Mutate, Fortitude, Precision},7 weights::Weight,8};9use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};10use pallet_common::{11 erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo, Error as CommonError,12};13use up_data_structs::{budget::Budget, TokenId};1415use crate::{Config, NativeFungibleHandle, Pallet};1617pub struct CommonWeights<T: Config>(PhantomData<T>);181920impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {21 fn create_multiple_items(_amount: &[up_data_structs::CreateItemData]) -> Weight {22 Weight::default()23 }2425 fn create_multiple_items_ex(26 _cost: &up_data_structs::CreateItemExData<T::CrossAccountId>,27 ) -> Weight {28 Weight::default()29 }3031 fn burn_item() -> Weight {32 Weight::default()33 }3435 fn set_collection_properties(_amount: u32) -> Weight {36 Weight::default()37 }3839 fn set_token_properties(_amount: u32) -> Weight {40 Weight::default()41 }4243 fn set_token_property_permissions(_amount: u32) -> Weight {44 Weight::default()45 }4647 fn transfer() -> Weight {48 <BalancesWeight<T> as WeightInfo>::transfer_allow_death()49 }5051 fn approve() -> Weight {52 Weight::default()53 }5455 fn approve_from() -> Weight {56 Weight::default()57 }5859 fn transfer_from() -> Weight {60 <BalancesWeight<T> as WeightInfo>::transfer_allow_death()61 }6263 fn burn_from() -> Weight {64 Weight::default()65 }6667 fn set_allowance_for_all() -> Weight {68 Weight::default()69 }7071 fn force_repair_item() -> Weight {72 Weight::default()73 }74}75767778impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {79 fn create_item(80 &self,81 _sender: <T>::CrossAccountId,82 _to: <T>::CrossAccountId,83 _data: up_data_structs::CreateItemData,84 _nesting_budget: &dyn up_data_structs::budget::Budget,85 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {86 fail!(<CommonError<T>>::UnsupportedOperation);87 }8889 fn create_multiple_items(90 &self,91 _sender: <T>::CrossAccountId,92 _to: <T>::CrossAccountId,93 _data: Vec<up_data_structs::CreateItemData>,94 _nesting_budget: &dyn up_data_structs::budget::Budget,95 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {96 fail!(<CommonError<T>>::UnsupportedOperation);97 }9899 fn create_multiple_items_ex(100 &self,101 _sender: <T>::CrossAccountId,102 _data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,103 _nesting_budget: &dyn up_data_structs::budget::Budget,104 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {105 fail!(<CommonError<T>>::UnsupportedOperation);106 }107108 fn burn_item(109 &self,110 _sender: <T>::CrossAccountId,111 _token: TokenId,112 _amount: u128,113 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {114 fail!(<CommonError<T>>::UnsupportedOperation);115 }116117 fn set_collection_properties(118 &self,119 _sender: <T>::CrossAccountId,120 _properties: Vec<up_data_structs::Property>,121 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {122 fail!(<CommonError<T>>::UnsupportedOperation);123 }124125 fn delete_collection_properties(126 &self,127 _sender: &<T>::CrossAccountId,128 _property_keys: Vec<up_data_structs::PropertyKey>,129 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {130 fail!(<CommonError<T>>::UnsupportedOperation);131 }132133 fn set_token_properties(134 &self,135 _sender: <T>::CrossAccountId,136 _token_id: TokenId,137 _properties: Vec<up_data_structs::Property>,138 _budget: &dyn up_data_structs::budget::Budget,139 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {140 fail!(<CommonError<T>>::UnsupportedOperation);141 }142143 fn delete_token_properties(144 &self,145 _sender: <T>::CrossAccountId,146 _token_id: TokenId,147 _property_keys: Vec<up_data_structs::PropertyKey>,148 _budget: &dyn up_data_structs::budget::Budget,149 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {150 fail!(<CommonError<T>>::UnsupportedOperation);151 }152153 fn get_token_properties_raw(154 &self,155 _token_id: TokenId,156 ) -> Option<up_data_structs::TokenProperties> {157 158 None159 }160161 fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {162 163 }164165 fn set_token_property_permissions(166 &self,167 _sender: &<T>::CrossAccountId,168 _property_permissions: Vec<up_data_structs::PropertyKeyPermission>,169 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {170 fail!(<CommonError<T>>::UnsupportedOperation);171 }172173 fn transfer(174 &self,175 sender: <T>::CrossAccountId,176 to: <T>::CrossAccountId,177 token: TokenId,178 amount: u128,179 _budget: &dyn up_data_structs::budget::Budget,180 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {181 ensure!(182 token == TokenId::default(),183 <CommonError<T>>::FungibleItemsHaveNoId184 );185186 <Pallet<T>>::transfer(&sender, &to, amount)187 }188189 fn approve(190 &self,191 _sender: <T>::CrossAccountId,192 _spender: <T>::CrossAccountId,193 _token: TokenId,194 _amount: u128,195 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {196 fail!(<CommonError<T>>::UnsupportedOperation);197 }198199 fn approve_from(200 &self,201 _sender: <T>::CrossAccountId,202 _from: <T>::CrossAccountId,203 _to: <T>::CrossAccountId,204 _token: TokenId,205 _amount: u128,206 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {207 fail!(<CommonError<T>>::UnsupportedOperation);208 }209210 fn transfer_from(211 &self,212 sender: <T>::CrossAccountId,213 from: <T>::CrossAccountId,214 to: <T>::CrossAccountId,215 token: TokenId,216 amount: u128,217 budget: &dyn up_data_structs::budget::Budget,218 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {219 ensure!(220 token == TokenId::default(),221 <CommonError<T>>::FungibleItemsHaveNoId222 );223224 <Pallet<T>>::transfer_from(&sender, &from, &to, amount, budget)225 }226227 fn burn_from(228 &self,229 _sender: <T>::CrossAccountId,230 _from: <T>::CrossAccountId,231 _token: TokenId,232 _amount: u128,233 _budget: &dyn up_data_structs::budget::Budget,234 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {235 fail!(<CommonError<T>>::UnsupportedOperation);236 }237238 fn check_nesting(239 &self,240 _sender: &<T>::CrossAccountId,241 _from: (up_data_structs::CollectionId, TokenId),242 _under: TokenId,243 _budget: &dyn up_data_structs::budget::Budget,244 ) -> frame_support::sp_runtime::DispatchResult {245 fail!(<CommonError<T>>::UnsupportedOperation);246 }247248 fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}249250 fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}251252 fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {253 let balance = <Pallet<T>>::total_balance(&account);254 if balance != 0 {255 vec![TokenId::default()]256 } else {257 vec![]258 }259 }260261 fn collection_tokens(&self) -> Vec<TokenId> {262 vec![TokenId::default()]263 }264265 fn token_exists(&self, token: TokenId) -> bool {266 token == TokenId::default()267 }268269 fn last_token_id(&self) -> TokenId {270 TokenId::default()271 }272273 fn token_owner(274 &self,275 _token: TokenId,276 ) -> Result<<T>::CrossAccountId, up_data_structs::TokenOwnerError> {277 Err(up_data_structs::TokenOwnerError::MultipleOwners)278 }279280 fn check_token_indirect_owner(281 &self,282 _token: TokenId,283 _maybe_owner: &<T>::CrossAccountId,284 _nesting_budget: &dyn up_data_structs::budget::Budget,285 ) -> Result<bool, frame_support::sp_runtime::DispatchError> {286 Ok(false)287 }288289 fn token_owners(&self, _token: TokenId) -> Vec<<T>::CrossAccountId> {290 vec![]291 }292293 fn token_property(294 &self,295 _token_id: TokenId,296 _key: &up_data_structs::PropertyKey,297 ) -> Option<up_data_structs::PropertyValue> {298 None299 }300301 fn token_properties(302 &self,303 _token: TokenId,304 _keys: Option<Vec<up_data_structs::PropertyKey>>,305 ) -> Vec<up_data_structs::Property> {306 vec![]307 }308309 fn total_supply(&self) -> u32 {310 1311 }312313 fn account_balance(&self, account: T::CrossAccountId) -> u32 {314 let balance = <Pallet<T>>::balance_of(&account);315 (balance != 0).into()316 }317318 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {319 if token != TokenId::default() {320 return 0;321 }322 <Pallet<T>>::balance_of(&account)323 }324325 fn total_pieces(&self, token: TokenId) -> Option<u128> {326 if token != TokenId::default() {327 return None;328 }329 Some(<Pallet<T>>::total_issuance())330 }331332 fn allowance(333 &self,334 _sender: <T>::CrossAccountId,335 _spender: <T>::CrossAccountId,336 _token: TokenId,337 ) -> u128 {338 0339 }340341 fn xcm_extensions(&self) -> Option<&dyn pallet_common::XcmExtensions<T>> {342 Some(self)343 }344345 fn set_allowance_for_all(346 &self,347 _owner: <T>::CrossAccountId,348 _operator: <T>::CrossAccountId,349 _approve: bool,350 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {351 fail!(<CommonError<T>>::UnsupportedOperation);352 }353354 fn allowance_for_all(355 &self,356 _owner: <T>::CrossAccountId,357 _operator: <T>::CrossAccountId,358 ) -> bool {359 false360 }361362 fn repair_item(363 &self,364 _token: TokenId,365 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {366 fail!(<CommonError<T>>::UnsupportedOperation);367 }368}369370impl<T: Config> pallet_common::XcmExtensions<T> for NativeFungibleHandle<T> {371 fn create_item_internal(372 &self,373 _depositor: &<T>::CrossAccountId,374 to: <T>::CrossAccountId,375 data: up_data_structs::CreateItemData,376 _nesting_budget: &dyn Budget,377 ) -> Result<TokenId, sp_runtime::DispatchError> {378 match &data {379 up_data_structs::CreateItemData::Fungible(fungible_data) => {380 T::Mutate::mint_into(381 to.as_sub(),382 fungible_data383 .value384 .try_into()385 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,386 )?;387388 Ok(TokenId::default())389 }390 _ => {391 fail!(<CommonError<T>>::NotFungibleDataUsedToMintFungibleCollectionToken)392 }393 }394 }395396 fn transfer_item_internal(397 &self,398 _depositor: &<T>::CrossAccountId,399 from: &<T>::CrossAccountId,400 to: &<T>::CrossAccountId,401 token: TokenId,402 amount: u128,403 _nesting_budget: &dyn Budget,404 ) -> sp_runtime::DispatchResult {405 ensure!(406 token == TokenId::default(),407 <CommonError<T>>::FungibleItemsHaveNoId408 );409410 <Pallet<T>>::transfer(from, to, amount)411 .map(|_| ())412 .map_err(|post_info| post_info.error)413 }414415 fn burn_item_internal(416 &self,417 from: T::CrossAccountId,418 token: TokenId,419 amount: u128,420 ) -> sp_runtime::DispatchResult {421 ensure!(422 token == TokenId::default(),423 <CommonError<T>>::FungibleItemsHaveNoId424 );425426 T::Mutate::burn_from(427 from.as_sub(),428 amount429 .try_into()430 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,431 Precision::Exact,432 Fortitude::Polite,433 )?;434435 Ok(())436 }437}