difftreelog
feat xcm nft support
in: master
5 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 alloc::boxed::Box;57use core::{58 marker::PhantomData,59 ops::{Deref, DerefMut},60 slice::from_ref,61 unreachable,62};6364use evm_coder::ToLog;65use frame_support::{66 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo},67 ensure, fail,68 traits::{69 fungible::{Balanced, Debt, Inspect},70 tokens::{Imbalance, Precision, Preservation},71 Get,72 },73 transactional,74};75pub use pallet::*;76use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};77use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};78use sp_core::H160;79use sp_runtime::{traits::Zero, ArithmeticError, DispatchError, DispatchResult};80use sp_std::vec::Vec;81use sp_weights::Weight;82use up_data_structs::{83 budget::Budget, mapping::TokenAddressMapping, AccessMode, Collection, CollectionId,84 CollectionLimits, CollectionMode, CollectionPermissions,85 CollectionProperties as CollectionPropertiesT, CollectionStats, CreateCollectionData,86 CreateItemData, CreateItemExData, PhantomType, PropertiesError, PropertiesPermissionMap,87 Property, PropertyKey, PropertyKeyPermission, PropertyPermission, PropertyScope, PropertyValue,88 RpcCollection, RpcCollectionFlags, SponsoringRateLimit, SponsorshipState, TokenChild,89 TokenData, TokenId, TokenOwnerError, TokenProperties, TrySetProperty, COLLECTION_ADMINS_LIMIT,90 COLLECTION_NUMBER_LIMIT, CUSTOM_DATA_LIMIT, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,91 MAX_SPONSOR_TIMEOUT, MAX_TOKEN_OWNERSHIP, MAX_TOKEN_PREFIX_LENGTH,92 NFT_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,93};94use up_pov_estimate_rpc::PovInfo;9596#[cfg(feature = "runtime-benchmarks")]97pub mod benchmarking;98pub mod dispatch;99pub mod erc;100pub mod eth;101pub mod helpers;102#[allow(missing_docs)]103pub mod weights;104105use weights::WeightInfo;106107/// Weight info.108pub type SelfWeightOf<T> = <T as Config>::WeightInfo;109110/// Collection handle contains information about collection data and id.111/// Also provides functionality to count consumed gas.112///113/// CollectionHandle is used as a generic wrapper for collections of all types (except native fungible).114/// It allows to perform common operations and queries on any collection type,115/// both completely general for all, as well as their respective implementations of [`CommonCollectionOperations`].116#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]117pub struct CollectionHandle<T: Config> {118 /// Collection id119 pub id: CollectionId,120 collection: Collection<T::AccountId>,121 /// Substrate recorder for counting consumed gas122 pub recorder: SubstrateRecorder<T>,123}124125impl<T: Config> WithRecorder<T> for CollectionHandle<T> {126 fn recorder(&self) -> &SubstrateRecorder<T> {127 &self.recorder128 }129 fn into_recorder(self) -> SubstrateRecorder<T> {130 self.recorder131 }132}133134impl<T: Config> CollectionHandle<T> {135 /// Same as [CollectionHandle::new] but with an explicit gas limit.136 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {137 Self::new_with_recorder(id, SubstrateRecorder::new(gas_limit))138 }139140 /// Same as [CollectionHandle::new] but with an existed [`SubstrateRecorder`].141 pub fn new_with_recorder(id: CollectionId, recorder: SubstrateRecorder<T>) -> Option<Self> {142 <CollectionById<T>>::get(id).map(|collection| Self {143 id,144 collection,145 recorder,146 })147 }148149 /// Retrives collection data from storage and creates collection handle with default parameters.150 /// If collection not found return `None`151 pub fn new(id: CollectionId) -> Option<Self> {152 Self::new_with_gas_limit(id, u64::MAX)153 }154155 /// Same as [`CollectionHandle::new`] but if collection not found [CollectionNotFound](Error::CollectionNotFound) returned.156 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {157 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)158 }159160 /// Consume gas for reading.161 pub fn consume_store_reads(162 &self,163 reads: u64,164 ) -> pallet_evm_coder_substrate::execution::Result<()> {165 self.recorder().consume_store_reads(reads)166 }167168 /// Consume gas for writing.169 pub fn consume_store_writes(170 &self,171 writes: u64,172 ) -> pallet_evm_coder_substrate::execution::Result<()> {173 self.recorder().consume_store_writes(writes)174 }175176 /// Consume gas for reading and writing.177 pub fn consume_store_reads_and_writes(178 &self,179 reads: u64,180 writes: u64,181 ) -> pallet_evm_coder_substrate::execution::Result<()> {182 self.recorder()183 .consume_store_reads_and_writes(reads, writes)184 }185186 /// Save collection to storage.187 pub fn save(&self) -> DispatchResult {188 <CollectionById<T>>::insert(self.id, &self.collection);189 Ok(())190 }191192 /// Set collection sponsor.193 ///194 /// Unique collections allows sponsoring for certain actions.195 /// This method allows you to set the sponsor of the collection.196 /// In order for sponsorship to become active, it must be confirmed through [`Self::confirm_sponsorship`].197 pub fn set_sponsor(198 &mut self,199 sender: &T::CrossAccountId,200 sponsor: T::AccountId,201 ) -> DispatchResult {202 self.check_is_internal()?;203 self.check_is_owner_or_admin(sender)?;204205 self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.clone());206207 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor));208 <PalletEvm<T>>::deposit_log(209 erc::CollectionHelpersEvents::CollectionChanged {210 collection_id: eth::collection_id_to_address(self.id),211 }212 .to_log(T::ContractAddress::get()),213 );214215 self.save()216 }217218 /// Force set `sponsor`.219 ///220 /// Differs from [`set_sponsor`][`Self::set_sponsor`] in that confirmation221 /// from the `sponsor` is not required.222 ///223 /// # Arguments224 ///225 /// * `sponsor`: ID of the account of the sponsor-to-be.226 pub fn force_set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {227 self.check_is_internal()?;228229 self.collection.sponsorship = SponsorshipState::Confirmed(sponsor.clone());230231 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor.clone()));232 <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sponsor));233 <PalletEvm<T>>::deposit_log(234 erc::CollectionHelpersEvents::CollectionChanged {235 collection_id: eth::collection_id_to_address(self.id),236 }237 .to_log(T::ContractAddress::get()),238 );239240 self.save()241 }242243 /// Confirm sponsorship244 ///245 /// In order for the sponsorship to become active, the user set as the sponsor must confirm their participation.246 /// Before confirming sponsorship, the user must be specified as the sponsor of the collection via [`Self::set_sponsor`].247 pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> DispatchResult {248 self.check_is_internal()?;249 ensure!(250 self.collection.sponsorship.pending_sponsor() == Some(sender),251 Error::<T>::ConfirmSponsorshipFail252 );253254 self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());255256 <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sender.clone()));257 <PalletEvm<T>>::deposit_log(258 erc::CollectionHelpersEvents::CollectionChanged {259 collection_id: eth::collection_id_to_address(self.id),260 }261 .to_log(T::ContractAddress::get()),262 );263264 self.save()265 }266267 /// Remove collection sponsor.268 pub fn remove_sponsor(&mut self, sender: &T::CrossAccountId) -> DispatchResult {269 self.check_is_internal()?;270 self.check_is_owner_or_admin(sender)?;271272 self.collection.sponsorship = SponsorshipState::Disabled;273274 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));275 <PalletEvm<T>>::deposit_log(276 erc::CollectionHelpersEvents::CollectionChanged {277 collection_id: eth::collection_id_to_address(self.id),278 }279 .to_log(T::ContractAddress::get()),280 );281 self.save()282 }283284 /// Force remove `sponsor`.285 ///286 /// Differs from `remove_sponsor` in that287 /// it doesn't require consent from the `owner` of the collection.288 pub fn force_remove_sponsor(&mut self) -> DispatchResult {289 self.check_is_internal()?;290291 self.collection.sponsorship = SponsorshipState::Disabled;292293 <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));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 );300 self.save()301 }302303 /// Checks that the collection was created with, and must be operated upon through **Unique API**.304 /// Now check only the `external` flag and if it's **true**, then return [`Error::CollectionIsExternal`] error.305 pub fn check_is_internal(&self) -> DispatchResult {306 if self.flags.external {307 return Err(<Error<T>>::CollectionIsExternal)?;308 }309310 Ok(())311 }312313 /// Checks that the collection was created with, and must be operated upon through an **assimilated API**.314 /// Now check only the `external` flag and if it's **false**, then return [`Error::CollectionIsInternal`] error.315 pub fn check_is_external(&self) -> DispatchResult {316 if !self.flags.external {317 return Err(<Error<T>>::CollectionIsInternal)?;318 }319320 Ok(())321 }322}323324impl<T: Config> Deref for CollectionHandle<T> {325 type Target = Collection<T::AccountId>;326327 fn deref(&self) -> &Self::Target {328 &self.collection329 }330}331332impl<T: Config> DerefMut for CollectionHandle<T> {333 fn deref_mut(&mut self) -> &mut Self::Target {334 &mut self.collection335 }336}337338impl<T: Config> CollectionHandle<T> {339 /// Checks if the `user` is the owner of the collection.340 pub fn check_is_owner(&self, user: &T::CrossAccountId) -> DispatchResult {341 ensure!(*user.as_sub() == self.owner, <Error<T>>::NoPermission);342 Ok(())343 }344345 /// Returns **true** if the `user` is the owner or administrator of the collection.346 pub fn is_owner_or_admin(&self, user: &T::CrossAccountId) -> bool {347 *user.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, user))348 }349350 /// Checks if the `user` is the owner or administrator of the collection.351 pub fn check_is_owner_or_admin(&self, user: &T::CrossAccountId) -> DispatchResult {352 ensure!(self.is_owner_or_admin(user), <Error<T>>::NoPermission);353 Ok(())354 }355356 /// Returns **true** if357 /// * the `user`is a collection owner or admin358 /// * the collection limits allow the owner/admins to transfer/burn any collection token359 pub fn ignores_token_restrictions(&self, user: &T::CrossAccountId) -> bool {360 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)361 }362363 /// Return **true** if `user` does not have enough token parts, and he can ignore such restrictions.364 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {365 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)366 }367368 /// Checks if the user is in the allow list. If not [Error::AddressNotInAllowlist] returns.369 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {370 ensure!(371 <Allowlist<T>>::get((self.id, user)),372 <Error<T>>::AddressNotInAllowlist373 );374 Ok(())375 }376377 /// Changes collection owner to another account378 /// #### Store read/writes379 /// 1 writes380 pub fn change_owner(381 &mut self,382 caller: T::CrossAccountId,383 new_owner: T::CrossAccountId,384 ) -> DispatchResult {385 self.check_is_internal()?;386 self.check_is_owner(&caller)?;387 self.collection.owner = new_owner.as_sub().clone();388389 <Pallet<T>>::deposit_event(Event::<T>::CollectionOwnerChanged(390 self.id,391 new_owner.as_sub().clone(),392 ));393 <PalletEvm<T>>::deposit_log(394 erc::CollectionHelpersEvents::CollectionChanged {395 collection_id: eth::collection_id_to_address(self.id),396 }397 .to_log(T::ContractAddress::get()),398 );399400 self.save()401 }402}403404#[frame_support::pallet]405pub mod pallet {406407 use dispatch::CollectionDispatch;408 use frame_support::{409 pallet_prelude::*, storage::Key, traits::StorageVersion, Blake2_128Concat,410 };411 use scale_info::TypeInfo;412 use up_data_structs::{mapping::TokenAddressMapping, TokenId};413 use weights::WeightInfo;414415 use super::*;416417 #[pallet::config]418 pub trait Config:419 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::Config + TypeInfo420 {421 /// Weight information for functions of this pallet.422 type WeightInfo: WeightInfo;423424 /// Events compatible with [`frame_system::Config::Event`].425 type RuntimeEvent: IsType<<Self as frame_system::Config>::RuntimeEvent> + From<Event<Self>>;426427 /// Handler of accounts and payment.428 type Currency: Balanced<Self::AccountId> + Inspect<Self::AccountId>;429430 /// Set price to create a collection.431 #[pallet::constant]432 type CollectionCreationPrice: Get<433 <<Self as Config>::Currency as Inspect<Self::AccountId>>::Balance,434 >;435436 /// Dispatcher of operations on collections.437 type CollectionDispatch: CollectionDispatch<Self>;438439 /// Account which holds the chain's treasury.440 type TreasuryAccountId: Get<Self::AccountId>;441442 /// Address under which the CollectionHelper contract would be available.443 #[pallet::constant]444 type ContractAddress: Get<H160>;445446 /// Mapper for token addresses to Ethereum addresses.447 type EvmTokenAddressMapping: TokenAddressMapping<H160>;448449 /// Mapper for token addresses to [`CrossAccountId`].450 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;451 }452453 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);454 /// Collection id for native fungible collction.455 pub const NATIVE_FUNGIBLE_COLLECTION_ID: CollectionId = CollectionId(0);456457 #[pallet::pallet]458 #[pallet::storage_version(STORAGE_VERSION)]459 pub struct Pallet<T>(_);460461 #[pallet::extra_constants]462 impl<T: Config> Pallet<T> {463 /// Maximum admins per collection.464 pub fn collection_admins_limit() -> u32 {465 COLLECTION_ADMINS_LIMIT466 }467 }468469 #[pallet::genesis_config]470 pub struct GenesisConfig<T>(PhantomData<T>);471472 impl<T: Config> Default for GenesisConfig<T> {473 fn default() -> Self {474 Self(Default::default())475 }476 }477478 #[pallet::genesis_build]479 impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {480 fn build(&self) {481 StorageVersion::new(1).put::<Pallet<T>>();482 }483 }484485 impl<T: Config> Pallet<T> {486 /// Helper function that handles deposit events487 pub fn deposit_event(event: Event<T>) {488 let event = <T as Config>::RuntimeEvent::from(event);489 let event = event.into();490 <frame_system::Pallet<T>>::deposit_event(event)491 }492 }493494 #[pallet::event]495 pub enum Event<T: Config> {496 /// New collection was created497 CollectionCreated(498 /// Globally unique identifier of newly created collection.499 CollectionId,500 /// [`CollectionMode`] converted into _u8_.501 u8,502 /// Collection owner.503 T::AccountId,504 ),505506 /// New collection was destroyed507 CollectionDestroyed(508 /// Globally unique identifier of collection.509 CollectionId,510 ),511512 /// New item was created.513 ItemCreated(514 /// Id of the collection where item was created.515 CollectionId,516 /// Id of an item. Unique within the collection.517 TokenId,518 /// Owner of newly created item519 T::CrossAccountId,520 /// Always 1 for NFT521 u128,522 ),523524 /// Collection item was burned.525 ItemDestroyed(526 /// Id of the collection where item was destroyed.527 CollectionId,528 /// Identifier of burned NFT.529 TokenId,530 /// Which user has destroyed its tokens.531 T::CrossAccountId,532 /// Amount of token pieces destroed. Always 1 for NFT.533 u128,534 ),535536 /// Item was transferred537 Transfer(538 /// Id of collection to which item is belong.539 CollectionId,540 /// Id of an item.541 TokenId,542 /// Original owner of item.543 T::CrossAccountId,544 /// New owner of item.545 T::CrossAccountId,546 /// Amount of token pieces transfered. Always 1 for NFT.547 u128,548 ),549550 /// Amount pieces of token owned by `sender` was approved for `spender`.551 Approved(552 /// Id of collection to which item is belong.553 CollectionId,554 /// Id of an item.555 TokenId,556 /// Original owner of item.557 T::CrossAccountId,558 /// Id for which the approval was granted.559 T::CrossAccountId,560 /// Amount of token pieces transfered. Always 1 for NFT.561 u128,562 ),563564 /// A `sender` approves operations on all owned tokens for `spender`.565 ApprovedForAll(566 /// Id of collection to which item is belong.567 CollectionId,568 /// Owner of a wallet.569 T::CrossAccountId,570 /// Id for which operator status was granted or rewoked.571 T::CrossAccountId,572 /// Is operator status granted or revoked?573 bool,574 ),575576 /// The colletion property has been added or edited.577 CollectionPropertySet(578 /// Id of collection to which property has been set.579 CollectionId,580 /// The property that was set.581 PropertyKey,582 ),583584 /// The property has been deleted.585 CollectionPropertyDeleted(586 /// Id of collection to which property has been deleted.587 CollectionId,588 /// The property that was deleted.589 PropertyKey,590 ),591592 /// The token property has been added or edited.593 TokenPropertySet(594 /// Identifier of the collection whose token has the property set.595 CollectionId,596 /// The token for which the property was set.597 TokenId,598 /// The property that was set.599 PropertyKey,600 ),601602 /// The token property has been deleted.603 TokenPropertyDeleted(604 /// Identifier of the collection whose token has the property deleted.605 CollectionId,606 /// The token for which the property was deleted.607 TokenId,608 /// The property that was deleted.609 PropertyKey,610 ),611612 /// The token property permission of a collection has been set.613 PropertyPermissionSet(614 /// ID of collection to which property permission has been set.615 CollectionId,616 /// The property permission that was set.617 PropertyKey,618 ),619620 /// Address was added to the allow list.621 AllowListAddressAdded(622 /// ID of the affected collection.623 CollectionId,624 /// Address of the added account.625 T::CrossAccountId,626 ),627628 /// Address was removed from the allow list.629 AllowListAddressRemoved(630 /// ID of the affected collection.631 CollectionId,632 /// Address of the removed account.633 T::CrossAccountId,634 ),635636 /// Collection admin was added.637 CollectionAdminAdded(638 /// ID of the affected collection.639 CollectionId,640 /// Admin address.641 T::CrossAccountId,642 ),643644 /// Collection admin was removed.645 CollectionAdminRemoved(646 /// ID of the affected collection.647 CollectionId,648 /// Removed admin address.649 T::CrossAccountId,650 ),651652 /// Collection limits were set.653 CollectionLimitSet(654 /// ID of the affected collection.655 CollectionId,656 ),657658 /// Collection owned was changed.659 CollectionOwnerChanged(660 /// ID of the affected collection.661 CollectionId,662 /// New owner address.663 T::AccountId,664 ),665666 /// Collection permissions were set.667 CollectionPermissionSet(668 /// ID of the affected collection.669 CollectionId,670 ),671672 /// Collection sponsor was set.673 CollectionSponsorSet(674 /// ID of the affected collection.675 CollectionId,676 /// New sponsor address.677 T::AccountId,678 ),679680 /// New sponsor was confirm.681 SponsorshipConfirmed(682 /// ID of the affected collection.683 CollectionId,684 /// New sponsor address.685 T::AccountId,686 ),687688 /// Collection sponsor was removed.689 CollectionSponsorRemoved(690 /// ID of the affected collection.691 CollectionId,692 ),693 }694695 #[pallet::error]696 pub enum Error<T> {697 /// This collection does not exist.698 CollectionNotFound,699 /// Sender parameter and item owner must be equal.700 MustBeTokenOwner,701 /// No permission to perform action702 NoPermission,703 /// Destroying only empty collections is allowed704 CantDestroyNotEmptyCollection,705 /// Collection is not in mint mode.706 PublicMintingNotAllowed,707 /// Address is not in allow list.708 AddressNotInAllowlist,709710 /// Collection name can not be longer than 63 char.711 CollectionNameLimitExceeded,712 /// Collection description can not be longer than 255 char.713 CollectionDescriptionLimitExceeded,714 /// Token prefix can not be longer than 15 char.715 CollectionTokenPrefixLimitExceeded,716 /// Total collections bound exceeded.717 TotalCollectionsLimitExceeded,718 /// Exceeded max admin count719 CollectionAdminCountExceeded,720 /// Collection limit bounds per collection exceeded721 CollectionLimitBoundsExceeded,722 /// Tried to enable permissions which are only permitted to be disabled723 OwnerPermissionsCantBeReverted,724 /// Collection settings not allowing items transferring725 TransferNotAllowed,726 /// Account token limit exceeded per collection727 AccountTokenLimitExceeded,728 /// Collection token limit exceeded729 CollectionTokenLimitExceeded,730 /// Metadata flag frozen731 MetadataFlagFrozen,732733 /// Item does not exist734 TokenNotFound,735 /// Item is balance not enough736 TokenValueTooLow,737 /// Requested value is more than the approved738 ApprovedValueTooLow,739 /// Tried to approve more than owned740 CantApproveMoreThanOwned,741 /// Only spending from eth mirror could be approved742 AddressIsNotEthMirror,743744 /// Can't transfer tokens to ethereum zero address745 AddressIsZero,746747 /// The operation is not supported748 UnsupportedOperation,749750 /// Insufficient funds to perform an action751 NotSufficientFounds,752753 /// User does not satisfy the nesting rule754 UserIsNotAllowedToNest,755 /// Only tokens from specific collections may nest tokens under this one756 SourceCollectionIsNotAllowedToNest,757758 /// Tried to store more data than allowed in collection field759 CollectionFieldSizeExceeded,760761 /// Tried to store more property data than allowed762 NoSpaceForProperty,763764 /// Tried to store more property keys than allowed765 PropertyLimitReached,766767 /// Property key is too long768 PropertyKeyIsTooLong,769770 /// Only ASCII letters, digits, and symbols `_`, `-`, and `.` are allowed771 InvalidCharacterInPropertyKey,772773 /// Empty property keys are forbidden774 EmptyPropertyKey,775776 /// Tried to access an external collection with an internal API777 CollectionIsExternal,778779 /// Tried to access an internal collection with an external API780 CollectionIsInternal,781782 /// This address is not set as sponsor, use setCollectionSponsor first.783 ConfirmSponsorshipFail,784785 /// The user is not an administrator.786 UserIsNotCollectionAdmin,787788 /// Fungible tokens hold no ID, and the default value of TokenId for a fungible collection is 0.789 FungibleItemsHaveNoId,790791 /// Not Fungible item data used to mint in Fungible collection.792 NotFungibleDataUsedToMintFungibleCollectionToken,793 }794795 /// Storage of the count of created collections. Essentially contains the last collection ID.796 #[pallet::storage]797 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;798799 /// Storage of the count of deleted collections.800 #[pallet::storage]801 pub type DestroyedCollectionCount<T> =802 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;803804 /// Storage of collection info.805 #[pallet::storage]806 pub type CollectionById<T> = StorageMap<807 Hasher = Blake2_128Concat,808 Key = CollectionId,809 Value = Collection<<T as frame_system::Config>::AccountId>,810 QueryKind = OptionQuery,811 >;812813 /// Storage of collection properties.814 #[pallet::storage]815 #[pallet::getter(fn collection_properties)]816 pub type CollectionProperties<T> = StorageMap<817 Hasher = Blake2_128Concat,818 Key = CollectionId,819 Value = CollectionPropertiesT,820 QueryKind = ValueQuery,821 >;822823 /// Storage of token property permissions of a collection.824 #[pallet::storage]825 #[pallet::getter(fn property_permissions)]826 pub type CollectionPropertyPermissions<T> = StorageMap<827 Hasher = Blake2_128Concat,828 Key = CollectionId,829 Value = PropertiesPermissionMap,830 QueryKind = ValueQuery,831 >;832833 /// Storage of the amount of collection admins.834 #[pallet::storage]835 pub type AdminAmount<T> = StorageMap<836 Hasher = Blake2_128Concat,837 Key = CollectionId,838 Value = u32,839 QueryKind = ValueQuery,840 >;841842 /// List of collection admins.843 #[pallet::storage]844 pub type IsAdmin<T: Config> = StorageNMap<845 Key = (846 Key<Blake2_128Concat, CollectionId>,847 Key<Blake2_128Concat, T::CrossAccountId>,848 ),849 Value = bool,850 QueryKind = ValueQuery,851 >;852853 /// Allowlisted collection users.854 #[pallet::storage]855 pub type Allowlist<T: Config> = StorageNMap<856 Key = (857 Key<Blake2_128Concat, CollectionId>,858 Key<Blake2_128Concat, T::CrossAccountId>,859 ),860 Value = bool,861 QueryKind = ValueQuery,862 >;863864 /// Not used by code, exists only to provide some types to metadata.865 #[pallet::storage]866 pub type DummyStorageValue<T: Config> = StorageValue<867 Value = (868 CollectionStats,869 CollectionId,870 TokenId,871 TokenChild,872 PhantomType<(873 TokenData<T::CrossAccountId>,874 RpcCollection<T::AccountId>,875 // PoV Estimate Info876 PovInfo,877 )>,878 ),879 QueryKind = OptionQuery,880 >;881}882883enum LazyValueState<'a, T> {884 Pending(Box<dyn FnOnce() -> T + 'a>),885 InProgress,886 Computed(T),887}888889/// Value representation with delayed initialization time.890pub struct LazyValue<'a, T> {891 state: LazyValueState<'a, T>,892}893894impl<'a, T> LazyValue<'a, T> {895 /// Create a new LazyValue.896 pub fn new(f: impl FnOnce() -> T + 'a) -> Self {897 Self {898 state: LazyValueState::Pending(Box::new(f)),899 }900 }901902 /// Get the value. If it is called the first time, the value will be initialized.903 pub fn value(&mut self) -> &T {904 self.force_value();905 self.value_mut()906 }907908 /// Get the value. If it is called the first time, the value will be initialized.909 pub fn value_mut(&mut self) -> &mut T {910 self.force_value();911912 if let LazyValueState::Computed(value) = &mut self.state {913 value914 } else {915 unreachable!()916 }917 }918919 fn into_inner(mut self) -> T {920 self.force_value();921 if let LazyValueState::Computed(value) = self.state {922 value923 } else {924 unreachable!()925 }926 }927928 /// Is value initialized?929 pub fn has_value(&self) -> bool {930 matches!(self.state, LazyValueState::Computed(_))931 }932933 fn force_value(&mut self) {934 use LazyValueState::*;935936 if self.has_value() {937 return;938 }939940 match sp_std::mem::replace(&mut self.state, InProgress) {941 Pending(f) => self.state = Computed(f()),942 _ => panic!("recursion isn't supported"),943 }944 }945}946947fn check_token_permissions<T: Config>(948 collection_admin_permitted: bool,949 token_owner_permitted: bool,950 is_collection_admin: &mut LazyValue<bool>,951 is_token_owner: &mut LazyValue<Result<bool, DispatchError>>,952 is_token_exist: &mut LazyValue<bool>,953) -> DispatchResult {954 if !(collection_admin_permitted && *is_collection_admin.value()955 || token_owner_permitted && (*is_token_owner.value())?)956 {957 fail!(<Error<T>>::NoPermission);958 }959960 let token_exist_due_to_owner_check_success =961 is_token_owner.has_value() && (*is_token_owner.value())?;962963 // If the token owner check has occurred and succeeded,964 // we know the token exists (otherwise, the owner check must fail).965 if !token_exist_due_to_owner_check_success {966 // If the token owner check didn't occur,967 // we must check the token's existence ourselves.968 if !is_token_exist.value() {969 fail!(<Error<T>>::TokenNotFound);970 }971 }972973 Ok(())974}975976impl<T: Config> Pallet<T> {977 /// Enshure that receiver address is correct.978 ///979 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens.980 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {981 ensure!(982 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,983 <Error<T>>::AddressIsZero984 );985 Ok(())986 }987988 /// Get a vector of collection admins.989 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {990 <IsAdmin<T>>::iter_prefix((collection,))991 .map(|(a, _)| a)992 .collect()993 }994995 /// Get a vector of users allowed to mint tokens.996 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {997 <Allowlist<T>>::iter_prefix((collection,))998 .map(|(a, _)| a)999 .collect()1000 }10011002 /// Is `user` allowed to mint token in `collection`.1003 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {1004 <Allowlist<T>>::get((collection, user))1005 }10061007 /// Get statistics of collections.1008 pub fn collection_stats() -> CollectionStats {1009 let created = <CreatedCollectionCount<T>>::get();1010 let destroyed = <DestroyedCollectionCount<T>>::get();1011 CollectionStats {1012 created: created.0,1013 destroyed: destroyed.0,1014 alive: created.0 - destroyed.0,1015 }1016 }10171018 /// Get the effective limits for the collection.1019 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {1020 let collection = <CollectionById<T>>::get(collection)?;1021 let limits = collection.limits;1022 let effective_limits = CollectionLimits {1023 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),1024 sponsored_data_size: Some(limits.sponsored_data_size()),1025 sponsored_data_rate_limit: Some(1026 limits1027 .sponsored_data_rate_limit1028 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),1029 ),1030 token_limit: Some(limits.token_limit()),1031 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(1032 match collection.mode {1033 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1034 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1035 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1036 },1037 )),1038 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),1039 owner_can_transfer: Some(limits.owner_can_transfer()),1040 owner_can_destroy: Some(limits.owner_can_destroy()),1041 transfers_enabled: Some(limits.transfers_enabled()),1042 };10431044 Some(effective_limits)1045 }10461047 /// Returns information about the `collection` adapted for rpc.1048 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {1049 let Collection {1050 name,1051 description,1052 owner,1053 mode,1054 token_prefix,1055 sponsorship,1056 limits,1057 permissions,1058 flags,1059 } = <CollectionById<T>>::get(collection)?;10601061 let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)1062 .into_iter()1063 .map(|(key, permission)| PropertyKeyPermission { key, permission })1064 .collect();10651066 let properties = <CollectionProperties<T>>::get(collection)1067 .into_iter()1068 .map(|(key, value)| Property { key, value })1069 .collect();10701071 let permissions = CollectionPermissions {1072 access: Some(permissions.access()),1073 mint_mode: Some(permissions.mint_mode()),1074 nesting: Some(permissions.nesting().clone()),1075 };10761077 Some(RpcCollection {1078 name: name.into_inner(),1079 description: description.into_inner(),1080 owner,1081 mode,1082 token_prefix: token_prefix.into_inner(),1083 sponsorship,1084 limits,1085 permissions,1086 token_property_permissions,1087 properties,1088 read_only: flags.external,10891090 flags: RpcCollectionFlags {1091 foreign: flags.foreign,1092 erc721metadata: flags.erc721metadata,1093 },1094 })1095 }1096}10971098macro_rules! limit_default {1099 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1100 $(1101 if let Some($new) = $new.$field {1102 let $old = $old.$field($($arg)?);1103 let _ = $new;1104 let _ = $old;1105 $check1106 } else {1107 $new.$field = $old.$field1108 }1109 )*1110 }};1111}1112macro_rules! limit_default_clone {1113 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1114 $(1115 if let Some($new) = $new.$field.clone() {1116 let $old = $old.$field($($arg)?);1117 let _ = $new;1118 let _ = $old;1119 $check1120 } else {1121 $new.$field = $old.$field.clone()1122 }1123 )*1124 }};1125}11261127impl<T: Config> Pallet<T> {1128 /// Create new collection.1129 ///1130 /// * `owner` - The owner of the collection.1131 /// * `data` - Description of the created collection.1132 /// * `flags` - Extra flags to store.1133 pub fn init_collection(1134 owner: T::CrossAccountId,1135 payer: T::CrossAccountId,1136 data: CreateCollectionData<T::CrossAccountId>,1137 ) -> Result<CollectionId, DispatchError> {1138 ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);11391140 // Take a (non-refundable) deposit of collection creation1141 {1142 let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();1143 imbalance.subsume(<T as Config>::Currency::deposit(1144 &T::TreasuryAccountId::get(),1145 T::CollectionCreationPrice::get(),1146 Precision::Exact,1147 )?);1148 let credit =1149 <T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)1150 .map_err(|_| Error::<T>::NotSufficientFounds)?;11511152 debug_assert!(credit.peek().is_zero())1153 }11541155 Self::init_collection_internal(owner, data)1156 }11571158 /// Initializes the collection with ForeignCollection flag. Returns [CollectionId] on success, [DispatchError] otherwise.1159 pub fn init_foreign_collection(1160 owner: T::CrossAccountId,1161 mut data: CreateCollectionData<T::CrossAccountId>,1162 ) -> Result<CollectionId, DispatchError> {1163 data.flags.foreign = true;1164 let id = Self::init_collection_internal(owner, data)?;1165 Ok(id)1166 }11671168 fn init_collection_internal(1169 owner: T::CrossAccountId,1170 data: CreateCollectionData<T::CrossAccountId>,1171 ) -> Result<CollectionId, DispatchError> {1172 {1173 ensure!(1174 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,1175 Error::<T>::CollectionTokenPrefixLimitExceeded1176 );1177 }11781179 let created_count = <CreatedCollectionCount<T>>::get()1180 .01181 .checked_add(1)1182 .ok_or(ArithmeticError::Overflow)?;1183 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;1184 let id = CollectionId(created_count);11851186 // bound Total number of collections1187 ensure!(1188 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,1189 <Error<T>>::TotalCollectionsLimitExceeded1190 );11911192 // =========11931194 let collection = Collection {1195 owner: owner.as_sub().clone(),1196 name: data.name,1197 mode: data.mode.clone(),1198 description: data.description,1199 token_prefix: data.token_prefix,1200 sponsorship: data1201 .pending_sponsor1202 .map(|sponsor| SponsorshipState::Unconfirmed(sponsor.as_sub().clone()))1203 .unwrap_or_default(),1204 limits: data1205 .limits1206 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))1207 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,1208 permissions: data1209 .permissions1210 .map(|permissions| {1211 Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions)1212 })1213 .unwrap_or_else(|| Ok(CollectionPermissions::default()))?,1214 flags: data.flags,1215 };12161217 let mut collection_properties = CollectionPropertiesT::new();1218 collection_properties1219 .try_set_from_iter(data.properties.into_iter())1220 .map_err(<Error<T>>::from)?;12211222 CollectionProperties::<T>::insert(id, collection_properties);12231224 let mut token_props_permissions = PropertiesPermissionMap::new();1225 token_props_permissions1226 .try_set_from_iter(data.token_property_permissions.into_iter())1227 .map_err(<Error<T>>::from)?;12281229 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);12301231 let mut admin_amount = 0u32;1232 for admin in data.admin_list.iter() {1233 if !<IsAdmin<T>>::get((id, admin)) {1234 <IsAdmin<T>>::insert((id, admin), true);1235 admin_amount = admin_amount1236 .checked_add(1)1237 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1238 }1239 }1240 ensure!(1241 admin_amount <= Self::collection_admins_limit(),1242 <Error<T>>::CollectionAdminCountExceeded,1243 );1244 <AdminAmount<T>>::insert(id, admin_amount);12451246 <CreatedCollectionCount<T>>::put(created_count);1247 <Pallet<T>>::deposit_event(Event::CollectionCreated(1248 id,1249 data.mode.id(),1250 owner.as_sub().clone(),1251 ));1252 <PalletEvm<T>>::deposit_log(1253 erc::CollectionHelpersEvents::CollectionCreated {1254 owner: *owner.as_eth(),1255 collection_id: eth::collection_id_to_address(id),1256 }1257 .to_log(T::ContractAddress::get()),1258 );1259 <CollectionById<T>>::insert(id, collection);1260 Ok(id)1261 }12621263 /// Destroy collection.1264 ///1265 /// * `collection` - Collection handler.1266 /// * `sender` - The owner or administrator of the collection.1267 pub fn destroy_collection(1268 collection: CollectionHandle<T>,1269 sender: &T::CrossAccountId,1270 ) -> DispatchResult {1271 ensure!(1272 collection.limits.owner_can_destroy(),1273 <Error<T>>::NoPermission,1274 );1275 collection.check_is_owner(sender)?;12761277 let destroyed_collections = <DestroyedCollectionCount<T>>::get()1278 .01279 .checked_add(1)1280 .ok_or(ArithmeticError::Overflow)?;12811282 // =========12831284 <DestroyedCollectionCount<T>>::put(destroyed_collections);1285 <CollectionById<T>>::remove(collection.id);1286 <AdminAmount<T>>::remove(collection.id);1287 let _ = <IsAdmin<T>>::clear_prefix((collection.id,), u32::MAX, None);1288 let _ = <Allowlist<T>>::clear_prefix((collection.id,), u32::MAX, None);1289 <CollectionProperties<T>>::remove(collection.id);12901291 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));12921293 <PalletEvm<T>>::deposit_log(1294 erc::CollectionHelpersEvents::CollectionDestroyed {1295 collection_id: eth::collection_id_to_address(collection.id),1296 }1297 .to_log(T::ContractAddress::get()),1298 );1299 Ok(())1300 }13011302 /// This function sets or removes a collection properties according to1303 /// `properties_updates` contents:1304 /// * sets a property under the <key> with the value provided `(<key>, Some(<value>))`1305 /// * removes a property under the <key> if the value is `None` `(<key>, None)`.1306 ///1307 /// This function fires an event for each property change.1308 /// In case of an error, all the changes (including the events) will be reverted1309 /// since the function is transactional.1310 #[transactional]1311 fn modify_collection_properties(1312 collection: &CollectionHandle<T>,1313 sender: &T::CrossAccountId,1314 properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,1315 ) -> DispatchResult {1316 collection.check_is_owner_or_admin(sender)?;13171318 let mut stored_properties = <CollectionProperties<T>>::get(collection.id);13191320 for (key, value) in properties_updates {1321 match value {1322 Some(value) => {1323 stored_properties1324 .try_set(key.clone(), value)1325 .map_err(<Error<T>>::from)?;13261327 Self::deposit_event(Event::CollectionPropertySet(collection.id, key));1328 <PalletEvm<T>>::deposit_log(1329 erc::CollectionHelpersEvents::CollectionChanged {1330 collection_id: eth::collection_id_to_address(collection.id),1331 }1332 .to_log(T::ContractAddress::get()),1333 );1334 }1335 None => {1336 stored_properties.remove(&key).map_err(<Error<T>>::from)?;13371338 Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, key));1339 <PalletEvm<T>>::deposit_log(1340 erc::CollectionHelpersEvents::CollectionChanged {1341 collection_id: eth::collection_id_to_address(collection.id),1342 }1343 .to_log(T::ContractAddress::get()),1344 );1345 }1346 }1347 }13481349 <CollectionProperties<T>>::set(collection.id, stored_properties);13501351 Ok(())1352 }13531354 /// Sets or unsets the approval of a given operator.1355 ///1356 /// The `operator` is allowed to transfer all token pieces of the `owner` on their behalf.1357 /// - `owner`: Token owner1358 /// - `operator`: Operator1359 /// - `approve`: Should operator status be granted or revoked?1360 pub fn set_allowance_for_all(1361 collection: &CollectionHandle<T>,1362 owner: &T::CrossAccountId,1363 operator: &T::CrossAccountId,1364 approve: bool,1365 set_allowance: impl FnOnce(),1366 log: evm_coder::ethereum::Log,1367 ) -> DispatchResult {1368 if collection.permissions.access() == AccessMode::AllowList {1369 collection.check_allowlist(owner)?;1370 collection.check_allowlist(operator)?;1371 }13721373 Self::ensure_correct_receiver(operator)?;13741375 set_allowance();13761377 <PalletEvm<T>>::deposit_log(log);1378 Self::deposit_event(Event::ApprovedForAll(1379 collection.id,1380 owner.clone(),1381 operator.clone(),1382 approve,1383 ));1384 Ok(())1385 }13861387 /// Set collection property.1388 ///1389 /// * `collection` - Collection handler.1390 /// * `sender` - The owner or administrator of the collection.1391 /// * `property` - The property to set.1392 pub fn set_collection_property(1393 collection: &CollectionHandle<T>,1394 sender: &T::CrossAccountId,1395 property: Property,1396 ) -> DispatchResult {1397 Self::set_collection_properties(collection, sender, [property].into_iter())1398 }13991400 /// Set a scoped collection property, where the scope is a special prefix1401 /// prohibiting a user access to change the property directly.1402 ///1403 /// * `collection_id` - ID of the collection for which the property is being set.1404 /// * `scope` - Property scope.1405 /// * `property` - The property to set.1406 pub fn set_scoped_collection_property(1407 collection_id: CollectionId,1408 scope: PropertyScope,1409 property: Property,1410 ) -> DispatchResult {1411 CollectionProperties::<T>::try_mutate(collection_id, |properties| {1412 properties.try_scoped_set(scope, property.key, property.value)1413 })1414 .map_err(<Error<T>>::from)?;14151416 Ok(())1417 }14181419 /// Set scoped collection properties, where the scope is a special prefix1420 /// prohibiting a user access to change the properties directly.1421 ///1422 /// * `collection_id` - ID of the collection for which the properties is being set.1423 /// * `scope` - Property scope.1424 /// * `properties` - The properties to set.1425 pub fn set_scoped_collection_properties(1426 collection_id: CollectionId,1427 scope: PropertyScope,1428 properties: impl Iterator<Item = Property>,1429 ) -> DispatchResult {1430 CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {1431 stored_properties.try_scoped_set_from_iter(scope, properties)1432 })1433 .map_err(<Error<T>>::from)?;14341435 Ok(())1436 }14371438 /// Set collection properties.1439 ///1440 /// * `collection` - Collection handler.1441 /// * `sender` - The owner or administrator of the collection.1442 /// * `properties` - The properties to set.1443 pub fn set_collection_properties(1444 collection: &CollectionHandle<T>,1445 sender: &T::CrossAccountId,1446 properties: impl Iterator<Item = Property>,1447 ) -> DispatchResult {1448 Self::modify_collection_properties(1449 collection,1450 sender,1451 properties.map(|property| (property.key, Some(property.value))),1452 )1453 }14541455 /// Delete collection property.1456 ///1457 /// * `collection` - Collection handler.1458 /// * `sender` - The owner or administrator of the collection.1459 /// * `property` - The property to delete.1460 pub fn delete_collection_property(1461 collection: &CollectionHandle<T>,1462 sender: &T::CrossAccountId,1463 property_key: PropertyKey,1464 ) -> DispatchResult {1465 Self::delete_collection_properties(collection, sender, [property_key].into_iter())1466 }14671468 /// Delete collection properties.1469 ///1470 /// * `collection` - Collection handler.1471 /// * `sender` - The owner or administrator of the collection.1472 /// * `properties` - The properties to delete.1473 pub fn delete_collection_properties(1474 collection: &CollectionHandle<T>,1475 sender: &T::CrossAccountId,1476 property_keys: impl Iterator<Item = PropertyKey>,1477 ) -> DispatchResult {1478 Self::modify_collection_properties(collection, sender, property_keys.map(|key| (key, None)))1479 }14801481 /// Set collection propetry permission without any checks.1482 ///1483 /// Used for migrations.1484 ///1485 /// * `collection` - Collection handler.1486 /// * `property_permissions` - Property permissions.1487 pub fn set_property_permission_unchecked(1488 collection: CollectionId,1489 property_permission: PropertyKeyPermission,1490 ) -> DispatchResult {1491 <CollectionPropertyPermissions<T>>::try_mutate(collection, |permissions| {1492 permissions.try_set(property_permission.key, property_permission.permission)1493 })1494 .map_err(<Error<T>>::from)?;1495 Ok(())1496 }14971498 /// Set collection property permission.1499 ///1500 /// * `collection` - Collection handler.1501 /// * `sender` - The owner or administrator of the collection.1502 /// * `property_permission` - Property permission.1503 pub fn set_property_permission(1504 collection: &CollectionHandle<T>,1505 sender: &T::CrossAccountId,1506 property_permission: PropertyKeyPermission,1507 ) -> DispatchResult {1508 Self::set_scoped_property_permission(1509 collection,1510 sender,1511 PropertyScope::None,1512 property_permission,1513 )1514 }15151516 /// Set collection property permission with scope.1517 ///1518 /// * `collection` - Collection handler.1519 /// * `sender` - The owner or administrator of the collection.1520 /// * `scope` - Property scope.1521 /// * `property_permission` - Property permission.1522 pub fn set_scoped_property_permission(1523 collection: &CollectionHandle<T>,1524 sender: &T::CrossAccountId,1525 scope: PropertyScope,1526 property_permission: PropertyKeyPermission,1527 ) -> DispatchResult {1528 collection.check_is_owner_or_admin(sender)?;15291530 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);1531 let current_permission = all_permissions.get(&property_permission.key);1532 if matches![1533 current_permission,1534 Some(PropertyPermission { mutable: false, .. })1535 ] {1536 return Err(<Error<T>>::NoPermission.into());1537 }15381539 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {1540 let property_permission = property_permission.clone();1541 permissions.try_scoped_set(1542 scope,1543 property_permission.key,1544 property_permission.permission,1545 )1546 })1547 .map_err(<Error<T>>::from)?;15481549 Self::deposit_event(Event::PropertyPermissionSet(1550 collection.id,1551 property_permission.key,1552 ));1553 <PalletEvm<T>>::deposit_log(1554 erc::CollectionHelpersEvents::CollectionChanged {1555 collection_id: eth::collection_id_to_address(collection.id),1556 }1557 .to_log(T::ContractAddress::get()),1558 );15591560 Ok(())1561 }15621563 /// Set token property permission.1564 ///1565 /// * `collection` - Collection handler.1566 /// * `sender` - The owner or administrator of the collection.1567 /// * `property_permissions` - Property permissions.1568 #[transactional]1569 pub fn set_token_property_permissions(1570 collection: &CollectionHandle<T>,1571 sender: &T::CrossAccountId,1572 property_permissions: Vec<PropertyKeyPermission>,1573 ) -> DispatchResult {1574 Self::set_scoped_token_property_permissions(1575 collection,1576 sender,1577 PropertyScope::None,1578 property_permissions,1579 )1580 }15811582 /// Set token property permission with scope.1583 ///1584 /// * `collection` - Collection handler.1585 /// * `sender` - The owner or administrator of the collection.1586 /// * `scope` - Property scope.1587 /// * `property_permissions` - Property permissions.1588 #[transactional]1589 pub fn set_scoped_token_property_permissions(1590 collection: &CollectionHandle<T>,1591 sender: &T::CrossAccountId,1592 scope: PropertyScope,1593 property_permissions: Vec<PropertyKeyPermission>,1594 ) -> DispatchResult {1595 for prop_pemission in property_permissions {1596 Self::set_scoped_property_permission(collection, sender, scope, prop_pemission)?;1597 }15981599 Ok(())1600 }16011602 /// Get collection property.1603 pub fn get_collection_property(1604 collection_id: CollectionId,1605 key: &PropertyKey,1606 ) -> Option<PropertyValue> {1607 Self::collection_properties(collection_id).get(key).cloned()1608 }16091610 /// Convert byte vector to property key vector.1611 pub fn bytes_keys_to_property_keys(1612 keys: Vec<Vec<u8>>,1613 ) -> Result<Vec<PropertyKey>, DispatchError> {1614 keys.into_iter()1615 .map(|key| -> Result<PropertyKey, DispatchError> {1616 key.try_into()1617 .map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())1618 })1619 .collect::<Result<Vec<PropertyKey>, DispatchError>>()1620 }16211622 /// Get properties according to given keys.1623 pub fn filter_collection_properties(1624 collection_id: CollectionId,1625 keys: Option<Vec<PropertyKey>>,1626 ) -> Result<Vec<Property>, DispatchError> {1627 let properties = Self::collection_properties(collection_id);16281629 let properties = keys1630 .map(|keys| {1631 keys.into_iter()1632 .filter_map(|key| {1633 properties.get(&key).map(|value| Property {1634 key,1635 value: value.clone(),1636 })1637 })1638 .collect()1639 })1640 .unwrap_or_else(|| {1641 properties1642 .into_iter()1643 .map(|(key, value)| Property { key, value })1644 .collect()1645 });16461647 Ok(properties)1648 }16491650 /// Get property permissions according to given keys.1651 pub fn filter_property_permissions(1652 collection_id: CollectionId,1653 keys: Option<Vec<PropertyKey>>,1654 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {1655 let permissions = Self::property_permissions(collection_id);16561657 let key_permissions = keys1658 .map(|keys| {1659 keys.into_iter()1660 .filter_map(|key| {1661 permissions1662 .get(&key)1663 .map(|permission| PropertyKeyPermission {1664 key,1665 permission: permission.clone(),1666 })1667 })1668 .collect()1669 })1670 .unwrap_or_else(|| {1671 permissions1672 .into_iter()1673 .map(|(key, permission)| PropertyKeyPermission { key, permission })1674 .collect()1675 });16761677 Ok(key_permissions)1678 }16791680 /// Toggle `user` participation in the `collection`'s allow list.1681 /// #### Store read/writes1682 /// 1 writes1683 pub fn toggle_allowlist(1684 collection: &CollectionHandle<T>,1685 sender: &T::CrossAccountId,1686 user: &T::CrossAccountId,1687 allowed: bool,1688 ) -> DispatchResult {1689 collection.check_is_owner_or_admin(sender)?;16901691 // =========16921693 if allowed {1694 <Allowlist<T>>::insert((collection.id, user), true);1695 Self::deposit_event(Event::<T>::AllowListAddressAdded(1696 collection.id,1697 user.clone(),1698 ));1699 } else {1700 <Allowlist<T>>::remove((collection.id, user));1701 Self::deposit_event(Event::<T>::AllowListAddressRemoved(1702 collection.id,1703 user.clone(),1704 ));1705 }17061707 <PalletEvm<T>>::deposit_log(1708 erc::CollectionHelpersEvents::CollectionChanged {1709 collection_id: eth::collection_id_to_address(collection.id),1710 }1711 .to_log(T::ContractAddress::get()),1712 );17131714 Ok(())1715 }17161717 /// Toggle `user` participation in the `collection`'s admin list.1718 /// #### Store read/writes1719 /// 2 reads, 2 writes1720 pub fn toggle_admin(1721 collection: &CollectionHandle<T>,1722 sender: &T::CrossAccountId,1723 user: &T::CrossAccountId,1724 admin: bool,1725 ) -> DispatchResult {1726 collection.check_is_internal()?;1727 collection.check_is_owner(sender)?;17281729 let is_admin = <IsAdmin<T>>::get((collection.id, user));1730 if is_admin == admin {1731 if admin {1732 return Ok(());1733 } else {1734 return Err(Error::<T>::UserIsNotCollectionAdmin.into());1735 }1736 }1737 let amount = <AdminAmount<T>>::get(collection.id);17381739 // =========17401741 if admin {1742 let amount = amount1743 .checked_add(1)1744 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1745 ensure!(1746 amount <= Self::collection_admins_limit(),1747 <Error<T>>::CollectionAdminCountExceeded,1748 );17491750 <AdminAmount<T>>::insert(collection.id, amount);1751 <IsAdmin<T>>::insert((collection.id, user), true);17521753 Self::deposit_event(Event::<T>::CollectionAdminAdded(1754 collection.id,1755 user.clone(),1756 ));1757 } else {1758 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));1759 <IsAdmin<T>>::remove((collection.id, user));17601761 Self::deposit_event(Event::<T>::CollectionAdminRemoved(1762 collection.id,1763 user.clone(),1764 ));1765 }17661767 <PalletEvm<T>>::deposit_log(1768 erc::CollectionHelpersEvents::CollectionChanged {1769 collection_id: eth::collection_id_to_address(collection.id),1770 }1771 .to_log(T::ContractAddress::get()),1772 );17731774 Ok(())1775 }17761777 /// Update collection limits.1778 pub fn update_limits(1779 user: &T::CrossAccountId,1780 collection: &mut CollectionHandle<T>,1781 new_limit: CollectionLimits,1782 ) -> DispatchResult {1783 collection.check_is_internal()?;1784 collection.check_is_owner_or_admin(user)?;17851786 collection.limits =1787 Self::clamp_limits(collection.mode.clone(), &collection.limits, new_limit)?;17881789 Self::deposit_event(Event::<T>::CollectionLimitSet(collection.id));1790 <PalletEvm<T>>::deposit_log(1791 erc::CollectionHelpersEvents::CollectionChanged {1792 collection_id: eth::collection_id_to_address(collection.id),1793 }1794 .to_log(T::ContractAddress::get()),1795 );17961797 collection.save()1798 }17991800 /// Merge set fields from `new_limit` to `old_limit`.1801 fn clamp_limits(1802 mode: CollectionMode,1803 old_limit: &CollectionLimits,1804 mut new_limit: CollectionLimits,1805 ) -> Result<CollectionLimits, DispatchError> {1806 let limits = old_limit;1807 limit_default!(old_limit, new_limit,1808 account_token_ownership_limit => ensure!(1809 new_limit <= MAX_TOKEN_OWNERSHIP,1810 <Error<T>>::CollectionLimitBoundsExceeded,1811 ),1812 sponsored_data_size => ensure!(1813 new_limit <= CUSTOM_DATA_LIMIT,1814 <Error<T>>::CollectionLimitBoundsExceeded,1815 ),18161817 sponsored_data_rate_limit => {},1818 token_limit => ensure!(1819 old_limit >= new_limit && new_limit > 0,1820 <Error<T>>::CollectionTokenLimitExceeded1821 ),18221823 sponsor_transfer_timeout(match mode {1824 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1825 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1826 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1827 }) => ensure!(1828 new_limit <= MAX_SPONSOR_TIMEOUT,1829 <Error<T>>::CollectionLimitBoundsExceeded,1830 ),1831 sponsor_approve_timeout => {},1832 owner_can_transfer => ensure!(1833 !limits.owner_can_transfer_instaled() ||1834 old_limit || !new_limit,1835 <Error<T>>::OwnerPermissionsCantBeReverted,1836 ),1837 owner_can_destroy => ensure!(1838 old_limit || !new_limit,1839 <Error<T>>::OwnerPermissionsCantBeReverted,1840 ),1841 transfers_enabled => {},1842 );1843 Ok(new_limit)1844 }18451846 /// Update collection permissions.1847 pub fn update_permissions(1848 user: &T::CrossAccountId,1849 collection: &mut CollectionHandle<T>,1850 new_permission: CollectionPermissions,1851 ) -> DispatchResult {1852 collection.check_is_internal()?;1853 collection.check_is_owner_or_admin(user)?;1854 collection.permissions = Self::clamp_permissions(1855 collection.mode.clone(),1856 &collection.permissions,1857 new_permission,1858 )?;18591860 Self::deposit_event(Event::<T>::CollectionPermissionSet(collection.id));1861 <PalletEvm<T>>::deposit_log(1862 erc::CollectionHelpersEvents::CollectionChanged {1863 collection_id: eth::collection_id_to_address(collection.id),1864 }1865 .to_log(T::ContractAddress::get()),1866 );18671868 collection.save()1869 }18701871 /// Merge set fields from `new_permission` to `old_permission`.1872 fn clamp_permissions(1873 _mode: CollectionMode,1874 old_permission: &CollectionPermissions,1875 mut new_permission: CollectionPermissions,1876 ) -> Result<CollectionPermissions, DispatchError> {1877 limit_default_clone!(old_permission, new_permission,1878 access => {},1879 mint_mode => {},1880 nesting => { /* todo check for permissive, if only it gets out of benchmarks */ },1881 );1882 Ok(new_permission)1883 }18841885 /// Repair possibly broken properties of a collection.1886 pub fn repair_collection(collection_id: CollectionId) -> DispatchResult {1887 CollectionProperties::<T>::mutate(collection_id, |properties| {1888 properties.recompute_consumed_space();1889 });18901891 Ok(())1892 }1893}18941895/// Indicates unsupported methods by returning [Error::UnsupportedOperation].1896#[macro_export]1897macro_rules! unsupported {1898 ($runtime:path) => {1899 Err($crate::Error::<$runtime>::UnsupportedOperation.into())1900 };1901}19021903/// Return weights for various worst-case operations.1904pub trait CommonWeightInfo<CrossAccountId> {1905 /// Weight of item creation.1906 fn create_item(data: &CreateItemData) -> Weight {1907 Self::create_multiple_items(from_ref(data))1908 }19091910 /// Weight of items creation.1911 fn create_multiple_items(amount: &[CreateItemData]) -> Weight;19121913 /// Weight of items creation.1914 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;19151916 /// The weight of the burning item.1917 fn burn_item() -> Weight;19181919 /// Property setting weight.1920 ///1921 /// * `amount`- The number of properties to set.1922 fn set_collection_properties(amount: u32) -> Weight;19231924 /// Collection property deletion weight.1925 ///1926 /// * `amount`- The number of properties to set.1927 fn delete_collection_properties(amount: u32) -> Weight {1928 Self::set_collection_properties(amount)1929 }19301931 /// Token property setting weight.1932 ///1933 /// * `amount`- The number of properties to set.1934 fn set_token_properties(amount: u32) -> Weight;19351936 /// Token property deletion weight.1937 ///1938 /// * `amount`- The number of properties to delete.1939 fn delete_token_properties(amount: u32) -> Weight {1940 Self::set_token_properties(amount)1941 }19421943 /// Token property permissions set weight.1944 ///1945 /// * `amount`- The number of property permissions to set.1946 fn set_token_property_permissions(amount: u32) -> Weight;19471948 /// Transfer price of the token or its parts.1949 fn transfer() -> Weight;19501951 /// The price of setting the permission of the operation from another user.1952 fn approve() -> Weight;19531954 /// The price of setting the permission of the operation from another user for eth mirror.1955 fn approve_from() -> Weight;19561957 /// Transfer price from another user.1958 fn transfer_from() -> Weight;19591960 /// The price of burning a token from another user.1961 fn burn_from() -> Weight;19621963 /// The price of setting approval for all1964 fn set_allowance_for_all() -> Weight;19651966 /// The price of repairing an item.1967 fn force_repair_item() -> Weight;1968}19691970/// Weight info extension trait for refungible pallet.1971pub trait RefungibleExtensionsWeightInfo {1972 /// Weight of token repartition.1973 fn repartition() -> Weight;1974}19751976/// Common collection operations.1977///1978/// It wraps methods in Fungible, Nonfungible and Refungible pallets1979/// and adds weight info.1980pub trait CommonCollectionOperations<T: Config> {1981 /// Create token.1982 ///1983 /// * `sender` - The user who mint the token and pays for the transaction.1984 /// * `to` - The user who will own the token.1985 /// * `data` - Token data.1986 /// * `nesting_budget` - A budget that can be spent on nesting tokens.1987 fn create_item(1988 &self,1989 sender: T::CrossAccountId,1990 to: T::CrossAccountId,1991 data: CreateItemData,1992 nesting_budget: &dyn Budget,1993 ) -> DispatchResultWithPostInfo;19941995 /// Create multiple tokens.1996 ///1997 /// * `sender` - The user who mint the token and pays for the transaction.1998 /// * `to` - The user who will own the token.1999 /// * `data` - Token data.2000 /// * `nesting_budget` - A budget that can be spent on nesting tokens.2001 fn create_multiple_items(2002 &self,2003 sender: T::CrossAccountId,2004 to: T::CrossAccountId,2005 data: Vec<CreateItemData>,2006 nesting_budget: &dyn Budget,2007 ) -> DispatchResultWithPostInfo;20082009 /// Create multiple tokens.2010 ///2011 /// * `sender` - The user who mint the token and pays for the transaction.2012 /// * `to` - The user who will own the token.2013 /// * `data` - Token data.2014 /// * `nesting_budget` - A budget that can be spent on nesting tokens.2015 fn create_multiple_items_ex(2016 &self,2017 sender: T::CrossAccountId,2018 data: CreateItemExData<T::CrossAccountId>,2019 nesting_budget: &dyn Budget,2020 ) -> DispatchResultWithPostInfo;20212022 /// Burn token.2023 ///2024 /// * `sender` - The user who owns the token.2025 /// * `token` - Token id that will burned.2026 /// * `amount` - The number of parts of the token that will be burned.2027 fn burn_item(2028 &self,2029 sender: T::CrossAccountId,2030 token: TokenId,2031 amount: u128,2032 ) -> DispatchResultWithPostInfo;20332034 /// Set collection properties.2035 ///2036 /// * `sender` - Must be either the owner of the collection or its admin.2037 /// * `properties` - Properties to be set.2038 fn set_collection_properties(2039 &self,2040 sender: T::CrossAccountId,2041 properties: Vec<Property>,2042 ) -> DispatchResultWithPostInfo;20432044 /// Delete collection properties.2045 ///2046 /// * `sender` - Must be either the owner of the collection or its admin.2047 /// * `properties` - The properties to be removed.2048 fn delete_collection_properties(2049 &self,2050 sender: &T::CrossAccountId,2051 property_keys: Vec<PropertyKey>,2052 ) -> DispatchResultWithPostInfo;20532054 /// Set token properties.2055 ///2056 /// The appropriate [`PropertyPermission`] for the token property2057 /// must be set with [`Self::set_token_property_permissions`].2058 ///2059 /// * `sender` - Must be either the owner of the token or its admin.2060 /// * `token_id` - The token for which the properties are being set.2061 /// * `properties` - Properties to be set.2062 /// * `budget` - Budget for setting properties.2063 fn set_token_properties(2064 &self,2065 sender: T::CrossAccountId,2066 token_id: TokenId,2067 properties: Vec<Property>,2068 budget: &dyn Budget,2069 ) -> DispatchResultWithPostInfo;20702071 /// Remove token properties.2072 ///2073 /// The appropriate [`PropertyPermission`] for the token property2074 /// must be set with [`Self::set_token_property_permissions`].2075 ///2076 /// * `sender` - Must be either the owner of the token or its admin.2077 /// * `token_id` - The token for which the properties are being remove.2078 /// * `property_keys` - Keys to remove corresponding properties.2079 /// * `budget` - Budget for removing properties.2080 fn delete_token_properties(2081 &self,2082 sender: T::CrossAccountId,2083 token_id: TokenId,2084 property_keys: Vec<PropertyKey>,2085 budget: &dyn Budget,2086 ) -> DispatchResultWithPostInfo;20872088 /// Get token properties raw map.2089 ///2090 /// * `token_id` - The token which properties are needed.2091 fn get_token_properties_raw(&self, token_id: TokenId) -> Option<TokenProperties>;20922093 /// Set token properties raw map.2094 ///2095 /// * `token_id` - The token for which the properties are being set.2096 /// * `map` - The raw map containing the token's properties.2097 fn set_token_properties_raw(&self, token_id: TokenId, map: TokenProperties);20982099 /// Set token property permissions.2100 ///2101 /// * `sender` - Must be either the owner of the token or its admin.2102 /// * `token_id` - The token for which the properties are being set.2103 /// * `property_permissions` - Property permissions to be set.2104 /// * `budget` - Budget for setting properties.2105 fn set_token_property_permissions(2106 &self,2107 sender: &T::CrossAccountId,2108 property_permissions: Vec<PropertyKeyPermission>,2109 ) -> DispatchResultWithPostInfo;21102111 /// Transfer amount of token pieces.2112 ///2113 /// * `sender` - Donor user.2114 /// * `to` - Recepient user.2115 /// * `token` - The token of which parts are being sent.2116 /// * `amount` - The number of parts of the token that will be transferred.2117 /// * `budget` - The maximum budget that can be spent on the transfer.2118 fn transfer(2119 &self,2120 sender: T::CrossAccountId,2121 to: T::CrossAccountId,2122 token: TokenId,2123 amount: u128,2124 budget: &dyn Budget,2125 ) -> DispatchResultWithPostInfo;21262127 /// Grant access to another account to transfer parts of the token owned by the calling user via [Self::transfer_from].2128 ///2129 /// * `sender` - The user who grants access to the token.2130 /// * `spender` - The user to whom the rights are granted.2131 /// * `token` - The token to which access is granted.2132 /// * `amount` - The amount of pieces that another user can dispose of.2133 fn approve(2134 &self,2135 sender: T::CrossAccountId,2136 spender: T::CrossAccountId,2137 token: TokenId,2138 amount: u128,2139 ) -> DispatchResultWithPostInfo;21402141 /// Grant access to another account to transfer parts of the token owned by the calling user's eth mirror via [Self::transfer_from].2142 ///2143 /// * `sender` - The user who grants access to the token.2144 /// * `from` - Spender's eth mirror.2145 /// * `to` - The user to whom the rights are granted.2146 /// * `token` - The token to which access is granted.2147 /// * `amount` - The amount of pieces that another user can dispose of.2148 fn approve_from(2149 &self,2150 sender: T::CrossAccountId,2151 from: T::CrossAccountId,2152 to: T::CrossAccountId,2153 token: TokenId,2154 amount: u128,2155 ) -> DispatchResultWithPostInfo;21562157 /// Send parts of a token owned by another user.2158 ///2159 /// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2160 ///2161 /// * `sender` - The user who must have access to the token (see [`Self::approve`]).2162 /// * `from` - The user who owns the token.2163 /// * `to` - Recepient user.2164 /// * `token` - The token of which parts are being sent.2165 /// * `amount` - The number of parts of the token that will be transferred.2166 /// * `budget` - The maximum budget that can be spent on the transfer.2167 fn transfer_from(2168 &self,2169 sender: T::CrossAccountId,2170 from: T::CrossAccountId,2171 to: T::CrossAccountId,2172 token: TokenId,2173 amount: u128,2174 budget: &dyn Budget,2175 ) -> DispatchResultWithPostInfo;21762177 /// Burn parts of a token owned by another user.2178 ///2179 /// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2180 ///2181 /// * `sender` - The user who must have access to the token (see [`Self::approve`]).2182 /// * `from` - The user who owns the token.2183 /// * `token` - The token of which parts are being sent.2184 /// * `amount` - The number of parts of the token that will be transferred.2185 /// * `budget` - The maximum budget that can be spent on the burn.2186 fn burn_from(2187 &self,2188 sender: T::CrossAccountId,2189 from: T::CrossAccountId,2190 token: TokenId,2191 amount: u128,2192 budget: &dyn Budget,2193 ) -> DispatchResultWithPostInfo;21942195 /// Check permission to nest token.2196 ///2197 /// * `sender` - The user who initiated the check.2198 /// * `from` - The token that is checked for embedding.2199 /// * `under` - Token under which to check.2200 /// * `budget` - The maximum budget that can be spent on the check.2201 fn check_nesting(2202 &self,2203 sender: &T::CrossAccountId,2204 from: (CollectionId, TokenId),2205 under: TokenId,2206 budget: &dyn Budget,2207 ) -> DispatchResult;22082209 /// Nest one token into another.2210 ///2211 /// * `under` - Token holder.2212 /// * `to_nest` - Nested token.2213 fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId));22142215 /// Unnest token.2216 ///2217 /// * `under` - Token holder.2218 /// * `to_nest` - Token to unnest.2219 fn unnest(&self, under: TokenId, to_nest: (CollectionId, TokenId));22202221 /// Get all user tokens.2222 ///2223 /// * `account` - Account for which you need to get tokens.2224 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;22252226 /// Get all the tokens in the collection.2227 fn collection_tokens(&self) -> Vec<TokenId>;22282229 /// Check if the token exists.2230 ///2231 /// * `token` - Id token to check.2232 fn token_exists(&self, token: TokenId) -> bool;22332234 /// Get the id of the last minted token.2235 fn last_token_id(&self) -> TokenId;22362237 /// Get the owner of the token.2238 ///2239 /// * `token` - The token for which you need to find out the owner.2240 fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError>;22412242 /// Checks if the `maybe_owner` is the indirect owner of the `token`.2243 ///2244 /// * `token` - Id token to check.2245 /// * `maybe_owner` - The account to check.2246 /// * `nesting_budget` - A budget that can be spent on nesting tokens.2247 fn check_token_indirect_owner(2248 &self,2249 token: TokenId,2250 maybe_owner: &T::CrossAccountId,2251 nesting_budget: &dyn Budget,2252 ) -> Result<bool, DispatchError>;22532254 /// Returns 10 tokens owners in no particular order.2255 ///2256 /// * `token` - The token for which you need to find out the owners.2257 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId>;22582259 /// Get the value of the token property by key.2260 ///2261 /// * `token` - Token with the property to get.2262 /// * `key` - Property name.2263 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue>;22642265 /// Get a set of token properties by key vector.2266 ///2267 /// * `token` - Token with the property to get.2268 /// * `keys` - Vector of property keys. If this parameter is [None](sp_std::result::Result),2269 /// then all properties are returned.2270 fn token_properties(&self, token: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property>;22712272 /// Amount of unique collection tokens2273 fn total_supply(&self) -> u32;22742275 /// Amount of different tokens account has.2276 ///2277 /// * `account` - The account for which need to get the balance.2278 fn account_balance(&self, account: T::CrossAccountId) -> u32;22792280 /// Amount of specific token account have.2281 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;22822283 /// Amount of token pieces2284 fn total_pieces(&self, token: TokenId) -> Option<u128>;22852286 /// Get the number of parts of the token that a trusted user can manage.2287 ///2288 /// * `sender` - Trusted user.2289 /// * `spender` - Owner of the token.2290 /// * `token` - The token for which to get the value.2291 fn allowance(2292 &self,2293 sender: T::CrossAccountId,2294 spender: T::CrossAccountId,2295 token: TokenId,2296 ) -> u128;22972298 /// Get extension for RFT collection.2299 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {2300 None2301 }23022303 /// Get XCM extensions.2304 fn xcm_extensions(&self) -> Option<&dyn XcmExtensions<T>> {2305 None2306 }23072308 /// The `operator` is allowed to transfer all tokens of the `owner` on their behalf.2309 /// * `owner` - Token owner2310 /// * `operator` - Operator2311 /// * `approve` - Should operator status be granted or revoked?2312 fn set_allowance_for_all(2313 &self,2314 owner: T::CrossAccountId,2315 operator: T::CrossAccountId,2316 approve: bool,2317 ) -> DispatchResultWithPostInfo;23182319 /// Tells whether the given `owner` approves the `operator`.2320 fn allowance_for_all(&self, owner: T::CrossAccountId, operator: T::CrossAccountId) -> bool;23212322 /// Repairs a possibly broken item.2323 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo;2324}23252326/// Extension for RFT collection.2327pub trait RefungibleExtensions<T>2328where2329 T: Config,2330{2331 /// Change the number of parts of the token.2332 ///2333 /// When the value changes down, this function is equivalent to burning parts of the token.2334 ///2335 /// * `sender` - The user calling the repartition operation. Must be the owner of the token.2336 /// * `token` - The token for which you want to change the number of parts.2337 /// * `amount` - The new value of the parts of the token.2338 fn repartition(2339 &self,2340 sender: &T::CrossAccountId,2341 token: TokenId,2342 amount: u128,2343 ) -> DispatchResultWithPostInfo;2344}23452346/// XCM extensions for fungible and NFT collections2347pub trait XcmExtensions<T>2348where2349 T: Config,2350{2351 /// Is the collection a foreign one?2352 fn is_foreign(&self) -> bool;23532354 /// Does the token have children?2355 fn token_has_children(&self, _token: TokenId) -> bool {2356 false2357 }23582359 /// Create a collection's item using a transaction.2360 ///2361 /// This function performs additional XCM-related checks before the actual creation.2362 #[transactional]2363 fn create_item(2364 &self,2365 depositor: &T::CrossAccountId,2366 to: T::CrossAccountId,2367 data: CreateItemData,2368 nesting_budget: &dyn Budget,2369 ) -> Result<TokenId, DispatchError> {2370 if T::CrossTokenAddressMapping::is_token_address(&to) {2371 return unsupported!(T);2372 }23732374 self.create_item_internal(depositor, to, data, nesting_budget)2375 }23762377 /// Create a collection's item.2378 fn create_item_internal(2379 &self,2380 depositor: &T::CrossAccountId,2381 to: T::CrossAccountId,2382 data: CreateItemData,2383 nesting_budget: &dyn Budget,2384 ) -> Result<TokenId, DispatchError>;23852386 /// Transfer an item from the `from` account to the `to` account using a transaction.2387 ///2388 /// This function performs additional XCM-related checks before the actual transfer.2389 #[transactional]2390 fn transfer_item(2391 &self,2392 depositor: &T::CrossAccountId,2393 from: &T::CrossAccountId,2394 to: &T::CrossAccountId,2395 token: TokenId,2396 amount: u128,2397 nesting_budget: &dyn Budget,2398 ) -> DispatchResult {2399 if T::CrossTokenAddressMapping::is_token_address(&to) {2400 return unsupported!(T);2401 }24022403 self.transfer_item_internal(depositor, from, to, token, amount, nesting_budget)2404 }24052406 /// Transfer an item from the `from` account to the `to` account.2407 fn transfer_item_internal(2408 &self,2409 depositor: &T::CrossAccountId,2410 from: &T::CrossAccountId,2411 to: &T::CrossAccountId,2412 token: TokenId,2413 amount: u128,2414 nesting_budget: &dyn Budget,2415 ) -> DispatchResult;24162417 /// Burn a collection's item using a transaction.2418 #[transactional]2419 fn burn_item(&self, from: T::CrossAccountId, token: TokenId, amount: u128) -> DispatchResult {2420 self.burn_item_internal(from, token, amount)2421 }24222423 /// Burn a collection's item.2424 fn burn_item_internal(2425 &self,2426 from: T::CrossAccountId,2427 token: TokenId,2428 amount: u128,2429 ) -> DispatchResult;2430}24312432/// Merge [`DispatchResult`] with [`Weight`] into [`DispatchResultWithPostInfo`].2433///2434/// Used for [`CommonCollectionOperations`] implementations and flexible enough to do so.2435pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {2436 let post_info = PostDispatchInfo {2437 actual_weight: Some(weight),2438 pays_fee: Pays::Yes,2439 };2440 match res {2441 Ok(()) => Ok(post_info),2442 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),2443 }2444}24452446impl<T: Config> From<PropertiesError> for Error<T> {2447 fn from(error: PropertiesError) -> Self {2448 match error {2449 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,2450 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,2451 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,2452 PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,2453 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,2454 }2455 }2456}24572458/// The type-safe interface for writing properties (setting or deleting) to tokens.2459/// It has two distinct implementations for newly created tokens and existing ones.2460///2461/// This type utilizes the lazy evaluation to avoid repeating the computation2462/// of several performance-heavy or PoV-heavy tasks,2463/// such as checking the indirect ownership or reading the token property permissions.2464pub struct PropertyWriter<'a, WriterVariant, T, Handle> {2465 collection: &'a Handle,2466 collection_lazy_info: PropertyWriterLazyCollectionInfo<'a>,2467 _phantom: PhantomData<(T, WriterVariant)>,2468}24692470impl<'a, T, Handle, WriterVariant> PropertyWriter<'a, WriterVariant, T, Handle>2471where2472 T: Config,2473 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2474{2475 fn internal_write_token_properties(2476 &mut self,2477 token_id: TokenId,2478 mut token_lazy_info: PropertyWriterLazyTokenInfo,2479 properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,2480 log: evm_coder::ethereum::Log,2481 ) -> DispatchResult {2482 for (key, value) in properties_updates {2483 let permission = self2484 .collection_lazy_info2485 .property_permissions2486 .value()2487 .get(&key)2488 .cloned()2489 .unwrap_or_else(PropertyPermission::none);24902491 match permission {2492 PropertyPermission { mutable: false, .. }2493 if token_lazy_info2494 .stored_properties2495 .value()2496 .get(&key)2497 .is_some() =>2498 {2499 return Err(<Error<T>>::NoPermission.into());2500 }25012502 PropertyPermission {2503 collection_admin,2504 token_owner,2505 ..2506 } => check_token_permissions::<T>(2507 collection_admin,2508 token_owner,2509 &mut self.collection_lazy_info.is_collection_admin,2510 &mut token_lazy_info.is_token_owner,2511 &mut token_lazy_info.is_token_exist,2512 )?,2513 }25142515 match value {2516 Some(value) => {2517 token_lazy_info2518 .stored_properties2519 .value_mut()2520 .try_set(key.clone(), value)2521 .map_err(<Error<T>>::from)?;25222523 <Pallet<T>>::deposit_event(Event::TokenPropertySet(2524 self.collection.id,2525 token_id,2526 key,2527 ));2528 }2529 None => {2530 token_lazy_info2531 .stored_properties2532 .value_mut()2533 .remove(&key)2534 .map_err(<Error<T>>::from)?;25352536 <Pallet<T>>::deposit_event(Event::TokenPropertyDeleted(2537 self.collection.id,2538 token_id,2539 key,2540 ));2541 }2542 }2543 }25442545 let properties_changed = token_lazy_info.stored_properties.has_value();2546 if properties_changed {2547 <PalletEvm<T>>::deposit_log(log);25482549 self.collection2550 .set_token_properties_raw(token_id, token_lazy_info.stored_properties.into_inner());2551 }25522553 Ok(())2554 }2555}25562557/// A helper structure for the [`PropertyWriter`] that holds2558/// the collection-related info. The info is loaded using lazy evaluation.2559/// This info is common for any token for which we write properties.2560pub struct PropertyWriterLazyCollectionInfo<'a> {2561 is_collection_admin: LazyValue<'a, bool>,2562 property_permissions: LazyValue<'a, PropertiesPermissionMap>,2563}25642565/// A helper structure for the [`PropertyWriter`] that holds2566/// the token-related info. The info is loaded using lazy evaluation.2567pub struct PropertyWriterLazyTokenInfo<'a> {2568 is_token_exist: LazyValue<'a, bool>,2569 is_token_owner: LazyValue<'a, Result<bool, DispatchError>>,2570 stored_properties: LazyValue<'a, TokenProperties>,2571}25722573impl<'a> PropertyWriterLazyTokenInfo<'a> {2574 /// Create a lazy token info.2575 pub fn new(2576 check_token_exist: impl FnOnce() -> bool + 'a,2577 check_token_owner: impl FnOnce() -> Result<bool, DispatchError> + 'a,2578 get_token_properties: impl FnOnce() -> TokenProperties + 'a,2579 ) -> Self {2580 Self {2581 is_token_exist: LazyValue::new(check_token_exist),2582 is_token_owner: LazyValue::new(check_token_owner),2583 stored_properties: LazyValue::new(get_token_properties),2584 }2585 }2586}25872588/// A marker structure that enables the writer implementation2589/// to provide the interface to write properties to **newly created** tokens.2590pub struct NewTokenPropertyWriter<T>(PhantomData<T>);2591impl<T: Config> NewTokenPropertyWriter<T> {2592 /// Creates a [`PropertyWriter`] for **newly created** tokens.2593 pub fn new<'a, Handle>(2594 collection: &'a Handle,2595 sender: &'a T::CrossAccountId,2596 ) -> PropertyWriter<'a, Self, T, Handle>2597 where2598 T: Config,2599 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2600 {2601 PropertyWriter {2602 collection,2603 collection_lazy_info: PropertyWriterLazyCollectionInfo {2604 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2605 property_permissions: LazyValue::new(|| {2606 <Pallet<T>>::property_permissions(collection.id)2607 }),2608 },2609 _phantom: PhantomData,2610 }2611 }2612}26132614impl<'a, T, Handle> PropertyWriter<'a, NewTokenPropertyWriter<T>, T, Handle>2615where2616 T: Config,2617 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2618{2619 /// A function to write properties to a **newly created** token.2620 pub fn write_token_properties(2621 &mut self,2622 mint_target_is_sender: bool,2623 token_id: TokenId,2624 properties_updates: impl Iterator<Item = Property>,2625 log: evm_coder::ethereum::Log,2626 ) -> DispatchResult {2627 let check_token_exist = || {2628 debug_assert!(self.collection.token_exists(token_id));2629 true2630 };26312632 let check_token_owner = || Ok(mint_target_is_sender);26332634 let get_token_properties = || {2635 debug_assert!(self.collection.get_token_properties_raw(token_id).is_none());2636 TokenProperties::new()2637 };26382639 self.internal_write_token_properties(2640 token_id,2641 PropertyWriterLazyTokenInfo::new(2642 check_token_exist,2643 check_token_owner,2644 get_token_properties,2645 ),2646 properties_updates.map(|p| (p.key, Some(p.value))),2647 log,2648 )2649 }2650}26512652/// A marker structure that enables the writer implementation2653/// to provide the interface to write properties to **already existing** tokens.2654pub struct ExistingTokenPropertyWriter<T>(PhantomData<T>);2655impl<T: Config> ExistingTokenPropertyWriter<T> {2656 /// Creates a [`PropertyWriter`] for **already existing** tokens.2657 pub fn new<'a, Handle>(2658 collection: &'a Handle,2659 sender: &'a T::CrossAccountId,2660 ) -> PropertyWriter<'a, Self, T, Handle>2661 where2662 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2663 {2664 PropertyWriter {2665 collection,2666 collection_lazy_info: PropertyWriterLazyCollectionInfo {2667 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2668 property_permissions: LazyValue::new(|| {2669 <Pallet<T>>::property_permissions(collection.id)2670 }),2671 },2672 _phantom: PhantomData,2673 }2674 }2675}26762677impl<'a, T, Handle> PropertyWriter<'a, ExistingTokenPropertyWriter<T>, T, Handle>2678where2679 T: Config,2680 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2681{2682 /// A function to write properties to an **already existing** token.2683 pub fn write_token_properties(2684 &mut self,2685 sender: &T::CrossAccountId,2686 token_id: TokenId,2687 properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,2688 nesting_budget: &dyn Budget,2689 log: evm_coder::ethereum::Log,2690 ) -> DispatchResult {2691 let check_token_exist = || self.collection.token_exists(token_id);2692 let check_token_owner = || {2693 self.collection2694 .check_token_indirect_owner(token_id, sender, nesting_budget)2695 };2696 let get_token_properties = || {2697 self.collection2698 .get_token_properties_raw(token_id)2699 .unwrap_or_default()2700 };27012702 self.internal_write_token_properties(2703 token_id,2704 PropertyWriterLazyTokenInfo::new(2705 check_token_exist,2706 check_token_owner,2707 get_token_properties,2708 ),2709 properties_updates,2710 log,2711 )2712 }2713}27142715/// A marker structure that enables the writer implementation2716/// to benchmark the token properties writing.2717#[cfg(feature = "runtime-benchmarks")]2718pub struct BenchmarkPropertyWriter<T>(PhantomData<T>);27192720#[cfg(feature = "runtime-benchmarks")]2721impl<T: Config> BenchmarkPropertyWriter<T> {2722 /// Creates a [`PropertyWriter`] for benchmarking tokens properties writing.2723 pub fn new<'a, Handle>(2724 collection: &'a Handle,2725 collection_lazy_info: PropertyWriterLazyCollectionInfo<'a>,2726 ) -> PropertyWriter<'a, Self, T, Handle>2727 where2728 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2729 {2730 PropertyWriter {2731 collection,2732 collection_lazy_info,2733 _phantom: PhantomData,2734 }2735 }27362737 /// Load the [`PropertyWriterLazyCollectionInfo`] from the storage.2738 pub fn load_collection_info<Handle>(2739 collection_handle: &Handle,2740 sender: &T::CrossAccountId,2741 ) -> PropertyWriterLazyCollectionInfo<'static>2742 where2743 Handle: Deref<Target = CollectionHandle<T>>,2744 {2745 let is_collection_admin = collection_handle.is_owner_or_admin(sender);2746 let property_permissions = <Pallet<T>>::property_permissions(collection_handle.id);27472748 PropertyWriterLazyCollectionInfo {2749 is_collection_admin: LazyValue::new(move || is_collection_admin),2750 property_permissions: LazyValue::new(move || property_permissions),2751 }2752 }27532754 /// Load the [`PropertyWriterLazyTokenInfo`] with token properties from the storage.2755 pub fn load_token_properties<Handle>(2756 collection: &Handle,2757 token_id: TokenId,2758 ) -> PropertyWriterLazyTokenInfo2759 where2760 Handle: CommonCollectionOperations<T>,2761 {2762 let stored_properties = collection2763 .get_token_properties_raw(token_id)2764 .unwrap_or_default();27652766 PropertyWriterLazyTokenInfo {2767 is_token_exist: LazyValue::new(|| true),2768 is_token_owner: LazyValue::new(|| Ok(true)),2769 stored_properties: LazyValue::new(move || stored_properties),2770 }2771 }2772}27732774#[cfg(feature = "runtime-benchmarks")]2775impl<'a, T, Handle> PropertyWriter<'a, BenchmarkPropertyWriter<T>, T, Handle>2776where2777 T: Config,2778 Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2779{2780 /// A function to benchmark the writing of token properties.2781 pub fn write_token_properties(2782 &mut self,2783 token_id: TokenId,2784 properties_updates: impl Iterator<Item = Property>,2785 log: evm_coder::ethereum::Log,2786 ) -> DispatchResult {2787 let check_token_exist = || true;2788 let check_token_owner = || Ok(true);2789 let get_token_properties = TokenProperties::new;27902791 self.internal_write_token_properties(2792 token_id,2793 PropertyWriterLazyTokenInfo::new(2794 check_token_exist,2795 check_token_owner,2796 get_token_properties,2797 ),2798 properties_updates.map(|p| (p.key, Some(p.value))),2799 log,2800 )2801 }2802}28032804/// Computes the weight of writing properties to tokens.2805/// * `properties_nums` - The properties num of each created token.2806/// * `per_token_weight_weight` - The function to obtain the weight2807/// of writing properties from a token's properties num.2808pub fn write_token_properties_total_weight<T: Config, I: Fn(u32) -> Weight>(2809 properties_nums: impl Iterator<Item = u32>,2810 per_token_weight: I,2811) -> Weight {2812 let mut weight = properties_nums2813 .filter_map(|properties_num| {2814 if properties_num > 0 {2815 Some(per_token_weight(properties_num))2816 } else {2817 None2818 }2819 })2820 .fold(Weight::zero(), |a, b| a.saturating_add(b));28212822 if !weight.is_zero() {2823 // If we are here, it means the token properties were written at least once.2824 // Because of that, some common collection data was also loaded; we must add this weight.2825 // However, this common data was loaded only once, which is guaranteed by the `PropertyWriter`.28262827 weight = weight.saturating_add(<SelfWeightOf<T>>::property_writer_load_collection_info());2828 }28292830 weight2831}28322833#[cfg(any(feature = "tests", test))]2834#[allow(missing_docs)]2835pub mod tests {2836 use crate::{Config, DispatchError, DispatchResult, LazyValue};28372838 const fn to_bool(u: u8) -> bool {2839 u != 02840 }28412842 #[derive(Debug)]2843 pub struct TestCase {2844 pub collection_admin: bool,2845 pub is_collection_admin: bool,2846 pub token_owner: bool,2847 pub is_token_owner: bool,2848 pub no_permission: bool,2849 }28502851 impl TestCase {2852 const fn new(2853 collection_admin: u8,2854 is_collection_admin: u8,2855 token_owner: u8,2856 is_token_owner: u8,2857 no_permission: u8,2858 ) -> Self {2859 Self {2860 collection_admin: to_bool(collection_admin),2861 is_collection_admin: to_bool(is_collection_admin),2862 token_owner: to_bool(token_owner),2863 is_token_owner: to_bool(is_token_owner),2864 no_permission: to_bool(no_permission),2865 }2866 }2867 }28682869 #[rustfmt::skip]2870 pub const TABLE: [TestCase; 16] = [2871 // ┌╴collection_admin2872 // │ ┌╴is_collection_admin2873 // │ │ ┌╴token_owner2874 // │ │ │ ┌╴is_token_ownership2875 // │ │ │ │ ┌╴no_permission2876 /* 0*/ TestCase::new(0, 0, 0, 0, 1),2877 /* 1*/ TestCase::new(0, 0, 0, 1, 1),2878 /* 2*/ TestCase::new(0, 0, 1, 0, 1),2879 /* 3*/ TestCase::new(0, 0, 1, 1, 0),2880 /* 4*/ TestCase::new(0, 1, 0, 0, 1),2881 /* 5*/ TestCase::new(0, 1, 0, 1, 1),2882 /* 6*/ TestCase::new(0, 1, 1, 0, 1),2883 /* 7*/ TestCase::new(0, 1, 1, 1, 0),2884 /* 8*/ TestCase::new(1, 0, 0, 0, 1),2885 /* 9*/ TestCase::new(1, 0, 0, 1, 1),2886 /* 10*/ TestCase::new(1, 0, 1, 0, 1),2887 /* 11*/ TestCase::new(1, 0, 1, 1, 0),2888 /* 12*/ TestCase::new(1, 1, 0, 0, 0),2889 /* 13*/ TestCase::new(1, 1, 0, 1, 0),2890 /* 14*/ TestCase::new(1, 1, 1, 0, 0),2891 /* 15*/ TestCase::new(1, 1, 1, 1, 0),2892 ];28932894 pub fn check_token_permissions<T: Config>(2895 collection_admin_permitted: bool,2896 token_owner_permitted: bool,2897 is_collection_admin: &mut LazyValue<bool>,2898 check_token_ownership: &mut LazyValue<Result<bool, DispatchError>>,2899 check_token_existence: &mut LazyValue<bool>,2900 ) -> DispatchResult {2901 crate::check_token_permissions::<T>(2902 collection_admin_permitted,2903 token_owner_permitted,2904 is_collection_admin,2905 check_token_ownership,2906 check_token_existence,2907 )2908 }2909}pallets/foreign-assets/src/lib.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -364,7 +364,7 @@
fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}
- fn deposit_asset(what: &MultiAsset, to: &MultiLocation, context: &XcmContext) -> XcmResult {
+ fn deposit_asset(what: &MultiAsset, to: &MultiLocation, _context: &XcmContext) -> XcmResult {
let collection_id = Self::multiasset_to_collection(what)?;
let dispatch =
T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;
@@ -397,7 +397,42 @@
from: &MultiLocation,
_maybe_context: Option<&XcmContext>,
) -> Result<staging_xcm_executor::Assets, XcmError> {
- Err(XcmError::Unimplemented)
+ let from = T::LocationToAccountId::convert_location(from)
+ .ok_or(XcmExecutorError::AccountIdConversionFailed)?;
+
+ let collection_id = Self::multiasset_to_collection(what)?;
+ let dispatch =
+ T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;
+
+ let collection = dispatch.as_dyn();
+ let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;
+
+ match what.fun {
+ Fungibility::Fungible(amount) => xcm_ext
+ .burn_item(from, TokenId::default(), amount)
+ .map_err(|_| XcmError::FailedToTransactAsset("fungible item withdraw failed"))?,
+
+ Fungibility::NonFungible(asset_instance) => {
+ let token_id =
+ Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?
+ .ok_or(XcmError::AssetNotFound)?;
+
+ if xcm_ext.token_has_children(token_id) {
+ return Err(XcmError::Unimplemented);
+ }
+
+ let depositor = &from;
+ let to = Self::pallet_account();
+ let amount = 1;
+ xcm_ext
+ .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)
+ .map_err(|_| {
+ XcmError::FailedToTransactAsset("nonfungible item withdraw failed")
+ })?;
+ }
+ }
+
+ Ok(what.clone().into())
}
fn internal_transfer_asset(
@@ -406,7 +441,49 @@
to: &MultiLocation,
_context: &XcmContext,
) -> Result<staging_xcm_executor::Assets, XcmError> {
- Err(XcmError::Unimplemented)
+ let collection_id = Self::multiasset_to_collection(what)?;
+
+ let dispatch =
+ T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;
+ let collection = dispatch.as_dyn();
+ let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;
+
+ let from = T::LocationToAccountId::convert_location(from)
+ .ok_or(XcmExecutorError::AccountIdConversionFailed)?;
+
+ let to = T::LocationToAccountId::convert_location(to)
+ .ok_or(XcmExecutorError::AccountIdConversionFailed)?;
+
+ let depositor = &from;
+
+ match what.fun {
+ Fungibility::Fungible(amount) => xcm_ext
+ .transfer_item(
+ depositor,
+ &from,
+ &to,
+ TokenId::default(),
+ amount,
+ &ZeroBudget,
+ )
+ .map_err(|_| XcmError::FailedToTransactAsset("fungible item transfer failed"))?,
+
+ Fungibility::NonFungible(asset_instance) => {
+ let token_id =
+ Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?
+ .ok_or(XcmError::AssetNotFound)?;
+
+ let amount = 1;
+
+ xcm_ext
+ .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)
+ .map_err(|_| {
+ XcmError::FailedToTransactAsset("nonfungible item transfer failed")
+ })?;
+ }
+ }
+
+ Ok(what.clone().into())
}
}
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -95,9 +95,8 @@
use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
use up_data_structs::{
- budget::{Budget, ZeroBudget},
- mapping::TokenAddressMapping,
- AccessMode, CollectionId, CreateCollectionData, Property, PropertyKey, TokenId,
+ budget::Budget, mapping::TokenAddressMapping, AccessMode, CollectionId, Property, PropertyKey,
+ TokenId,
};
use weights::WeightInfo;
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -576,6 +576,10 @@
self.flags.foreign
}
+ fn token_has_children(&self, token: TokenId) -> bool {
+ <Pallet<T>>::token_has_children(self.id, token)
+ }
+
fn create_item_internal(
&self,
depositor: &<T>::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -117,8 +117,8 @@
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};
use up_data_structs::{
budget::Budget, mapping::TokenAddressMapping, AccessMode, AuxPropertyValue, CollectionId,
- CreateCollectionData, CreateNftExData, CustomDataLimit, PropertiesPermissionMap, Property,
- PropertyKey, PropertyKeyPermission, PropertyScope, PropertyValue, TokenChild, TokenId,
+ CreateNftExData, CustomDataLimit, PropertiesPermissionMap, Property, PropertyKey,
+ PropertyKeyPermission, PropertyScope, PropertyValue, TokenChild, TokenId,
TokenProperties as TokenPropertiesT,
};
use weights::WeightInfo;