1234567891011121314151617#![recursion_limit = "1024"]18#![cfg_attr(not(feature = "std"), no_std)]19#![allow(20 clippy::too_many_arguments,21 clippy::unnecessary_mut_passed,22 clippy::unused_unit23)]2425extern crate alloc;2627use frame_support::{28 decl_module, decl_storage, decl_error, decl_event,29 dispatch::DispatchResult,30 ensure,31 weights::{Weight},32 transactional,33 pallet_prelude::{DispatchResultWithPostInfo, ConstU32},34 BoundedVec,35};36use scale_info::TypeInfo;37use frame_system::{self as system, ensure_signed};38use sp_runtime::{sp_std::prelude::Vec};39use up_data_structs::{40 MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,41 CreateItemData, CollectionLimits, CollectionPermissions, CollectionId, CollectionMode, TokenId,42 SponsorshipState, CreateCollectionData, CreateItemExData, budget, Property, PropertyKey,43 PropertyKeyPermission,44};45use pallet_evm::account::CrossAccountId;46use pallet_common::{47 CollectionHandle, Pallet as PalletCommon, CommonWeightInfo, dispatch::dispatch_tx,48 dispatch::CollectionDispatch,49};50pub mod eth;5152#[cfg(feature = "runtime-benchmarks")]53mod benchmarking;54pub mod weights;55use weights::WeightInfo;5657const NESTING_BUDGET: u32 = 5;5859decl_error! {60 61 pub enum Error for Module<T: Config> {62 63 CollectionDecimalPointLimitExceeded,64 65 ConfirmUnsetSponsorFail,66 67 EmptyArgument,68 }69}7071pub trait Config: system::Config + pallet_common::Config + Sized + TypeInfo {72 type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;7374 75 type WeightInfo: WeightInfo;76 type CommonWeightInfo: CommonWeightInfo<Self::CrossAccountId>;77}7879decl_event! {80 pub enum Event<T>81 where82 <T as frame_system::Config>::AccountId,83 <T as pallet_evm::account::Config>::CrossAccountId,84 {85 86 87 88 89 90 CollectionSponsorRemoved(CollectionId),9192 93 94 95 96 97 98 99 CollectionAdminAdded(CollectionId, CrossAccountId),100101 102 103 104 105 106 107 108 CollectionOwnedChanged(CollectionId, AccountId),109110 111 112 113 114 115 116 117 CollectionSponsorSet(CollectionId, AccountId),118119 120 121 122 123 124 125 126 SponsorshipConfirmed(CollectionId, AccountId),127128 129 130 131 132 133 134 135 CollectionAdminRemoved(CollectionId, CrossAccountId),136137 138 139 140 141 142 143 144 AllowListAddressRemoved(CollectionId, CrossAccountId),145146 147 148 149 150 151 152 153 AllowListAddressAdded(CollectionId, CrossAccountId),154155 156 157 158 159 160 CollectionLimitSet(CollectionId),161162 CollectionPermissionSet(CollectionId),163 }164}165166type SelfWeightOf<T> = <T as Config>::WeightInfo;167168169170171172173174175176177178179180181182183184185186187188189190decl_storage! {191 trait Store for Module<T: Config> as Unique {192193 194 195 ChainVersion: u64;196 197198 199 200 201 pub CreateItemBasket get(fn create_item_basket): map hasher(blake2_128_concat) (CollectionId, T::AccountId) => Option<T::BlockNumber>;202 203 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber>;204 205 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>;206 207 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): nmap hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId, hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>;208 209210 211 212 #[deprecated]213 pub VariableMetaDataBasket get(fn variable_meta_data_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber>;214 pub TokenPropertyBasket get(fn token_property_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber>;215216 217 pub NftApproveBasket get(fn nft_approve_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber>;218 pub FungibleApproveBasket get(fn fungible_approve_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>;219 pub RefungibleApproveBasket get(fn refungible_approve_basket): nmap hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId, hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>;220 }221}222223decl_module! {224 pub struct Module<T: Config> for enum Call225 where226 origin: T::Origin227 {228 type Error = Error<T>;229230 fn deposit_event() = default;231232 fn on_initialize(_now: T::BlockNumber) -> Weight {233 0234 }235236 fn on_runtime_upgrade() -> Weight {237 let limit = None;238239 <VariableMetaDataBasket<T>>::remove_all(limit);240241 0242 }243244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 #[weight = <SelfWeightOf<T>>::create_collection()]261 #[transactional]262 #[deprecated]263 pub fn create_collection(origin,264 collection_name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,265 collection_description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,266 token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,267 mode: CollectionMode) -> DispatchResult {268 let data: CreateCollectionData<T::AccountId> = CreateCollectionData {269 name: collection_name,270 description: collection_description,271 token_prefix,272 mode,273 ..Default::default()274 };275 Self::create_collection_ex(origin, data)276 }277278 279 280 281 #[weight = <SelfWeightOf<T>>::create_collection()]282 #[transactional]283 pub fn create_collection_ex(origin, data: CreateCollectionData<T::AccountId>) -> DispatchResult {284 let sender = ensure_signed(origin)?;285286 287288 T::CollectionDispatch::create(T::CrossAccountId::from_sub(sender), data)?;289290 Ok(())291 }292293 294 295 296 297 298 299 300 301 302 #[weight = <SelfWeightOf<T>>::destroy_collection()]303 #[transactional]304 pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {305 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);306 let collection = <CollectionHandle<T>>::try_get(collection_id)?;307 collection.check_is_internal()?;308309 310311 T::CollectionDispatch::destroy(sender, collection)?;312313 <NftTransferBasket<T>>::remove_prefix(collection_id, None);314 <FungibleTransferBasket<T>>::remove_prefix(collection_id, None);315 <ReFungibleTransferBasket<T>>::remove_prefix((collection_id,), None);316317 <NftApproveBasket<T>>::remove_prefix(collection_id, None);318 <FungibleApproveBasket<T>>::remove_prefix(collection_id, None);319 <RefungibleApproveBasket<T>>::remove_prefix((collection_id,), None);320321 Ok(())322 }323324 325 326 327 328 329 330 331 332 333 334 335 336 #[weight = <SelfWeightOf<T>>::add_to_allow_list()]337 #[transactional]338 pub fn add_to_allow_list(origin, collection_id: CollectionId, address: T::CrossAccountId) -> DispatchResult{339340 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);341 let collection = <CollectionHandle<T>>::try_get(collection_id)?;342 collection.check_is_internal()?;343344 <PalletCommon<T>>::toggle_allowlist(345 &collection,346 &sender,347 &address,348 true,349 )?;350351 Self::deposit_event(Event::<T>::AllowListAddressAdded(352 collection_id,353 address354 ));355356 Ok(())357 }358359 360 361 362 363 364 365 366 367 368 369 370 371 #[weight = <SelfWeightOf<T>>::remove_from_allow_list()]372 #[transactional]373 pub fn remove_from_allow_list(origin, collection_id: CollectionId, address: T::CrossAccountId) -> DispatchResult{374375 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);376 let collection = <CollectionHandle<T>>::try_get(collection_id)?;377 collection.check_is_internal()?;378379 <PalletCommon<T>>::toggle_allowlist(380 &collection,381 &sender,382 &address,383 false,384 )?;385386 <Pallet<T>>::deposit_event(Event::<T>::AllowListAddressRemoved(387 collection_id,388 address389 ));390391 Ok(())392 }393394 395 396 397 398 399 400 401 402 403 404 405 #[weight = <SelfWeightOf<T>>::change_collection_owner()]406 #[transactional]407 pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {408409 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);410411 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;412 target_collection.check_is_internal()?;413 target_collection.check_is_owner(&sender)?;414415 target_collection.owner = new_owner.clone();416 <Pallet<T>>::deposit_event(Event::<T>::CollectionOwnedChanged(417 collection_id,418 new_owner419 ));420421 target_collection.save()422 }423424 425 426 427 428 429 430 431 432 433 434 435 436 437 #[weight = <SelfWeightOf<T>>::add_collection_admin()]438 #[transactional]439 pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::CrossAccountId) -> DispatchResult {440 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);441 let collection = <CollectionHandle<T>>::try_get(collection_id)?;442 collection.check_is_internal()?;443444 <Pallet<T>>::deposit_event(Event::<T>::CollectionAdminAdded(445 collection_id,446 new_admin_id.clone()447 ));448449 <PalletCommon<T>>::toggle_admin(&collection, &sender, &new_admin_id, true)450 }451452 453 454 455 456 457 458 459 460 461 462 463 464 #[weight = <SelfWeightOf<T>>::remove_collection_admin()]465 #[transactional]466 pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::CrossAccountId) -> DispatchResult {467 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);468 let collection = <CollectionHandle<T>>::try_get(collection_id)?;469 collection.check_is_internal()?;470471 <Pallet<T>>::deposit_event(Event::<T>::CollectionAdminRemoved(472 collection_id,473 account_id.clone()474 ));475476 <PalletCommon<T>>::toggle_admin(&collection, &sender, &account_id, false)477 }478479 480 481 482 483 484 485 486 487 488 #[weight = <SelfWeightOf<T>>::set_collection_sponsor()]489 #[transactional]490 pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {491 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);492493 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;494 target_collection.check_is_owner_or_admin(&sender)?;495 target_collection.check_is_internal()?;496497 target_collection.set_sponsor(new_sponsor.clone())?;498499 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(500 collection_id,501 new_sponsor502 ));503504 target_collection.save()505 }506507 508 509 510 511 512 513 514 #[weight = <SelfWeightOf<T>>::confirm_sponsorship()]515 #[transactional]516 pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {517 let sender = ensure_signed(origin)?;518519 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;520 target_collection.check_is_internal()?;521 ensure!(522 target_collection.confirm_sponsorship(&sender)?,523 Error::<T>::ConfirmUnsetSponsorFail524 );525526 <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(527 collection_id,528 sender529 ));530531 target_collection.save()532 }533534 535 536 537 538 539 540 541 542 543 #[weight = <SelfWeightOf<T>>::remove_collection_sponsor()]544 #[transactional]545 pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {546 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);547548 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;549 target_collection.check_is_internal()?;550 target_collection.check_is_owner(&sender)?;551552 target_collection.sponsorship = SponsorshipState::Disabled;553554 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(555 collection_id556 ));557 target_collection.save()558 }559560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 #[weight = T::CommonWeightInfo::create_item()]579 #[transactional]580 pub fn create_item(origin, collection_id: CollectionId, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResultWithPostInfo {581 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);582 let budget = budget::Value::new(NESTING_BUDGET);583584 dispatch_tx::<T, _>(collection_id, |d| d.create_item(sender, owner, data, &budget))585 }586587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 #[weight = T::CommonWeightInfo::create_multiple_items(&items_data)]606 #[transactional]607 pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::CrossAccountId, items_data: Vec<CreateItemData>) -> DispatchResultWithPostInfo {608 ensure!(!items_data.is_empty(), Error::<T>::EmptyArgument);609 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);610 let budget = budget::Value::new(NESTING_BUDGET);611612 dispatch_tx::<T, _>(collection_id, |d| d.create_multiple_items(sender, owner, items_data, &budget))613 }614615 #[weight = T::CommonWeightInfo::set_collection_properties(properties.len() as u32)]616 #[transactional]617 pub fn set_collection_properties(618 origin,619 collection_id: CollectionId,620 properties: Vec<Property>621 ) -> DispatchResultWithPostInfo {622 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);623624 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);625626 dispatch_tx::<T, _>(collection_id, |d| d.set_collection_properties(sender, properties))627 }628629 #[weight = T::CommonWeightInfo::delete_collection_properties(property_keys.len() as u32)]630 #[transactional]631 pub fn delete_collection_properties(632 origin,633 collection_id: CollectionId,634 property_keys: Vec<PropertyKey>,635 ) -> DispatchResultWithPostInfo {636 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);637638 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);639640 dispatch_tx::<T, _>(collection_id, |d| d.delete_collection_properties(&sender, property_keys))641 }642643 #[weight = T::CommonWeightInfo::set_token_properties(properties.len() as u32)]644 #[transactional]645 pub fn set_token_properties(646 origin,647 collection_id: CollectionId,648 token_id: TokenId,649 properties: Vec<Property>650 ) -> DispatchResultWithPostInfo {651 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);652653 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);654655 dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))656 }657658 #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]659 #[transactional]660 pub fn delete_token_properties(661 origin,662 collection_id: CollectionId,663 token_id: TokenId,664 property_keys: Vec<PropertyKey>665 ) -> DispatchResultWithPostInfo {666 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);667668 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);669670 dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))671 }672673 #[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]674 #[transactional]675 pub fn set_token_property_permissions(676 origin,677 collection_id: CollectionId,678 property_permissions: Vec<PropertyKeyPermission>,679 ) -> DispatchResultWithPostInfo {680 ensure!(!property_permissions.is_empty(), Error::<T>::EmptyArgument);681682 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);683684 dispatch_tx::<T, _>(collection_id, |d| d.set_token_property_permissions(&sender, property_permissions))685 }686687 #[weight = T::CommonWeightInfo::create_multiple_items_ex(&data)]688 #[transactional]689 pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {690 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);691 let budget = budget::Value::new(NESTING_BUDGET);692693 dispatch_tx::<T, _>(collection_id, |d| d.create_multiple_items_ex(sender, data, &budget))694 }695696 697698 699 700 701 702 703 704 705 706 707 708 709 #[weight = <SelfWeightOf<T>>::set_transfers_enabled_flag()]710 #[transactional]711 pub fn set_transfers_enabled_flag(origin, collection_id: CollectionId, value: bool) -> DispatchResult {712 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);713 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;714 target_collection.check_is_internal()?;715 target_collection.check_is_owner(&sender)?;716717 718719 target_collection.limits.transfers_enabled = Some(value);720 target_collection.save()721 }722723 724 725 726 727 728 729 730 731 732 733 734 735 736 #[weight = T::CommonWeightInfo::burn_item()]737 #[transactional]738 pub fn burn_item(origin, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {739 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);740741 let post_info = dispatch_tx::<T, _>(collection_id, |d| d.burn_item(sender, item_id, value))?;742 if value == 1 {743 <NftTransferBasket<T>>::remove(collection_id, item_id);744 <NftApproveBasket<T>>::remove(collection_id, item_id);745 }746 747 748 749 Ok(post_info)750 }751752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 #[weight = T::CommonWeightInfo::burn_from()]769 #[transactional]770 pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {771 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);772 let budget = budget::Value::new(NESTING_BUDGET);773774 dispatch_tx::<T, _>(collection_id, |d| d.burn_from(sender, from, item_id, value, &budget))775 }776777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 #[weight = T::CommonWeightInfo::transfer()]801 #[transactional]802 pub fn transfer(origin, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {803 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);804 let budget = budget::Value::new(NESTING_BUDGET);805806 dispatch_tx::<T, _>(collection_id, |d| d.transfer(sender, recipient, item_id, value, &budget))807 }808809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 #[weight = T::CommonWeightInfo::approve()]825 #[transactional]826 pub fn approve(origin, spender: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, amount: u128) -> DispatchResultWithPostInfo {827 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);828829 dispatch_tx::<T, _>(collection_id, |d| d.approve(sender, spender, item_id, amount))830 }831832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 #[weight = T::CommonWeightInfo::transfer_from()]852 #[transactional]853 pub fn transfer_from(origin, from: T::CrossAccountId, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128 ) -> DispatchResultWithPostInfo {854 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);855 let budget = budget::Value::new(NESTING_BUDGET);856857 dispatch_tx::<T, _>(collection_id, |d| d.transfer_from(sender, from, recipient, item_id, value, &budget))858 }859860 #[weight = <SelfWeightOf<T>>::set_collection_limits()]861 #[transactional]862 pub fn set_collection_limits(863 origin,864 collection_id: CollectionId,865 new_limit: CollectionLimits,866 ) -> DispatchResult {867 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);868 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;869 target_collection.check_is_internal()?;870 target_collection.check_is_owner_or_admin(&sender)?;871 let old_limit = &target_collection.limits;872873 target_collection.limits = <PalletCommon<T>>::clamp_limits(target_collection.mode.clone(), &old_limit, new_limit)?;874875 <Pallet<T>>::deposit_event(Event::<T>::CollectionLimitSet(876 collection_id877 ));878879 target_collection.save()880 }881882 #[weight = <SelfWeightOf<T>>::set_collection_limits()]883 #[transactional]884 pub fn set_collection_permissions(885 origin,886 collection_id: CollectionId,887 new_limit: CollectionPermissions,888 ) -> DispatchResult {889 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);890 let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;891 target_collection.check_is_internal()?;892 target_collection.check_is_owner_or_admin(&sender)?;893 let old_limit = &target_collection.permissions;894895 target_collection.permissions = <PalletCommon<T>>::clamp_permissions(target_collection.mode.clone(), &old_limit, new_limit)?;896897 <Pallet<T>>::deposit_event(Event::<T>::CollectionPermissionSet(898 collection_id899 ));900901 target_collection.save()902 }903 }904}