123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566#![recursion_limit = "1024"]67#![cfg_attr(not(feature = "std"), no_std)]68#![allow(69 clippy::too_many_arguments,70 clippy::unnecessary_mut_passed,71 clippy::unused_unit72)]7374extern crate alloc;7576use frame_support::pallet_prelude::*;77use frame_system::pallet_prelude::*;78pub use pallet::*;79pub mod eth;8081#[cfg(feature = "runtime-benchmarks")]82pub mod benchmarking;83pub mod weights;8485#[frame_support::pallet]86pub mod pallet {87 use frame_support::{88 dispatch::{DispatchErrorWithPostInfo, DispatchResult, PostDispatchInfo},89 ensure, fail,90 storage::Key,91 BoundedVec,92 };93 use frame_system::{ensure_root, ensure_signed};94 use pallet_common::{95 dispatch::{dispatch_tx, CollectionDispatch},96 CollectionHandle, CollectionIssuer, CommonWeightInfo, Pallet as PalletCommon,97 RefungibleExtensionsWeightInfo,98 };99 use pallet_evm::account::CrossAccountId;100 use pallet_structure::weights::WeightInfo as StructureWeightInfo;101 use scale_info::TypeInfo;102 use sp_std::{vec, vec::Vec};103 use up_data_structs::{104 budget, CollectionId, CollectionLimits, CollectionMode, CollectionPermissions,105 CreateCollectionData, CreateItemData, CreateItemExData, Property, PropertyKey,106 PropertyKeyPermission, TokenId, COLLECTION_ADMINS_LIMIT, MAX_COLLECTION_DESCRIPTION_LENGTH,107 MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_PROPERTIES_SIZE, MAX_PROPERTIES_PER_ITEM,108 MAX_PROPERTY_KEY_LENGTH, MAX_PROPERTY_VALUE_LENGTH, MAX_TOKEN_PREFIX_LENGTH,109 MAX_TOKEN_PROPERTIES_SIZE,110 };111 use weights::WeightInfo;112113 use super::*;114115 116 #[pallet::error]117 pub enum Error<T> {118 119 CollectionDecimalPointLimitExceeded,120 121 EmptyArgument,122 123 RepartitionCalledOnNonRefungibleCollection,124 }125126 127 #[pallet::config]128 pub trait Config: frame_system::Config + pallet_common::Config + Sized + TypeInfo {129 130 type WeightInfo: WeightInfo;131132 133 type CommonWeightInfo: CommonWeightInfo<Self::CrossAccountId>;134135 type StructureWeightInfo: StructureWeightInfo;136137 138 type RefungibleExtensionsWeightInfo: RefungibleExtensionsWeightInfo;139 }140141 #[pallet::pallet]142 pub struct Pallet<T>(_);143144 pub type SelfWeightOf<T> = <T as Config>::WeightInfo;145146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168169 170 171 #[pallet::storage]172 pub type ChainVersion<T> = StorageValue<_, u64, ValueQuery>;173 174175 176 177 178 #[pallet::storage]179 #[pallet::getter(fn create_item_busket)]180 pub type CreateItemBasket<T: Config> = StorageMap<181 Hasher = Blake2_128Concat,182 Key = (CollectionId, T::AccountId),183 Value = BlockNumberFor<T>,184 QueryKind = OptionQuery,185 >;186 187 #[pallet::storage]188 #[pallet::getter(fn nft_transfer_basket)]189 pub type NftTransferBasket<T: Config> = StorageDoubleMap<190 Hasher1 = Blake2_128Concat,191 Key1 = CollectionId,192 Hasher2 = Blake2_128Concat,193 Key2 = TokenId,194 Value = BlockNumberFor<T>,195 QueryKind = OptionQuery,196 >;197 198 #[pallet::storage]199 #[pallet::getter(fn fungible_transfer_basket)]200 pub type FungibleTransferBasket<T: Config> = StorageDoubleMap<201 Hasher1 = Blake2_128Concat,202 Key1 = CollectionId,203 Hasher2 = Twox64Concat,204 Key2 = T::AccountId,205 Value = BlockNumberFor<T>,206 QueryKind = OptionQuery,207 >;208 209 #[pallet::storage]210 #[pallet::getter(fn refungible_transfer_basket)]211 pub type ReFungibleTransferBasket<T: Config> = StorageNMap<212 Key = (213 Key<Blake2_128Concat, CollectionId>,214 Key<Blake2_128Concat, TokenId>,215 Key<Twox64Concat, T::AccountId>,216 ),217 Value = BlockNumberFor<T>,218 QueryKind = OptionQuery,219 >;220 221222 223 #[pallet::storage]224 #[pallet::getter(fn token_property_basket)]225 pub type TokenPropertyBasket<T: Config> = StorageDoubleMap<226 Hasher1 = Blake2_128Concat,227 Key1 = CollectionId,228 Hasher2 = Blake2_128Concat,229 Key2 = TokenId,230 Value = BlockNumberFor<T>,231 QueryKind = OptionQuery,232 >;233234 235 #[pallet::storage]236 #[pallet::getter(fn nft_approve_basket)]237 pub type NftApproveBasket<T: Config> = StorageDoubleMap<238 Hasher1 = Blake2_128Concat,239 Key1 = CollectionId,240 Hasher2 = Blake2_128Concat,241 Key2 = TokenId,242 Value = BlockNumberFor<T>,243 QueryKind = OptionQuery,244 >;245 246 #[pallet::storage]247 #[pallet::getter(fn fungible_approve_basket)]248 pub type FungibleApproveBasket<T: Config> = StorageDoubleMap<249 Hasher1 = Blake2_128Concat,250 Key1 = CollectionId,251 Hasher2 = Twox64Concat,252 Key2 = T::AccountId,253 Value = BlockNumberFor<T>,254 QueryKind = OptionQuery,255 >;256 257 #[pallet::storage]258 #[pallet::getter(fn refungible_approve_basket)]259 pub type RefungibleApproveBasket<T: Config> = StorageNMap<260 Key = (261 Key<Blake2_128Concat, CollectionId>,262 Key<Blake2_128Concat, TokenId>,263 Key<Twox64Concat, T::AccountId>,264 ),265 Value = BlockNumberFor<T>,266 QueryKind = OptionQuery,267 >;268269 #[pallet::extra_constants]270 impl<T: Config> Pallet<T> {271 272 fn nesting_budget() -> u32 {273 5274 }275276 277 fn max_collection_name_length() -> u32 {278 MAX_COLLECTION_NAME_LENGTH279 }280281 282 fn max_collection_description_length() -> u32 {283 MAX_COLLECTION_DESCRIPTION_LENGTH284 }285286 287 fn max_token_prefix_length() -> u32 {288 MAX_TOKEN_PREFIX_LENGTH289 }290291 292 fn collection_admins_limit() -> u32 {293 COLLECTION_ADMINS_LIMIT294 }295296 297 fn max_property_key_length() -> u32 {298 MAX_PROPERTY_KEY_LENGTH299 }300301 302 fn max_property_value_length() -> u32 {303 MAX_PROPERTY_VALUE_LENGTH304 }305306 307 fn max_properties_per_item() -> u32 {308 MAX_PROPERTIES_PER_ITEM309 }310311 312 fn max_collection_properties_size() -> u32 {313 MAX_COLLECTION_PROPERTIES_SIZE314 }315316 317 fn max_token_properties_size() -> u32 {318 MAX_TOKEN_PROPERTIES_SIZE319 }320321 322 fn nft_default_collection_limits() -> CollectionLimits {323 CollectionLimits::with_default_limits(CollectionMode::NFT)324 }325326 327 fn rft_default_collection_limits() -> CollectionLimits {328 CollectionLimits::with_default_limits(CollectionMode::ReFungible)329 }330331 332 fn ft_default_collection_limits() -> CollectionLimits {333 CollectionLimits::with_default_limits(CollectionMode::Fungible(0))334 }335 }336337 338 #[pallet::call]339 impl<T: Config> Pallet<T> {340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 #[pallet::call_index(0)]366 #[pallet::weight(<SelfWeightOf<T>>::create_collection())]367 pub fn create_collection(368 origin: OriginFor<T>,369 collection_name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,370 collection_description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,371 token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,372 mode: CollectionMode,373 ) -> DispatchResult {374 let data: CreateCollectionData<T::CrossAccountId> = CreateCollectionData {375 name: collection_name,376 description: collection_description,377 token_prefix,378 mode,379 ..Default::default()380 };381 Self::create_collection_ex(origin, data)382 }383384 385 386 387 388 389 390 391 392 393 394 395 #[pallet::call_index(1)]396 #[pallet::weight(<SelfWeightOf<T>>::create_collection())]397 pub fn create_collection_ex(398 origin: OriginFor<T>,399 data: CreateCollectionData<T::CrossAccountId>,400 ) -> DispatchResult {401 let sender = ensure_signed(origin)?;402403 404 let sender = T::CrossAccountId::from_sub(sender);405 let _id = T::CollectionDispatch::create(406 sender.clone(),407 CollectionIssuer::User(sender),408 data,409 )?;410411 Ok(())412 }413414 415 416 417 418 419 420 421 422 423 #[pallet::call_index(2)]424 #[pallet::weight(<SelfWeightOf<T>>::destroy_collection())]425 pub fn destroy_collection(426 origin: OriginFor<T>,427 collection_id: CollectionId,428 ) -> DispatchResult {429 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);430431 Self::destroy_collection_internal(sender, collection_id)432 }433434 435 436 437 438 439 440 441 442 443 444 445 #[pallet::call_index(3)]446 #[pallet::weight(<SelfWeightOf<T>>::add_to_allow_list())]447 pub fn add_to_allow_list(448 origin: OriginFor<T>,449 collection_id: CollectionId,450 address: T::CrossAccountId,451 ) -> DispatchResult {452 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {453 fail!(<pallet_common::Error<T>>::UnsupportedOperation);454 }455456 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);457 let collection = <CollectionHandle<T>>::try_get(collection_id)?;458 collection.check_is_internal()?;459460 <PalletCommon<T>>::toggle_allowlist(&collection, &sender, &address, true)?;461462 Ok(())463 }464465 466 467 468 469 470 471 472 473 474 475 476 #[pallet::call_index(4)]477 #[pallet::weight(<SelfWeightOf<T>>::remove_from_allow_list())]478 pub fn remove_from_allow_list(479 origin: OriginFor<T>,480 collection_id: CollectionId,481 address: T::CrossAccountId,482 ) -> DispatchResult {483 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {484 fail!(<pallet_common::Error<T>>::UnsupportedOperation);485 }486487 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);488 let collection = <CollectionHandle<T>>::try_get(collection_id)?;489 collection.check_is_internal()?;490491 <PalletCommon<T>>::toggle_allowlist(&collection, &sender, &address, false)?;492493 Ok(())494 }495496 497 498 499 500 501 502 503 504 505 506 #[pallet::call_index(5)]507 #[pallet::weight(<SelfWeightOf<T>>::change_collection_owner())]508 pub fn change_collection_owner(509 origin: OriginFor<T>,510 collection_id: CollectionId,511 new_owner: T::AccountId,512 ) -> DispatchResult {513 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {514 fail!(<pallet_common::Error<T>>::UnsupportedOperation);515 }516 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);517 let new_owner = T::CrossAccountId::from_sub(new_owner);518 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;519 target_collection.change_owner(sender, new_owner)520 }521522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 #[pallet::call_index(6)]539 #[pallet::weight(<SelfWeightOf<T>>::add_collection_admin())]540 pub fn add_collection_admin(541 origin: OriginFor<T>,542 collection_id: CollectionId,543 new_admin_id: T::CrossAccountId,544 ) -> DispatchResult {545 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {546 fail!(<pallet_common::Error<T>>::UnsupportedOperation);547 }548 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);549 let collection = <CollectionHandle<T>>::try_get(collection_id)?;550 <PalletCommon<T>>::toggle_admin(&collection, &sender, &new_admin_id, true)551 }552553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 #[pallet::call_index(7)]568 #[pallet::weight(<SelfWeightOf<T>>::remove_collection_admin())]569 pub fn remove_collection_admin(570 origin: OriginFor<T>,571 collection_id: CollectionId,572 account_id: T::CrossAccountId,573 ) -> DispatchResult {574 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {575 fail!(<pallet_common::Error<T>>::UnsupportedOperation);576 }577 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);578 let collection = <CollectionHandle<T>>::try_get(collection_id)?;579 <PalletCommon<T>>::toggle_admin(&collection, &sender, &account_id, false)580 }581582 583 584 585 586 587 588 589 590 591 592 593 594 595 #[pallet::call_index(8)]596 #[pallet::weight(<SelfWeightOf<T>>::set_collection_sponsor())]597 pub fn set_collection_sponsor(598 origin: OriginFor<T>,599 collection_id: CollectionId,600 new_sponsor: T::AccountId,601 ) -> DispatchResult {602 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {603 fail!(<pallet_common::Error<T>>::UnsupportedOperation);604 }605 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);606 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;607 target_collection.set_sponsor(&sender, new_sponsor.clone())608 }609610 611 612 613 614 615 616 617 618 619 620 621 622 623 #[pallet::call_index(9)]624 #[pallet::weight(<SelfWeightOf<T>>::confirm_sponsorship())]625 pub fn confirm_sponsorship(626 origin: OriginFor<T>,627 collection_id: CollectionId,628 ) -> DispatchResult {629 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {630 fail!(<pallet_common::Error<T>>::UnsupportedOperation);631 }632 let sender = ensure_signed(origin)?;633 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;634 target_collection.confirm_sponsorship(&sender)635 }636637 638 639 640 641 642 643 644 645 646 #[pallet::call_index(10)]647 #[pallet::weight(<SelfWeightOf<T>>::remove_collection_sponsor())]648 pub fn remove_collection_sponsor(649 origin: OriginFor<T>,650 collection_id: CollectionId,651 ) -> DispatchResult {652 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {653 fail!(<pallet_common::Error<T>>::UnsupportedOperation);654 }655 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);656 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;657 target_collection.remove_sponsor(&sender)658 }659660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 #[pallet::call_index(11)]679 #[pallet::weight(T::CommonWeightInfo::create_item(data) + <Pallet<T>>::nesting_budget_predispatch_weight())]680 pub fn create_item(681 origin: OriginFor<T>,682 collection_id: CollectionId,683 owner: T::CrossAccountId,684 data: CreateItemData,685 ) -> DispatchResultWithPostInfo {686 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);687 let budget = Self::structure_nesting_budget();688689 Self::refund_nesting_budget(690 dispatch_tx::<T, _>(collection_id, |d| {691 d.create_item(sender, owner, data, &budget)692 }),693 budget,694 )695 }696697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 #[pallet::call_index(12)]716 #[pallet::weight(T::CommonWeightInfo::create_multiple_items(items_data) + <Pallet<T>>::nesting_budget_predispatch_weight())]717 pub fn create_multiple_items(718 origin: OriginFor<T>,719 collection_id: CollectionId,720 owner: T::CrossAccountId,721 items_data: Vec<CreateItemData>,722 ) -> DispatchResultWithPostInfo {723 ensure!(!items_data.is_empty(), Error::<T>::EmptyArgument);724 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);725 let budget = Self::structure_nesting_budget();726727 Self::refund_nesting_budget(728 dispatch_tx::<T, _>(collection_id, |d| {729 d.create_multiple_items(sender, owner, items_data, &budget)730 }),731 budget,732 )733 }734735 736 737 738 739 740 741 742 743 744 745 746 747 #[pallet::call_index(13)]748 #[pallet::weight(T::CommonWeightInfo::set_collection_properties(properties.len() as u32))]749 pub fn set_collection_properties(750 origin: OriginFor<T>,751 collection_id: CollectionId,752 properties: Vec<Property>,753 ) -> DispatchResultWithPostInfo {754 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);755756 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);757758 dispatch_tx::<T, _>(collection_id, |d| {759 d.set_collection_properties(sender, properties)760 })761 }762763 764 765 766 767 768 769 770 771 772 773 774 775 #[pallet::call_index(14)]776 #[pallet::weight(T::CommonWeightInfo::delete_collection_properties(property_keys.len() as u32))]777 pub fn delete_collection_properties(778 origin: OriginFor<T>,779 collection_id: CollectionId,780 property_keys: Vec<PropertyKey>,781 ) -> DispatchResultWithPostInfo {782 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);783784 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);785786 dispatch_tx::<T, _>(collection_id, |d| {787 d.delete_collection_properties(&sender, property_keys)788 })789 }790791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 #[pallet::call_index(15)]810 #[pallet::weight(T::CommonWeightInfo::set_token_properties(properties.len() as u32) + <Pallet<T>>::nesting_budget_predispatch_weight())]811 pub fn set_token_properties(812 origin: OriginFor<T>,813 collection_id: CollectionId,814 token_id: TokenId,815 properties: Vec<Property>,816 ) -> DispatchResultWithPostInfo {817 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);818819 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);820 let budget = Self::structure_nesting_budget();821822 Self::refund_nesting_budget(823 dispatch_tx::<T, _>(collection_id, |d| {824 d.set_token_properties(sender, token_id, properties, &budget)825 }),826 budget,827 )828 }829830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 #[pallet::call_index(16)]846 #[pallet::weight(T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32) + <Pallet<T>>::nesting_budget_predispatch_weight())]847 pub fn delete_token_properties(848 origin: OriginFor<T>,849 collection_id: CollectionId,850 token_id: TokenId,851 property_keys: Vec<PropertyKey>,852 ) -> DispatchResultWithPostInfo {853 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);854855 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);856 let budget = Self::structure_nesting_budget();857858 Self::refund_nesting_budget(859 dispatch_tx::<T, _>(collection_id, |d| {860 d.delete_token_properties(sender, token_id, property_keys, &budget)861 }),862 budget,863 )864 }865866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 #[pallet::call_index(17)]882 #[pallet::weight(T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32))]883 pub fn set_token_property_permissions(884 origin: OriginFor<T>,885 collection_id: CollectionId,886 property_permissions: Vec<PropertyKeyPermission>,887 ) -> DispatchResultWithPostInfo {888 ensure!(!property_permissions.is_empty(), Error::<T>::EmptyArgument);889890 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);891892 dispatch_tx::<T, _>(collection_id, |d| {893 d.set_token_property_permissions(&sender, property_permissions)894 })895 }896897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 #[pallet::call_index(18)]913 #[pallet::weight(T::CommonWeightInfo::create_multiple_items_ex(data) + <Pallet<T>>::nesting_budget_predispatch_weight())]914 pub fn create_multiple_items_ex(915 origin: OriginFor<T>,916 collection_id: CollectionId,917 data: CreateItemExData<T::CrossAccountId>,918 ) -> DispatchResultWithPostInfo {919 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);920 let budget = Self::structure_nesting_budget();921922 Self::refund_nesting_budget(923 dispatch_tx::<T, _>(collection_id, |d| {924 d.create_multiple_items_ex(sender, data, &budget)925 }),926 budget,927 )928 }929930 931 932 933 934 935 936 937 938 939 940 #[pallet::call_index(19)]941 #[pallet::weight(<SelfWeightOf<T>>::set_transfers_enabled_flag())]942 pub fn set_transfers_enabled_flag(943 origin: OriginFor<T>,944 collection_id: CollectionId,945 value: bool,946 ) -> DispatchResult {947 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {948 fail!(<pallet_common::Error<T>>::UnsupportedOperation);949 }950 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);951 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;952 target_collection.check_is_internal()?;953 target_collection.check_is_owner(&sender)?;954955 956957 target_collection.limits.transfers_enabled = Some(value);958 target_collection.save()959 }960961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 #[pallet::call_index(20)]978 #[pallet::weight(T::CommonWeightInfo::burn_item())]979 pub fn burn_item(980 origin: OriginFor<T>,981 collection_id: CollectionId,982 item_id: TokenId,983 value: u128,984 ) -> DispatchResultWithPostInfo {985 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);986987 let post_info =988 dispatch_tx::<T, _>(collection_id, |d| d.burn_item(sender, item_id, value))?;989 if value == 1 {990 <NftTransferBasket<T>>::remove(collection_id, item_id);991 <NftApproveBasket<T>>::remove(collection_id, item_id);992 }993 994 995 996 Ok(post_info)997 }998999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 #[pallet::call_index(21)]1023 #[pallet::weight(T::CommonWeightInfo::burn_from() + <Pallet<T>>::nesting_budget_predispatch_weight())]1024 pub fn burn_from(1025 origin: OriginFor<T>,1026 collection_id: CollectionId,1027 from: T::CrossAccountId,1028 item_id: TokenId,1029 value: u128,1030 ) -> DispatchResultWithPostInfo {1031 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1032 let budget = Self::structure_nesting_budget();10331034 Self::refund_nesting_budget(1035 dispatch_tx::<T, _>(collection_id, |d| {1036 d.burn_from(sender, from, item_id, value, &budget)1037 }),1038 budget,1039 )1040 }10411042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 #[pallet::call_index(22)]1064 #[pallet::weight(T::CommonWeightInfo::transfer() + <Pallet<T>>::nesting_budget_predispatch_weight())]1065 pub fn transfer(1066 origin: OriginFor<T>,1067 recipient: T::CrossAccountId,1068 collection_id: CollectionId,1069 item_id: TokenId,1070 value: u128,1071 ) -> DispatchResultWithPostInfo {1072 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1073 let budget = Self::structure_nesting_budget();10741075 Self::refund_nesting_budget(1076 dispatch_tx::<T, _>(collection_id, |d| {1077 d.transfer(sender, recipient, item_id, value, &budget)1078 }),1079 budget,1080 )1081 }10821083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 #[pallet::call_index(23)]1099 #[pallet::weight(T::CommonWeightInfo::approve())]1100 pub fn approve(1101 origin: OriginFor<T>,1102 spender: T::CrossAccountId,1103 collection_id: CollectionId,1104 item_id: TokenId,1105 amount: u128,1106 ) -> DispatchResultWithPostInfo {1107 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);11081109 dispatch_tx::<T, _>(collection_id, |d| {1110 d.approve(sender, spender, item_id, amount)1111 })1112 }11131114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 #[pallet::call_index(24)]1131 #[pallet::weight(T::CommonWeightInfo::approve_from())]1132 pub fn approve_from(1133 origin: OriginFor<T>,1134 from: T::CrossAccountId,1135 to: T::CrossAccountId,1136 collection_id: CollectionId,1137 item_id: TokenId,1138 amount: u128,1139 ) -> DispatchResultWithPostInfo {1140 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);11411142 dispatch_tx::<T, _>(collection_id, |d| {1143 d.approve_from(sender, from, to, item_id, amount)1144 })1145 }11461147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 #[pallet::call_index(25)]1172 #[pallet::weight(T::CommonWeightInfo::transfer_from() + <Pallet<T>>::nesting_budget_predispatch_weight())]1173 pub fn transfer_from(1174 origin: OriginFor<T>,1175 from: T::CrossAccountId,1176 recipient: T::CrossAccountId,1177 collection_id: CollectionId,1178 item_id: TokenId,1179 value: u128,1180 ) -> DispatchResultWithPostInfo {1181 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1182 let budget = Self::structure_nesting_budget();11831184 Self::refund_nesting_budget(1185 dispatch_tx::<T, _>(collection_id, |d| {1186 d.transfer_from(sender, from, recipient, item_id, value, &budget)1187 }),1188 budget,1189 )1190 }11911192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 #[pallet::call_index(26)]1205 #[pallet::weight(<SelfWeightOf<T>>::set_collection_limits())]1206 pub fn set_collection_limits(1207 origin: OriginFor<T>,1208 collection_id: CollectionId,1209 new_limit: CollectionLimits,1210 ) -> DispatchResult {1211 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {1212 fail!(<pallet_common::Error<T>>::UnsupportedOperation);1213 }1214 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1215 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;1216 <PalletCommon<T>>::update_limits(&sender, &mut target_collection, new_limit)1217 }12181219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 #[pallet::call_index(27)]1232 #[pallet::weight(<SelfWeightOf<T>>::set_collection_limits())]1233 pub fn set_collection_permissions(1234 origin: OriginFor<T>,1235 collection_id: CollectionId,1236 new_permission: CollectionPermissions,1237 ) -> DispatchResult {1238 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {1239 fail!(<pallet_common::Error<T>>::UnsupportedOperation);1240 }1241 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1242 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;1243 <PalletCommon<T>>::update_permissions(&sender, &mut target_collection, new_permission)1244 }12451246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 #[pallet::call_index(28)]1258 #[pallet::weight(T::RefungibleExtensionsWeightInfo::repartition())]1259 pub fn repartition(1260 origin: OriginFor<T>,1261 collection_id: CollectionId,1262 token_id: TokenId,1263 amount: u128,1264 ) -> DispatchResultWithPostInfo {1265 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1266 dispatch_tx::<T, _>(collection_id, |d| {1267 if let Some(refungible_extensions) = d.refungible_extensions() {1268 refungible_extensions.repartition(&sender, token_id, amount)1269 } else {1270 fail!(<Error<T>>::RepartitionCalledOnNonRefungibleCollection)1271 }1272 })1273 }12741275 1276 1277 1278 1279 1280 1281 1282 1283 1284 #[pallet::call_index(29)]1285 #[pallet::weight(T::CommonWeightInfo::set_allowance_for_all())]1286 pub fn set_allowance_for_all(1287 origin: OriginFor<T>,1288 collection_id: CollectionId,1289 operator: T::CrossAccountId,1290 approve: bool,1291 ) -> DispatchResultWithPostInfo {1292 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1293 dispatch_tx::<T, _>(collection_id, |d| {1294 d.set_allowance_for_all(sender, operator, approve)1295 })1296 }12971298 1299 1300 1301 1302 1303 #[pallet::call_index(30)]1304 #[pallet::weight(<SelfWeightOf<T>>::force_repair_collection())]1305 pub fn force_repair_collection(1306 origin: OriginFor<T>,1307 collection_id: CollectionId,1308 ) -> DispatchResult {1309 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {1310 fail!(<pallet_common::Error<T>>::UnsupportedOperation);1311 }1312 ensure_root(origin)?;1313 <PalletCommon<T>>::repair_collection(collection_id)1314 }13151316 1317 1318 1319 1320 1321 1322 #[pallet::call_index(31)]1323 #[pallet::weight(T::CommonWeightInfo::force_repair_item())]1324 pub fn force_repair_item(1325 origin: OriginFor<T>,1326 collection_id: CollectionId,1327 item_id: TokenId,1328 ) -> DispatchResultWithPostInfo {1329 ensure_root(origin)?;1330 dispatch_tx::<T, _>(collection_id, |d| d.repair_item(item_id))1331 }1332 }13331334 impl<T: Config> Pallet<T> {1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 pub fn force_set_sponsor(1345 sponsor: T::AccountId,1346 collection_id: CollectionId,1347 ) -> DispatchResult {1348 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;1349 target_collection.force_set_sponsor(sponsor)1350 }13511352 1353 1354 1355 1356 1357 1358 1359 1360 pub fn force_remove_collection_sponsor(collection_id: CollectionId) -> DispatchResult {1361 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;1362 target_collection.force_remove_sponsor()1363 }13641365 #[inline(always)]1366 pub(crate) fn destroy_collection_internal(1367 sender: T::CrossAccountId,1368 collection_id: CollectionId,1369 ) -> DispatchResult {1370 T::CollectionDispatch::destroy(sender, collection_id)?;13711372 1373 13741375 let _ = <NftTransferBasket<T>>::clear_prefix(collection_id, u32::MAX, None);1376 let _ = <FungibleTransferBasket<T>>::clear_prefix(collection_id, u32::MAX, None);1377 let _ = <ReFungibleTransferBasket<T>>::clear_prefix((collection_id,), u32::MAX, None);13781379 let _ = <NftApproveBasket<T>>::clear_prefix(collection_id, u32::MAX, None);1380 let _ = <FungibleApproveBasket<T>>::clear_prefix(collection_id, u32::MAX, None);1381 let _ = <RefungibleApproveBasket<T>>::clear_prefix((collection_id,), u32::MAX, None);13821383 Ok(())1384 }13851386 fn structure_nesting_budget() -> budget::Value {1387 budget::Value::new(Self::nesting_budget())1388 }13891390 fn nesting_budget_predispatch_weight() -> Weight {1391 T::StructureWeightInfo::find_parent().saturating_mul(Self::nesting_budget() as u64)1392 }13931394 pub fn refund_nesting_budget(1395 mut result: DispatchResultWithPostInfo,1396 budget: budget::Value,1397 ) -> DispatchResultWithPostInfo {1398 let refund_amount = budget.refund_amount();1399 let consumed = Self::nesting_budget() - refund_amount;14001401 match &mut result {1402 Ok(PostDispatchInfo {1403 actual_weight: Some(weight),1404 ..1405 })1406 | Err(DispatchErrorWithPostInfo {1407 post_info: PostDispatchInfo {1408 actual_weight: Some(weight),1409 ..1410 },1411 ..1412 }) => {1413 *weight += T::StructureWeightInfo::find_parent().saturating_mul(consumed as u64)1414 }1415 _ => {}1416 }14171418 result1419 }1420 }1421}