difftreelog
doc: modify properties fns
in: master
3 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//! # Common pallet18//!19//! The Common pallet provides functionality for handling collections.20//!21//! ## Overview22//!23//! The Common pallet provides an interface for common collection operations for different collection types24//! (see [CommonCollectionOperations]), as well as a generic dispatcher for these, see [dispatch] module.25//! It also provides this functionality to EVM, see [erc] and [eth] modules.26//!27//! The Common pallet provides functions for:28//!29//! - Setting and approving collection sponsor.30//! - Get\set\delete allow list.31//! - Get\set\delete collection properties.32//! - Get\set\delete collection property permissions.33//! - Get\set\delete token property permissions.34//! - Get\set\delete collection administrators.35//! - Checking access permissions.36//!37//! ### Terminology38//! **Collection sponsor** - For the collection, you can set a sponsor, at whose expense it will39//! be possible to mint tokens.40//!41//! **Allow list** - List of users who have the right to minting tokens.42//!43//! **Collection properties** - Collection properties are simply key-value stores where various44//! metadata can be placed.45//!46//! **Permissions on token properties** - For each property in the token can be set permission47//! to change, see [`PropertyPermission`].48//!49//! **Collection administrator** - For a collection, you can set administrators who have the right50//! to most actions on the collection.5152#![warn(missing_docs)]53#![cfg_attr(not(feature = "std"), no_std)]54extern crate alloc;5556use core::ops::{Deref, DerefMut};57use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};58use sp_std::vec::Vec;59use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};60use evm_coder::ToLog;61use frame_support::{62 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},63 ensure,64 traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},65 dispatch::Pays,66 transactional,67};68use pallet_evm::GasWeightMapping;69use up_data_structs::{70 COLLECTION_NUMBER_LIMIT,71 Collection,72 RpcCollection,73 CollectionFlags,74 RpcCollectionFlags,75 CollectionId,76 CreateItemData,77 MAX_TOKEN_PREFIX_LENGTH,78 COLLECTION_ADMINS_LIMIT,79 TokenId,80 TokenChild,81 CollectionStats,82 MAX_TOKEN_OWNERSHIP,83 CollectionMode,84 NFT_SPONSOR_TRANSFER_TIMEOUT,85 FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,86 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,87 MAX_SPONSOR_TIMEOUT,88 CUSTOM_DATA_LIMIT,89 CollectionLimits,90 CreateCollectionData,91 SponsorshipState,92 CreateItemExData,93 SponsoringRateLimit,94 budget::Budget,95 PhantomType,96 Property,97 Properties,98 PropertiesPermissionMap,99 PropertyKey,100 PropertyValue,101 PropertyPermission,102 PropertiesError,103 PropertyKeyPermission,104 TokenData,105 TrySetProperty,106 PropertyScope,107 // RMRK108 RmrkCollectionInfo,109 RmrkInstanceInfo,110 RmrkResourceInfo,111 RmrkPropertyInfo,112 RmrkBaseInfo,113 RmrkPartType,114 RmrkBoundedTheme,115 RmrkNftChild,116 CollectionPermissions,117};118use up_pov_estimate_rpc::PovInfo;119120pub use pallet::*;121use sp_core::H160;122use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};123#[cfg(feature = "runtime-benchmarks")]124pub mod benchmarking;125pub mod dispatch;126pub mod erc;127pub mod eth;128pub mod weights;129130/// Weight info.131pub type SelfWeightOf<T> = <T as Config>::WeightInfo;132133/// Collection handle contains information about collection data and id.134/// Also provides functionality to count consumed gas.135///136/// CollectionHandle is used as a generic wrapper for collections of all types.137/// It allows to perform common operations and queries on any collection type,138/// both completely general for all, as well as their respective implementations of [`CommonCollectionOperations`].139#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]140pub struct CollectionHandle<T: Config> {141 /// Collection id142 pub id: CollectionId,143 collection: Collection<T::AccountId>,144 /// Substrate recorder for counting consumed gas145 pub recorder: SubstrateRecorder<T>,146}147148impl<T: Config> WithRecorder<T> for CollectionHandle<T> {149 fn recorder(&self) -> &SubstrateRecorder<T> {150 &self.recorder151 }152 fn into_recorder(self) -> SubstrateRecorder<T> {153 self.recorder154 }155}156157impl<T: Config> CollectionHandle<T> {158 /// Same as [CollectionHandle::new] but with an explicit gas limit.159 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {160 <CollectionById<T>>::get(id).map(|collection| Self {161 id,162 collection,163 recorder: SubstrateRecorder::new(gas_limit),164 })165 }166167 /// Same as [CollectionHandle::new] but with an existed [`SubstrateRecorder`].168 pub fn new_with_recorder(id: CollectionId, recorder: SubstrateRecorder<T>) -> Option<Self> {169 <CollectionById<T>>::get(id).map(|collection| Self {170 id,171 collection,172 recorder,173 })174 }175176 /// Retrives collection data from storage and creates collection handle with default parameters.177 /// If collection not found return `None`178 pub fn new(id: CollectionId) -> Option<Self> {179 Self::new_with_gas_limit(id, u64::MAX)180 }181182 /// Same as [`CollectionHandle::new`] but if collection not found [CollectionNotFound](Error::CollectionNotFound) returned.183 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {184 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)185 }186187 /// Consume gas for reading.188 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {189 self.recorder190 .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_ref_time(191 <T as frame_system::Config>::DbWeight::get()192 .read193 .saturating_mul(reads),194 )))195 }196197 /// Consume gas for writing.198 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {199 self.recorder200 .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_ref_time(201 <T as frame_system::Config>::DbWeight::get()202 .write203 .saturating_mul(writes),204 )))205 }206207 /// Consume gas for reading and writing.208 pub fn consume_store_reads_and_writes(209 &self,210 reads: u64,211 writes: u64,212 ) -> evm_coder::execution::Result<()> {213 let weight = <T as frame_system::Config>::DbWeight::get();214 let reads = weight.read.saturating_mul(reads);215 let writes = weight.read.saturating_mul(writes);216 self.recorder217 .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_ref_time(218 reads.saturating_add(writes),219 )))220 }221222 /// Save collection to storage.223 pub fn save(&self) -> DispatchResult {224 <CollectionById<T>>::insert(self.id, &self.collection);225 Ok(())226 }227228 /// Set collection sponsor.229 ///230 /// Unique collections allows sponsoring for certain actions.231 /// This method allows you to set the sponsor of the collection.232 /// In order for sponsorship to become active, it must be confirmed through [`Self::confirm_sponsorship`].233 pub fn set_sponsor(234 &mut self,235 sender: &T::CrossAccountId,236 sponsor: T::AccountId,237 ) -> DispatchResult {238 self.check_is_internal()?;239 self.check_is_owner_or_admin(sender)?;240241 self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.clone());242243 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor));244 <PalletEvm<T>>::deposit_log(245 erc::CollectionHelpersEvents::CollectionChanged {246 collection_id: eth::collection_id_to_address(self.id),247 }248 .to_log(T::ContractAddress::get()),249 );250251 self.save()252 }253254 /// Force set `sponsor`.255 ///256 /// Differs from [`set_sponsor`][`Self::set_sponsor`] in that confirmation257 /// from the `sponsor` is not required.258 ///259 /// # Arguments260 ///261 /// * `sender`: Caller's account.262 /// * `sponsor`: ID of the account of the sponsor-to-be.263 pub fn force_set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {264 self.check_is_internal()?;265266 self.collection.sponsorship = SponsorshipState::Confirmed(sponsor.clone());267268 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor.clone()));269 <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sponsor));270 <PalletEvm<T>>::deposit_log(271 erc::CollectionHelpersEvents::CollectionChanged {272 collection_id: eth::collection_id_to_address(self.id),273 }274 .to_log(T::ContractAddress::get()),275 );276277 self.save()278 }279280 /// Confirm sponsorship281 ///282 /// In order for the sponsorship to become active, the user set as the sponsor must confirm their participation.283 /// Before confirming sponsorship, the user must be specified as the sponsor of the collection via [`Self::set_sponsor`].284 pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> DispatchResult {285 self.check_is_internal()?;286 ensure!(287 self.collection.sponsorship.pending_sponsor() == Some(sender),288 Error::<T>::ConfirmSponsorshipFail289 );290291 self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());292293 <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sender.clone()));294 <PalletEvm<T>>::deposit_log(295 erc::CollectionHelpersEvents::CollectionChanged {296 collection_id: eth::collection_id_to_address(self.id),297 }298 .to_log(T::ContractAddress::get()),299 );300301 self.save()302 }303304 /// Remove collection sponsor.305 pub fn remove_sponsor(&mut self, sender: &T::CrossAccountId) -> DispatchResult {306 self.check_is_internal()?;307 self.check_is_owner_or_admin(sender)?;308309 self.collection.sponsorship = SponsorshipState::Disabled;310311 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));312 <PalletEvm<T>>::deposit_log(313 erc::CollectionHelpersEvents::CollectionChanged {314 collection_id: eth::collection_id_to_address(self.id),315 }316 .to_log(T::ContractAddress::get()),317 );318 self.save()319 }320321 /// Force remove `sponsor`.322 ///323 /// Differs from `remove_sponsor` in that324 /// it doesn't require consent from the `owner` of the collection.325 pub fn force_remove_sponsor(&mut self) -> DispatchResult {326 self.check_is_internal()?;327328 self.collection.sponsorship = SponsorshipState::Disabled;329330 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));331 <PalletEvm<T>>::deposit_log(332 erc::CollectionHelpersEvents::CollectionChanged {333 collection_id: eth::collection_id_to_address(self.id),334 }335 .to_log(T::ContractAddress::get()),336 );337 self.save()338 }339340 /// Checks that the collection was created with, and must be operated upon through **Unique API**.341 /// Now check only the `external` flag and if it's **true**, then return [`Error::CollectionIsExternal`] error.342 pub fn check_is_internal(&self) -> DispatchResult {343 if self.flags.external {344 return Err(<Error<T>>::CollectionIsExternal)?;345 }346347 Ok(())348 }349350 /// Checks that the collection was created with, and must be operated upon through an **assimilated API**.351 /// Now check only the `external` flag and if it's **false**, then return [`Error::CollectionIsInternal`] error.352 pub fn check_is_external(&self) -> DispatchResult {353 if !self.flags.external {354 return Err(<Error<T>>::CollectionIsInternal)?;355 }356357 Ok(())358 }359}360361impl<T: Config> Deref for CollectionHandle<T> {362 type Target = Collection<T::AccountId>;363364 fn deref(&self) -> &Self::Target {365 &self.collection366 }367}368369impl<T: Config> DerefMut for CollectionHandle<T> {370 fn deref_mut(&mut self) -> &mut Self::Target {371 &mut self.collection372 }373}374375impl<T: Config> CollectionHandle<T> {376 /// Checks if the `user` is the owner of the collection.377 pub fn check_is_owner(&self, user: &T::CrossAccountId) -> DispatchResult {378 ensure!(*user.as_sub() == self.owner, <Error<T>>::NoPermission);379 Ok(())380 }381382 /// Returns **true** if the `user` is the owner or administrator of the collection.383 pub fn is_owner_or_admin(&self, user: &T::CrossAccountId) -> bool {384 *user.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, user))385 }386387 /// Checks if the `user` is the owner or administrator of the collection.388 pub fn check_is_owner_or_admin(&self, user: &T::CrossAccountId) -> DispatchResult {389 ensure!(self.is_owner_or_admin(user), <Error<T>>::NoPermission);390 Ok(())391 }392393 /// Return **true** if `user` was not allowed to have tokens, and he can ignore such restrictions.394 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {395 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)396 }397398 /// Return **true** if `user` does not have enough token parts, and he can ignore such restrictions.399 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {400 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)401 }402403 /// Checks if the user is in the allow list. If not [Error::AddressNotInAllowlist] returns.404 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {405 ensure!(406 <Allowlist<T>>::get((self.id, user)),407 <Error<T>>::AddressNotInAllowlist408 );409 Ok(())410 }411412 /// Changes collection owner to another account413 /// #### Store read/writes414 /// 1 writes415 pub fn change_owner(416 &mut self,417 caller: T::CrossAccountId,418 new_owner: T::CrossAccountId,419 ) -> DispatchResult {420 self.check_is_internal()?;421 self.check_is_owner(&caller)?;422 self.collection.owner = new_owner.as_sub().clone();423424 <Pallet<T>>::deposit_event(Event::<T>::CollectionOwnerChanged(425 self.id,426 new_owner.as_sub().clone(),427 ));428 <PalletEvm<T>>::deposit_log(429 erc::CollectionHelpersEvents::CollectionChanged {430 collection_id: eth::collection_id_to_address(self.id),431 }432 .to_log(T::ContractAddress::get()),433 );434435 self.save()436 }437}438439#[frame_support::pallet]440pub mod pallet {441 use super::*;442 use dispatch::CollectionDispatch;443 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};444 use frame_system::pallet_prelude::*;445 use frame_support::traits::Currency;446 use up_data_structs::{TokenId, mapping::TokenAddressMapping};447 use scale_info::TypeInfo;448 use weights::WeightInfo;449450 #[pallet::config]451 pub trait Config:452 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::Config + TypeInfo453 {454 /// Weight information for functions of this pallet.455 type WeightInfo: WeightInfo;456457 /// Events compatible with [`frame_system::Config::Event`].458 type RuntimeEvent: IsType<<Self as frame_system::Config>::RuntimeEvent> + From<Event<Self>>;459460 /// Handler of accounts and payment.461 type Currency: Currency<Self::AccountId>;462463 /// Set price to create a collection.464 #[pallet::constant]465 type CollectionCreationPrice: Get<466 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,467 >;468469 /// Dispatcher of operations on collections.470 type CollectionDispatch: CollectionDispatch<Self>;471472 /// Account which holds the chain's treasury.473 type TreasuryAccountId: Get<Self::AccountId>;474475 /// Address under which the CollectionHelper contract would be available.476 #[pallet::constant]477 type ContractAddress: Get<H160>;478479 /// Mapper for token addresses to Ethereum addresses.480 type EvmTokenAddressMapping: TokenAddressMapping<H160>;481482 /// Mapper for token addresses to [`CrossAccountId`].483 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;484 }485486 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);487488 #[pallet::pallet]489 #[pallet::storage_version(STORAGE_VERSION)]490 #[pallet::generate_store(pub(super) trait Store)]491 pub struct Pallet<T>(_);492493 #[pallet::extra_constants]494 impl<T: Config> Pallet<T> {495 /// Maximum admins per collection.496 pub fn collection_admins_limit() -> u32 {497 COLLECTION_ADMINS_LIMIT498 }499 }500501 #[pallet::event]502 #[pallet::generate_deposit(pub fn deposit_event)]503 pub enum Event<T: Config> {504 /// New collection was created505 CollectionCreated(506 /// Globally unique identifier of newly created collection.507 CollectionId,508 /// [`CollectionMode`] converted into _u8_.509 u8,510 /// Collection owner.511 T::AccountId,512 ),513514 /// New collection was destroyed515 CollectionDestroyed(516 /// Globally unique identifier of collection.517 CollectionId,518 ),519520 /// New item was created.521 ItemCreated(522 /// Id of the collection where item was created.523 CollectionId,524 /// Id of an item. Unique within the collection.525 TokenId,526 /// Owner of newly created item527 T::CrossAccountId,528 /// Always 1 for NFT529 u128,530 ),531532 /// Collection item was burned.533 ItemDestroyed(534 /// Id of the collection where item was destroyed.535 CollectionId,536 /// Identifier of burned NFT.537 TokenId,538 /// Which user has destroyed its tokens.539 T::CrossAccountId,540 /// Amount of token pieces destroed. Always 1 for NFT.541 u128,542 ),543544 /// Item was transferred545 Transfer(546 /// Id of collection to which item is belong.547 CollectionId,548 /// Id of an item.549 TokenId,550 /// Original owner of item.551 T::CrossAccountId,552 /// New owner of item.553 T::CrossAccountId,554 /// Amount of token pieces transfered. Always 1 for NFT.555 u128,556 ),557558 /// Amount pieces of token owned by `sender` was approved for `spender`.559 Approved(560 /// Id of collection to which item is belong.561 CollectionId,562 /// Id of an item.563 TokenId,564 /// Original owner of item.565 T::CrossAccountId,566 /// Id for which the approval was granted.567 T::CrossAccountId,568 /// Amount of token pieces transfered. Always 1 for NFT.569 u128,570 ),571572 /// A `sender` approves operations on all owned tokens for `spender`.573 ApprovedForAll(574 /// Id of collection to which item is belong.575 CollectionId,576 /// Owner of a wallet.577 T::CrossAccountId,578 /// Id for which operator status was granted or rewoked.579 T::CrossAccountId,580 /// Is operator status granted or revoked?581 bool,582 ),583584 /// The colletion property has been added or edited.585 CollectionPropertySet(586 /// Id of collection to which property has been set.587 CollectionId,588 /// The property that was set.589 PropertyKey,590 ),591592 /// The property has been deleted.593 CollectionPropertyDeleted(594 /// Id of collection to which property has been deleted.595 CollectionId,596 /// The property that was deleted.597 PropertyKey,598 ),599600 /// The token property has been added or edited.601 TokenPropertySet(602 /// Identifier of the collection whose token has the property set.603 CollectionId,604 /// The token for which the property was set.605 TokenId,606 /// The property that was set.607 PropertyKey,608 ),609610 /// The token property has been deleted.611 TokenPropertyDeleted(612 /// Identifier of the collection whose token has the property deleted.613 CollectionId,614 /// The token for which the property was deleted.615 TokenId,616 /// The property that was deleted.617 PropertyKey,618 ),619620 /// The token property permission of a collection has been set.621 PropertyPermissionSet(622 /// ID of collection to which property permission has been set.623 CollectionId,624 /// The property permission that was set.625 PropertyKey,626 ),627628 /// Address was added to the allow list.629 AllowListAddressAdded(630 /// ID of the affected collection.631 CollectionId,632 /// Address of the added account.633 T::CrossAccountId,634 ),635636 /// Address was removed from the allow list.637 AllowListAddressRemoved(638 /// ID of the affected collection.639 CollectionId,640 /// Address of the removed account.641 T::CrossAccountId,642 ),643644 /// Collection admin was added.645 CollectionAdminAdded(646 /// ID of the affected collection.647 CollectionId,648 /// Admin address.649 T::CrossAccountId,650 ),651652 /// Collection admin was removed.653 CollectionAdminRemoved(654 /// ID of the affected collection.655 CollectionId,656 /// Removed admin address.657 T::CrossAccountId,658 ),659660 /// Collection limits were set.661 CollectionLimitSet(662 /// ID of the affected collection.663 CollectionId,664 ),665666 /// Collection owned was changed.667 CollectionOwnerChanged(668 /// ID of the affected collection.669 CollectionId,670 /// New owner address.671 T::AccountId,672 ),673674 /// Collection permissions were set.675 CollectionPermissionSet(676 /// ID of the affected collection.677 CollectionId,678 ),679680 /// Collection sponsor was set.681 CollectionSponsorSet(682 /// ID of the affected collection.683 CollectionId,684 /// New sponsor address.685 T::AccountId,686 ),687688 /// New sponsor was confirm.689 SponsorshipConfirmed(690 /// ID of the affected collection.691 CollectionId,692 /// New sponsor address.693 T::AccountId,694 ),695696 /// Collection sponsor was removed.697 CollectionSponsorRemoved(698 /// ID of the affected collection.699 CollectionId,700 ),701 }702703 #[pallet::error]704 pub enum Error<T> {705 /// This collection does not exist.706 CollectionNotFound,707 /// Sender parameter and item owner must be equal.708 MustBeTokenOwner,709 /// No permission to perform action710 NoPermission,711 /// Destroying only empty collections is allowed712 CantDestroyNotEmptyCollection,713 /// Collection is not in mint mode.714 PublicMintingNotAllowed,715 /// Address is not in allow list.716 AddressNotInAllowlist,717718 /// Collection name can not be longer than 63 char.719 CollectionNameLimitExceeded,720 /// Collection description can not be longer than 255 char.721 CollectionDescriptionLimitExceeded,722 /// Token prefix can not be longer than 15 char.723 CollectionTokenPrefixLimitExceeded,724 /// Total collections bound exceeded.725 TotalCollectionsLimitExceeded,726 /// Exceeded max admin count727 CollectionAdminCountExceeded,728 /// Collection limit bounds per collection exceeded729 CollectionLimitBoundsExceeded,730 /// Tried to enable permissions which are only permitted to be disabled731 OwnerPermissionsCantBeReverted,732 /// Collection settings not allowing items transferring733 TransferNotAllowed,734 /// Account token limit exceeded per collection735 AccountTokenLimitExceeded,736 /// Collection token limit exceeded737 CollectionTokenLimitExceeded,738 /// Metadata flag frozen739 MetadataFlagFrozen,740741 /// Item does not exist742 TokenNotFound,743 /// Item is balance not enough744 TokenValueTooLow,745 /// Requested value is more than the approved746 ApprovedValueTooLow,747 /// Tried to approve more than owned748 CantApproveMoreThanOwned,749 /// Only spending from eth mirror could be approved750 AddressIsNotEthMirror,751752 /// Can't transfer tokens to ethereum zero address753 AddressIsZero,754755 /// The operation is not supported756 UnsupportedOperation,757758 /// Insufficient funds to perform an action759 NotSufficientFounds,760761 /// User does not satisfy the nesting rule762 UserIsNotAllowedToNest,763 /// Only tokens from specific collections may nest tokens under this one764 SourceCollectionIsNotAllowedToNest,765766 /// Tried to store more data than allowed in collection field767 CollectionFieldSizeExceeded,768769 /// Tried to store more property data than allowed770 NoSpaceForProperty,771772 /// Tried to store more property keys than allowed773 PropertyLimitReached,774775 /// Property key is too long776 PropertyKeyIsTooLong,777778 /// Only ASCII letters, digits, and symbols `_`, `-`, and `.` are allowed779 InvalidCharacterInPropertyKey,780781 /// Empty property keys are forbidden782 EmptyPropertyKey,783784 /// Tried to access an external collection with an internal API785 CollectionIsExternal,786787 /// Tried to access an internal collection with an external API788 CollectionIsInternal,789790 /// This address is not set as sponsor, use setCollectionSponsor first.791 ConfirmSponsorshipFail,792793 /// The user is not an administrator.794 UserIsNotCollectionAdmin,795 }796797 /// Storage of the count of created collections. Essentially contains the last collection ID.798 #[pallet::storage]799 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;800801 /// Storage of the count of deleted collections.802 #[pallet::storage]803 pub type DestroyedCollectionCount<T> =804 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;805806 /// Storage of collection info.807 #[pallet::storage]808 pub type CollectionById<T> = StorageMap<809 Hasher = Blake2_128Concat,810 Key = CollectionId,811 Value = Collection<<T as frame_system::Config>::AccountId>,812 QueryKind = OptionQuery,813 >;814815 /// Storage of collection properties.816 #[pallet::storage]817 #[pallet::getter(fn collection_properties)]818 pub type CollectionProperties<T> = StorageMap<819 Hasher = Blake2_128Concat,820 Key = CollectionId,821 Value = Properties,822 QueryKind = ValueQuery,823 OnEmpty = up_data_structs::CollectionProperties,824 >;825826 /// Storage of token property permissions of a collection.827 #[pallet::storage]828 #[pallet::getter(fn property_permissions)]829 pub type CollectionPropertyPermissions<T> = StorageMap<830 Hasher = Blake2_128Concat,831 Key = CollectionId,832 Value = PropertiesPermissionMap,833 QueryKind = ValueQuery,834 >;835836 /// Storage of the amount of collection admins.837 #[pallet::storage]838 pub type AdminAmount<T> = StorageMap<839 Hasher = Blake2_128Concat,840 Key = CollectionId,841 Value = u32,842 QueryKind = ValueQuery,843 >;844845 /// List of collection admins.846 #[pallet::storage]847 pub type IsAdmin<T: Config> = StorageNMap<848 Key = (849 Key<Blake2_128Concat, CollectionId>,850 Key<Blake2_128Concat, T::CrossAccountId>,851 ),852 Value = bool,853 QueryKind = ValueQuery,854 >;855856 /// Allowlisted collection users.857 #[pallet::storage]858 pub type Allowlist<T: Config> = StorageNMap<859 Key = (860 Key<Blake2_128Concat, CollectionId>,861 Key<Blake2_128Concat, T::CrossAccountId>,862 ),863 Value = bool,864 QueryKind = ValueQuery,865 >;866867 /// Not used by code, exists only to provide some types to metadata.868 #[pallet::storage]869 pub type DummyStorageValue<T: Config> = StorageValue<870 Value = (871 CollectionStats,872 CollectionId,873 TokenId,874 TokenChild,875 PhantomType<(876 TokenData<T::CrossAccountId>,877 RpcCollection<T::AccountId>,878 // RMRK879 RmrkCollectionInfo<T::AccountId>,880 RmrkInstanceInfo<T::AccountId>,881 RmrkResourceInfo,882 RmrkPropertyInfo,883 RmrkBaseInfo<T::AccountId>,884 RmrkPartType,885 RmrkBoundedTheme,886 RmrkNftChild,887 // PoV Estimate Info888 PovInfo,889 )>,890 ),891 QueryKind = OptionQuery,892 >;893894 #[pallet::hooks]895 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {896 fn on_runtime_upgrade() -> Weight {897 StorageVersion::new(1).put::<Pallet<T>>();898899 Weight::zero()900 }901 }902}903904impl<T: Config> Pallet<T> {905 /// Enshure that receiver address is correct.906 ///907 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens.908 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {909 ensure!(910 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,911 <Error<T>>::AddressIsZero912 );913 Ok(())914 }915916 /// Get a vector of collection admins.917 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {918 <IsAdmin<T>>::iter_prefix((collection,))919 .map(|(a, _)| a)920 .collect()921 }922923 /// Get a vector of users allowed to mint tokens.924 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {925 <Allowlist<T>>::iter_prefix((collection,))926 .map(|(a, _)| a)927 .collect()928 }929930 /// Is `user` allowed to mint token in `collection`.931 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {932 <Allowlist<T>>::get((collection, user))933 }934935 /// Get statistics of collections.936 pub fn collection_stats() -> CollectionStats {937 let created = <CreatedCollectionCount<T>>::get();938 let destroyed = <DestroyedCollectionCount<T>>::get();939 CollectionStats {940 created: created.0,941 destroyed: destroyed.0,942 alive: created.0 - destroyed.0,943 }944 }945946 /// Get the effective limits for the collection.947 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {948 let collection = <CollectionById<T>>::get(collection)?;949 let limits = collection.limits;950 let effective_limits = CollectionLimits {951 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),952 sponsored_data_size: Some(limits.sponsored_data_size()),953 sponsored_data_rate_limit: Some(954 limits955 .sponsored_data_rate_limit956 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),957 ),958 token_limit: Some(limits.token_limit()),959 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(960 match collection.mode {961 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,962 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,963 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,964 },965 )),966 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),967 owner_can_transfer: Some(limits.owner_can_transfer()),968 owner_can_destroy: Some(limits.owner_can_destroy()),969 transfers_enabled: Some(limits.transfers_enabled()),970 };971972 Some(effective_limits)973 }974975 /// Returns information about the `collection` adapted for rpc.976 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {977 let Collection {978 name,979 description,980 owner,981 mode,982 token_prefix,983 sponsorship,984 limits,985 permissions,986 flags,987 } = <CollectionById<T>>::get(collection)?;988989 let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)990 .into_iter()991 .map(|(key, permission)| PropertyKeyPermission { key, permission })992 .collect();993994 let properties = <CollectionProperties<T>>::get(collection)995 .into_iter()996 .map(|(key, value)| Property { key, value })997 .collect();998999 let permissions = CollectionPermissions {1000 access: Some(permissions.access()),1001 mint_mode: Some(permissions.mint_mode()),1002 nesting: Some(permissions.nesting().clone()),1003 };10041005 Some(RpcCollection {1006 name: name.into_inner(),1007 description: description.into_inner(),1008 owner,1009 mode,1010 token_prefix: token_prefix.into_inner(),1011 sponsorship,1012 limits,1013 permissions,1014 token_property_permissions,1015 properties,1016 read_only: flags.external,10171018 flags: RpcCollectionFlags {1019 foreign: flags.foreign,1020 erc721metadata: flags.erc721metadata,1021 },1022 })1023 }1024}10251026macro_rules! limit_default {1027 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1028 $(1029 if let Some($new) = $new.$field {1030 let $old = $old.$field($($arg)?);1031 let _ = $new;1032 let _ = $old;1033 $check1034 } else {1035 $new.$field = $old.$field1036 }1037 )*1038 }};1039}1040macro_rules! limit_default_clone {1041 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1042 $(1043 if let Some($new) = $new.$field.clone() {1044 let $old = $old.$field($($arg)?);1045 let _ = $new;1046 let _ = $old;1047 $check1048 } else {1049 $new.$field = $old.$field.clone()1050 }1051 )*1052 }};1053}10541055impl<T: Config> Pallet<T> {1056 /// Create new collection.1057 ///1058 /// * `owner` - The owner of the collection.1059 /// * `data` - Description of the created collection.1060 /// * `flags` - Extra flags to store.1061 pub fn init_collection(1062 owner: T::CrossAccountId,1063 payer: T::CrossAccountId,1064 data: CreateCollectionData<T::AccountId>,1065 flags: CollectionFlags,1066 ) -> Result<CollectionId, DispatchError> {1067 {1068 ensure!(1069 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,1070 Error::<T>::CollectionTokenPrefixLimitExceeded1071 );1072 }10731074 let created_count = <CreatedCollectionCount<T>>::get()1075 .01076 .checked_add(1)1077 .ok_or(ArithmeticError::Overflow)?;1078 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;1079 let id = CollectionId(created_count);10801081 // bound Total number of collections1082 ensure!(1083 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,1084 <Error<T>>::TotalCollectionsLimitExceeded1085 );10861087 // =========10881089 let collection = Collection {1090 owner: owner.as_sub().clone(),1091 name: data.name,1092 mode: data.mode.clone(),1093 description: data.description,1094 token_prefix: data.token_prefix,1095 sponsorship: data1096 .pending_sponsor1097 .map(SponsorshipState::Unconfirmed)1098 .unwrap_or_default(),1099 limits: data1100 .limits1101 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))1102 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,1103 permissions: data1104 .permissions1105 .map(|permissions| {1106 Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions)1107 })1108 .unwrap_or_else(|| Ok(CollectionPermissions::default()))?,1109 flags,1110 };11111112 let mut collection_properties = up_data_structs::CollectionProperties::get();1113 collection_properties1114 .try_set_from_iter(data.properties.into_iter())1115 .map_err(<Error<T>>::from)?;11161117 CollectionProperties::<T>::insert(id, collection_properties);11181119 let mut token_props_permissions = PropertiesPermissionMap::new();1120 token_props_permissions1121 .try_set_from_iter(data.token_property_permissions.into_iter())1122 .map_err(<Error<T>>::from)?;11231124 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);11251126 // Take a (non-refundable) deposit of collection creation1127 {1128 let mut imbalance =1129 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();1130 imbalance.subsume(1131 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(1132 &T::TreasuryAccountId::get(),1133 T::CollectionCreationPrice::get(),1134 ),1135 );1136 <T as Config>::Currency::settle(1137 payer.as_sub(),1138 imbalance,1139 WithdrawReasons::TRANSFER,1140 ExistenceRequirement::KeepAlive,1141 )1142 .map_err(|_| Error::<T>::NotSufficientFounds)?;1143 }11441145 <CreatedCollectionCount<T>>::put(created_count);1146 <Pallet<T>>::deposit_event(Event::CollectionCreated(1147 id,1148 data.mode.id(),1149 owner.as_sub().clone(),1150 ));1151 <PalletEvm<T>>::deposit_log(1152 erc::CollectionHelpersEvents::CollectionCreated {1153 owner: *owner.as_eth(),1154 collection_id: eth::collection_id_to_address(id),1155 }1156 .to_log(T::ContractAddress::get()),1157 );1158 <CollectionById<T>>::insert(id, collection);1159 Ok(id)1160 }11611162 /// Destroy collection.1163 ///1164 /// * `collection` - Collection handler.1165 /// * `sender` - The owner or administrator of the collection.1166 pub fn destroy_collection(1167 collection: CollectionHandle<T>,1168 sender: &T::CrossAccountId,1169 ) -> DispatchResult {1170 ensure!(1171 collection.limits.owner_can_destroy(),1172 <Error<T>>::NoPermission,1173 );1174 collection.check_is_owner(sender)?;11751176 let destroyed_collections = <DestroyedCollectionCount<T>>::get()1177 .01178 .checked_add(1)1179 .ok_or(ArithmeticError::Overflow)?;11801181 // =========11821183 <DestroyedCollectionCount<T>>::put(destroyed_collections);1184 <CollectionById<T>>::remove(collection.id);1185 <AdminAmount<T>>::remove(collection.id);1186 let _ = <IsAdmin<T>>::clear_prefix((collection.id,), u32::MAX, None);1187 let _ = <Allowlist<T>>::clear_prefix((collection.id,), u32::MAX, None);1188 <CollectionProperties<T>>::remove(collection.id);11891190 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));11911192 <PalletEvm<T>>::deposit_log(1193 erc::CollectionHelpersEvents::CollectionDestroyed {1194 collection_id: eth::collection_id_to_address(collection.id),1195 }1196 .to_log(T::ContractAddress::get()),1197 );1198 Ok(())1199 }12001201 fn modify_collection_properties(1202 collection: &CollectionHandle<T>,1203 sender: &T::CrossAccountId,1204 properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,1205 ) -> DispatchResult {1206 collection.check_is_owner_or_admin(sender)?;12071208 let mut stored_properties = <CollectionProperties<T>>::get(collection.id);12091210 for (key, value) in properties_updates {1211 match value {1212 Some(value) => {1213 stored_properties1214 .try_set(key.clone(), value)1215 .map_err(<Error<T>>::from)?;12161217 Self::deposit_event(Event::CollectionPropertySet(collection.id, key));1218 <PalletEvm<T>>::deposit_log(1219 erc::CollectionHelpersEvents::CollectionChanged {1220 collection_id: eth::collection_id_to_address(collection.id),1221 }1222 .to_log(T::ContractAddress::get()),1223 );1224 }1225 None => {1226 stored_properties.remove(&key).map_err(<Error<T>>::from)?;12271228 Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, key));1229 <PalletEvm<T>>::deposit_log(1230 erc::CollectionHelpersEvents::CollectionChanged {1231 collection_id: eth::collection_id_to_address(collection.id),1232 }1233 .to_log(T::ContractAddress::get()),1234 );1235 }1236 }1237 }12381239 <CollectionProperties<T>>::set(collection.id, stored_properties);12401241 Ok(())1242 }12431244 /// Set collection property.1245 ///1246 /// * `collection` - Collection handler.1247 /// * `sender` - The owner or administrator of the collection.1248 /// * `property` - The property to set.1249 pub fn set_collection_property(1250 collection: &CollectionHandle<T>,1251 sender: &T::CrossAccountId,1252 property: Property,1253 ) -> DispatchResult {1254 Self::set_collection_properties(collection, sender, [property].into_iter())1255 }12561257 /// Set a scoped collection property, where the scope is a special prefix1258 /// prohibiting a user access to change the property directly.1259 ///1260 /// * `collection_id` - ID of the collection for which the property is being set.1261 /// * `scope` - Property scope.1262 /// * `property` - The property to set.1263 pub fn set_scoped_collection_property(1264 collection_id: CollectionId,1265 scope: PropertyScope,1266 property: Property,1267 ) -> DispatchResult {1268 CollectionProperties::<T>::try_mutate(collection_id, |properties| {1269 properties.try_scoped_set(scope, property.key, property.value)1270 })1271 .map_err(<Error<T>>::from)?;12721273 Ok(())1274 }12751276 /// Set scoped collection properties, where the scope is a special prefix1277 /// prohibiting a user access to change the properties directly.1278 ///1279 /// * `collection_id` - ID of the collection for which the properties is being set.1280 /// * `scope` - Property scope.1281 /// * `properties` - The properties to set.1282 pub fn set_scoped_collection_properties(1283 collection_id: CollectionId,1284 scope: PropertyScope,1285 properties: impl Iterator<Item = Property>,1286 ) -> DispatchResult {1287 CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {1288 stored_properties.try_scoped_set_from_iter(scope, properties)1289 })1290 .map_err(<Error<T>>::from)?;12911292 Ok(())1293 }12941295 /// Set collection properties.1296 ///1297 /// * `collection` - Collection handler.1298 /// * `sender` - The owner or administrator of the collection.1299 /// * `properties` - The properties to set.1300 #[transactional]1301 pub fn set_collection_properties(1302 collection: &CollectionHandle<T>,1303 sender: &T::CrossAccountId,1304 properties: impl Iterator<Item = Property>,1305 ) -> DispatchResult {1306 Self::modify_collection_properties(1307 collection,1308 sender,1309 properties.map(|property| (property.key, Some(property.value))),1310 )1311 }13121313 /// Delete collection property.1314 ///1315 /// * `collection` - Collection handler.1316 /// * `sender` - The owner or administrator of the collection.1317 /// * `property` - The property to delete.1318 pub fn delete_collection_property(1319 collection: &CollectionHandle<T>,1320 sender: &T::CrossAccountId,1321 property_key: PropertyKey,1322 ) -> DispatchResult {1323 Self::delete_collection_properties(collection, sender, [property_key].into_iter())1324 }13251326 /// Delete collection properties.1327 ///1328 /// * `collection` - Collection handler.1329 /// * `sender` - The owner or administrator of the collection.1330 /// * `properties` - The properties to delete.1331 #[transactional]1332 pub fn delete_collection_properties(1333 collection: &CollectionHandle<T>,1334 sender: &T::CrossAccountId,1335 property_keys: impl Iterator<Item = PropertyKey>,1336 ) -> DispatchResult {1337 Self::modify_collection_properties(collection, sender, property_keys.map(|key| (key, None)))1338 }13391340 /// Set collection propetry permission without any checks.1341 ///1342 /// Used for migrations.1343 ///1344 /// * `collection` - Collection handler.1345 /// * `property_permissions` - Property permissions.1346 pub fn set_property_permission_unchecked(1347 collection: CollectionId,1348 property_permission: PropertyKeyPermission,1349 ) -> DispatchResult {1350 <CollectionPropertyPermissions<T>>::try_mutate(collection, |permissions| {1351 permissions.try_set(property_permission.key, property_permission.permission)1352 })1353 .map_err(<Error<T>>::from)?;1354 Ok(())1355 }13561357 /// Set collection property permission.1358 ///1359 /// * `collection` - Collection handler.1360 /// * `sender` - The owner or administrator of the collection.1361 /// * `property_permission` - Property permission.1362 pub fn set_property_permission(1363 collection: &CollectionHandle<T>,1364 sender: &T::CrossAccountId,1365 property_permission: PropertyKeyPermission,1366 ) -> DispatchResult {1367 Self::set_scoped_property_permission(1368 collection,1369 sender,1370 PropertyScope::None,1371 property_permission,1372 )1373 }13741375 /// Set collection property permission with scope.1376 ///1377 /// * `collection` - Collection handler.1378 /// * `sender` - The owner or administrator of the collection.1379 /// * `scope` - Property scope.1380 /// * `property_permission` - Property permission.1381 pub fn set_scoped_property_permission(1382 collection: &CollectionHandle<T>,1383 sender: &T::CrossAccountId,1384 scope: PropertyScope,1385 property_permission: PropertyKeyPermission,1386 ) -> DispatchResult {1387 collection.check_is_owner_or_admin(sender)?;13881389 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);1390 let current_permission = all_permissions.get(&property_permission.key);1391 if matches![1392 current_permission,1393 Some(PropertyPermission { mutable: false, .. })1394 ] {1395 return Err(<Error<T>>::NoPermission.into());1396 }13971398 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {1399 let property_permission = property_permission.clone();1400 permissions.try_scoped_set(1401 scope,1402 property_permission.key,1403 property_permission.permission,1404 )1405 })1406 .map_err(<Error<T>>::from)?;14071408 Self::deposit_event(Event::PropertyPermissionSet(1409 collection.id,1410 property_permission.key,1411 ));1412 <PalletEvm<T>>::deposit_log(1413 erc::CollectionHelpersEvents::CollectionChanged {1414 collection_id: eth::collection_id_to_address(collection.id),1415 }1416 .to_log(T::ContractAddress::get()),1417 );14181419 Ok(())1420 }14211422 /// Set token property permission.1423 ///1424 /// * `collection` - Collection handler.1425 /// * `sender` - The owner or administrator of the collection.1426 /// * `property_permissions` - Property permissions.1427 #[transactional]1428 pub fn set_token_property_permissions(1429 collection: &CollectionHandle<T>,1430 sender: &T::CrossAccountId,1431 property_permissions: Vec<PropertyKeyPermission>,1432 ) -> DispatchResult {1433 Self::set_scoped_token_property_permissions(1434 collection,1435 sender,1436 PropertyScope::None,1437 property_permissions,1438 )1439 }14401441 /// Set token property permission with scope.1442 ///1443 /// * `collection` - Collection handler.1444 /// * `sender` - The owner or administrator of the collection.1445 /// * `scope` - Property scope.1446 /// * `property_permissions` - Property permissions.1447 #[transactional]1448 pub fn set_scoped_token_property_permissions(1449 collection: &CollectionHandle<T>,1450 sender: &T::CrossAccountId,1451 scope: PropertyScope,1452 property_permissions: Vec<PropertyKeyPermission>,1453 ) -> DispatchResult {1454 for prop_pemission in property_permissions {1455 Self::set_scoped_property_permission(collection, sender, scope, prop_pemission)?;1456 }14571458 Ok(())1459 }14601461 /// Get collection property.1462 pub fn get_collection_property(1463 collection_id: CollectionId,1464 key: &PropertyKey,1465 ) -> Option<PropertyValue> {1466 Self::collection_properties(collection_id).get(key).cloned()1467 }14681469 /// Convert byte vector to property key vector.1470 pub fn bytes_keys_to_property_keys(1471 keys: Vec<Vec<u8>>,1472 ) -> Result<Vec<PropertyKey>, DispatchError> {1473 keys.into_iter()1474 .map(|key| -> Result<PropertyKey, DispatchError> {1475 key.try_into()1476 .map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())1477 })1478 .collect::<Result<Vec<PropertyKey>, DispatchError>>()1479 }14801481 /// Get properties according to given keys.1482 pub fn filter_collection_properties(1483 collection_id: CollectionId,1484 keys: Option<Vec<PropertyKey>>,1485 ) -> Result<Vec<Property>, DispatchError> {1486 let properties = Self::collection_properties(collection_id);14871488 let properties = keys1489 .map(|keys| {1490 keys.into_iter()1491 .filter_map(|key| {1492 properties.get(&key).map(|value| Property {1493 key,1494 value: value.clone(),1495 })1496 })1497 .collect()1498 })1499 .unwrap_or_else(|| {1500 properties1501 .into_iter()1502 .map(|(key, value)| Property { key, value })1503 .collect()1504 });15051506 Ok(properties)1507 }15081509 /// Get property permissions according to given keys.1510 pub fn filter_property_permissions(1511 collection_id: CollectionId,1512 keys: Option<Vec<PropertyKey>>,1513 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {1514 let permissions = Self::property_permissions(collection_id);15151516 let key_permissions = keys1517 .map(|keys| {1518 keys.into_iter()1519 .filter_map(|key| {1520 permissions1521 .get(&key)1522 .map(|permission| PropertyKeyPermission {1523 key,1524 permission: permission.clone(),1525 })1526 })1527 .collect()1528 })1529 .unwrap_or_else(|| {1530 permissions1531 .into_iter()1532 .map(|(key, permission)| PropertyKeyPermission { key, permission })1533 .collect()1534 });15351536 Ok(key_permissions)1537 }15381539 /// Toggle `user` participation in the `collection`'s allow list.1540 /// #### Store read/writes1541 /// 1 writes1542 pub fn toggle_allowlist(1543 collection: &CollectionHandle<T>,1544 sender: &T::CrossAccountId,1545 user: &T::CrossAccountId,1546 allowed: bool,1547 ) -> DispatchResult {1548 collection.check_is_owner_or_admin(sender)?;15491550 // =========15511552 if allowed {1553 <Allowlist<T>>::insert((collection.id, user), true);1554 Self::deposit_event(Event::<T>::AllowListAddressAdded(1555 collection.id,1556 user.clone(),1557 ));1558 } else {1559 <Allowlist<T>>::remove((collection.id, user));1560 Self::deposit_event(Event::<T>::AllowListAddressRemoved(1561 collection.id,1562 user.clone(),1563 ));1564 }15651566 <PalletEvm<T>>::deposit_log(1567 erc::CollectionHelpersEvents::CollectionChanged {1568 collection_id: eth::collection_id_to_address(collection.id),1569 }1570 .to_log(T::ContractAddress::get()),1571 );15721573 Ok(())1574 }15751576 /// Toggle `user` participation in the `collection`'s admin list.1577 /// #### Store read/writes1578 /// 2 reads, 2 writes1579 pub fn toggle_admin(1580 collection: &CollectionHandle<T>,1581 sender: &T::CrossAccountId,1582 user: &T::CrossAccountId,1583 admin: bool,1584 ) -> DispatchResult {1585 collection.check_is_internal()?;1586 collection.check_is_owner(sender)?;15871588 let is_admin = <IsAdmin<T>>::get((collection.id, user));1589 if is_admin == admin {1590 if admin {1591 return Ok(());1592 } else {1593 return Err(Error::<T>::UserIsNotCollectionAdmin.into());1594 }1595 }1596 let amount = <AdminAmount<T>>::get(collection.id);15971598 // =========15991600 if admin {1601 let amount = amount1602 .checked_add(1)1603 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1604 ensure!(1605 amount <= Self::collection_admins_limit(),1606 <Error<T>>::CollectionAdminCountExceeded,1607 );16081609 <AdminAmount<T>>::insert(collection.id, amount);1610 <IsAdmin<T>>::insert((collection.id, user), true);16111612 Self::deposit_event(Event::<T>::CollectionAdminAdded(1613 collection.id,1614 user.clone(),1615 ));1616 } else {1617 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));1618 <IsAdmin<T>>::remove((collection.id, user));16191620 Self::deposit_event(Event::<T>::CollectionAdminRemoved(1621 collection.id,1622 user.clone(),1623 ));1624 }16251626 <PalletEvm<T>>::deposit_log(1627 erc::CollectionHelpersEvents::CollectionChanged {1628 collection_id: eth::collection_id_to_address(collection.id),1629 }1630 .to_log(T::ContractAddress::get()),1631 );16321633 Ok(())1634 }16351636 /// Update collection limits.1637 pub fn update_limits(1638 user: &T::CrossAccountId,1639 collection: &mut CollectionHandle<T>,1640 new_limit: CollectionLimits,1641 ) -> DispatchResult {1642 collection.check_is_internal()?;1643 collection.check_is_owner_or_admin(user)?;16441645 collection.limits =1646 Self::clamp_limits(collection.mode.clone(), &collection.limits, new_limit)?;16471648 Self::deposit_event(Event::<T>::CollectionLimitSet(collection.id));1649 <PalletEvm<T>>::deposit_log(1650 erc::CollectionHelpersEvents::CollectionChanged {1651 collection_id: eth::collection_id_to_address(collection.id),1652 }1653 .to_log(T::ContractAddress::get()),1654 );16551656 collection.save()1657 }16581659 /// Merge set fields from `new_limit` to `old_limit`.1660 fn clamp_limits(1661 mode: CollectionMode,1662 old_limit: &CollectionLimits,1663 mut new_limit: CollectionLimits,1664 ) -> Result<CollectionLimits, DispatchError> {1665 let limits = old_limit;1666 limit_default!(old_limit, new_limit,1667 account_token_ownership_limit => ensure!(1668 new_limit <= MAX_TOKEN_OWNERSHIP,1669 <Error<T>>::CollectionLimitBoundsExceeded,1670 ),1671 sponsored_data_size => ensure!(1672 new_limit <= CUSTOM_DATA_LIMIT,1673 <Error<T>>::CollectionLimitBoundsExceeded,1674 ),16751676 sponsored_data_rate_limit => {},1677 token_limit => ensure!(1678 old_limit >= new_limit && new_limit > 0,1679 <Error<T>>::CollectionTokenLimitExceeded1680 ),16811682 sponsor_transfer_timeout(match mode {1683 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1684 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1685 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1686 }) => ensure!(1687 new_limit <= MAX_SPONSOR_TIMEOUT,1688 <Error<T>>::CollectionLimitBoundsExceeded,1689 ),1690 sponsor_approve_timeout => {},1691 owner_can_transfer => ensure!(1692 !limits.owner_can_transfer_instaled() ||1693 old_limit || !new_limit,1694 <Error<T>>::OwnerPermissionsCantBeReverted,1695 ),1696 owner_can_destroy => ensure!(1697 old_limit || !new_limit,1698 <Error<T>>::OwnerPermissionsCantBeReverted,1699 ),1700 transfers_enabled => {},1701 );1702 Ok(new_limit)1703 }17041705 /// Update collection permissions.1706 pub fn update_permissions(1707 user: &T::CrossAccountId,1708 collection: &mut CollectionHandle<T>,1709 new_permission: CollectionPermissions,1710 ) -> DispatchResult {1711 collection.check_is_internal()?;1712 collection.check_is_owner_or_admin(user)?;1713 collection.permissions = Self::clamp_permissions(1714 collection.mode.clone(),1715 &collection.permissions,1716 new_permission,1717 )?;17181719 Self::deposit_event(Event::<T>::CollectionPermissionSet(collection.id));1720 <PalletEvm<T>>::deposit_log(1721 erc::CollectionHelpersEvents::CollectionChanged {1722 collection_id: eth::collection_id_to_address(collection.id),1723 }1724 .to_log(T::ContractAddress::get()),1725 );17261727 collection.save()1728 }17291730 /// Merge set fields from `new_permission` to `old_permission`.1731 fn clamp_permissions(1732 _mode: CollectionMode,1733 old_permission: &CollectionPermissions,1734 mut new_permission: CollectionPermissions,1735 ) -> Result<CollectionPermissions, DispatchError> {1736 limit_default_clone!(old_permission, new_permission,1737 access => {},1738 mint_mode => {},1739 nesting => { /* todo check for permissive, if only it gets out of benchmarks */ },1740 );1741 Ok(new_permission)1742 }17431744 /// Repair possibly broken properties of a collection.1745 pub fn repair_collection(collection_id: CollectionId) -> DispatchResult {1746 CollectionProperties::<T>::mutate(collection_id, |properties| {1747 properties.recompute_consumed_space();1748 });17491750 Ok(())1751 }1752}17531754/// Indicates unsupported methods by returning [Error::UnsupportedOperation].1755#[macro_export]1756macro_rules! unsupported {1757 ($runtime:path) => {1758 Err($crate::Error::<$runtime>::UnsupportedOperation.into())1759 };1760}17611762/// Return weights for various worst-case operations.1763pub trait CommonWeightInfo<CrossAccountId> {1764 /// Weight of item creation.1765 fn create_item() -> Weight;17661767 /// Weight of items creation.1768 fn create_multiple_items(amount: &[CreateItemData]) -> Weight;17691770 /// Weight of items creation.1771 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;17721773 /// The weight of the burning item.1774 fn burn_item() -> Weight;17751776 /// Property setting weight.1777 ///1778 /// * `amount`- The number of properties to set.1779 fn set_collection_properties(amount: u32) -> Weight;17801781 /// Collection property deletion weight.1782 ///1783 /// * `amount`- The number of properties to set.1784 fn delete_collection_properties(amount: u32) -> Weight;17851786 /// Token property setting weight.1787 ///1788 /// * `amount`- The number of properties to set.1789 fn set_token_properties(amount: u32) -> Weight;17901791 /// Token property deletion weight.1792 ///1793 /// * `amount`- The number of properties to delete.1794 fn delete_token_properties(amount: u32) -> Weight;17951796 /// Token property permissions set weight.1797 ///1798 /// * `amount`- The number of property permissions to set.1799 fn set_token_property_permissions(amount: u32) -> Weight;18001801 /// Transfer price of the token or its parts.1802 fn transfer() -> Weight;18031804 /// The price of setting the permission of the operation from another user.1805 fn approve() -> Weight;18061807 /// The price of setting the permission of the operation from another user for eth mirror.1808 fn approve_from() -> Weight;18091810 /// Transfer price from another user.1811 fn transfer_from() -> Weight;18121813 /// The price of burning a token from another user.1814 fn burn_from() -> Weight;18151816 /// Differs from burn_item in case of Fungible and Refungible, as it should burn1817 /// whole users's balance.1818 ///1819 /// This method shouldn't be used directly, as it doesn't count breadth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead1820 fn burn_recursively_self_raw() -> Weight;18211822 /// Cost of iterating over `amount` children while burning, without counting child burning itself.1823 ///1824 /// This method shouldn't be used directly, as it doesn't count depth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead1825 fn burn_recursively_breadth_raw(amount: u32) -> Weight;18261827 /// The price of recursive burning a token.1828 ///1829 /// `max_selfs` - The maximum burning weight of the token itself.1830 /// `max_breadth` - The maximum number of nested tokens to burn.1831 fn burn_recursively(max_selfs: u32, max_breadth: u32) -> Weight {1832 Self::burn_recursively_self_raw()1833 .saturating_mul(max_selfs.max(1) as u64)1834 .saturating_add(Self::burn_recursively_breadth_raw(max_breadth))1835 }18361837 /// The price of retrieving token owner1838 fn token_owner() -> Weight;18391840 /// The price of setting approval for all1841 fn set_allowance_for_all() -> Weight;18421843 /// The price of repairing an item.1844 fn force_repair_item() -> Weight;1845}18461847/// Weight info extension trait for refungible pallet.1848pub trait RefungibleExtensionsWeightInfo {1849 /// Weight of token repartition.1850 fn repartition() -> Weight;1851}18521853/// Common collection operations.1854///1855/// It wraps methods in Fungible, Nonfungible and Refungible pallets1856/// and adds weight info.1857pub trait CommonCollectionOperations<T: Config> {1858 /// Create token.1859 ///1860 /// * `sender` - The user who mint the token and pays for the transaction.1861 /// * `to` - The user who will own the token.1862 /// * `data` - Token data.1863 /// * `nesting_budget` - A budget that can be spent on nesting tokens.1864 fn create_item(1865 &self,1866 sender: T::CrossAccountId,1867 to: T::CrossAccountId,1868 data: CreateItemData,1869 nesting_budget: &dyn Budget,1870 ) -> DispatchResultWithPostInfo;18711872 /// Create multiple tokens.1873 ///1874 /// * `sender` - The user who mint the token and pays for the transaction.1875 /// * `to` - The user who will own the token.1876 /// * `data` - Token data.1877 /// * `nesting_budget` - A budget that can be spent on nesting tokens.1878 fn create_multiple_items(1879 &self,1880 sender: T::CrossAccountId,1881 to: T::CrossAccountId,1882 data: Vec<CreateItemData>,1883 nesting_budget: &dyn Budget,1884 ) -> DispatchResultWithPostInfo;18851886 /// Create multiple tokens.1887 ///1888 /// * `sender` - The user who mint the token and pays for the transaction.1889 /// * `to` - The user who will own the token.1890 /// * `data` - Token data.1891 /// * `nesting_budget` - A budget that can be spent on nesting tokens.1892 fn create_multiple_items_ex(1893 &self,1894 sender: T::CrossAccountId,1895 data: CreateItemExData<T::CrossAccountId>,1896 nesting_budget: &dyn Budget,1897 ) -> DispatchResultWithPostInfo;18981899 /// Burn token.1900 ///1901 /// * `sender` - The user who owns the token.1902 /// * `token` - Token id that will burned.1903 /// * `amount` - The number of parts of the token that will be burned.1904 fn burn_item(1905 &self,1906 sender: T::CrossAccountId,1907 token: TokenId,1908 amount: u128,1909 ) -> DispatchResultWithPostInfo;19101911 /// Burn token and all nested tokens recursievly.1912 ///1913 /// * `sender` - The user who owns the token.1914 /// * `token` - Token id that will burned.1915 /// * `self_budget` - The budget that can be spent on burning tokens.1916 /// * `breadth_budget` - The budget that can be spent on burning nested tokens.1917 fn burn_item_recursively(1918 &self,1919 sender: T::CrossAccountId,1920 token: TokenId,1921 self_budget: &dyn Budget,1922 breadth_budget: &dyn Budget,1923 ) -> DispatchResultWithPostInfo;19241925 /// Set collection properties.1926 ///1927 /// * `sender` - Must be either the owner of the collection or its admin.1928 /// * `properties` - Properties to be set.1929 fn set_collection_properties(1930 &self,1931 sender: T::CrossAccountId,1932 properties: Vec<Property>,1933 ) -> DispatchResultWithPostInfo;19341935 /// Delete collection properties.1936 ///1937 /// * `sender` - Must be either the owner of the collection or its admin.1938 /// * `properties` - The properties to be removed.1939 fn delete_collection_properties(1940 &self,1941 sender: &T::CrossAccountId,1942 property_keys: Vec<PropertyKey>,1943 ) -> DispatchResultWithPostInfo;19441945 /// Set token properties.1946 ///1947 /// The appropriate [`PropertyPermission`] for the token property1948 /// must be set with [`Self::set_token_property_permissions`].1949 ///1950 /// * `sender` - Must be either the owner of the token or its admin.1951 /// * `token_id` - The token for which the properties are being set.1952 /// * `properties` - Properties to be set.1953 /// * `budget` - Budget for setting properties.1954 fn set_token_properties(1955 &self,1956 sender: T::CrossAccountId,1957 token_id: TokenId,1958 properties: Vec<Property>,1959 budget: &dyn Budget,1960 ) -> DispatchResultWithPostInfo;19611962 /// Remove token properties.1963 ///1964 /// The appropriate [`PropertyPermission`] for the token property1965 /// must be set with [`Self::set_token_property_permissions`].1966 ///1967 /// * `sender` - Must be either the owner of the token or its admin.1968 /// * `token_id` - The token for which the properties are being remove.1969 /// * `property_keys` - Keys to remove corresponding properties.1970 /// * `budget` - Budget for removing properties.1971 fn delete_token_properties(1972 &self,1973 sender: T::CrossAccountId,1974 token_id: TokenId,1975 property_keys: Vec<PropertyKey>,1976 budget: &dyn Budget,1977 ) -> DispatchResultWithPostInfo;19781979 /// Set token property permissions.1980 ///1981 /// * `sender` - Must be either the owner of the token or its admin.1982 /// * `token_id` - The token for which the properties are being set.1983 /// * `property_permissions` - Property permissions to be set.1984 /// * `budget` - Budget for setting properties.1985 fn set_token_property_permissions(1986 &self,1987 sender: &T::CrossAccountId,1988 property_permissions: Vec<PropertyKeyPermission>,1989 ) -> DispatchResultWithPostInfo;19901991 /// Transfer amount of token pieces.1992 ///1993 /// * `sender` - Donor user.1994 /// * `to` - Recepient user.1995 /// * `token` - The token of which parts are being sent.1996 /// * `amount` - The number of parts of the token that will be transferred.1997 /// * `budget` - The maximum budget that can be spent on the transfer.1998 fn transfer(1999 &self,2000 sender: T::CrossAccountId,2001 to: T::CrossAccountId,2002 token: TokenId,2003 amount: u128,2004 budget: &dyn Budget,2005 ) -> DispatchResultWithPostInfo;20062007 /// Grant access to another account to transfer parts of the token owned by the calling user via [Self::transfer_from].2008 ///2009 /// * `sender` - The user who grants access to the token.2010 /// * `spender` - The user to whom the rights are granted.2011 /// * `token` - The token to which access is granted.2012 /// * `amount` - The amount of pieces that another user can dispose of.2013 fn approve(2014 &self,2015 sender: T::CrossAccountId,2016 spender: T::CrossAccountId,2017 token: TokenId,2018 amount: u128,2019 ) -> DispatchResultWithPostInfo;20202021 /// Grant access to another account to transfer parts of the token owned by the calling user's eth mirror via [Self::transfer_from].2022 ///2023 /// * `sender` - The user who grants access to the token.2024 /// * `from` - Spender's eth mirror.2025 /// * `to` - The user to whom the rights are granted.2026 /// * `token` - The token to which access is granted.2027 /// * `amount` - The amount of pieces that another user can dispose of.2028 fn approve_from(2029 &self,2030 sender: T::CrossAccountId,2031 from: T::CrossAccountId,2032 to: T::CrossAccountId,2033 token: TokenId,2034 amount: u128,2035 ) -> DispatchResultWithPostInfo;20362037 /// Send parts of a token owned by another user.2038 ///2039 /// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2040 ///2041 /// * `sender` - The user who must have access to the token (see [`Self::approve`]).2042 /// * `from` - The user who owns the token.2043 /// * `to` - Recepient user.2044 /// * `token` - The token of which parts are being sent.2045 /// * `amount` - The number of parts of the token that will be transferred.2046 /// * `budget` - The maximum budget that can be spent on the transfer.2047 fn transfer_from(2048 &self,2049 sender: T::CrossAccountId,2050 from: T::CrossAccountId,2051 to: T::CrossAccountId,2052 token: TokenId,2053 amount: u128,2054 budget: &dyn Budget,2055 ) -> DispatchResultWithPostInfo;20562057 /// Burn parts of a token owned by another user.2058 ///2059 /// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2060 ///2061 /// * `sender` - The user who must have access to the token (see [`Self::approve`]).2062 /// * `from` - The user who owns the token.2063 /// * `token` - The token of which parts are being sent.2064 /// * `amount` - The number of parts of the token that will be transferred.2065 /// * `budget` - The maximum budget that can be spent on the burn.2066 fn burn_from(2067 &self,2068 sender: T::CrossAccountId,2069 from: T::CrossAccountId,2070 token: TokenId,2071 amount: u128,2072 budget: &dyn Budget,2073 ) -> DispatchResultWithPostInfo;20742075 /// Check permission to nest token.2076 ///2077 /// * `sender` - The user who initiated the check.2078 /// * `from` - The token that is checked for embedding.2079 /// * `under` - Token under which to check.2080 /// * `budget` - The maximum budget that can be spent on the check.2081 fn check_nesting(2082 &self,2083 sender: T::CrossAccountId,2084 from: (CollectionId, TokenId),2085 under: TokenId,2086 budget: &dyn Budget,2087 ) -> DispatchResult;20882089 /// Nest one token into another.2090 ///2091 /// * `under` - Token holder.2092 /// * `to_nest` - Nested token.2093 fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId));20942095 /// Unnest token.2096 ///2097 /// * `under` - Token holder.2098 /// * `to_nest` - Token to unnest.2099 fn unnest(&self, under: TokenId, to_nest: (CollectionId, TokenId));21002101 /// Get all user tokens.2102 ///2103 /// * `account` - Account for which you need to get tokens.2104 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;21052106 /// Get all the tokens in the collection.2107 fn collection_tokens(&self) -> Vec<TokenId>;21082109 /// Check if the token exists.2110 ///2111 /// * `token` - Id token to check.2112 fn token_exists(&self, token: TokenId) -> bool;21132114 /// Get the id of the last minted token.2115 fn last_token_id(&self) -> TokenId;21162117 /// Get the owner of the token.2118 ///2119 /// * `token` - The token for which you need to find out the owner.2120 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;21212122 /// Returns 10 tokens owners in no particular order.2123 ///2124 /// * `token` - The token for which you need to find out the owners.2125 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId>;21262127 /// Get the value of the token property by key.2128 ///2129 /// * `token` - Token with the property to get.2130 /// * `key` - Property name.2131 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue>;21322133 /// Get a set of token properties by key vector.2134 ///2135 /// * `token` - Token with the property to get.2136 /// * `keys` - Vector of property keys. If this parameter is [None](sp_std::result::Result),2137 /// then all properties are returned.2138 fn token_properties(&self, token: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property>;21392140 /// Amount of unique collection tokens2141 fn total_supply(&self) -> u32;21422143 /// Amount of different tokens account has.2144 ///2145 /// * `account` - The account for which need to get the balance.2146 fn account_balance(&self, account: T::CrossAccountId) -> u32;21472148 /// Amount of specific token account have.2149 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;21502151 /// Amount of token pieces2152 fn total_pieces(&self, token: TokenId) -> Option<u128>;21532154 /// Get the number of parts of the token that a trusted user can manage.2155 ///2156 /// * `sender` - Trusted user.2157 /// * `spender` - Owner of the token.2158 /// * `token` - The token for which to get the value.2159 fn allowance(2160 &self,2161 sender: T::CrossAccountId,2162 spender: T::CrossAccountId,2163 token: TokenId,2164 ) -> u128;21652166 /// Get extension for RFT collection.2167 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>>;21682169 /// The `operator` is allowed to transfer all tokens of the `owner` on their behalf.2170 /// * `owner` - Token owner2171 /// * `operator` - Operator2172 /// * `approve` - Should operator status be granted or revoked?2173 fn set_allowance_for_all(2174 &self,2175 owner: T::CrossAccountId,2176 operator: T::CrossAccountId,2177 approve: bool,2178 ) -> DispatchResultWithPostInfo;21792180 /// Tells whether the given `owner` approves the `operator`.2181 fn allowance_for_all(&self, owner: T::CrossAccountId, operator: T::CrossAccountId) -> bool;21822183 /// Repairs a possibly broken item.2184 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo;2185}21862187/// Extension for RFT collection.2188pub trait RefungibleExtensions<T>2189where2190 T: Config,2191{2192 /// Change the number of parts of the token.2193 ///2194 /// When the value changes down, this function is equivalent to burning parts of the token.2195 ///2196 /// * `sender` - The user calling the repartition operation. Must be the owner of the token.2197 /// * `token` - The token for which you want to change the number of parts.2198 /// * `amount` - The new value of the parts of the token.2199 fn repartition(2200 &self,2201 sender: &T::CrossAccountId,2202 token: TokenId,2203 amount: u128,2204 ) -> DispatchResultWithPostInfo;2205}22062207/// Merge [`DispatchResult`] with [`Weight`] into [`DispatchResultWithPostInfo`].2208///2209/// Used for [`CommonCollectionOperations`] implementations and flexible enough to do so.2210pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {2211 let post_info = PostDispatchInfo {2212 actual_weight: Some(weight),2213 pays_fee: Pays::Yes,2214 };2215 match res {2216 Ok(()) => Ok(post_info),2217 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),2218 }2219}22202221impl<T: Config> From<PropertiesError> for Error<T> {2222 fn from(error: PropertiesError) -> Self {2223 match error {2224 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,2225 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,2226 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,2227 PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,2228 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,2229 }2230 }2231}pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -577,14 +577,23 @@
})
}
- /// Batch operation to add, edit or remove properties for the token
+ /// A batch operation to add, edit or remove properties for a token.
+ /// It sets or removes a token's properties according to
+ /// `properties_updates` contents:
+ /// * sets a property under the <key> with the value provided `(<key>, Some(<value>))`
+ /// * removes a property under the <key> if the value is `None` `(<key>, None)`.
///
- /// All affected properties should have mutable permission and sender should have
- /// permission to edit those properties.
- ///
- /// - `nesting_budget`: Limit for searching parents in depth to check ownership.
+ /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
/// - `is_token_create`: Indicates that method is called during token initialization.
/// Allows to bypass ownership check.
+ ///
+ /// All affected properties should have `mutable` permission
+ /// to be **deleted** or to be **set more than once**,
+ /// and the sender should have permission to edit those properties.
+ ///
+ /// This function fires an event for each property change.
+ /// In case of an error, all the changes (including the events) will be reverted
+ /// since the function is transactional.
#[transactional]
fn modify_token_properties(
collection: &NonfungibleHandle<T>,
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -515,6 +515,23 @@
Ok(())
}
+ /// A batch operation to add, edit or remove properties for a token.
+ /// It sets or removes a token's properties according to
+ /// `properties_updates` contents:
+ /// * sets a property under the <key> with the value provided `(<key>, Some(<value>))`
+ /// * removes a property under the <key> if the value is `None` `(<key>, None)`.
+ ///
+ /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
+ /// - `is_token_create`: Indicates that method is called during token initialization.
+ /// Allows to bypass ownership check.
+ ///
+ /// All affected properties should have `mutable` permission
+ /// to be **deleted** or to be **set more than once**,
+ /// and the sender should have permission to edit those properties.
+ ///
+ /// This function fires an event for each property change.
+ /// In case of an error, all the changes (including the events) will be reverted
+ /// since the function is transactional.
#[transactional]
fn modify_token_properties(
collection: &RefungibleHandle<T>,