difftreelog
Add change_property_permissions
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,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),296 }297298 #[pallet::error]299 pub enum Error<T> {300 /// This collection does not exist.301 CollectionNotFound,302 /// Sender parameter and item owner must be equal.303 MustBeTokenOwner,304 /// No permission to perform action305 NoPermission,306 /// Collection is not in mint mode.307 PublicMintingNotAllowed,308 /// Address is not in allow list.309 AddressNotInAllowlist,310311 /// Collection name can not be longer than 63 char.312 CollectionNameLimitExceeded,313 /// Collection description can not be longer than 255 char.314 CollectionDescriptionLimitExceeded,315 /// Token prefix can not be longer than 15 char.316 CollectionTokenPrefixLimitExceeded,317 /// Total collections bound exceeded.318 TotalCollectionsLimitExceeded,319 /// variable_data exceeded data limit.320 TokenVariableDataLimitExceeded,321 /// Exceeded max admin count322 CollectionAdminCountExceeded,323 /// Collection limit bounds per collection exceeded324 CollectionLimitBoundsExceeded,325 /// Tried to enable permissions which are only permitted to be disabled326 OwnerPermissionsCantBeReverted,327 /// Collection settings not allowing items transferring328 TransferNotAllowed,329 /// Account token limit exceeded per collection330 AccountTokenLimitExceeded,331 /// Collection token limit exceeded332 CollectionTokenLimitExceeded,333 /// Metadata flag frozen334 MetadataFlagFrozen,335336 /// Item not exists.337 TokenNotFound,338 /// Item balance not enough.339 TokenValueTooLow,340 /// Requested value more than approved.341 ApprovedValueTooLow,342 /// Tried to approve more than owned343 CantApproveMoreThanOwned,344345 /// Can't transfer tokens to ethereum zero address346 AddressIsZero,347 /// Target collection doesn't supports this operation348 UnsupportedOperation,349350 /// Not sufficient founds to perform action351 NotSufficientFounds,352353 /// Collection has nesting disabled354 NestingIsDisabled,355 /// Only owner may nest tokens under this collection356 OnlyOwnerAllowedToNest,357 /// Only tokens from specific collections may nest tokens under this358 SourceCollectionIsNotAllowedToNest,359360 /// Tried to store more data than allowed in collection field361 CollectionFieldSizeExceeded,362 }363364 #[pallet::storage]365 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;366 #[pallet::storage]367 pub type DestroyedCollectionCount<T> =368 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;369370 /// Collection info371 #[pallet::storage]372 pub type CollectionById<T> = StorageMap<373 Hasher = Blake2_128Concat,374 Key = CollectionId,375 Value = Collection<<T as frame_system::Config>::AccountId>,376 QueryKind = OptionQuery,377 >;378379 /// Collection properties380 #[pallet::storage]381 pub type CollectionProperties<T> = StorageMap<382 Hasher = Blake2_128Concat,383 Key = CollectionId,384 Value = Properties,385 QueryKind = ValueQuery,386 OnEmpty = up_data_structs::CollectionProperties,387 >;388389 #[pallet::storage]390 #[pallet::getter(fn property_permission)]391 pub type CollectionPropertyPermissions<T> = StorageMap<392 Hasher = Blake2_128Concat,393 Key = CollectionId,394 Value = PropertiesPermissionMap,395 QueryKind = ValueQuery,396 >;397398 /// Large variable-size collection fields are extracted here399 #[pallet::storage]400 pub type CollectionData<T> = StorageNMap<401 Key = (402 Key<Twox64Concat, CollectionId>,403 Key<Twox64Concat, CollectionField>,404 ),405 Value = BoundedVec<u8, ConstU32<COLLECTION_FIELD_LIMIT>>,406 QueryKind = ValueQuery,407 >;408409 #[pallet::storage]410 pub type AdminAmount<T> = StorageMap<411 Hasher = Blake2_128Concat,412 Key = CollectionId,413 Value = u32,414 QueryKind = ValueQuery,415 >;416417 /// List of collection admins418 #[pallet::storage]419 pub type IsAdmin<T: Config> = StorageNMap<420 Key = (421 Key<Blake2_128Concat, CollectionId>,422 Key<Blake2_128Concat, T::CrossAccountId>,423 ),424 Value = bool,425 QueryKind = ValueQuery,426 >;427428 /// Allowlisted collection users429 #[pallet::storage]430 pub type Allowlist<T: Config> = StorageNMap<431 Key = (432 Key<Blake2_128Concat, CollectionId>,433 Key<Blake2_128Concat, T::CrossAccountId>,434 ),435 Value = bool,436 QueryKind = ValueQuery,437 >;438439 /// Not used by code, exists only to provide some types to metadata440 #[pallet::storage]441 pub type DummyStorageValue<T: Config> = StorageValue<442 Value = (443 CollectionStats,444 CollectionId,445 TokenId,446 PhantomType<RpcCollection<T::AccountId>>,447 ),448 QueryKind = OptionQuery,449 >;450451 #[pallet::hooks]452 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {453 fn on_runtime_upgrade() -> Weight {454 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {455 use up_data_structs::{CollectionVersion1, CollectionVersion2};456 <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {457 Self::set_field_raw(458 id,459 CollectionField::OffchainSchema,460 v.offchain_schema.clone().into_inner(),461 )462 .expect("data has lower bounds than field");463 Self::set_field_raw(464 id,465 CollectionField::VariableOnChainSchema,466 v.variable_on_chain_schema.clone().into_inner(),467 )468 .expect("data has lower bounds than field");469 Self::set_field_raw(470 id,471 CollectionField::ConstOnChainSchema,472 v.const_on_chain_schema.clone().into_inner(),473 )474 .expect("data has lower bounds than field");475476 Some(CollectionVersion2::from(v))477 });478 }479480 0481 }482 }483}484485impl<T: Config> Pallet<T> {486 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens487 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {488 ensure!(489 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,490 <Error<T>>::AddressIsZero491 );492 Ok(())493 }494 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {495 <IsAdmin<T>>::iter_prefix((collection,))496 .map(|(a, _)| a)497 .collect()498 }499 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {500 <Allowlist<T>>::iter_prefix((collection,))501 .map(|(a, _)| a)502 .collect()503 }504 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {505 <Allowlist<T>>::get((collection, user))506 }507 pub fn collection_stats() -> CollectionStats {508 let created = <CreatedCollectionCount<T>>::get();509 let destroyed = <DestroyedCollectionCount<T>>::get();510 CollectionStats {511 created: created.0,512 destroyed: destroyed.0,513 alive: created.0 - destroyed.0,514 }515 }516517 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {518 let collection = <CollectionById<T>>::get(collection);519 if collection.is_none() {520 return None;521 }522523 let collection = collection.unwrap();524 let limits = collection.limits;525 let effective_limits = CollectionLimits {526 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),527 sponsored_data_size: Some(limits.sponsored_data_size()),528 sponsored_data_rate_limit: Some(529 limits530 .sponsored_data_rate_limit531 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),532 ),533 token_limit: Some(limits.token_limit()),534 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(535 match collection.mode {536 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,537 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,538 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,539 },540 )),541 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),542 owner_can_transfer: Some(limits.owner_can_transfer()),543 owner_can_destroy: Some(limits.owner_can_destroy()),544 transfers_enabled: Some(limits.transfers_enabled()),545 nesting_rule: Some(limits.nesting_rule().clone()),546 };547548 Some(effective_limits)549 }550551 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {552 let Collection {553 name,554 description,555 owner,556 mode,557 access,558 token_prefix,559 mint_mode,560 schema_version,561 sponsorship,562 limits,563 meta_update_permission,564 ..565 } = <CollectionById<T>>::get(collection)?;566 Some(RpcCollection {567 name: name.into_inner(),568 description: description.into_inner(),569 owner,570 mode,571 access,572 token_prefix: token_prefix.into_inner(),573 mint_mode,574 schema_version,575 sponsorship,576 limits,577 meta_update_permission,578 offchain_schema: <CollectionData<T>>::get((579 collection,580 CollectionField::OffchainSchema,581 ))582 .into_inner(),583 const_on_chain_schema: <CollectionData<T>>::get((584 collection,585 CollectionField::ConstOnChainSchema,586 ))587 .into_inner(),588 variable_on_chain_schema: <CollectionData<T>>::get((589 collection,590 CollectionField::VariableOnChainSchema,591 ))592 .into_inner(),593 })594 }595}596597impl<T: Config> Pallet<T> {598 pub fn init_collection(599 owner: T::AccountId,600 data: CreateCollectionData<T::AccountId>,601 ) -> Result<CollectionId, DispatchError> {602 {603 ensure!(604 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,605 Error::<T>::CollectionTokenPrefixLimitExceeded606 );607 }608609 let created_count = <CreatedCollectionCount<T>>::get()610 .0611 .checked_add(1)612 .ok_or(ArithmeticError::Overflow)?;613 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;614 let id = CollectionId(created_count);615616 // bound Total number of collections617 ensure!(618 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,619 <Error<T>>::TotalCollectionsLimitExceeded620 );621622 // =========623624 let collection = Collection {625 owner: owner.clone(),626 name: data.name,627 mode: data.mode.clone(),628 mint_mode: false,629 access: data.access.unwrap_or_default(),630 description: data.description,631 token_prefix: data.token_prefix,632 schema_version: data.schema_version.unwrap_or_default(),633 sponsorship: data634 .pending_sponsor635 .map(SponsorshipState::Unconfirmed)636 .unwrap_or_default(),637 limits: data638 .limits639 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))640 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,641 meta_update_permission: data.meta_update_permission.unwrap_or_default(),642 // token_property_permissions: data.token_property_permissions.unwrap_or_default(),643 // properties: Properties::from_collection_props_vec(data.properties)?644 };645646 CollectionProperties::<T>::insert(647 id,648 Properties::from_collection_props_vec(data.properties)?,649 );650651 let token_props_permissions: PropertiesPermissionMap = data652 .token_property_permissions653 .into_iter()654 .map(|property| (property.key, property.permission))655 .collect::<BTreeMap<_, _>>()656 .try_into()657 .map_err(|_| PropertiesError::PropertyLimitReached)?;658659 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);660661 // Take a (non-refundable) deposit of collection creation662 {663 let mut imbalance =664 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();665 imbalance.subsume(666 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(667 &T::TreasuryAccountId::get(),668 T::CollectionCreationPrice::get(),669 ),670 );671 <T as Config>::Currency::settle(672 &owner,673 imbalance,674 WithdrawReasons::TRANSFER,675 ExistenceRequirement::KeepAlive,676 )677 .map_err(|_| Error::<T>::NotSufficientFounds)?;678 }679680 <CreatedCollectionCount<T>>::put(created_count);681 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));682 <CollectionById<T>>::insert(id, collection);683 Self::set_field_raw(684 id,685 CollectionField::OffchainSchema,686 data.offchain_schema.into_inner(),687 )688 .expect("data has lower bounds than field");689 Self::set_field_raw(690 id,691 CollectionField::VariableOnChainSchema,692 data.variable_on_chain_schema.into_inner(),693 )694 .expect("data has lower bounds than field");695 Self::set_field_raw(696 id,697 CollectionField::ConstOnChainSchema,698 data.const_on_chain_schema.into_inner(),699 )700 .expect("data has lower bounds than field");701 Ok(id)702 }703704 pub fn destroy_collection(705 collection: CollectionHandle<T>,706 sender: &T::CrossAccountId,707 ) -> DispatchResult {708 ensure!(709 collection.limits.owner_can_destroy(),710 <Error<T>>::NoPermission,711 );712 collection.check_is_owner(sender)?;713714 let destroyed_collections = <DestroyedCollectionCount<T>>::get()715 .0716 .checked_add(1)717 .ok_or(ArithmeticError::Overflow)?;718719 // =========720721 <DestroyedCollectionCount<T>>::put(destroyed_collections);722 <CollectionById<T>>::remove(collection.id);723 <CollectionData<T>>::remove_prefix((collection.id,), None);724 <AdminAmount<T>>::remove(collection.id);725 <IsAdmin<T>>::remove_prefix((collection.id,), None);726 <Allowlist<T>>::remove_prefix((collection.id,), None);727728 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));729 Ok(())730 }731732 pub fn change_collection_property(733 collection: &CollectionHandle<T>,734 sender: &T::CrossAccountId,735 property: Property,736 ) -> DispatchResult {737 collection.check_is_owner_or_admin(sender)?;738739 CollectionProperties::<T>::try_mutate(740 collection.id,741 |properties| properties.try_change_property(property.clone())742 )?;743744 <Pallet<T>>::deposit_event(Event::CollectionPropertySet(collection.id, property));745746 Ok(())747 }748749 pub fn change_collection_properties(750 collection: &CollectionHandle<T>,751 sender: &T::CrossAccountId,752 properties: Vec<Property>,753 ) -> DispatchResult {754 for property in properties {755 Self::change_collection_property(collection, sender, property)?;756 }757758 Ok(())759 }760761 pub fn change_property_permission(762 collection: &CollectionHandle<T>,763 sender: &T::CrossAccountId,764 property_key: PropertyKey,765 permission: PropertyPermission,766 ) -> DispatchResult {767 collection.check_is_owner_or_admin(sender)?;768769 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {770 permissions.try_insert(property_key, permission)771 })772 .map_err(|_| PropertiesError::PropertyLimitReached)?;773774 Ok(())775 }776777 fn set_field_raw(778 collection_id: CollectionId,779 field: CollectionField,780 value: Vec<u8>,781 ) -> DispatchResult {782 if !value.is_empty() {783 <CollectionData<T>>::insert(784 (collection_id, field),785 BoundedVec::try_from(value).map_err(|_| <Error<T>>::CollectionFieldSizeExceeded)?,786 )787 } else {788 <CollectionData<T>>::remove((collection_id, field));789 }790 Ok(())791 }792793 pub fn set_field(794 collection: &CollectionHandle<T>,795 sender: &T::CrossAccountId,796 field: CollectionField,797 value: Vec<u8>,798 ) -> DispatchResult {799 collection.check_is_owner_or_admin(sender)?;800801 // =========802803 Self::set_field_raw(collection.id, field, value)804 }805806 pub fn toggle_allowlist(807 collection: &CollectionHandle<T>,808 sender: &T::CrossAccountId,809 user: &T::CrossAccountId,810 allowed: bool,811 ) -> DispatchResult {812 collection.check_is_owner_or_admin(sender)?;813814 // =========815816 if allowed {817 <Allowlist<T>>::insert((collection.id, user), true);818 } else {819 <Allowlist<T>>::remove((collection.id, user));820 }821822 Ok(())823 }824825 pub fn toggle_admin(826 collection: &CollectionHandle<T>,827 sender: &T::CrossAccountId,828 user: &T::CrossAccountId,829 admin: bool,830 ) -> DispatchResult {831 collection.check_is_owner_or_admin(sender)?;832833 let was_admin = <IsAdmin<T>>::get((collection.id, user));834 if was_admin == admin {835 return Ok(());836 }837 let amount = <AdminAmount<T>>::get(collection.id);838839 if admin {840 let amount = amount841 .checked_add(1)842 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;843 ensure!(844 amount <= Self::collection_admins_limit(),845 <Error<T>>::CollectionAdminCountExceeded,846 );847848 // =========849850 <AdminAmount<T>>::insert(collection.id, amount);851 <IsAdmin<T>>::insert((collection.id, user), true);852 } else {853 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));854 <IsAdmin<T>>::remove((collection.id, user));855 }856857 Ok(())858 }859860 pub fn clamp_limits(861 mode: CollectionMode,862 old_limit: &CollectionLimits,863 mut new_limit: CollectionLimits,864 ) -> Result<CollectionLimits, DispatchError> {865 macro_rules! limit_default {866 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{867 $(868 if let Some($new) = $new.$field {869 let $old = $old.$field($($arg)?);870 let _ = $new;871 let _ = $old;872 $check873 } else {874 $new.$field = $old.$field875 }876 )*877 }};878 }879880 limit_default!(old_limit, new_limit,881 account_token_ownership_limit => ensure!(882 new_limit <= MAX_TOKEN_OWNERSHIP,883 <Error<T>>::CollectionLimitBoundsExceeded,884 ),885 sponsor_transfer_timeout(match mode {886 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,887 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,888 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,889 }) => ensure!(890 new_limit <= MAX_SPONSOR_TIMEOUT,891 <Error<T>>::CollectionLimitBoundsExceeded,892 ),893 sponsored_data_size => ensure!(894 new_limit <= CUSTOM_DATA_LIMIT,895 <Error<T>>::CollectionLimitBoundsExceeded,896 ),897 token_limit => ensure!(898 old_limit >= new_limit && new_limit > 0,899 <Error<T>>::CollectionTokenLimitExceeded900 ),901 owner_can_transfer => ensure!(902 old_limit || !new_limit,903 <Error<T>>::OwnerPermissionsCantBeReverted,904 ),905 owner_can_destroy => ensure!(906 old_limit || !new_limit,907 <Error<T>>::OwnerPermissionsCantBeReverted,908 ),909 sponsored_data_rate_limit => {},910 transfers_enabled => {},911 );912 Ok(new_limit)913 }914}915916#[macro_export]917macro_rules! unsupported {918 () => {919 Err(<Error<T>>::UnsupportedOperation.into())920 };921}922923/// Worst cases924pub trait CommonWeightInfo<CrossAccountId> {925 fn create_item() -> Weight;926 fn create_multiple_items(amount: u32) -> Weight;927 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;928 fn burn_item() -> Weight;929 fn change_collection_properties(amount: u32) -> Weight;930 fn change_token_properties(amount: u32) -> Weight;931 fn transfer() -> Weight;932 fn approve() -> Weight;933 fn transfer_from() -> Weight;934 fn burn_from() -> Weight;935 fn set_variable_metadata(bytes: u32) -> Weight;936}937938pub trait CommonCollectionOperations<T: Config> {939 fn create_item(940 &self,941 sender: T::CrossAccountId,942 to: T::CrossAccountId,943 data: CreateItemData,944 nesting_budget: &dyn Budget,945 ) -> DispatchResultWithPostInfo;946 fn create_multiple_items(947 &self,948 sender: T::CrossAccountId,949 to: T::CrossAccountId,950 data: Vec<CreateItemData>,951 nesting_budget: &dyn Budget,952 ) -> DispatchResultWithPostInfo;953 fn create_multiple_items_ex(954 &self,955 sender: T::CrossAccountId,956 data: CreateItemExData<T::CrossAccountId>,957 nesting_budget: &dyn Budget,958 ) -> DispatchResultWithPostInfo;959 fn burn_item(960 &self,961 sender: T::CrossAccountId,962 token: TokenId,963 amount: u128,964 ) -> DispatchResultWithPostInfo;965966 fn change_collection_properties(967 &self,968 sender: T::CrossAccountId,969 properties: Vec<Property>,970 ) -> DispatchResultWithPostInfo;971972 fn change_token_properties(973 &self,974 sender: T::CrossAccountId,975 token_id: TokenId,976 property: Vec<Property>,977 ) -> DispatchResultWithPostInfo;978979 fn transfer(980 &self,981 sender: T::CrossAccountId,982 to: T::CrossAccountId,983 token: TokenId,984 amount: u128,985 nesting_budget: &dyn Budget,986 ) -> DispatchResultWithPostInfo;987 fn approve(988 &self,989 sender: T::CrossAccountId,990 spender: T::CrossAccountId,991 token: TokenId,992 amount: u128,993 ) -> DispatchResultWithPostInfo;994 fn transfer_from(995 &self,996 sender: T::CrossAccountId,997 from: T::CrossAccountId,998 to: T::CrossAccountId,999 token: TokenId,1000 amount: u128,1001 nesting_budget: &dyn Budget,1002 ) -> DispatchResultWithPostInfo;1003 fn burn_from(1004 &self,1005 sender: T::CrossAccountId,1006 from: T::CrossAccountId,1007 token: TokenId,1008 amount: u128,1009 nesting_budget: &dyn Budget,1010 ) -> DispatchResultWithPostInfo;10111012 fn set_variable_metadata(1013 &self,1014 sender: T::CrossAccountId,1015 token: TokenId,1016 data: BoundedVec<u8, CustomDataLimit>,1017 ) -> DispatchResultWithPostInfo;10181019 fn check_nesting(1020 &self,1021 sender: T::CrossAccountId,1022 from: (CollectionId, TokenId),1023 under: TokenId,1024 budget: &dyn Budget,1025 ) -> DispatchResult;10261027 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;1028 fn collection_tokens(&self) -> Vec<TokenId>;1029 fn token_exists(&self, token: TokenId) -> bool;1030 fn last_token_id(&self) -> TokenId;10311032 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1033 fn const_metadata(&self, token: TokenId) -> Vec<u8>;1034 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;10351036 /// Amount of unique collection tokens1037 fn total_supply(&self) -> u32;1038 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1039 fn account_balance(&self, account: T::CrossAccountId) -> u32;1040 /// Amount of specific token account have (Applicable to fungible/refungible)1041 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;1042 fn allowance(1043 &self,1044 sender: T::CrossAccountId,1045 spender: T::CrossAccountId,1046 token: TokenId,1047 ) -> u128;1048}10491050// Flexible enough for implementing CommonCollectionOperations1051pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {1052 let post_info = PostDispatchInfo {1053 actual_weight: Some(weight),1054 pays_fee: Pays::Yes,1055 };1056 match res {1057 Ok(()) => Ok(post_info),1058 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),1059 }1060}pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -21,7 +21,7 @@
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::ArithmeticError;
use sp_std::{vec::Vec, vec};
-use up_data_structs::{CustomDataLimit, Property};
+use up_data_structs::{CustomDataLimit, Property, PropertyKeyPermission,};
use crate::{
Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,
@@ -58,6 +58,10 @@
<SelfWeightOf<T>>::change_token_properties(amount)
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_property_permissions(amount)
+ }
+
fn transfer() -> Weight {
<SelfWeightOf<T>>::transfer()
}
@@ -250,6 +254,14 @@
fail!(<Error<T>>::PropertiesNotAllowed)
}
+ fn change_property_permissions(
+ &self,
+ _sender: &T::CrossAccountId,
+ _property_permissions: Vec<PropertyKeyPermission>,
+ ) -> DispatchResultWithPostInfo {
+ fail!(<Error<T>>::PropertiesNotAllowed)
+ }
+
fn set_variable_metadata(
&self,
_sender: T::CrossAccountId,
pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -37,6 +37,7 @@
fn burn_item() -> Weight;
fn change_collection_properties(amount: u32) -> Weight;
fn change_token_properties(amount: u32) -> Weight;
+ fn change_property_permissions(amount: u32) -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
@@ -82,6 +83,11 @@
0
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Fungible Balance (r:2 w:2)
fn transfer() -> Weight {
(17_713_000 as Weight)
@@ -150,6 +156,11 @@
0
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Fungible Balance (r:2 w:2)
fn transfer() -> Weight {
(17_713_000 as Weight)
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -18,7 +18,7 @@
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
use up_data_structs::{
- TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,
+ TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property, PropertyKeyPermission,
};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::DispatchError;
@@ -58,6 +58,10 @@
<SelfWeightOf<T>>::change_token_properties(amount)
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_property_permissions(amount)
+ }
+
fn transfer() -> Weight {
<SelfWeightOf<T>>::transfer()
}
@@ -176,6 +180,19 @@
)
}
+ fn change_property_permissions(
+ &self,
+ sender: &T::CrossAccountId,
+ property_permissions: Vec<PropertyKeyPermission>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::change_property_permissions(property_permissions.len() as u32);
+
+ with_weight(
+ <Pallet<T>>::change_property_permissions(self, sender, property_permissions),
+ weight
+ )
+ }
+
fn burn_item(
&self,
sender: T::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -21,6 +21,7 @@
use up_data_structs::{
AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
+ PropertyKeyPermission,
};
use pallet_evm::account::CrossAccountId;
use pallet_common::{
@@ -324,6 +325,18 @@
<PalletCommon<T>>::change_collection_properties(collection, sender, properties)
}
+ pub fn change_property_permissions(
+ collection: &CollectionHandle<T>,
+ sender: &T::CrossAccountId,
+ property_permissions: Vec<PropertyKeyPermission>
+ ) -> DispatchResult {
+ <PalletCommon<T>>::change_property_permissions(
+ collection,
+ sender,
+ property_permissions,
+ )
+ }
+
pub fn transfer(
collection: &NonfungibleHandle<T>,
from: &T::CrossAccountId,
pallets/nonfungible/src/weights.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -38,6 +38,7 @@
fn burn_item() -> Weight;
fn change_collection_properties(amount: u32) -> Weight;
fn change_token_properties(amount: u32) -> Weight;
+ fn change_property_permissions(amount: u32) -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
@@ -103,6 +104,11 @@
(50_000_000 as Weight).saturating_mul(amount as Weight)
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
// Storage: Nonfungible TokenData (r:1 w:1)
// Storage: Nonfungible AccountBalance (r:2 w:2)
// Storage: Nonfungible Allowance (r:1 w:0)
@@ -203,6 +209,11 @@
(50_000_000 as Weight).saturating_mul(amount as Weight)
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
// Storage: Nonfungible TokenData (r:1 w:1)
// Storage: Nonfungible AccountBalance (r:2 w:2)
// Storage: Nonfungible Allowance (r:1 w:0)
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -20,7 +20,7 @@
use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};
use up_data_structs::{
CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,
- budget::Budget, Property,
+ budget::Budget, Property, PropertyKeyPermission,
};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::DispatchError;
@@ -74,6 +74,10 @@
<SelfWeightOf<T>>::change_token_properties(amount)
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_property_permissions(amount)
+ }
+
fn transfer() -> Weight {
max_weight_of!(
transfer_normal(),
@@ -269,6 +273,14 @@
fail!(<Error<T>>::PropertiesNotAllowed)
}
+ fn change_property_permissions(
+ &self,
+ _sender: &T::CrossAccountId,
+ _property_permissions: Vec<PropertyKeyPermission>,
+ ) -> DispatchResultWithPostInfo {
+ fail!(<Error<T>>::PropertiesNotAllowed)
+ }
+
fn set_variable_metadata(
&self,
sender: T::CrossAccountId,
pallets/refungible/src/weights.rsdiffbeforeafterboth--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -40,6 +40,7 @@
fn burn_item_fully() -> Weight;
fn change_collection_properties(amount: u32) -> Weight;
fn change_token_properties(amount: u32) -> Weight;
+ fn change_property_permissions(amount: u32) -> Weight;
fn transfer_normal() -> Weight;
fn transfer_creating() -> Weight;
fn transfer_removing() -> Weight;
@@ -142,6 +143,11 @@
0
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Refungible Balance (r:2 w:2)
fn transfer_normal() -> Weight {
(19_766_000 as Weight)
@@ -321,6 +327,11 @@
0
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Refungible Balance (r:2 w:2)
fn transfer_normal() -> Weight {
(19_766_000 as Weight)
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -39,7 +39,7 @@
MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
AccessMode, CreateItemData, CollectionLimits, CollectionId, CollectionMode, TokenId,
SchemaVersion, SponsorshipState, MetaUpdatePermission, CreateCollectionData, CustomDataLimit,
- CreateItemExData, budget, CollectionField, Property,
+ CreateItemExData, budget, CollectionField, Property, PropertyKeyPermission,
};
use pallet_evm::account::CrossAccountId;
use pallet_common::{
@@ -723,6 +723,20 @@
dispatch_call::<T, _>(collection_id, |d| d.change_token_properties(sender, token_id, properties))
}
+ #[weight = T::CommonWeightInfo::change_property_permissions(property_permissions.len() as u32)]
+ #[transactional]
+ pub fn change_property_permissions(
+ origin,
+ collection_id: CollectionId,
+ property_permissions: Vec<PropertyKeyPermission>,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(!property_permissions.is_empty(), Error::<T>::EmptyArgument);
+
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+ dispatch_call::<T, _>(collection_id, |d| d.change_property_permissions(&sender, property_permissions))
+ }
+
#[weight = T::CommonWeightInfo::create_multiple_items_ex(&data)]
#[transactional]
pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {
runtime/common/src/weights.rsdiffbeforeafterboth--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -62,6 +62,10 @@
dispatch_weight::<T>() + max_weight_of!(change_token_properties(amount))
}
+ fn change_property_permissions(amount: u32) -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(change_property_permissions(amount))
+ }
+
fn transfer() -> Weight {
dispatch_weight::<T>() + max_weight_of!(transfer())
}