difftreelog
Add extrinsic: delete collection property
in: master
10 files changed
pallets/common/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use core::ops::{Deref, DerefMut};20use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};21use sp_std::{vec::Vec, collections::btree_map::BTreeMap};22use pallet_evm::account::CrossAccountId;23use frame_support::{24 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},25 ensure, fail,26 traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},27 BoundedVec,28 weights::Pays,29};30use pallet_evm::GasWeightMapping;31use up_data_structs::{32 COLLECTION_NUMBER_LIMIT, Collection, RpcCollection, CollectionId, CreateItemData,33 MAX_TOKEN_PREFIX_LENGTH, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission, TokenId,34 CollectionStats, MAX_TOKEN_OWNERSHIP, CollectionMode, NFT_SPONSOR_TRANSFER_TIMEOUT,35 FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT,36 CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,37 CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,38 PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,39 PropertiesError, PropertyKeyPermission,40};41pub use pallet::*;42use sp_core::H160;43use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};44#[cfg(feature = "runtime-benchmarks")]45pub mod benchmarking;46pub mod dispatch;47pub mod erc;48pub mod eth;4950#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]51pub struct CollectionHandle<T: Config> {52 pub id: CollectionId,53 collection: Collection<T::AccountId>,54 pub recorder: SubstrateRecorder<T>,55}56impl<T: Config> WithRecorder<T> for CollectionHandle<T> {57 fn recorder(&self) -> &SubstrateRecorder<T> {58 &self.recorder59 }60 fn into_recorder(self) -> SubstrateRecorder<T> {61 self.recorder62 }63}64impl<T: Config> CollectionHandle<T> {65 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {66 <CollectionById<T>>::get(id).map(|collection| Self {67 id,68 collection,69 recorder: SubstrateRecorder::new(eth::collection_id_to_address(id), gas_limit),70 })71 }72 pub fn new(id: CollectionId) -> Option<Self> {73 Self::new_with_gas_limit(id, u64::MAX)74 }75 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {76 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)77 }78 pub fn log_mirrored(&self, log: impl evm_coder::ToLog) {79 self.recorder.log_mirrored(log)80 }81 pub fn log_direct(&self, log: impl evm_coder::ToLog) {82 self.recorder.log_direct(log)83 }84 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {85 self.recorder86 .consume_gas(T::GasWeightMapping::weight_to_gas(87 <T as frame_system::Config>::DbWeight::get()88 .read89 .saturating_mul(reads),90 ))91 }92 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {93 self.recorder94 .consume_gas(T::GasWeightMapping::weight_to_gas(95 <T as frame_system::Config>::DbWeight::get()96 .write97 .saturating_mul(writes),98 ))99 }100 pub fn submit_logs(self) {101 self.recorder.submit_logs()102 }103 pub fn save(self) -> DispatchResult {104 self.recorder.submit_logs();105 <CollectionById<T>>::insert(self.id, self.collection);106 Ok(())107 }108}109impl<T: Config> Deref for CollectionHandle<T> {110 type Target = Collection<T::AccountId>;111112 fn deref(&self) -> &Self::Target {113 &self.collection114 }115}116117impl<T: Config> DerefMut for CollectionHandle<T> {118 fn deref_mut(&mut self) -> &mut Self::Target {119 &mut self.collection120 }121}122123impl<T: Config> CollectionHandle<T> {124 pub fn check_is_owner(&self, subject: &T::CrossAccountId) -> DispatchResult {125 ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);126 Ok(())127 }128 pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {129 *subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))130 }131 pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {132 ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);133 Ok(())134 }135 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {136 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)137 }138 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {139 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)140 }141 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {142 ensure!(143 <Allowlist<T>>::get((self.id, user)),144 <Error<T>>::AddressNotInAllowlist145 );146 Ok(())147 }148149 pub fn check_can_update_meta(150 &self,151 subject: &T::CrossAccountId,152 item_owner: &T::CrossAccountId,153 ) -> DispatchResult {154 match self.meta_update_permission {155 MetaUpdatePermission::ItemOwner => {156 ensure!(subject == item_owner, <Error<T>>::NoPermission);157 Ok(())158 }159 MetaUpdatePermission::Admin => self.check_is_owner_or_admin(subject),160 MetaUpdatePermission::None => fail!(<Error<T>>::NoPermission),161 }162 }163}164165#[frame_support::pallet]166pub mod pallet {167 use super::*;168 use pallet_evm::account;169 use dispatch::CollectionDispatch;170 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};171 use frame_system::pallet_prelude::*;172 use frame_support::traits::Currency;173 use up_data_structs::{TokenId, mapping::TokenAddressMapping};174 use scale_info::TypeInfo;175176 #[pallet::config]177 pub trait Config:178 frame_system::Config + pallet_evm_coder_substrate::Config + TypeInfo + account::Config179 {180 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;181182 type Currency: Currency<Self::AccountId>;183184 #[pallet::constant]185 type CollectionCreationPrice: Get<186 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,187 >;188 type CollectionDispatch: CollectionDispatch<Self>;189190 type TreasuryAccountId: Get<Self::AccountId>;191192 type EvmTokenAddressMapping: TokenAddressMapping<H160>;193 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;194 }195196 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);197198 #[pallet::pallet]199 #[pallet::storage_version(STORAGE_VERSION)]200 #[pallet::generate_store(pub(super) trait Store)]201 pub struct Pallet<T>(_);202203 #[pallet::extra_constants]204 impl<T: Config> Pallet<T> {205 pub fn collection_admins_limit() -> u32 {206 COLLECTION_ADMINS_LIMIT207 }208 }209210 #[pallet::event]211 #[pallet::generate_deposit(pub fn deposit_event)]212 pub enum Event<T: Config> {213 /// New collection was created214 ///215 /// # Arguments216 ///217 /// * collection_id: Globally unique identifier of newly created collection.218 ///219 /// * mode: [CollectionMode] converted into u8.220 ///221 /// * account_id: Collection owner.222 CollectionCreated(CollectionId, u8, T::AccountId),223224 /// New collection was destroyed225 ///226 /// # Arguments227 ///228 /// * collection_id: Globally unique identifier of collection.229 CollectionDestroyed(CollectionId),230231 /// New item was created.232 ///233 /// # Arguments234 ///235 /// * collection_id: Id of the collection where item was created.236 ///237 /// * item_id: Id of an item. Unique within the collection.238 ///239 /// * recipient: Owner of newly created item240 ///241 /// * amount: Always 1 for NFT242 ItemCreated(CollectionId, TokenId, T::CrossAccountId, u128),243244 /// Collection item was burned.245 ///246 /// # Arguments247 ///248 /// * collection_id.249 ///250 /// * item_id: Identifier of burned NFT.251 ///252 /// * owner: which user has destroyed its tokens253 ///254 /// * amount: Always 1 for NFT255 ItemDestroyed(CollectionId, TokenId, T::CrossAccountId, u128),256257 /// Item was transferred258 ///259 /// * collection_id: Id of collection to which item is belong260 ///261 /// * item_id: Id of an item262 ///263 /// * sender: Original owner of item264 ///265 /// * recipient: New owner of item266 ///267 /// * amount: Always 1 for NFT268 Transfer(269 CollectionId,270 TokenId,271 T::CrossAccountId,272 T::CrossAccountId,273 u128,274 ),275276 /// * collection_id277 ///278 /// * item_id279 ///280 /// * sender281 ///282 /// * spender283 ///284 /// * amount285 Approved(286 CollectionId,287 TokenId,288 T::CrossAccountId,289 T::CrossAccountId,290 u128,291 ),292293 CollectionPropertySet(CollectionId, Property),294295 TokenPropertySet(CollectionId, TokenId, Property),296297 TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),298299 PropertyPermissionSet(CollectionId, PropertyKeyPermission),300 }301302 #[pallet::error]303 pub enum Error<T> {304 /// This collection does not exist.305 CollectionNotFound,306 /// Sender parameter and item owner must be equal.307 MustBeTokenOwner,308 /// No permission to perform action309 NoPermission,310 /// Collection is not in mint mode.311 PublicMintingNotAllowed,312 /// Address is not in allow list.313 AddressNotInAllowlist,314315 /// Collection name can not be longer than 63 char.316 CollectionNameLimitExceeded,317 /// Collection description can not be longer than 255 char.318 CollectionDescriptionLimitExceeded,319 /// Token prefix can not be longer than 15 char.320 CollectionTokenPrefixLimitExceeded,321 /// Total collections bound exceeded.322 TotalCollectionsLimitExceeded,323 /// variable_data exceeded data limit.324 TokenVariableDataLimitExceeded,325 /// Exceeded max admin count326 CollectionAdminCountExceeded,327 /// Collection limit bounds per collection exceeded328 CollectionLimitBoundsExceeded,329 /// Tried to enable permissions which are only permitted to be disabled330 OwnerPermissionsCantBeReverted,331 /// Collection settings not allowing items transferring332 TransferNotAllowed,333 /// Account token limit exceeded per collection334 AccountTokenLimitExceeded,335 /// Collection token limit exceeded336 CollectionTokenLimitExceeded,337 /// Metadata flag frozen338 MetadataFlagFrozen,339340 /// Item not exists.341 TokenNotFound,342 /// Item balance not enough.343 TokenValueTooLow,344 /// Requested value more than approved.345 ApprovedValueTooLow,346 /// Tried to approve more than owned347 CantApproveMoreThanOwned,348349 /// Can't transfer tokens to ethereum zero address350 AddressIsZero,351 /// Target collection doesn't supports this operation352 UnsupportedOperation,353354 /// Not sufficient founds to perform action355 NotSufficientFounds,356357 /// Collection has nesting disabled358 NestingIsDisabled,359 /// Only owner may nest tokens under this collection360 OnlyOwnerAllowedToNest,361 /// Only tokens from specific collections may nest tokens under this362 SourceCollectionIsNotAllowedToNest,363364 /// Tried to store more data than allowed in collection field365 CollectionFieldSizeExceeded,366 }367368 #[pallet::storage]369 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;370 #[pallet::storage]371 pub type DestroyedCollectionCount<T> =372 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;373374 /// Collection info375 #[pallet::storage]376 pub type CollectionById<T> = StorageMap<377 Hasher = Blake2_128Concat,378 Key = CollectionId,379 Value = Collection<<T as frame_system::Config>::AccountId>,380 QueryKind = OptionQuery,381 >;382383 /// Collection properties384 #[pallet::storage]385 pub type CollectionProperties<T> = StorageMap<386 Hasher = Blake2_128Concat,387 Key = CollectionId,388 Value = Properties,389 QueryKind = ValueQuery,390 OnEmpty = up_data_structs::CollectionProperties,391 >;392393 #[pallet::storage]394 #[pallet::getter(fn property_permission)]395 pub type CollectionPropertyPermissions<T> = StorageMap<396 Hasher = Blake2_128Concat,397 Key = CollectionId,398 Value = PropertiesPermissionMap,399 QueryKind = ValueQuery,400 >;401402 /// Large variable-size collection fields are extracted here403 #[pallet::storage]404 pub type CollectionData<T> = StorageNMap<405 Key = (406 Key<Twox64Concat, CollectionId>,407 Key<Twox64Concat, CollectionField>,408 ),409 Value = BoundedVec<u8, ConstU32<COLLECTION_FIELD_LIMIT>>,410 QueryKind = ValueQuery,411 >;412413 #[pallet::storage]414 pub type AdminAmount<T> = StorageMap<415 Hasher = Blake2_128Concat,416 Key = CollectionId,417 Value = u32,418 QueryKind = ValueQuery,419 >;420421 /// List of collection admins422 #[pallet::storage]423 pub type IsAdmin<T: Config> = StorageNMap<424 Key = (425 Key<Blake2_128Concat, CollectionId>,426 Key<Blake2_128Concat, T::CrossAccountId>,427 ),428 Value = bool,429 QueryKind = ValueQuery,430 >;431432 /// Allowlisted collection users433 #[pallet::storage]434 pub type Allowlist<T: Config> = StorageNMap<435 Key = (436 Key<Blake2_128Concat, CollectionId>,437 Key<Blake2_128Concat, T::CrossAccountId>,438 ),439 Value = bool,440 QueryKind = ValueQuery,441 >;442443 /// Not used by code, exists only to provide some types to metadata444 #[pallet::storage]445 pub type DummyStorageValue<T: Config> = StorageValue<446 Value = (447 CollectionStats,448 CollectionId,449 TokenId,450 PhantomType<RpcCollection<T::AccountId>>,451 ),452 QueryKind = OptionQuery,453 >;454455 #[pallet::hooks]456 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {457 fn on_runtime_upgrade() -> Weight {458 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {459 use up_data_structs::{CollectionVersion1, CollectionVersion2};460 <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {461 Self::set_field_raw(462 id,463 CollectionField::OffchainSchema,464 v.offchain_schema.clone().into_inner(),465 )466 .expect("data has lower bounds than field");467 Self::set_field_raw(468 id,469 CollectionField::VariableOnChainSchema,470 v.variable_on_chain_schema.clone().into_inner(),471 )472 .expect("data has lower bounds than field");473 Self::set_field_raw(474 id,475 CollectionField::ConstOnChainSchema,476 v.const_on_chain_schema.clone().into_inner(),477 )478 .expect("data has lower bounds than field");479480 Some(CollectionVersion2::from(v))481 });482 }483484 0485 }486 }487}488489impl<T: Config> Pallet<T> {490 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens491 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {492 ensure!(493 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,494 <Error<T>>::AddressIsZero495 );496 Ok(())497 }498 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {499 <IsAdmin<T>>::iter_prefix((collection,))500 .map(|(a, _)| a)501 .collect()502 }503 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {504 <Allowlist<T>>::iter_prefix((collection,))505 .map(|(a, _)| a)506 .collect()507 }508 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {509 <Allowlist<T>>::get((collection, user))510 }511 pub fn collection_stats() -> CollectionStats {512 let created = <CreatedCollectionCount<T>>::get();513 let destroyed = <DestroyedCollectionCount<T>>::get();514 CollectionStats {515 created: created.0,516 destroyed: destroyed.0,517 alive: created.0 - destroyed.0,518 }519 }520521 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {522 let collection = <CollectionById<T>>::get(collection);523 if collection.is_none() {524 return None;525 }526527 let collection = collection.unwrap();528 let limits = collection.limits;529 let effective_limits = CollectionLimits {530 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),531 sponsored_data_size: Some(limits.sponsored_data_size()),532 sponsored_data_rate_limit: Some(533 limits534 .sponsored_data_rate_limit535 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),536 ),537 token_limit: Some(limits.token_limit()),538 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(539 match collection.mode {540 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,541 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,542 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,543 },544 )),545 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),546 owner_can_transfer: Some(limits.owner_can_transfer()),547 owner_can_destroy: Some(limits.owner_can_destroy()),548 transfers_enabled: Some(limits.transfers_enabled()),549 nesting_rule: Some(limits.nesting_rule().clone()),550 };551552 Some(effective_limits)553 }554555 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {556 let Collection {557 name,558 description,559 owner,560 mode,561 access,562 token_prefix,563 mint_mode,564 schema_version,565 sponsorship,566 limits,567 meta_update_permission,568 ..569 } = <CollectionById<T>>::get(collection)?;570 Some(RpcCollection {571 name: name.into_inner(),572 description: description.into_inner(),573 owner,574 mode,575 access,576 token_prefix: token_prefix.into_inner(),577 mint_mode,578 schema_version,579 sponsorship,580 limits,581 meta_update_permission,582 offchain_schema: <CollectionData<T>>::get((583 collection,584 CollectionField::OffchainSchema,585 ))586 .into_inner(),587 const_on_chain_schema: <CollectionData<T>>::get((588 collection,589 CollectionField::ConstOnChainSchema,590 ))591 .into_inner(),592 variable_on_chain_schema: <CollectionData<T>>::get((593 collection,594 CollectionField::VariableOnChainSchema,595 ))596 .into_inner(),597 })598 }599}600601impl<T: Config> Pallet<T> {602 pub fn init_collection(603 owner: T::AccountId,604 data: CreateCollectionData<T::AccountId>,605 ) -> Result<CollectionId, DispatchError> {606 {607 ensure!(608 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,609 Error::<T>::CollectionTokenPrefixLimitExceeded610 );611 }612613 let created_count = <CreatedCollectionCount<T>>::get()614 .0615 .checked_add(1)616 .ok_or(ArithmeticError::Overflow)?;617 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;618 let id = CollectionId(created_count);619620 // bound Total number of collections621 ensure!(622 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,623 <Error<T>>::TotalCollectionsLimitExceeded624 );625626 // =========627628 let collection = Collection {629 owner: owner.clone(),630 name: data.name,631 mode: data.mode.clone(),632 mint_mode: false,633 access: data.access.unwrap_or_default(),634 description: data.description,635 token_prefix: data.token_prefix,636 schema_version: data.schema_version.unwrap_or_default(),637 sponsorship: data638 .pending_sponsor639 .map(SponsorshipState::Unconfirmed)640 .unwrap_or_default(),641 limits: data642 .limits643 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))644 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,645 meta_update_permission: data.meta_update_permission.unwrap_or_default(),646 // token_property_permissions: data.token_property_permissions.unwrap_or_default(),647 // properties: Properties::from_collection_props_vec(data.properties)?648 };649650 CollectionProperties::<T>::insert(651 id,652 Properties::from_collection_props_vec(data.properties)?,653 );654655 let token_props_permissions: PropertiesPermissionMap = data656 .token_property_permissions657 .into_iter()658 .map(|property| (property.key, property.permission))659 .collect::<BTreeMap<_, _>>()660 .try_into()661 .map_err(|_| PropertiesError::PropertyLimitReached)?;662663 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);664665 // Take a (non-refundable) deposit of collection creation666 {667 let mut imbalance =668 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();669 imbalance.subsume(670 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(671 &T::TreasuryAccountId::get(),672 T::CollectionCreationPrice::get(),673 ),674 );675 <T as Config>::Currency::settle(676 &owner,677 imbalance,678 WithdrawReasons::TRANSFER,679 ExistenceRequirement::KeepAlive,680 )681 .map_err(|_| Error::<T>::NotSufficientFounds)?;682 }683684 <CreatedCollectionCount<T>>::put(created_count);685 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));686 <CollectionById<T>>::insert(id, collection);687 Self::set_field_raw(688 id,689 CollectionField::OffchainSchema,690 data.offchain_schema.into_inner(),691 )692 .expect("data has lower bounds than field");693 Self::set_field_raw(694 id,695 CollectionField::VariableOnChainSchema,696 data.variable_on_chain_schema.into_inner(),697 )698 .expect("data has lower bounds than field");699 Self::set_field_raw(700 id,701 CollectionField::ConstOnChainSchema,702 data.const_on_chain_schema.into_inner(),703 )704 .expect("data has lower bounds than field");705 Ok(id)706 }707708 pub fn destroy_collection(709 collection: CollectionHandle<T>,710 sender: &T::CrossAccountId,711 ) -> DispatchResult {712 ensure!(713 collection.limits.owner_can_destroy(),714 <Error<T>>::NoPermission,715 );716 collection.check_is_owner(sender)?;717718 let destroyed_collections = <DestroyedCollectionCount<T>>::get()719 .0720 .checked_add(1)721 .ok_or(ArithmeticError::Overflow)?;722723 // =========724725 <DestroyedCollectionCount<T>>::put(destroyed_collections);726 <CollectionById<T>>::remove(collection.id);727 <CollectionData<T>>::remove_prefix((collection.id,), None);728 <AdminAmount<T>>::remove(collection.id);729 <IsAdmin<T>>::remove_prefix((collection.id,), None);730 <Allowlist<T>>::remove_prefix((collection.id,), None);731732 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));733 Ok(())734 }735736 pub fn set_collection_property(737 collection: &CollectionHandle<T>,738 sender: &T::CrossAccountId,739 property: Property,740 ) -> DispatchResult {741 collection.check_is_owner_or_admin(sender)?;742743 CollectionProperties::<T>::try_mutate(collection.id, |properties| {744 properties.try_set_property(property.clone())745 })?;746747 Self::deposit_event(Event::CollectionPropertySet(collection.id, property));748749 Ok(())750 }751752 pub fn set_collection_properties(753 collection: &CollectionHandle<T>,754 sender: &T::CrossAccountId,755 properties: Vec<Property>,756 ) -> DispatchResult {757 for property in properties {758 Self::set_collection_property(collection, sender, property)?;759 }760761 Ok(())762 }763764 pub fn set_property_permission(765 collection: &CollectionHandle<T>,766 sender: &T::CrossAccountId,767 property_permission: PropertyKeyPermission,768 ) -> DispatchResult {769 collection.check_is_owner_or_admin(sender)?;770771 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);772 let current_permission = all_permissions.get(&property_permission.key);773 if matches![774 current_permission,775 Some(PropertyPermission::AdminConst | PropertyPermission::ItemOwnerConst)776 ] {777 return Err(<Error<T>>::NoPermission.into());778 }779780 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {781 let property_permission = property_permission.clone();782 permissions.try_insert(property_permission.key, property_permission.permission)783 })784 .map_err(|_| PropertiesError::PropertyLimitReached)?;785786 Self::deposit_event(Event::PropertyPermissionSet(787 collection.id,788 property_permission,789 ));790791 Ok(())792 }793794 pub fn set_property_permissions(795 collection: &CollectionHandle<T>,796 sender: &T::CrossAccountId,797 property_permissions: Vec<PropertyKeyPermission>,798 ) -> DispatchResult {799 for prop_pemission in property_permissions {800 Self::set_property_permission(collection, sender, prop_pemission)?;801 }802803 Ok(())804 }805806 fn set_field_raw(807 collection_id: CollectionId,808 field: CollectionField,809 value: Vec<u8>,810 ) -> DispatchResult {811 if !value.is_empty() {812 <CollectionData<T>>::insert(813 (collection_id, field),814 BoundedVec::try_from(value).map_err(|_| <Error<T>>::CollectionFieldSizeExceeded)?,815 )816 } else {817 <CollectionData<T>>::remove((collection_id, field));818 }819 Ok(())820 }821822 pub fn set_field(823 collection: &CollectionHandle<T>,824 sender: &T::CrossAccountId,825 field: CollectionField,826 value: Vec<u8>,827 ) -> DispatchResult {828 collection.check_is_owner_or_admin(sender)?;829830 // =========831832 Self::set_field_raw(collection.id, field, value)833 }834835 pub fn toggle_allowlist(836 collection: &CollectionHandle<T>,837 sender: &T::CrossAccountId,838 user: &T::CrossAccountId,839 allowed: bool,840 ) -> DispatchResult {841 collection.check_is_owner_or_admin(sender)?;842843 // =========844845 if allowed {846 <Allowlist<T>>::insert((collection.id, user), true);847 } else {848 <Allowlist<T>>::remove((collection.id, user));849 }850851 Ok(())852 }853854 pub fn toggle_admin(855 collection: &CollectionHandle<T>,856 sender: &T::CrossAccountId,857 user: &T::CrossAccountId,858 admin: bool,859 ) -> DispatchResult {860 collection.check_is_owner_or_admin(sender)?;861862 let was_admin = <IsAdmin<T>>::get((collection.id, user));863 if was_admin == admin {864 return Ok(());865 }866 let amount = <AdminAmount<T>>::get(collection.id);867868 if admin {869 let amount = amount870 .checked_add(1)871 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;872 ensure!(873 amount <= Self::collection_admins_limit(),874 <Error<T>>::CollectionAdminCountExceeded,875 );876877 // =========878879 <AdminAmount<T>>::insert(collection.id, amount);880 <IsAdmin<T>>::insert((collection.id, user), true);881 } else {882 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));883 <IsAdmin<T>>::remove((collection.id, user));884 }885886 Ok(())887 }888889 pub fn clamp_limits(890 mode: CollectionMode,891 old_limit: &CollectionLimits,892 mut new_limit: CollectionLimits,893 ) -> Result<CollectionLimits, DispatchError> {894 macro_rules! limit_default {895 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{896 $(897 if let Some($new) = $new.$field {898 let $old = $old.$field($($arg)?);899 let _ = $new;900 let _ = $old;901 $check902 } else {903 $new.$field = $old.$field904 }905 )*906 }};907 }908909 limit_default!(old_limit, new_limit,910 account_token_ownership_limit => ensure!(911 new_limit <= MAX_TOKEN_OWNERSHIP,912 <Error<T>>::CollectionLimitBoundsExceeded,913 ),914 sponsor_transfer_timeout(match mode {915 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,916 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,917 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,918 }) => ensure!(919 new_limit <= MAX_SPONSOR_TIMEOUT,920 <Error<T>>::CollectionLimitBoundsExceeded,921 ),922 sponsored_data_size => ensure!(923 new_limit <= CUSTOM_DATA_LIMIT,924 <Error<T>>::CollectionLimitBoundsExceeded,925 ),926 token_limit => ensure!(927 old_limit >= new_limit && new_limit > 0,928 <Error<T>>::CollectionTokenLimitExceeded929 ),930 owner_can_transfer => ensure!(931 old_limit || !new_limit,932 <Error<T>>::OwnerPermissionsCantBeReverted,933 ),934 owner_can_destroy => ensure!(935 old_limit || !new_limit,936 <Error<T>>::OwnerPermissionsCantBeReverted,937 ),938 sponsored_data_rate_limit => {},939 transfers_enabled => {},940 );941 Ok(new_limit)942 }943}944945#[macro_export]946macro_rules! unsupported {947 () => {948 Err(<Error<T>>::UnsupportedOperation.into())949 };950}951952/// Worst cases953pub trait CommonWeightInfo<CrossAccountId> {954 fn create_item() -> Weight;955 fn create_multiple_items(amount: u32) -> Weight;956 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;957 fn burn_item() -> Weight;958 fn set_collection_properties(amount: u32) -> Weight;959 fn set_token_properties(amount: u32) -> Weight;960 fn delete_token_properties(amount: u32) -> Weight;961 fn set_property_permissions(amount: u32) -> Weight;962 fn transfer() -> Weight;963 fn approve() -> Weight;964 fn transfer_from() -> Weight;965 fn burn_from() -> Weight;966 fn set_variable_metadata(bytes: u32) -> Weight;967}968969pub trait CommonCollectionOperations<T: Config> {970 fn create_item(971 &self,972 sender: T::CrossAccountId,973 to: T::CrossAccountId,974 data: CreateItemData,975 nesting_budget: &dyn Budget,976 ) -> DispatchResultWithPostInfo;977 fn create_multiple_items(978 &self,979 sender: T::CrossAccountId,980 to: T::CrossAccountId,981 data: Vec<CreateItemData>,982 nesting_budget: &dyn Budget,983 ) -> DispatchResultWithPostInfo;984 fn create_multiple_items_ex(985 &self,986 sender: T::CrossAccountId,987 data: CreateItemExData<T::CrossAccountId>,988 nesting_budget: &dyn Budget,989 ) -> DispatchResultWithPostInfo;990 fn burn_item(991 &self,992 sender: T::CrossAccountId,993 token: TokenId,994 amount: u128,995 ) -> DispatchResultWithPostInfo;996 fn set_collection_properties(997 &self,998 sender: T::CrossAccountId,999 properties: Vec<Property>,1000 ) -> DispatchResultWithPostInfo;1001 fn set_token_properties(1002 &self,1003 sender: T::CrossAccountId,1004 token_id: TokenId,1005 property: Vec<Property>,1006 ) -> DispatchResultWithPostInfo;1007 fn delete_token_properties(1008 &self,1009 sender: T::CrossAccountId,1010 token_id: TokenId,1011 property_keys: Vec<PropertyKey>,1012 ) -> DispatchResultWithPostInfo;1013 fn set_property_permissions(1014 &self,1015 sender: &T::CrossAccountId,1016 property_permissions: Vec<PropertyKeyPermission>,1017 ) -> DispatchResultWithPostInfo;1018 fn transfer(1019 &self,1020 sender: T::CrossAccountId,1021 to: T::CrossAccountId,1022 token: TokenId,1023 amount: u128,1024 nesting_budget: &dyn Budget,1025 ) -> DispatchResultWithPostInfo;1026 fn approve(1027 &self,1028 sender: T::CrossAccountId,1029 spender: T::CrossAccountId,1030 token: TokenId,1031 amount: u128,1032 ) -> DispatchResultWithPostInfo;1033 fn transfer_from(1034 &self,1035 sender: T::CrossAccountId,1036 from: T::CrossAccountId,1037 to: T::CrossAccountId,1038 token: TokenId,1039 amount: u128,1040 nesting_budget: &dyn Budget,1041 ) -> DispatchResultWithPostInfo;1042 fn burn_from(1043 &self,1044 sender: T::CrossAccountId,1045 from: T::CrossAccountId,1046 token: TokenId,1047 amount: u128,1048 nesting_budget: &dyn Budget,1049 ) -> DispatchResultWithPostInfo;10501051 fn set_variable_metadata(1052 &self,1053 sender: T::CrossAccountId,1054 token: TokenId,1055 data: BoundedVec<u8, CustomDataLimit>,1056 ) -> DispatchResultWithPostInfo;10571058 fn check_nesting(1059 &self,1060 sender: T::CrossAccountId,1061 from: (CollectionId, TokenId),1062 under: TokenId,1063 budget: &dyn Budget,1064 ) -> DispatchResult;10651066 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;1067 fn collection_tokens(&self) -> Vec<TokenId>;1068 fn token_exists(&self, token: TokenId) -> bool;1069 fn last_token_id(&self) -> TokenId;10701071 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1072 fn const_metadata(&self, token: TokenId) -> Vec<u8>;1073 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;10741075 /// Amount of unique collection tokens1076 fn total_supply(&self) -> u32;1077 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1078 fn account_balance(&self, account: T::CrossAccountId) -> u32;1079 /// Amount of specific token account have (Applicable to fungible/refungible)1080 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;1081 fn allowance(1082 &self,1083 sender: T::CrossAccountId,1084 spender: T::CrossAccountId,1085 token: TokenId,1086 ) -> u128;1087}10881089// Flexible enough for implementing CommonCollectionOperations1090pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {1091 let post_info = PostDispatchInfo {1092 actual_weight: Some(weight),1093 pays_fee: Pays::Yes,1094 };1095 match res {1096 Ok(()) => Ok(post_info),1097 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),1098 }1099}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use core::ops::{Deref, DerefMut};20use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};21use sp_std::{vec::Vec, collections::btree_map::BTreeMap};22use pallet_evm::account::CrossAccountId;23use frame_support::{24 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},25 ensure, fail,26 traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},27 BoundedVec,28 weights::Pays,29};30use pallet_evm::GasWeightMapping;31use up_data_structs::{32 COLLECTION_NUMBER_LIMIT, Collection, RpcCollection, CollectionId, CreateItemData,33 MAX_TOKEN_PREFIX_LENGTH, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission, TokenId,34 CollectionStats, MAX_TOKEN_OWNERSHIP, CollectionMode, NFT_SPONSOR_TRANSFER_TIMEOUT,35 FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT,36 CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,37 CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,38 PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,39 PropertiesError, PropertyKeyPermission,40};41pub use pallet::*;42use sp_core::H160;43use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};44#[cfg(feature = "runtime-benchmarks")]45pub mod benchmarking;46pub mod dispatch;47pub mod erc;48pub mod eth;4950#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]51pub struct CollectionHandle<T: Config> {52 pub id: CollectionId,53 collection: Collection<T::AccountId>,54 pub recorder: SubstrateRecorder<T>,55}56impl<T: Config> WithRecorder<T> for CollectionHandle<T> {57 fn recorder(&self) -> &SubstrateRecorder<T> {58 &self.recorder59 }60 fn into_recorder(self) -> SubstrateRecorder<T> {61 self.recorder62 }63}64impl<T: Config> CollectionHandle<T> {65 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {66 <CollectionById<T>>::get(id).map(|collection| Self {67 id,68 collection,69 recorder: SubstrateRecorder::new(eth::collection_id_to_address(id), gas_limit),70 })71 }72 pub fn new(id: CollectionId) -> Option<Self> {73 Self::new_with_gas_limit(id, u64::MAX)74 }75 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {76 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)77 }78 pub fn log_mirrored(&self, log: impl evm_coder::ToLog) {79 self.recorder.log_mirrored(log)80 }81 pub fn log_direct(&self, log: impl evm_coder::ToLog) {82 self.recorder.log_direct(log)83 }84 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {85 self.recorder86 .consume_gas(T::GasWeightMapping::weight_to_gas(87 <T as frame_system::Config>::DbWeight::get()88 .read89 .saturating_mul(reads),90 ))91 }92 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {93 self.recorder94 .consume_gas(T::GasWeightMapping::weight_to_gas(95 <T as frame_system::Config>::DbWeight::get()96 .write97 .saturating_mul(writes),98 ))99 }100 pub fn submit_logs(self) {101 self.recorder.submit_logs()102 }103 pub fn save(self) -> DispatchResult {104 self.recorder.submit_logs();105 <CollectionById<T>>::insert(self.id, self.collection);106 Ok(())107 }108}109impl<T: Config> Deref for CollectionHandle<T> {110 type Target = Collection<T::AccountId>;111112 fn deref(&self) -> &Self::Target {113 &self.collection114 }115}116117impl<T: Config> DerefMut for CollectionHandle<T> {118 fn deref_mut(&mut self) -> &mut Self::Target {119 &mut self.collection120 }121}122123impl<T: Config> CollectionHandle<T> {124 pub fn check_is_owner(&self, subject: &T::CrossAccountId) -> DispatchResult {125 ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);126 Ok(())127 }128 pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {129 *subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))130 }131 pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {132 ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);133 Ok(())134 }135 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {136 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)137 }138 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {139 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)140 }141 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {142 ensure!(143 <Allowlist<T>>::get((self.id, user)),144 <Error<T>>::AddressNotInAllowlist145 );146 Ok(())147 }148149 pub fn check_can_update_meta(150 &self,151 subject: &T::CrossAccountId,152 item_owner: &T::CrossAccountId,153 ) -> DispatchResult {154 match self.meta_update_permission {155 MetaUpdatePermission::ItemOwner => {156 ensure!(subject == item_owner, <Error<T>>::NoPermission);157 Ok(())158 }159 MetaUpdatePermission::Admin => self.check_is_owner_or_admin(subject),160 MetaUpdatePermission::None => fail!(<Error<T>>::NoPermission),161 }162 }163}164165#[frame_support::pallet]166pub mod pallet {167 use super::*;168 use pallet_evm::account;169 use dispatch::CollectionDispatch;170 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};171 use frame_system::pallet_prelude::*;172 use frame_support::traits::Currency;173 use up_data_structs::{TokenId, mapping::TokenAddressMapping};174 use scale_info::TypeInfo;175176 #[pallet::config]177 pub trait Config:178 frame_system::Config + pallet_evm_coder_substrate::Config + TypeInfo + account::Config179 {180 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;181182 type Currency: Currency<Self::AccountId>;183184 #[pallet::constant]185 type CollectionCreationPrice: Get<186 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,187 >;188 type CollectionDispatch: CollectionDispatch<Self>;189190 type TreasuryAccountId: Get<Self::AccountId>;191192 type EvmTokenAddressMapping: TokenAddressMapping<H160>;193 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;194 }195196 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);197198 #[pallet::pallet]199 #[pallet::storage_version(STORAGE_VERSION)]200 #[pallet::generate_store(pub(super) trait Store)]201 pub struct Pallet<T>(_);202203 #[pallet::extra_constants]204 impl<T: Config> Pallet<T> {205 pub fn collection_admins_limit() -> u32 {206 COLLECTION_ADMINS_LIMIT207 }208 }209210 #[pallet::event]211 #[pallet::generate_deposit(pub fn deposit_event)]212 pub enum Event<T: Config> {213 /// New collection was created214 ///215 /// # Arguments216 ///217 /// * collection_id: Globally unique identifier of newly created collection.218 ///219 /// * mode: [CollectionMode] converted into u8.220 ///221 /// * account_id: Collection owner.222 CollectionCreated(CollectionId, u8, T::AccountId),223224 /// New collection was destroyed225 ///226 /// # Arguments227 ///228 /// * collection_id: Globally unique identifier of collection.229 CollectionDestroyed(CollectionId),230231 /// New item was created.232 ///233 /// # Arguments234 ///235 /// * collection_id: Id of the collection where item was created.236 ///237 /// * item_id: Id of an item. Unique within the collection.238 ///239 /// * recipient: Owner of newly created item240 ///241 /// * amount: Always 1 for NFT242 ItemCreated(CollectionId, TokenId, T::CrossAccountId, u128),243244 /// Collection item was burned.245 ///246 /// # Arguments247 ///248 /// * collection_id.249 ///250 /// * item_id: Identifier of burned NFT.251 ///252 /// * owner: which user has destroyed its tokens253 ///254 /// * amount: Always 1 for NFT255 ItemDestroyed(CollectionId, TokenId, T::CrossAccountId, u128),256257 /// Item was transferred258 ///259 /// * collection_id: Id of collection to which item is belong260 ///261 /// * item_id: Id of an item262 ///263 /// * sender: Original owner of item264 ///265 /// * recipient: New owner of item266 ///267 /// * amount: Always 1 for NFT268 Transfer(269 CollectionId,270 TokenId,271 T::CrossAccountId,272 T::CrossAccountId,273 u128,274 ),275276 /// * collection_id277 ///278 /// * item_id279 ///280 /// * sender281 ///282 /// * spender283 ///284 /// * amount285 Approved(286 CollectionId,287 TokenId,288 T::CrossAccountId,289 T::CrossAccountId,290 u128,291 ),292293 CollectionPropertySet(CollectionId, Property),294295 CollectionPropertyDeleted(CollectionId, PropertyKey),296297 TokenPropertySet(CollectionId, TokenId, Property),298299 TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),300301 PropertyPermissionSet(CollectionId, PropertyKeyPermission),302 }303304 #[pallet::error]305 pub enum Error<T> {306 /// This collection does not exist.307 CollectionNotFound,308 /// Sender parameter and item owner must be equal.309 MustBeTokenOwner,310 /// No permission to perform action311 NoPermission,312 /// Collection is not in mint mode.313 PublicMintingNotAllowed,314 /// Address is not in allow list.315 AddressNotInAllowlist,316317 /// Collection name can not be longer than 63 char.318 CollectionNameLimitExceeded,319 /// Collection description can not be longer than 255 char.320 CollectionDescriptionLimitExceeded,321 /// Token prefix can not be longer than 15 char.322 CollectionTokenPrefixLimitExceeded,323 /// Total collections bound exceeded.324 TotalCollectionsLimitExceeded,325 /// variable_data exceeded data limit.326 TokenVariableDataLimitExceeded,327 /// Exceeded max admin count328 CollectionAdminCountExceeded,329 /// Collection limit bounds per collection exceeded330 CollectionLimitBoundsExceeded,331 /// Tried to enable permissions which are only permitted to be disabled332 OwnerPermissionsCantBeReverted,333 /// Collection settings not allowing items transferring334 TransferNotAllowed,335 /// Account token limit exceeded per collection336 AccountTokenLimitExceeded,337 /// Collection token limit exceeded338 CollectionTokenLimitExceeded,339 /// Metadata flag frozen340 MetadataFlagFrozen,341342 /// Item not exists.343 TokenNotFound,344 /// Item balance not enough.345 TokenValueTooLow,346 /// Requested value more than approved.347 ApprovedValueTooLow,348 /// Tried to approve more than owned349 CantApproveMoreThanOwned,350351 /// Can't transfer tokens to ethereum zero address352 AddressIsZero,353 /// Target collection doesn't supports this operation354 UnsupportedOperation,355356 /// Not sufficient founds to perform action357 NotSufficientFounds,358359 /// Collection has nesting disabled360 NestingIsDisabled,361 /// Only owner may nest tokens under this collection362 OnlyOwnerAllowedToNest,363 /// Only tokens from specific collections may nest tokens under this364 SourceCollectionIsNotAllowedToNest,365366 /// Tried to store more data than allowed in collection field367 CollectionFieldSizeExceeded,368 }369370 #[pallet::storage]371 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;372 #[pallet::storage]373 pub type DestroyedCollectionCount<T> =374 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;375376 /// Collection info377 #[pallet::storage]378 pub type CollectionById<T> = StorageMap<379 Hasher = Blake2_128Concat,380 Key = CollectionId,381 Value = Collection<<T as frame_system::Config>::AccountId>,382 QueryKind = OptionQuery,383 >;384385 /// Collection properties386 #[pallet::storage]387 pub type CollectionProperties<T> = StorageMap<388 Hasher = Blake2_128Concat,389 Key = CollectionId,390 Value = Properties,391 QueryKind = ValueQuery,392 OnEmpty = up_data_structs::CollectionProperties,393 >;394395 #[pallet::storage]396 #[pallet::getter(fn property_permission)]397 pub type CollectionPropertyPermissions<T> = StorageMap<398 Hasher = Blake2_128Concat,399 Key = CollectionId,400 Value = PropertiesPermissionMap,401 QueryKind = ValueQuery,402 >;403404 /// Large variable-size collection fields are extracted here405 #[pallet::storage]406 pub type CollectionData<T> = StorageNMap<407 Key = (408 Key<Twox64Concat, CollectionId>,409 Key<Twox64Concat, CollectionField>,410 ),411 Value = BoundedVec<u8, ConstU32<COLLECTION_FIELD_LIMIT>>,412 QueryKind = ValueQuery,413 >;414415 #[pallet::storage]416 pub type AdminAmount<T> = StorageMap<417 Hasher = Blake2_128Concat,418 Key = CollectionId,419 Value = u32,420 QueryKind = ValueQuery,421 >;422423 /// List of collection admins424 #[pallet::storage]425 pub type IsAdmin<T: Config> = StorageNMap<426 Key = (427 Key<Blake2_128Concat, CollectionId>,428 Key<Blake2_128Concat, T::CrossAccountId>,429 ),430 Value = bool,431 QueryKind = ValueQuery,432 >;433434 /// Allowlisted collection users435 #[pallet::storage]436 pub type Allowlist<T: Config> = StorageNMap<437 Key = (438 Key<Blake2_128Concat, CollectionId>,439 Key<Blake2_128Concat, T::CrossAccountId>,440 ),441 Value = bool,442 QueryKind = ValueQuery,443 >;444445 /// Not used by code, exists only to provide some types to metadata446 #[pallet::storage]447 pub type DummyStorageValue<T: Config> = StorageValue<448 Value = (449 CollectionStats,450 CollectionId,451 TokenId,452 PhantomType<RpcCollection<T::AccountId>>,453 ),454 QueryKind = OptionQuery,455 >;456457 #[pallet::hooks]458 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {459 fn on_runtime_upgrade() -> Weight {460 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {461 use up_data_structs::{CollectionVersion1, CollectionVersion2};462 <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {463 Self::set_field_raw(464 id,465 CollectionField::OffchainSchema,466 v.offchain_schema.clone().into_inner(),467 )468 .expect("data has lower bounds than field");469 Self::set_field_raw(470 id,471 CollectionField::VariableOnChainSchema,472 v.variable_on_chain_schema.clone().into_inner(),473 )474 .expect("data has lower bounds than field");475 Self::set_field_raw(476 id,477 CollectionField::ConstOnChainSchema,478 v.const_on_chain_schema.clone().into_inner(),479 )480 .expect("data has lower bounds than field");481482 Some(CollectionVersion2::from(v))483 });484 }485486 0487 }488 }489}490491impl<T: Config> Pallet<T> {492 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens493 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {494 ensure!(495 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,496 <Error<T>>::AddressIsZero497 );498 Ok(())499 }500 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {501 <IsAdmin<T>>::iter_prefix((collection,))502 .map(|(a, _)| a)503 .collect()504 }505 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {506 <Allowlist<T>>::iter_prefix((collection,))507 .map(|(a, _)| a)508 .collect()509 }510 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {511 <Allowlist<T>>::get((collection, user))512 }513 pub fn collection_stats() -> CollectionStats {514 let created = <CreatedCollectionCount<T>>::get();515 let destroyed = <DestroyedCollectionCount<T>>::get();516 CollectionStats {517 created: created.0,518 destroyed: destroyed.0,519 alive: created.0 - destroyed.0,520 }521 }522523 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {524 let collection = <CollectionById<T>>::get(collection);525 if collection.is_none() {526 return None;527 }528529 let collection = collection.unwrap();530 let limits = collection.limits;531 let effective_limits = CollectionLimits {532 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),533 sponsored_data_size: Some(limits.sponsored_data_size()),534 sponsored_data_rate_limit: Some(535 limits536 .sponsored_data_rate_limit537 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),538 ),539 token_limit: Some(limits.token_limit()),540 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(541 match collection.mode {542 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,543 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,544 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,545 },546 )),547 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),548 owner_can_transfer: Some(limits.owner_can_transfer()),549 owner_can_destroy: Some(limits.owner_can_destroy()),550 transfers_enabled: Some(limits.transfers_enabled()),551 nesting_rule: Some(limits.nesting_rule().clone()),552 };553554 Some(effective_limits)555 }556557 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {558 let Collection {559 name,560 description,561 owner,562 mode,563 access,564 token_prefix,565 mint_mode,566 schema_version,567 sponsorship,568 limits,569 meta_update_permission,570 ..571 } = <CollectionById<T>>::get(collection)?;572 Some(RpcCollection {573 name: name.into_inner(),574 description: description.into_inner(),575 owner,576 mode,577 access,578 token_prefix: token_prefix.into_inner(),579 mint_mode,580 schema_version,581 sponsorship,582 limits,583 meta_update_permission,584 offchain_schema: <CollectionData<T>>::get((585 collection,586 CollectionField::OffchainSchema,587 ))588 .into_inner(),589 const_on_chain_schema: <CollectionData<T>>::get((590 collection,591 CollectionField::ConstOnChainSchema,592 ))593 .into_inner(),594 variable_on_chain_schema: <CollectionData<T>>::get((595 collection,596 CollectionField::VariableOnChainSchema,597 ))598 .into_inner(),599 })600 }601}602603impl<T: Config> Pallet<T> {604 pub fn init_collection(605 owner: T::AccountId,606 data: CreateCollectionData<T::AccountId>,607 ) -> Result<CollectionId, DispatchError> {608 {609 ensure!(610 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,611 Error::<T>::CollectionTokenPrefixLimitExceeded612 );613 }614615 let created_count = <CreatedCollectionCount<T>>::get()616 .0617 .checked_add(1)618 .ok_or(ArithmeticError::Overflow)?;619 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;620 let id = CollectionId(created_count);621622 // bound Total number of collections623 ensure!(624 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,625 <Error<T>>::TotalCollectionsLimitExceeded626 );627628 // =========629630 let collection = Collection {631 owner: owner.clone(),632 name: data.name,633 mode: data.mode.clone(),634 mint_mode: false,635 access: data.access.unwrap_or_default(),636 description: data.description,637 token_prefix: data.token_prefix,638 schema_version: data.schema_version.unwrap_or_default(),639 sponsorship: data640 .pending_sponsor641 .map(SponsorshipState::Unconfirmed)642 .unwrap_or_default(),643 limits: data644 .limits645 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))646 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,647 meta_update_permission: data.meta_update_permission.unwrap_or_default(),648 // token_property_permissions: data.token_property_permissions.unwrap_or_default(),649 // properties: Properties::from_collection_props_vec(data.properties)?650 };651652 CollectionProperties::<T>::insert(653 id,654 Properties::from_collection_props_vec(data.properties)?,655 );656657 let token_props_permissions: PropertiesPermissionMap = data658 .token_property_permissions659 .into_iter()660 .map(|property| (property.key, property.permission))661 .collect::<BTreeMap<_, _>>()662 .try_into()663 .map_err(|_| PropertiesError::PropertyLimitReached)?;664665 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);666667 // Take a (non-refundable) deposit of collection creation668 {669 let mut imbalance =670 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();671 imbalance.subsume(672 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(673 &T::TreasuryAccountId::get(),674 T::CollectionCreationPrice::get(),675 ),676 );677 <T as Config>::Currency::settle(678 &owner,679 imbalance,680 WithdrawReasons::TRANSFER,681 ExistenceRequirement::KeepAlive,682 )683 .map_err(|_| Error::<T>::NotSufficientFounds)?;684 }685686 <CreatedCollectionCount<T>>::put(created_count);687 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));688 <CollectionById<T>>::insert(id, collection);689 Self::set_field_raw(690 id,691 CollectionField::OffchainSchema,692 data.offchain_schema.into_inner(),693 )694 .expect("data has lower bounds than field");695 Self::set_field_raw(696 id,697 CollectionField::VariableOnChainSchema,698 data.variable_on_chain_schema.into_inner(),699 )700 .expect("data has lower bounds than field");701 Self::set_field_raw(702 id,703 CollectionField::ConstOnChainSchema,704 data.const_on_chain_schema.into_inner(),705 )706 .expect("data has lower bounds than field");707 Ok(id)708 }709710 pub fn destroy_collection(711 collection: CollectionHandle<T>,712 sender: &T::CrossAccountId,713 ) -> DispatchResult {714 ensure!(715 collection.limits.owner_can_destroy(),716 <Error<T>>::NoPermission,717 );718 collection.check_is_owner(sender)?;719720 let destroyed_collections = <DestroyedCollectionCount<T>>::get()721 .0722 .checked_add(1)723 .ok_or(ArithmeticError::Overflow)?;724725 // =========726727 <DestroyedCollectionCount<T>>::put(destroyed_collections);728 <CollectionById<T>>::remove(collection.id);729 <CollectionData<T>>::remove_prefix((collection.id,), None);730 <AdminAmount<T>>::remove(collection.id);731 <IsAdmin<T>>::remove_prefix((collection.id,), None);732 <Allowlist<T>>::remove_prefix((collection.id,), None);733734 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));735 Ok(())736 }737738 pub fn set_collection_property(739 collection: &CollectionHandle<T>,740 sender: &T::CrossAccountId,741 property: Property,742 ) -> DispatchResult {743 collection.check_is_owner_or_admin(sender)?;744745 CollectionProperties::<T>::try_mutate(collection.id, |properties| {746 properties.try_set_property(property.clone())747 })?;748749 Self::deposit_event(Event::CollectionPropertySet(collection.id, property));750751 Ok(())752 }753754 pub fn set_collection_properties(755 collection: &CollectionHandle<T>,756 sender: &T::CrossAccountId,757 properties: Vec<Property>,758 ) -> DispatchResult {759 for property in properties {760 Self::set_collection_property(collection, sender, property)?;761 }762763 Ok(())764 }765766 pub fn delete_collection_property(767 collection: &CollectionHandle<T>,768 sender: &T::CrossAccountId,769 property_key: PropertyKey,770 ) -> DispatchResult {771 collection.check_is_owner_or_admin(sender)?;772773 CollectionProperties::<T>::mutate(collection.id, |properties| {774 properties.remove_property(&property_key);775 });776777 Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, property_key));778779 Ok(())780 }781782 pub fn delete_collection_properties(783 collection: &CollectionHandle<T>,784 sender: &T::CrossAccountId,785 property_keys: Vec<PropertyKey>,786 ) -> DispatchResult {787 for key in property_keys {788 Self::delete_collection_property(collection, sender, key)?;789 }790791 Ok(())792 }793794 pub fn set_property_permission(795 collection: &CollectionHandle<T>,796 sender: &T::CrossAccountId,797 property_permission: PropertyKeyPermission,798 ) -> DispatchResult {799 collection.check_is_owner_or_admin(sender)?;800801 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);802 let current_permission = all_permissions.get(&property_permission.key);803 if matches![804 current_permission,805 Some(PropertyPermission::AdminConst | PropertyPermission::ItemOwnerConst)806 ] {807 return Err(<Error<T>>::NoPermission.into());808 }809810 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {811 let property_permission = property_permission.clone();812 permissions.try_insert(property_permission.key, property_permission.permission)813 })814 .map_err(|_| PropertiesError::PropertyLimitReached)?;815816 Self::deposit_event(Event::PropertyPermissionSet(817 collection.id,818 property_permission,819 ));820821 Ok(())822 }823824 pub fn set_property_permissions(825 collection: &CollectionHandle<T>,826 sender: &T::CrossAccountId,827 property_permissions: Vec<PropertyKeyPermission>,828 ) -> DispatchResult {829 for prop_pemission in property_permissions {830 Self::set_property_permission(collection, sender, prop_pemission)?;831 }832833 Ok(())834 }835836 fn set_field_raw(837 collection_id: CollectionId,838 field: CollectionField,839 value: Vec<u8>,840 ) -> DispatchResult {841 if !value.is_empty() {842 <CollectionData<T>>::insert(843 (collection_id, field),844 BoundedVec::try_from(value).map_err(|_| <Error<T>>::CollectionFieldSizeExceeded)?,845 )846 } else {847 <CollectionData<T>>::remove((collection_id, field));848 }849 Ok(())850 }851852 pub fn set_field(853 collection: &CollectionHandle<T>,854 sender: &T::CrossAccountId,855 field: CollectionField,856 value: Vec<u8>,857 ) -> DispatchResult {858 collection.check_is_owner_or_admin(sender)?;859860 // =========861862 Self::set_field_raw(collection.id, field, value)863 }864865 pub fn toggle_allowlist(866 collection: &CollectionHandle<T>,867 sender: &T::CrossAccountId,868 user: &T::CrossAccountId,869 allowed: bool,870 ) -> DispatchResult {871 collection.check_is_owner_or_admin(sender)?;872873 // =========874875 if allowed {876 <Allowlist<T>>::insert((collection.id, user), true);877 } else {878 <Allowlist<T>>::remove((collection.id, user));879 }880881 Ok(())882 }883884 pub fn toggle_admin(885 collection: &CollectionHandle<T>,886 sender: &T::CrossAccountId,887 user: &T::CrossAccountId,888 admin: bool,889 ) -> DispatchResult {890 collection.check_is_owner_or_admin(sender)?;891892 let was_admin = <IsAdmin<T>>::get((collection.id, user));893 if was_admin == admin {894 return Ok(());895 }896 let amount = <AdminAmount<T>>::get(collection.id);897898 if admin {899 let amount = amount900 .checked_add(1)901 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;902 ensure!(903 amount <= Self::collection_admins_limit(),904 <Error<T>>::CollectionAdminCountExceeded,905 );906907 // =========908909 <AdminAmount<T>>::insert(collection.id, amount);910 <IsAdmin<T>>::insert((collection.id, user), true);911 } else {912 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));913 <IsAdmin<T>>::remove((collection.id, user));914 }915916 Ok(())917 }918919 pub fn clamp_limits(920 mode: CollectionMode,921 old_limit: &CollectionLimits,922 mut new_limit: CollectionLimits,923 ) -> Result<CollectionLimits, DispatchError> {924 macro_rules! limit_default {925 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{926 $(927 if let Some($new) = $new.$field {928 let $old = $old.$field($($arg)?);929 let _ = $new;930 let _ = $old;931 $check932 } else {933 $new.$field = $old.$field934 }935 )*936 }};937 }938939 limit_default!(old_limit, new_limit,940 account_token_ownership_limit => ensure!(941 new_limit <= MAX_TOKEN_OWNERSHIP,942 <Error<T>>::CollectionLimitBoundsExceeded,943 ),944 sponsor_transfer_timeout(match mode {945 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,946 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,947 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,948 }) => ensure!(949 new_limit <= MAX_SPONSOR_TIMEOUT,950 <Error<T>>::CollectionLimitBoundsExceeded,951 ),952 sponsored_data_size => ensure!(953 new_limit <= CUSTOM_DATA_LIMIT,954 <Error<T>>::CollectionLimitBoundsExceeded,955 ),956 token_limit => ensure!(957 old_limit >= new_limit && new_limit > 0,958 <Error<T>>::CollectionTokenLimitExceeded959 ),960 owner_can_transfer => ensure!(961 old_limit || !new_limit,962 <Error<T>>::OwnerPermissionsCantBeReverted,963 ),964 owner_can_destroy => ensure!(965 old_limit || !new_limit,966 <Error<T>>::OwnerPermissionsCantBeReverted,967 ),968 sponsored_data_rate_limit => {},969 transfers_enabled => {},970 );971 Ok(new_limit)972 }973}974975#[macro_export]976macro_rules! unsupported {977 () => {978 Err(<Error<T>>::UnsupportedOperation.into())979 };980}981982/// Worst cases983pub trait CommonWeightInfo<CrossAccountId> {984 fn create_item() -> Weight;985 fn create_multiple_items(amount: u32) -> Weight;986 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;987 fn burn_item() -> Weight;988 fn set_collection_properties(amount: u32) -> Weight;989 fn delete_collection_properties(amount: u32) -> Weight;990 fn set_token_properties(amount: u32) -> Weight;991 fn delete_token_properties(amount: u32) -> Weight;992 fn set_property_permissions(amount: u32) -> Weight;993 fn transfer() -> Weight;994 fn approve() -> Weight;995 fn transfer_from() -> Weight;996 fn burn_from() -> Weight;997 fn set_variable_metadata(bytes: u32) -> Weight;998}9991000pub trait CommonCollectionOperations<T: Config> {1001 fn create_item(1002 &self,1003 sender: T::CrossAccountId,1004 to: T::CrossAccountId,1005 data: CreateItemData,1006 nesting_budget: &dyn Budget,1007 ) -> DispatchResultWithPostInfo;1008 fn create_multiple_items(1009 &self,1010 sender: T::CrossAccountId,1011 to: T::CrossAccountId,1012 data: Vec<CreateItemData>,1013 nesting_budget: &dyn Budget,1014 ) -> DispatchResultWithPostInfo;1015 fn create_multiple_items_ex(1016 &self,1017 sender: T::CrossAccountId,1018 data: CreateItemExData<T::CrossAccountId>,1019 nesting_budget: &dyn Budget,1020 ) -> DispatchResultWithPostInfo;1021 fn burn_item(1022 &self,1023 sender: T::CrossAccountId,1024 token: TokenId,1025 amount: u128,1026 ) -> DispatchResultWithPostInfo;1027 fn set_collection_properties(1028 &self,1029 sender: T::CrossAccountId,1030 properties: Vec<Property>,1031 ) -> DispatchResultWithPostInfo;1032 fn delete_collection_properties(1033 &self,1034 sender: &T::CrossAccountId,1035 property_keys: Vec<PropertyKey>,1036 ) -> DispatchResultWithPostInfo;1037 fn set_token_properties(1038 &self,1039 sender: T::CrossAccountId,1040 token_id: TokenId,1041 property: Vec<Property>,1042 ) -> DispatchResultWithPostInfo;1043 fn delete_token_properties(1044 &self,1045 sender: T::CrossAccountId,1046 token_id: TokenId,1047 property_keys: Vec<PropertyKey>,1048 ) -> DispatchResultWithPostInfo;1049 fn set_property_permissions(1050 &self,1051 sender: &T::CrossAccountId,1052 property_permissions: Vec<PropertyKeyPermission>,1053 ) -> DispatchResultWithPostInfo;1054 fn transfer(1055 &self,1056 sender: T::CrossAccountId,1057 to: T::CrossAccountId,1058 token: TokenId,1059 amount: u128,1060 nesting_budget: &dyn Budget,1061 ) -> DispatchResultWithPostInfo;1062 fn approve(1063 &self,1064 sender: T::CrossAccountId,1065 spender: T::CrossAccountId,1066 token: TokenId,1067 amount: u128,1068 ) -> DispatchResultWithPostInfo;1069 fn transfer_from(1070 &self,1071 sender: T::CrossAccountId,1072 from: T::CrossAccountId,1073 to: T::CrossAccountId,1074 token: TokenId,1075 amount: u128,1076 nesting_budget: &dyn Budget,1077 ) -> DispatchResultWithPostInfo;1078 fn burn_from(1079 &self,1080 sender: T::CrossAccountId,1081 from: T::CrossAccountId,1082 token: TokenId,1083 amount: u128,1084 nesting_budget: &dyn Budget,1085 ) -> DispatchResultWithPostInfo;10861087 fn set_variable_metadata(1088 &self,1089 sender: T::CrossAccountId,1090 token: TokenId,1091 data: BoundedVec<u8, CustomDataLimit>,1092 ) -> DispatchResultWithPostInfo;10931094 fn check_nesting(1095 &self,1096 sender: T::CrossAccountId,1097 from: (CollectionId, TokenId),1098 under: TokenId,1099 budget: &dyn Budget,1100 ) -> DispatchResult;11011102 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;1103 fn collection_tokens(&self) -> Vec<TokenId>;1104 fn token_exists(&self, token: TokenId) -> bool;1105 fn last_token_id(&self) -> TokenId;11061107 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1108 fn const_metadata(&self, token: TokenId) -> Vec<u8>;1109 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;11101111 /// Amount of unique collection tokens1112 fn total_supply(&self) -> u32;1113 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1114 fn account_balance(&self, account: T::CrossAccountId) -> u32;1115 /// Amount of specific token account have (Applicable to fungible/refungible)1116 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;1117 fn allowance(1118 &self,1119 sender: T::CrossAccountId,1120 spender: T::CrossAccountId,1121 token: TokenId,1122 ) -> u128;1123}11241125// Flexible enough for implementing CommonCollectionOperations1126pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {1127 let post_info = PostDispatchInfo {1128 actual_weight: Some(weight),1129 pays_fee: Pays::Yes,1130 };1131 match res {1132 Ok(()) => Ok(post_info),1133 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),1134 }1135}pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -54,6 +54,10 @@
<SelfWeightOf<T>>::set_collection_properties(amount)
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::delete_collection_properties(amount)
+ }
+
fn set_token_properties(amount: u32) -> Weight {
<SelfWeightOf<T>>::set_token_properties(amount)
}
@@ -249,6 +253,14 @@
fail!(<Error<T>>::PropertiesNotAllowed)
}
+ fn delete_collection_properties(
+ &self,
+ _sender: &T::CrossAccountId,
+ _property_keys: Vec<PropertyKey>,
+ ) -> DispatchResultWithPostInfo {
+ fail!(<Error<T>>::PropertiesNotAllowed)
+ }
+
fn set_token_properties(
&self,
_sender: T::CrossAccountId,
pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -36,6 +36,7 @@
fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
fn set_collection_properties(amount: u32) -> Weight;
+ fn delete_collection_properties(amount: u32) -> Weight;
fn set_token_properties(amount: u32) -> Weight;
fn delete_token_properties(amount: u32) -> Weight;
fn set_property_permissions(amount: u32) -> Weight;
@@ -79,6 +80,11 @@
0
}
+ fn delete_collection_properties(_amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
fn set_token_properties(_amount: u32) -> Weight {
// Error
0
@@ -157,6 +163,11 @@
0
}
+ fn delete_collection_properties(_amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
fn set_token_properties(_amount: u32) -> Weight {
// Error
0
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -55,6 +55,10 @@
<SelfWeightOf<T>>::set_collection_properties(amount)
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::delete_collection_properties(amount)
+ }
+
fn set_token_properties(amount: u32) -> Weight {
<SelfWeightOf<T>>::set_token_properties(amount)
}
@@ -171,6 +175,19 @@
)
}
+ fn delete_collection_properties(
+ &self,
+ sender: &T::CrossAccountId,
+ property_keys: Vec<PropertyKey>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);
+
+ with_weight(
+ <Pallet<T>>::delete_collection_properties(self, &sender, property_keys),
+ weight
+ )
+ }
+
fn set_token_properties(
&self,
sender: T::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -366,6 +366,14 @@
<PalletCommon<T>>::set_collection_properties(collection, sender, properties)
}
+ pub fn delete_collection_properties(
+ collection: &CollectionHandle<T>,
+ sender: &T::CrossAccountId,
+ property_keys: Vec<PropertyKey>,
+ ) -> DispatchResult {
+ <PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)
+ }
+
pub fn set_property_permissions(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
pallets/nonfungible/src/weights.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -37,6 +37,7 @@
fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
fn set_collection_properties(amount: u32) -> Weight;
+ fn delete_collection_properties(amount: u32) -> Weight;
fn set_token_properties(amount: u32) -> Weight;
fn delete_token_properties(amount: u32) -> Weight;
fn set_property_permissions(amount: u32) -> Weight;
@@ -100,6 +101,11 @@
(50_000_000 as Weight).saturating_mul(amount as Weight)
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
fn set_token_properties(amount: u32) -> Weight {
// TODO calculate appropriate weight
(50_000_000 as Weight).saturating_mul(amount as Weight)
@@ -210,6 +216,11 @@
(50_000_000 as Weight).saturating_mul(amount as Weight)
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
fn set_token_properties(amount: u32) -> Weight {
// TODO calculate appropriate weight
(50_000_000 as Weight).saturating_mul(amount as Weight)
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -70,6 +70,10 @@
<SelfWeightOf<T>>::set_collection_properties(amount)
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::delete_collection_properties(amount)
+ }
+
fn set_token_properties(amount: u32) -> Weight {
<SelfWeightOf<T>>::set_token_properties(amount)
}
@@ -268,6 +272,14 @@
fail!(<Error<T>>::PropertiesNotAllowed)
}
+ fn delete_collection_properties(
+ &self,
+ _sender: &T::CrossAccountId,
+ _property_keys: Vec<PropertyKey>,
+ ) -> DispatchResultWithPostInfo {
+ fail!(<Error<T>>::PropertiesNotAllowed)
+ }
+
fn set_token_properties(
&self,
_sender: T::CrossAccountId,
pallets/refungible/src/weights.rsdiffbeforeafterboth--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -39,6 +39,7 @@
fn burn_item_partial() -> Weight;
fn burn_item_fully() -> Weight;
fn set_collection_properties(amount: u32) -> Weight;
+ fn delete_collection_properties(amount: u32) -> Weight;
fn set_token_properties(amount: u32) -> Weight;
fn delete_token_properties(amount: u32) -> Weight;
fn set_property_permissions(amount: u32) -> Weight;
@@ -139,6 +140,11 @@
0
}
+ fn delete_collection_properties(_amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
fn set_token_properties(_amount: u32) -> Weight {
// Error
0
@@ -328,6 +334,11 @@
0
}
+ fn delete_collection_properties(_amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
fn set_token_properties(_amount: u32) -> Weight {
// Error
0
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -708,6 +708,20 @@
dispatch_call::<T, _>(collection_id, |d| d.set_collection_properties(sender, properties))
}
+ #[weight = T::CommonWeightInfo::delete_collection_properties(property_keys.len() as u32)]
+ #[transactional]
+ pub fn delete_collection_properties(
+ origin,
+ collection_id: CollectionId,
+ property_keys: Vec<PropertyKey>,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
+
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+ dispatch_call::<T, _>(collection_id, |d| d.delete_collection_properties(&sender, property_keys))
+ }
+
#[weight = T::CommonWeightInfo::set_token_properties(properties.len() as u32)]
#[transactional]
pub fn set_token_properties(
@@ -723,19 +737,19 @@
dispatch_call::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))
}
- #[weight = T::CommonWeightInfo::delete_token_properties(properties.len() as u32)]
+ #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]
#[transactional]
pub fn delete_token_properties(
origin,
collection_id: CollectionId,
token_id: TokenId,
- properties: Vec<PropertyKey>
+ property_keys: Vec<PropertyKey>
) -> DispatchResultWithPostInfo {
- ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
+ ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
- dispatch_call::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, properties))
+ dispatch_call::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))
}
#[weight = T::CommonWeightInfo::set_property_permissions(property_permissions.len() as u32)]
runtime/common/src/weights.rsdiffbeforeafterboth--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -58,6 +58,10 @@
dispatch_weight::<T>() + max_weight_of!(set_collection_properties(amount))
}
+ fn delete_collection_properties(amount: u32) -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(delete_collection_properties(amount))
+ }
+
fn set_token_properties(amount: u32) -> Weight {
dispatch_weight::<T>() + max_weight_of!(set_token_properties(amount))
}