difftreelog
feat(rmrk) add resource at minting
in: master
2 files changed
pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::{DispatchError, Permill, traits::StaticLookup};22use sp_std::vec::Vec;23use up_data_structs::{*, mapping::TokenAddressMapping};24use pallet_common::{25 Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations,26};27use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle, TokenData};28use pallet_structure::{Pallet as PalletStructure, Error as StructureError};29use pallet_evm::account::CrossAccountId;30use core::convert::AsRef;3132pub use pallet::*;3334#[cfg(feature = "runtime-benchmarks")]35pub mod benchmarking;36pub mod misc;37pub mod property;38pub mod weights;3940pub type SelfWeightOf<T> = <T as Config>::WeightInfo;4142use weights::WeightInfo;43use misc::*;44pub use property::*;4546use RmrkProperty::*;4748pub const NESTING_BUDGET: u32 = 5;4950#[frame_support::pallet]51pub mod pallet {52 use super::*;53 use pallet_evm::account;5455 #[pallet::config]56 pub trait Config:57 frame_system::Config + pallet_common::Config + pallet_nonfungible::Config + account::Config58 {59 type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;60 type WeightInfo: WeightInfo;61 }6263 #[pallet::storage]64 #[pallet::getter(fn collection_index)]65 pub type CollectionIndex<T: Config> = StorageValue<_, RmrkCollectionId, ValueQuery>;6667 #[pallet::storage]68 pub type UniqueCollectionId<T: Config> =69 StorageMap<_, Twox64Concat, RmrkCollectionId, CollectionId, ValueQuery>;7071 #[pallet::pallet]72 #[pallet::generate_store(pub(super) trait Store)]73 pub struct Pallet<T>(_);7475 #[pallet::event]76 #[pallet::generate_deposit(pub(super) fn deposit_event)]77 pub enum Event<T: Config> {78 CollectionCreated {79 issuer: T::AccountId,80 collection_id: RmrkCollectionId,81 },82 CollectionDestroyed {83 issuer: T::AccountId,84 collection_id: RmrkCollectionId,85 },86 IssuerChanged {87 old_issuer: T::AccountId,88 new_issuer: T::AccountId,89 collection_id: RmrkCollectionId,90 },91 CollectionLocked {92 issuer: T::AccountId,93 collection_id: RmrkCollectionId,94 },95 NftMinted {96 owner: T::AccountId,97 collection_id: RmrkCollectionId,98 nft_id: RmrkNftId,99 },100 NFTBurned {101 owner: T::AccountId,102 nft_id: RmrkNftId,103 },104 NFTSent {105 sender: T::AccountId,106 recipient: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,107 collection_id: RmrkCollectionId,108 nft_id: RmrkNftId,109 approval_required: bool,110 },111 NFTAccepted {112 sender: T::AccountId,113 recipient: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,114 collection_id: RmrkCollectionId,115 nft_id: RmrkNftId,116 },117 NFTRejected {118 sender: T::AccountId,119 collection_id: RmrkCollectionId,120 nft_id: RmrkNftId,121 },122 PropertySet {123 collection_id: RmrkCollectionId,124 maybe_nft_id: Option<RmrkNftId>,125 key: RmrkKeyString,126 value: RmrkValueString,127 },128 ResourceAdded {129 nft_id: RmrkNftId,130 resource_id: RmrkResourceId,131 },132 ResourceRemoval {133 nft_id: RmrkNftId,134 resource_id: RmrkResourceId,135 },136 ResourceAccepted {137 nft_id: RmrkNftId,138 resource_id: RmrkResourceId,139 },140 ResourceRemovalAccepted {141 nft_id: RmrkNftId,142 resource_id: RmrkResourceId,143 },144 PrioritySet {145 collection_id: RmrkCollectionId,146 nft_id: RmrkNftId,147 },148 }149150 #[pallet::error]151 pub enum Error<T> {152 /* Unique-specific events */153 CorruptedCollectionType,154 NftTypeEncodeError,155 RmrkPropertyKeyIsTooLong,156 RmrkPropertyValueIsTooLong,157 UnableToDecodeRmrkData,158159 /* RMRK compatible events */160 CollectionNotEmpty,161 NoAvailableCollectionId,162 NoAvailableNftId,163 CollectionUnknown,164 NoPermission,165 NonTransferable,166 CollectionFullOrLocked,167 ResourceDoesntExist,168 CannotSendToDescendentOrSelf,169 CannotAcceptNonOwnedNft,170 CannotRejectNonOwnedNft,171 ResourceNotPending,172 }173174 #[pallet::call]175 impl<T: Config> Pallet<T> {176 /// Create a collection177 #[transactional]178 #[pallet::weight(<SelfWeightOf<T>>::create_collection())]179 pub fn create_collection(180 origin: OriginFor<T>,181 metadata: RmrkString,182 max: Option<u32>,183 symbol: RmrkCollectionSymbol,184 ) -> DispatchResult {185 let sender = ensure_signed(origin)?;186187 let limits = CollectionLimits {188 owner_can_transfer: Some(false),189 token_limit: max,190 ..Default::default()191 };192193 let data = CreateCollectionData {194 limits: Some(limits),195 token_prefix: symbol196 .into_inner()197 .try_into()198 .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,199 permissions: Some(CollectionPermissions {200 nesting: Some(NestingPermissions {201 token_owner: true,202 admin: false,203 restricted: None,204205 permissive: false,206 }),207 ..Default::default()208 }),209 ..Default::default()210 };211212 let unique_collection_id = Self::init_collection(213 T::CrossAccountId::from_sub(sender.clone()),214 data,215 [216 Self::rmrk_property(Metadata, &metadata)?,217 Self::rmrk_property(CollectionType, &misc::CollectionType::Regular)?,218 ]219 .into_iter(),220 )?;221 let rmrk_collection_id = <CollectionIndex<T>>::get();222223 <UniqueCollectionId<T>>::insert(rmrk_collection_id, unique_collection_id);224225 <PalletCommon<T>>::set_scoped_collection_property(226 unique_collection_id,227 PropertyScope::Rmrk,228 Self::rmrk_property(RmrkInternalCollectionId, &rmrk_collection_id)?,229 )?;230231 <CollectionIndex<T>>::mutate(|n| *n += 1);232233 Self::deposit_event(Event::CollectionCreated {234 issuer: sender,235 collection_id: rmrk_collection_id,236 });237238 Ok(())239 }240241 /// destroy collection242 #[transactional]243 #[pallet::weight(<SelfWeightOf<T>>::destroy_collection())]244 pub fn destroy_collection(245 origin: OriginFor<T>,246 collection_id: RmrkCollectionId,247 ) -> DispatchResult {248 let sender = ensure_signed(origin)?;249 let cross_sender = T::CrossAccountId::from_sub(sender.clone());250251 let collection = Self::get_typed_nft_collection(252 Self::unique_collection_id(collection_id)?,253 misc::CollectionType::Regular,254 )?;255 collection.check_is_external()?;256257 <PalletNft<T>>::destroy_collection(collection, &cross_sender)258 .map_err(Self::map_unique_err_to_proxy)?;259260 Self::deposit_event(Event::CollectionDestroyed {261 issuer: sender,262 collection_id,263 });264265 Ok(())266 }267268 /// Change the issuer of a collection269 ///270 /// Parameters:271 /// - `origin`: sender of the transaction272 /// - `collection_id`: collection id of the nft to change issuer of273 /// - `new_issuer`: Collection's new issuer274 #[transactional]275 #[pallet::weight(<SelfWeightOf<T>>::change_collection_issuer())]276 pub fn change_collection_issuer(277 origin: OriginFor<T>,278 collection_id: RmrkCollectionId,279 new_issuer: <T::Lookup as StaticLookup>::Source,280 ) -> DispatchResult {281 let sender = ensure_signed(origin)?;282283 let collection = Self::get_nft_collection(Self::unique_collection_id(collection_id)?)?;284 collection.check_is_external()?;285286 let new_issuer = T::Lookup::lookup(new_issuer)?;287288 Self::change_collection_owner(289 Self::unique_collection_id(collection_id)?,290 misc::CollectionType::Regular,291 sender.clone(),292 new_issuer.clone(),293 )?;294295 Self::deposit_event(Event::IssuerChanged {296 old_issuer: sender,297 new_issuer,298 collection_id,299 });300301 Ok(())302 }303304 /// lock collection305 #[transactional]306 #[pallet::weight(<SelfWeightOf<T>>::lock_collection())]307 pub fn lock_collection(308 origin: OriginFor<T>,309 collection_id: RmrkCollectionId,310 ) -> DispatchResult {311 let sender = ensure_signed(origin)?;312 let cross_sender = T::CrossAccountId::from_sub(sender.clone());313314 let collection = Self::get_typed_nft_collection(315 Self::unique_collection_id(collection_id)?,316 misc::CollectionType::Regular,317 )?;318 collection.check_is_external()?;319320 Self::check_collection_owner(&collection, &cross_sender)?;321322 let token_count = collection.total_supply();323324 let mut collection = collection.into_inner();325 collection.limits.token_limit = Some(token_count);326 collection.save()?;327328 Self::deposit_event(Event::CollectionLocked {329 issuer: sender,330 collection_id,331 });332333 Ok(())334 }335336 /// Mints an NFT in the specified collection337 /// Sets metadata and the royalty attribute338 ///339 /// Parameters:340 /// - `collection_id`: The class of the asset to be minted.341 /// - `nft_id`: The nft value of the asset to be minted.342 /// - `recipient`: Receiver of the royalty343 /// - `royalty`: Permillage reward from each trade for the Recipient344 /// - `metadata`: Arbitrary data about an nft, e.g. IPFS hash345 /// - `transferable`: Ability to transfer this NFT346 #[transactional]347 #[pallet::weight(<SelfWeightOf<T>>::mint_nft())]348 pub fn mint_nft(349 origin: OriginFor<T>,350 owner: T::AccountId,351 collection_id: RmrkCollectionId,352 recipient: Option<T::AccountId>,353 royalty_amount: Option<Permill>,354 metadata: RmrkString,355 transferable: bool,356 ) -> DispatchResult {357 let sender = ensure_signed(origin)?;358 let sender = T::CrossAccountId::from_sub(sender);359 let cross_owner = T::CrossAccountId::from_sub(owner.clone());360361 let collection = Self::get_typed_nft_collection(362 Self::unique_collection_id(collection_id)?,363 misc::CollectionType::Regular,364 )?;365 collection.check_is_external()?;366367 let royalty_info = royalty_amount.map(|amount| rmrk_traits::RoyaltyInfo {368 recipient: recipient.unwrap_or_else(|| owner.clone()),369 amount,370 });371372 let nft_id = Self::create_nft(373 &sender,374 &cross_owner,375 &collection,376 [377 Self::rmrk_property(TokenType, &NftType::Regular)?,378 Self::rmrk_property(Transferable, &transferable)?,379 Self::rmrk_property(PendingNftAccept, &false)?,380 Self::rmrk_property(RoyaltyInfo, &royalty_info)?,381 Self::rmrk_property(Metadata, &metadata)?,382 Self::rmrk_property(Equipped, &false)?,383 Self::rmrk_property(ResourceCollection, &None::<CollectionId>)?,384 Self::rmrk_property(ResourcePriorities, &<Vec<u8>>::new())?,385 ]386 .into_iter(),387 )388 .map_err(|err| match err {389 DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),390 err => Self::map_unique_err_to_proxy(err),391 })?;392393 Self::deposit_event(Event::NftMinted {394 owner,395 collection_id,396 nft_id: nft_id.0,397 });398399 Ok(())400 }401402 /// burn nft403 #[transactional]404 #[pallet::weight(<SelfWeightOf<T>>::burn_nft(*max_burns))]405 pub fn burn_nft(406 origin: OriginFor<T>,407 collection_id: RmrkCollectionId,408 nft_id: RmrkNftId,409 max_burns: u32,410 ) -> DispatchResult {411 let sender = ensure_signed(origin)?;412 let cross_sender = T::CrossAccountId::from_sub(sender.clone());413414 let collection = Self::get_typed_nft_collection(415 Self::unique_collection_id(collection_id)?,416 misc::CollectionType::Regular,417 )?;418 collection.check_is_external()?;419420 Self::destroy_nft(421 cross_sender,422 Self::unique_collection_id(collection_id)?,423 nft_id.into(),424 max_burns,425 <Error<T>>::NoPermission,426 )427 .map_err(|err| Self::map_unique_err_to_proxy(err.error))?;428429 Self::deposit_event(Event::NFTBurned {430 owner: sender,431 nft_id,432 });433434 Ok(())435 }436437 /// Transfers a NFT from an Account or NFT A to another Account or NFT B438 ///439 /// Parameters:440 /// - `origin`: sender of the transaction441 /// - `rmrk_collection_id`: collection id of the nft to be transferred442 /// - `rmrk_nft_id`: nft id of the nft to be transferred443 /// - `new_owner`: new owner of the nft which can be either an account or a NFT444 #[transactional]445 #[pallet::weight(<SelfWeightOf<T>>::send())]446 pub fn send(447 origin: OriginFor<T>,448 rmrk_collection_id: RmrkCollectionId,449 rmrk_nft_id: RmrkNftId,450 new_owner: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,451 ) -> DispatchResult {452 let sender = ensure_signed(origin.clone())?;453 let cross_sender = T::CrossAccountId::from_sub(sender.clone());454455 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;456 let nft_id = rmrk_nft_id.into();457458 let collection =459 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;460 collection.check_is_external()?;461462 let token_data =463 <TokenData<T>>::get((collection_id, nft_id)).ok_or(<Error<T>>::NoAvailableNftId)?;464465 let from = token_data.owner;466467 ensure!(468 Self::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Transferable)?,469 <Error<T>>::NonTransferable470 );471472 ensure!(473 !Self::get_nft_property_decoded(474 collection_id,475 nft_id,476 RmrkProperty::PendingNftAccept477 )?,478 <Error<T>>::NoPermission479 );480481 let target_owner;482 let approval_required;483484 match new_owner {485 RmrkAccountIdOrCollectionNftTuple::AccountId(ref account_id) => {486 target_owner = T::CrossAccountId::from_sub(account_id.clone());487 approval_required = false;488 }489 RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(490 target_collection_id,491 target_nft_id,492 ) => {493 let target_collection_id = Self::unique_collection_id(target_collection_id)?;494495 let target_nft_budget = budget::Value::new(NESTING_BUDGET);496497 let target_nft_owner = <PalletStructure<T>>::get_checked_topmost_owner(498 target_collection_id,499 target_nft_id.into(),500 Some((collection_id, nft_id)),501 &target_nft_budget,502 )503 .map_err(Self::map_unique_err_to_proxy)?;504505 approval_required = cross_sender != target_nft_owner;506507 if approval_required {508 target_owner = target_nft_owner;509510 <PalletNft<T>>::set_scoped_token_property(511 collection.id,512 nft_id,513 PropertyScope::Rmrk,514 Self::rmrk_property(PendingNftAccept, &approval_required)?,515 )?;516 } else {517 target_owner = T::CrossTokenAddressMapping::token_to_address(518 target_collection_id,519 target_nft_id.into(),520 );521 }522 }523 }524525 let src_nft_budget = budget::Value::new(NESTING_BUDGET);526527 <PalletNft<T>>::transfer_from(528 &collection,529 &cross_sender,530 &from,531 &target_owner,532 nft_id,533 &src_nft_budget,534 )535 .map_err(Self::map_unique_err_to_proxy)?;536537 Self::deposit_event(Event::NFTSent {538 sender,539 recipient: new_owner,540 collection_id: rmrk_collection_id,541 nft_id: rmrk_nft_id,542 approval_required,543 });544545 Ok(())546 }547548 /// Accepts an NFT sent from another account to self or owned NFT549 ///550 /// Parameters:551 /// - `origin`: sender of the transaction552 /// - `rmrk_collection_id`: collection id of the nft to be accepted553 /// - `rmrk_nft_id`: nft id of the nft to be accepted554 /// - `new_owner`: either origin's account ID or origin-owned NFT, whichever the NFT was555 /// sent to556 #[transactional]557 #[pallet::weight(<SelfWeightOf<T>>::accept_nft())]558 pub fn accept_nft(559 origin: OriginFor<T>,560 rmrk_collection_id: RmrkCollectionId,561 rmrk_nft_id: RmrkNftId,562 new_owner: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,563 ) -> DispatchResult {564 let sender = ensure_signed(origin.clone())?;565 let cross_sender = T::CrossAccountId::from_sub(sender.clone());566567 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;568 let nft_id = rmrk_nft_id.into();569570 let collection =571 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;572 collection.check_is_external()?;573574 let new_cross_owner = match new_owner {575 RmrkAccountIdOrCollectionNftTuple::AccountId(ref account_id) => {576 T::CrossAccountId::from_sub(account_id.clone())577 }578 RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(579 target_collection_id,580 target_nft_id,581 ) => {582 let target_collection_id = Self::unique_collection_id(target_collection_id)?;583584 T::CrossTokenAddressMapping::token_to_address(585 target_collection_id,586 TokenId(target_nft_id),587 )588 }589 };590591 let budget = budget::Value::new(NESTING_BUDGET);592593 <PalletNft<T>>::transfer(594 &collection,595 &cross_sender,596 &new_cross_owner,597 nft_id,598 &budget,599 )600 .map_err(|err| {601 if err == <CommonError<T>>::UserIsNotAllowedToNest.into() {602 <Error<T>>::CannotAcceptNonOwnedNft.into()603 } else {604 Self::map_unique_err_to_proxy(err)605 }606 })?;607608 <PalletNft<T>>::set_scoped_token_property(609 collection.id,610 nft_id,611 PropertyScope::Rmrk,612 Self::rmrk_property(PendingNftAccept, &false)?,613 )?;614615 Self::deposit_event(Event::NFTAccepted {616 sender,617 recipient: new_owner,618 collection_id: rmrk_collection_id,619 nft_id: rmrk_nft_id,620 });621622 Ok(())623 }624625 /// Rejects an NFT sent from another account to self or owned NFT626 ///627 /// Parameters:628 /// - `origin`: sender of the transaction629 /// - `rmrk_collection_id`: collection id of the nft to be accepted630 /// - `rmrk_nft_id`: nft id of the nft to be accepted631 #[transactional]632 #[pallet::weight(<SelfWeightOf<T>>::reject_nft())]633 pub fn reject_nft(634 origin: OriginFor<T>,635 rmrk_collection_id: RmrkCollectionId,636 rmrk_nft_id: RmrkNftId,637 ) -> DispatchResult {638 let sender = ensure_signed(origin)?;639 let cross_sender = T::CrossAccountId::from_sub(sender.clone());640641 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;642 let nft_id = rmrk_nft_id.into();643644 let collection =645 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;646 collection.check_is_external()?;647648 ensure!(649 Self::get_nft_property_decoded(650 collection_id,651 nft_id,652 RmrkProperty::PendingNftAccept653 )?,654 <Error<T>>::NoPermission655 );656657 Self::destroy_nft(658 cross_sender,659 collection_id,660 nft_id,661 NESTING_BUDGET,662 <Error<T>>::CannotRejectNonOwnedNft,663 )664 .map_err(|err| Self::map_unique_err_to_proxy(err.error))?;665666 Self::deposit_event(Event::NFTRejected {667 sender,668 collection_id: rmrk_collection_id,669 nft_id: rmrk_nft_id,670 });671672 Ok(())673 }674675 /// accept the addition of a new resource to an existing NFT676 #[transactional]677 #[pallet::weight(<SelfWeightOf<T>>::accept_resource())]678 pub fn accept_resource(679 origin: OriginFor<T>,680 rmrk_collection_id: RmrkCollectionId,681 rmrk_nft_id: RmrkNftId,682 rmrk_resource_id: RmrkResourceId,683 ) -> DispatchResult {684 let sender = ensure_signed(origin)?;685 let cross_sender = T::CrossAccountId::from_sub(sender);686687 let collection_id = Self::unique_collection_id(rmrk_collection_id)688 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;689 let collection =690 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;691 collection.check_is_external()?;692693 let nft_id = rmrk_nft_id.into();694 let resource_id = rmrk_resource_id.into();695696 let budget = budget::Value::new(NESTING_BUDGET);697698 let nft_owner =699 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)700 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;701702 let resource_collection_id: Option<CollectionId> =703 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)704 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;705706 let resource_collection_id =707 resource_collection_id.ok_or(<Error<T>>::ResourceDoesntExist)?;708709 let is_pending: bool = Self::get_nft_property_decoded(710 resource_collection_id,711 resource_id,712 PendingResourceAccept,713 )714 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;715716 ensure!(is_pending, <Error<T>>::ResourceNotPending);717718 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);719720 <PalletNft<T>>::set_scoped_token_property(721 resource_collection_id,722 rmrk_resource_id.into(),723 PropertyScope::Rmrk,724 Self::rmrk_property(PendingResourceAccept, &false)?,725 )?;726727 Self::deposit_event(Event::<T>::ResourceAccepted {728 nft_id: rmrk_nft_id,729 resource_id: rmrk_resource_id,730 });731732 Ok(())733 }734735 /// accept the removal of a resource of an existing NFT736 #[transactional]737 #[pallet::weight(<SelfWeightOf<T>>::accept_resource_removal())]738 pub fn accept_resource_removal(739 origin: OriginFor<T>,740 rmrk_collection_id: RmrkCollectionId,741 rmrk_nft_id: RmrkNftId,742 rmrk_resource_id: RmrkResourceId,743 ) -> DispatchResult {744 let sender = ensure_signed(origin)?;745 let cross_sender = T::CrossAccountId::from_sub(sender);746747 let collection_id = Self::unique_collection_id(rmrk_collection_id)748 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;749 let collection =750 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;751 collection.check_is_external()?;752753 let nft_id = rmrk_nft_id.into();754 let resource_id = rmrk_resource_id.into();755756 let budget = budget::Value::new(NESTING_BUDGET);757758 let nft_owner =759 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)760 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;761762 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);763764 let resource_collection_id: Option<CollectionId> =765 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)766 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;767768 let resource_collection_id =769 resource_collection_id.ok_or(<Error<T>>::ResourceDoesntExist)?;770771 let is_pending: bool = Self::get_nft_property_decoded(772 resource_collection_id,773 resource_id,774 PendingResourceRemoval,775 )776 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;777778 ensure!(is_pending, <Error<T>>::ResourceNotPending);779780 let resource_collection = Self::get_typed_nft_collection(781 resource_collection_id,782 misc::CollectionType::Resource,783 )?;784785 let resource_data = <TokenData<T>>::get((resource_collection_id, resource_id))786 .ok_or(<Error<T>>::ResourceDoesntExist)?;787788 let resource_owner = resource_data.owner;789790 <PalletNft<T>>::burn(791 &resource_collection,792 &resource_owner,793 rmrk_resource_id.into(),794 )795 .map_err(Self::map_unique_err_to_proxy)?;796797 Self::deposit_event(Event::<T>::ResourceRemovalAccepted {798 nft_id: rmrk_nft_id,799 resource_id: rmrk_resource_id,800 });801802 Ok(())803 }804805 /// set a custom value on an NFT806 #[transactional]807 #[pallet::weight(<SelfWeightOf<T>>::set_property())]808 pub fn set_property(809 origin: OriginFor<T>,810 #[pallet::compact] rmrk_collection_id: RmrkCollectionId,811 maybe_nft_id: Option<RmrkNftId>,812 key: RmrkKeyString,813 value: RmrkValueString,814 ) -> DispatchResult {815 let sender = ensure_signed(origin)?;816 let sender = T::CrossAccountId::from_sub(sender);817818 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;819 let collection =820 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;821 collection.check_is_external()?;822823 let budget = budget::Value::new(NESTING_BUDGET);824825 match maybe_nft_id {826 Some(nft_id) => {827 let token_id: TokenId = nft_id.into();828829 Self::ensure_nft_type(collection_id, token_id, NftType::Regular)?;830 Self::ensure_nft_owner(collection_id, token_id, &sender, &budget)?;831832 <PalletNft<T>>::set_scoped_token_property(833 collection_id,834 token_id,835 PropertyScope::Rmrk,836 Self::rmrk_property(UserProperty(key.as_slice()), &value)?,837 )?;838 }839 None => {840 let collection = Self::get_typed_nft_collection(841 collection_id,842 misc::CollectionType::Regular,843 )?;844845 Self::check_collection_owner(&collection, &sender)?;846847 <PalletCommon<T>>::set_scoped_collection_property(848 collection_id,849 PropertyScope::Rmrk,850 Self::rmrk_property(UserProperty(key.as_slice()), &value)?,851 )?;852 }853 }854855 Self::deposit_event(Event::PropertySet {856 collection_id: rmrk_collection_id,857 maybe_nft_id,858 key,859 value,860 });861862 Ok(())863 }864865 /// set a different order of resource priority866 #[transactional]867 #[pallet::weight(<SelfWeightOf<T>>::set_priority())]868 pub fn set_priority(869 origin: OriginFor<T>,870 rmrk_collection_id: RmrkCollectionId,871 rmrk_nft_id: RmrkNftId,872 priorities: BoundedVec<RmrkResourceId, RmrkMaxPriorities>,873 ) -> DispatchResult {874 let sender = ensure_signed(origin)?;875 let sender = T::CrossAccountId::from_sub(sender);876877 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;878 let nft_id = rmrk_nft_id.into();879880 let collection =881 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;882 collection.check_is_external()?;883884 let budget = budget::Value::new(NESTING_BUDGET);885886 Self::ensure_nft_type(collection_id, nft_id, NftType::Regular)?;887 Self::ensure_nft_owner(collection_id, nft_id, &sender, &budget)?;888889 <PalletNft<T>>::set_scoped_token_property(890 collection_id,891 nft_id,892 PropertyScope::Rmrk,893 Self::rmrk_property(ResourcePriorities, &priorities.into_inner())?,894 )?;895896 Self::deposit_event(Event::<T>::PrioritySet {897 collection_id: rmrk_collection_id,898 nft_id: rmrk_nft_id,899 });900901 Ok(())902 }903904 /// Create basic resource905 #[transactional]906 #[pallet::weight(<SelfWeightOf<T>>::add_basic_resource())]907 pub fn add_basic_resource(908 origin: OriginFor<T>,909 rmrk_collection_id: RmrkCollectionId,910 nft_id: RmrkNftId,911 resource: RmrkBasicResource,912 ) -> DispatchResult {913 let sender = ensure_signed(origin.clone())?;914915 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;916 let collection =917 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;918 collection.check_is_external()?;919920 let resource_id = Self::resource_add(921 sender,922 collection_id,923 nft_id.into(),924 [925 Self::rmrk_property(TokenType, &NftType::Resource)?,926 Self::rmrk_property(ResourceType, &misc::ResourceType::Basic)?,927 Self::rmrk_property(Src, &resource.src)?,928 Self::rmrk_property(Metadata, &resource.metadata)?,929 Self::rmrk_property(License, &resource.license)?,930 Self::rmrk_property(Thumb, &resource.thumb)?,931 ]932 .into_iter(),933 )?;934935 Self::deposit_event(Event::ResourceAdded {936 nft_id,937 resource_id,938 });939 Ok(())940 }941942 /// Create composable resource943 #[transactional]944 #[pallet::weight(<SelfWeightOf<T>>::add_composable_resource())]945 pub fn add_composable_resource(946 origin: OriginFor<T>,947 rmrk_collection_id: RmrkCollectionId,948 nft_id: RmrkNftId,949 resource: RmrkComposableResource,950 ) -> DispatchResult {951 let sender = ensure_signed(origin.clone())?;952953 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;954 let collection =955 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;956 collection.check_is_external()?;957958 let resource_id = Self::resource_add(959 sender,960 collection_id,961 nft_id.into(),962 [963 Self::rmrk_property(TokenType, &NftType::Resource)?,964 Self::rmrk_property(ResourceType, &misc::ResourceType::Composable)?,965 Self::rmrk_property(Parts, &resource.parts)?,966 Self::rmrk_property(Base, &resource.base)?,967 Self::rmrk_property(Src, &resource.src)?,968 Self::rmrk_property(Metadata, &resource.metadata)?,969 Self::rmrk_property(License, &resource.license)?,970 Self::rmrk_property(Thumb, &resource.thumb)?,971 ]972 .into_iter(),973 )?;974975 Self::deposit_event(Event::ResourceAdded {976 nft_id,977 resource_id,978 });979 Ok(())980 }981982 /// Create slot resource983 #[transactional]984 #[pallet::weight(<SelfWeightOf<T>>::add_slot_resource())]985 pub fn add_slot_resource(986 origin: OriginFor<T>,987 rmrk_collection_id: RmrkCollectionId,988 nft_id: RmrkNftId,989 resource: RmrkSlotResource,990 ) -> DispatchResult {991 let sender = ensure_signed(origin.clone())?;992993 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;994 let collection =995 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;996 collection.check_is_external()?;997998 let resource_id = Self::resource_add(999 sender,1000 collection_id,1001 nft_id.into(),1002 [1003 Self::rmrk_property(TokenType, &NftType::Resource)?,1004 Self::rmrk_property(ResourceType, &misc::ResourceType::Slot)?,1005 Self::rmrk_property(Base, &resource.base)?,1006 Self::rmrk_property(Src, &resource.src)?,1007 Self::rmrk_property(Metadata, &resource.metadata)?,1008 Self::rmrk_property(Slot, &resource.slot)?,1009 Self::rmrk_property(License, &resource.license)?,1010 Self::rmrk_property(Thumb, &resource.thumb)?,1011 ]1012 .into_iter(),1013 )?;10141015 Self::deposit_event(Event::ResourceAdded {1016 nft_id,1017 resource_id,1018 });1019 Ok(())1020 }10211022 /// remove resource1023 #[transactional]1024 #[pallet::weight(<SelfWeightOf<T>>::remove_resource())]1025 pub fn remove_resource(1026 origin: OriginFor<T>,1027 rmrk_collection_id: RmrkCollectionId,1028 nft_id: RmrkNftId,1029 resource_id: RmrkResourceId,1030 ) -> DispatchResult {1031 let sender = ensure_signed(origin.clone())?;10321033 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;1034 let collection =1035 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1036 collection.check_is_external()?;10371038 Self::resource_remove(sender, collection_id, nft_id.into(), resource_id.into())?;10391040 Self::deposit_event(Event::ResourceRemoval {1041 nft_id,1042 resource_id,1043 });1044 Ok(())1045 }1046 }1047}10481049impl<T: Config> Pallet<T> {1050 pub fn rmrk_property_key(rmrk_key: RmrkProperty) -> Result<PropertyKey, DispatchError> {1051 let key = rmrk_key.to_key::<T>()?;10521053 let scoped_key = PropertyScope::Rmrk1054 .apply(key)1055 .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)?;10561057 Ok(scoped_key)1058 }10591060 // todo think about renaming these1061 pub fn rmrk_property<E: Encode>(1062 rmrk_key: RmrkProperty,1063 value: &E,1064 ) -> Result<Property, DispatchError> {1065 let key = rmrk_key.to_key::<T>()?;10661067 let value = value1068 .encode()1069 .try_into()1070 .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong)?;10711072 let property = Property { key, value };10731074 Ok(property)1075 }10761077 pub fn decode_property<D: Decode>(vec: PropertyValue) -> Result<D, DispatchError> {1078 vec.decode()1079 .map_err(|_| <Error<T>>::UnableToDecodeRmrkData.into())1080 }10811082 pub fn rebind<L, S>(vec: &BoundedVec<u8, L>) -> Result<BoundedVec<u8, S>, DispatchError>1083 where1084 BoundedVec<u8, S>: TryFrom<Vec<u8>>,1085 {1086 vec.rebind()1087 .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong.into())1088 }10891090 fn init_collection(1091 sender: T::CrossAccountId,1092 data: CreateCollectionData<T::AccountId>,1093 properties: impl Iterator<Item = Property>,1094 ) -> Result<CollectionId, DispatchError> {1095 let collection_id = <PalletNft<T>>::init_collection(sender, data, true);10961097 if let Err(DispatchError::Arithmetic(_)) = &collection_id {1098 return Err(<Error<T>>::NoAvailableCollectionId.into());1099 }11001101 <PalletCommon<T>>::set_scoped_collection_properties(1102 collection_id?,1103 PropertyScope::Rmrk,1104 properties,1105 )?;11061107 collection_id1108 }11091110 pub fn create_nft(1111 sender: &T::CrossAccountId,1112 owner: &T::CrossAccountId,1113 collection: &NonfungibleHandle<T>,1114 properties: impl Iterator<Item = Property>,1115 ) -> Result<TokenId, DispatchError> {1116 let data = CreateNftExData {1117 properties: BoundedVec::default(),1118 owner: owner.clone(),1119 };11201121 let budget = budget::Value::new(NESTING_BUDGET);11221123 <PalletNft<T>>::create_item(collection, sender, data, &budget)?;11241125 let nft_id = <PalletNft<T>>::current_token_id(collection.id);11261127 <PalletNft<T>>::set_scoped_token_properties(1128 collection.id,1129 nft_id,1130 PropertyScope::Rmrk,1131 properties,1132 )?;11331134 Ok(nft_id)1135 }11361137 fn destroy_nft(1138 sender: T::CrossAccountId,1139 collection_id: CollectionId,1140 token_id: TokenId,1141 max_burns: u32,1142 error_if_not_owned: Error<T>,1143 ) -> DispatchResultWithPostInfo {1144 let collection =1145 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;11461147 let token_data =1148 <TokenData<T>>::get((collection_id, token_id)).ok_or(<Error<T>>::NoAvailableNftId)?;11491150 let from = token_data.owner;11511152 let owner_check_budget = budget::Value::new(NESTING_BUDGET);11531154 ensure!(1155 <PalletStructure<T>>::check_indirectly_owned(1156 sender.clone(),1157 collection_id,1158 token_id,1159 None,1160 &owner_check_budget1161 )?,1162 error_if_not_owned,1163 );11641165 let burns_budget = budget::Value::new(max_burns);1166 let breadth_budget = budget::Value::new(max_burns);11671168 <PalletNft<T>>::burn_recursively(1169 &collection,1170 &from,1171 token_id,1172 &burns_budget,1173 &breadth_budget,1174 )1175 }11761177 fn resource_add(1178 sender: T::AccountId,1179 collection_id: CollectionId,1180 token_id: TokenId,1181 resource_properties: impl Iterator<Item = Property>,1182 ) -> Result<RmrkResourceId, DispatchError> {1183 let collection =1184 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1185 ensure!(collection.owner == sender, Error::<T>::NoPermission);11861187 let sender = T::CrossAccountId::from_sub(sender);1188 let budget = budget::Value::new(NESTING_BUDGET);11891190 let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, token_id, &budget)1191 .map_err(Self::map_unique_err_to_proxy)?;11921193 let pending = sender != nft_owner;11941195 let resource_collection_id: Option<CollectionId> =1196 Self::get_nft_property_decoded(collection_id, token_id, ResourceCollection)?;11971198 let resource_collection_id = match resource_collection_id {1199 Some(id) => id,1200 None => {1201 let resource_collection_id = Self::init_collection(1202 sender.clone(),1203 CreateCollectionData {1204 ..Default::default()1205 },1206 [Self::rmrk_property(1207 CollectionType,1208 &misc::CollectionType::Resource,1209 )?]1210 .into_iter(),1211 )?;12121213 <PalletNft<T>>::set_scoped_token_property(1214 collection_id,1215 token_id,1216 PropertyScope::Rmrk,1217 Self::rmrk_property(ResourceCollection, &Some(resource_collection_id))?,1218 )?;12191220 resource_collection_id1221 }1222 };12231224 let resource_collection =1225 Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?;12261227 // todo probably add extra connections to bases, slots, etc., when RMRK starts to use them12281229 let resource_id = Self::create_nft(1230 &sender,1231 &nft_owner,1232 &resource_collection,1233 resource_properties.chain(1234 [1235 Self::rmrk_property(PendingResourceAccept, &pending)?,1236 Self::rmrk_property(PendingResourceRemoval, &false)?,1237 ]1238 .into_iter(),1239 ),1240 )1241 .map_err(|err| match err {1242 DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),1243 err => Self::map_unique_err_to_proxy(err),1244 })?;12451246 Ok(resource_id.0)1247 }12481249 fn resource_remove(1250 sender: T::AccountId,1251 collection_id: CollectionId,1252 nft_id: TokenId,1253 resource_id: TokenId,1254 ) -> DispatchResult {1255 let collection =1256 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1257 ensure!(collection.owner == sender, Error::<T>::NoPermission);12581259 let resource_collection_id: Option<CollectionId> =1260 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)?;12611262 let resource_collection_id =1263 resource_collection_id.ok_or(Error::<T>::ResourceDoesntExist)?;12641265 let resource_collection =1266 Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?;1267 ensure!(1268 <PalletNft<T>>::token_exists(&resource_collection, resource_id),1269 Error::<T>::ResourceDoesntExist1270 );12711272 let budget = up_data_structs::budget::Value::new(NESTING_BUDGET);1273 let topmost_owner =1274 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?;12751276 let sender = T::CrossAccountId::from_sub(sender);1277 if topmost_owner == sender {1278 <PalletNft<T>>::burn(&resource_collection, &sender, resource_id)1279 .map_err(Self::map_unique_err_to_proxy)?;1280 } else {1281 <PalletNft<T>>::set_scoped_token_property(1282 resource_collection_id,1283 resource_id,1284 PropertyScope::Rmrk,1285 Self::rmrk_property(PendingResourceRemoval, &true)?,1286 )?;1287 }12881289 Ok(())1290 }12911292 fn change_collection_owner(1293 collection_id: CollectionId,1294 collection_type: misc::CollectionType,1295 sender: T::AccountId,1296 new_owner: T::AccountId,1297 ) -> DispatchResult {1298 let collection = Self::get_typed_nft_collection(collection_id, collection_type)?;1299 Self::check_collection_owner(&collection, &T::CrossAccountId::from_sub(sender))?;13001301 let mut collection = collection.into_inner();13021303 collection.owner = new_owner;1304 collection.save()1305 }13061307 fn check_collection_owner(1308 collection: &NonfungibleHandle<T>,1309 account: &T::CrossAccountId,1310 ) -> DispatchResult {1311 collection1312 .check_is_owner(account)1313 .map_err(Self::map_unique_err_to_proxy)1314 }13151316 pub fn last_collection_idx() -> RmrkCollectionId {1317 <CollectionIndex<T>>::get()1318 }13191320 pub fn unique_collection_id(1321 rmrk_collection_id: RmrkCollectionId,1322 ) -> Result<CollectionId, DispatchError> {1323 <UniqueCollectionId<T>>::try_get(rmrk_collection_id)1324 .map_err(|_| <Error<T>>::CollectionUnknown.into())1325 }13261327 pub fn rmrk_collection_id(1328 unique_collection_id: CollectionId,1329 ) -> Result<RmrkCollectionId, DispatchError> {1330 Self::get_collection_property_decoded(unique_collection_id, RmrkInternalCollectionId)1331 }13321333 pub fn get_nft_collection(1334 collection_id: CollectionId,1335 ) -> Result<NonfungibleHandle<T>, DispatchError> {1336 let collection = <CollectionHandle<T>>::try_get(collection_id)1337 .map_err(|_| <Error<T>>::CollectionUnknown)?;13381339 match collection.mode {1340 CollectionMode::NFT => Ok(NonfungibleHandle::cast(collection)),1341 _ => Err(<Error<T>>::CollectionUnknown.into()),1342 }1343 }13441345 pub fn collection_exists(collection_id: CollectionId) -> bool {1346 <CollectionHandle<T>>::try_get(collection_id).is_ok()1347 }13481349 pub fn get_collection_property(1350 collection_id: CollectionId,1351 key: RmrkProperty,1352 ) -> Result<PropertyValue, DispatchError> {1353 let collection_property = <PalletCommon<T>>::collection_properties(collection_id)1354 .get(&Self::rmrk_property_key(key)?)1355 .ok_or(<Error<T>>::CollectionUnknown)?1356 .clone();13571358 Ok(collection_property)1359 }13601361 pub fn get_collection_property_decoded<V: Decode>(1362 collection_id: CollectionId,1363 key: RmrkProperty,1364 ) -> Result<V, DispatchError> {1365 Self::decode_property(Self::get_collection_property(collection_id, key)?)1366 }13671368 pub fn get_collection_type(1369 collection_id: CollectionId,1370 ) -> Result<misc::CollectionType, DispatchError> {1371 Self::get_collection_property_decoded(collection_id, CollectionType)1372 .map_err(|_| <Error<T>>::CorruptedCollectionType.into())1373 }13741375 pub fn ensure_collection_type(1376 collection_id: CollectionId,1377 collection_type: misc::CollectionType,1378 ) -> DispatchResult {1379 let actual_type = Self::get_collection_type(collection_id)?;1380 ensure!(1381 actual_type == collection_type,1382 <CommonError<T>>::NoPermission1383 );13841385 Ok(())1386 }13871388 pub fn get_typed_nft_collection(1389 collection_id: CollectionId,1390 collection_type: misc::CollectionType,1391 ) -> Result<NonfungibleHandle<T>, DispatchError> {1392 Self::ensure_collection_type(collection_id, collection_type)?;13931394 Self::get_nft_collection(collection_id)1395 }13961397 pub fn get_typed_nft_collection_mapped(1398 rmrk_collection_id: RmrkCollectionId,1399 collection_type: misc::CollectionType,1400 ) -> Result<(NonfungibleHandle<T>, CollectionId), DispatchError> {1401 let unique_collection_id = match collection_type {1402 misc::CollectionType::Regular => Self::unique_collection_id(rmrk_collection_id)?,1403 _ => rmrk_collection_id.into(),1404 };14051406 let collection = Self::get_typed_nft_collection(unique_collection_id, collection_type)?;14071408 Ok((collection, unique_collection_id))1409 }14101411 pub fn get_nft_property(1412 collection_id: CollectionId,1413 nft_id: TokenId,1414 key: RmrkProperty,1415 ) -> Result<PropertyValue, DispatchError> {1416 let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))1417 .get(&Self::rmrk_property_key(key)?)1418 .ok_or(<Error<T>>::NoAvailableNftId)? // todo replace with better error?1419 .clone();14201421 Ok(nft_property)1422 }14231424 pub fn get_nft_property_decoded<V: Decode>(1425 collection_id: CollectionId,1426 nft_id: TokenId,1427 key: RmrkProperty,1428 ) -> Result<V, DispatchError> {1429 Self::decode_property(Self::get_nft_property(collection_id, nft_id, key)?)1430 }14311432 pub fn nft_exists(collection_id: CollectionId, nft_id: TokenId) -> bool {1433 <TokenData<T>>::contains_key((collection_id, nft_id))1434 }14351436 pub fn get_nft_type(1437 collection_id: CollectionId,1438 token_id: TokenId,1439 ) -> Result<NftType, DispatchError> {1440 Self::get_nft_property_decoded(collection_id, token_id, TokenType)1441 .map_err(|_| <Error<T>>::NoAvailableNftId.into())1442 }14431444 pub fn ensure_nft_type(1445 collection_id: CollectionId,1446 token_id: TokenId,1447 nft_type: NftType,1448 ) -> DispatchResult {1449 let actual_type = Self::get_nft_type(collection_id, token_id)?;1450 ensure!(actual_type == nft_type, <Error<T>>::NoPermission);14511452 Ok(())1453 }14541455 pub fn ensure_nft_owner(1456 collection_id: CollectionId,1457 token_id: TokenId,1458 possible_owner: &T::CrossAccountId,1459 nesting_budget: &dyn budget::Budget,1460 ) -> DispatchResult {1461 let is_owned = <PalletStructure<T>>::check_indirectly_owned(1462 possible_owner.clone(),1463 collection_id,1464 token_id,1465 None,1466 nesting_budget,1467 )1468 .map_err(Self::map_unique_err_to_proxy)?;14691470 ensure!(is_owned, <Error<T>>::NoPermission);14711472 Ok(())1473 }14741475 pub fn filter_user_properties<Key, Value, R, Mapper>(1476 collection_id: CollectionId,1477 token_id: Option<TokenId>,1478 filter_keys: Option<Vec<RmrkPropertyKey>>,1479 mapper: Mapper,1480 ) -> Result<Vec<R>, DispatchError>1481 where1482 Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,1483 Value: Decode + Default,1484 Mapper: Fn(Key, Value) -> R,1485 {1486 filter_keys1487 .map(|keys| {1488 let properties = keys1489 .into_iter()1490 .filter_map(|key| {1491 let key: Key = key.try_into().ok()?;14921493 let value = match token_id {1494 Some(token_id) => Self::get_nft_property_decoded(1495 collection_id,1496 token_id,1497 UserProperty(key.as_ref()),1498 ),1499 None => Self::get_collection_property_decoded(1500 collection_id,1501 UserProperty(key.as_ref()),1502 ),1503 }1504 .ok()?;15051506 Some(mapper(key, value))1507 })1508 .collect();15091510 Ok(properties)1511 })1512 .unwrap_or_else(|| {1513 let properties =1514 Self::iterate_user_properties(collection_id, token_id, mapper)?.collect();15151516 Ok(properties)1517 })1518 }15191520 pub fn iterate_user_properties<Key, Value, R, Mapper>(1521 collection_id: CollectionId,1522 token_id: Option<TokenId>,1523 mapper: Mapper,1524 ) -> Result<impl Iterator<Item = R>, DispatchError>1525 where1526 Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,1527 Value: Decode + Default,1528 Mapper: Fn(Key, Value) -> R,1529 {1530 let key_prefix = Self::rmrk_property_key(UserProperty(b""))?;15311532 let properties = match token_id {1533 Some(token_id) => <PalletNft<T>>::token_properties((collection_id, token_id)),1534 None => <PalletCommon<T>>::collection_properties(collection_id),1535 };15361537 let properties = properties.into_iter().filter_map(move |(key, value)| {1538 let key = key.as_slice().strip_prefix(key_prefix.as_slice())?;15391540 let key: Key = key.to_vec().try_into().ok()?;1541 let value: Value = value.decode().ok()?;15421543 Some(mapper(key, value))1544 });15451546 Ok(properties)1547 }15481549 fn map_unique_err_to_proxy(err: DispatchError) -> DispatchError {1550 map_unique_err_to_proxy! {1551 match err {1552 CommonError::NoPermission => NoPermission,1553 CommonError::CollectionTokenLimitExceeded => CollectionFullOrLocked,1554 CommonError::PublicMintingNotAllowed => NoPermission,1555 CommonError::TokenNotFound => NoAvailableNftId,1556 CommonError::ApprovedValueTooLow => NoPermission,1557 CommonError::CantDestroyNotEmptyCollection => CollectionNotEmpty,1558 StructureError::TokenNotFound => NoAvailableNftId,1559 StructureError::OuroborosDetected => CannotSendToDescendentOrSelf,1560 }1561 }1562 }1563}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::{DispatchError, Permill, traits::StaticLookup};22use sp_std::vec::Vec;23use up_data_structs::{*, mapping::TokenAddressMapping};24use pallet_common::{25 Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations,26};27use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle, TokenData};28use pallet_structure::{Pallet as PalletStructure, Error as StructureError};29use pallet_evm::account::CrossAccountId;30use core::convert::AsRef;3132pub use pallet::*;3334#[cfg(feature = "runtime-benchmarks")]35pub mod benchmarking;36pub mod misc;37pub mod property;38pub mod weights;3940pub type SelfWeightOf<T> = <T as Config>::WeightInfo;4142use weights::WeightInfo;43use misc::*;44pub use property::*;4546use RmrkProperty::*;4748pub const NESTING_BUDGET: u32 = 5;4950#[frame_support::pallet]51pub mod pallet {52 use super::*;53 use pallet_evm::account;5455 #[pallet::config]56 pub trait Config:57 frame_system::Config + pallet_common::Config + pallet_nonfungible::Config + account::Config58 {59 type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;60 type WeightInfo: WeightInfo;61 }6263 #[pallet::storage]64 #[pallet::getter(fn collection_index)]65 pub type CollectionIndex<T: Config> = StorageValue<_, RmrkCollectionId, ValueQuery>;6667 #[pallet::storage]68 pub type UniqueCollectionId<T: Config> =69 StorageMap<_, Twox64Concat, RmrkCollectionId, CollectionId, ValueQuery>;7071 #[pallet::pallet]72 #[pallet::generate_store(pub(super) trait Store)]73 pub struct Pallet<T>(_);7475 #[pallet::event]76 #[pallet::generate_deposit(pub(super) fn deposit_event)]77 pub enum Event<T: Config> {78 CollectionCreated {79 issuer: T::AccountId,80 collection_id: RmrkCollectionId,81 },82 CollectionDestroyed {83 issuer: T::AccountId,84 collection_id: RmrkCollectionId,85 },86 IssuerChanged {87 old_issuer: T::AccountId,88 new_issuer: T::AccountId,89 collection_id: RmrkCollectionId,90 },91 CollectionLocked {92 issuer: T::AccountId,93 collection_id: RmrkCollectionId,94 },95 NftMinted {96 owner: T::AccountId,97 collection_id: RmrkCollectionId,98 nft_id: RmrkNftId,99 },100 NFTBurned {101 owner: T::AccountId,102 nft_id: RmrkNftId,103 },104 NFTSent {105 sender: T::AccountId,106 recipient: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,107 collection_id: RmrkCollectionId,108 nft_id: RmrkNftId,109 approval_required: bool,110 },111 NFTAccepted {112 sender: T::AccountId,113 recipient: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,114 collection_id: RmrkCollectionId,115 nft_id: RmrkNftId,116 },117 NFTRejected {118 sender: T::AccountId,119 collection_id: RmrkCollectionId,120 nft_id: RmrkNftId,121 },122 PropertySet {123 collection_id: RmrkCollectionId,124 maybe_nft_id: Option<RmrkNftId>,125 key: RmrkKeyString,126 value: RmrkValueString,127 },128 ResourceAdded {129 nft_id: RmrkNftId,130 resource_id: RmrkResourceId,131 },132 ResourceRemoval {133 nft_id: RmrkNftId,134 resource_id: RmrkResourceId,135 },136 ResourceAccepted {137 nft_id: RmrkNftId,138 resource_id: RmrkResourceId,139 },140 ResourceRemovalAccepted {141 nft_id: RmrkNftId,142 resource_id: RmrkResourceId,143 },144 PrioritySet {145 collection_id: RmrkCollectionId,146 nft_id: RmrkNftId,147 },148 }149150 #[pallet::error]151 pub enum Error<T> {152 /* Unique-specific events */153 CorruptedCollectionType,154 NftTypeEncodeError,155 RmrkPropertyKeyIsTooLong,156 RmrkPropertyValueIsTooLong,157 UnableToDecodeRmrkData,158159 /* RMRK compatible events */160 CollectionNotEmpty,161 NoAvailableCollectionId,162 NoAvailableNftId,163 CollectionUnknown,164 NoPermission,165 NonTransferable,166 CollectionFullOrLocked,167 ResourceDoesntExist,168 CannotSendToDescendentOrSelf,169 CannotAcceptNonOwnedNft,170 CannotRejectNonOwnedNft,171 ResourceNotPending,172 }173174 #[pallet::call]175 impl<T: Config> Pallet<T> {176 /// Create a collection177 #[transactional]178 #[pallet::weight(<SelfWeightOf<T>>::create_collection())]179 pub fn create_collection(180 origin: OriginFor<T>,181 metadata: RmrkString,182 max: Option<u32>,183 symbol: RmrkCollectionSymbol,184 ) -> DispatchResult {185 let sender = ensure_signed(origin)?;186187 let limits = CollectionLimits {188 owner_can_transfer: Some(false),189 token_limit: max,190 ..Default::default()191 };192193 let data = CreateCollectionData {194 limits: Some(limits),195 token_prefix: symbol196 .into_inner()197 .try_into()198 .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,199 permissions: Some(CollectionPermissions {200 nesting: Some(NestingPermissions {201 token_owner: true,202 admin: false,203 restricted: None,204205 permissive: false,206 }),207 ..Default::default()208 }),209 ..Default::default()210 };211212 let unique_collection_id = Self::init_collection(213 T::CrossAccountId::from_sub(sender.clone()),214 data,215 [216 Self::rmrk_property(Metadata, &metadata)?,217 Self::rmrk_property(CollectionType, &misc::CollectionType::Regular)?,218 ]219 .into_iter(),220 )?;221 let rmrk_collection_id = <CollectionIndex<T>>::get();222223 <UniqueCollectionId<T>>::insert(rmrk_collection_id, unique_collection_id);224225 <PalletCommon<T>>::set_scoped_collection_property(226 unique_collection_id,227 PropertyScope::Rmrk,228 Self::rmrk_property(RmrkInternalCollectionId, &rmrk_collection_id)?,229 )?;230231 <CollectionIndex<T>>::mutate(|n| *n += 1);232233 Self::deposit_event(Event::CollectionCreated {234 issuer: sender,235 collection_id: rmrk_collection_id,236 });237238 Ok(())239 }240241 /// destroy collection242 #[transactional]243 #[pallet::weight(<SelfWeightOf<T>>::destroy_collection())]244 pub fn destroy_collection(245 origin: OriginFor<T>,246 collection_id: RmrkCollectionId,247 ) -> DispatchResult {248 let sender = ensure_signed(origin)?;249 let cross_sender = T::CrossAccountId::from_sub(sender.clone());250251 let collection = Self::get_typed_nft_collection(252 Self::unique_collection_id(collection_id)?,253 misc::CollectionType::Regular,254 )?;255 collection.check_is_external()?;256257 <PalletNft<T>>::destroy_collection(collection, &cross_sender)258 .map_err(Self::map_unique_err_to_proxy)?;259260 Self::deposit_event(Event::CollectionDestroyed {261 issuer: sender,262 collection_id,263 });264265 Ok(())266 }267268 /// Change the issuer of a collection269 ///270 /// Parameters:271 /// - `origin`: sender of the transaction272 /// - `collection_id`: collection id of the nft to change issuer of273 /// - `new_issuer`: Collection's new issuer274 #[transactional]275 #[pallet::weight(<SelfWeightOf<T>>::change_collection_issuer())]276 pub fn change_collection_issuer(277 origin: OriginFor<T>,278 collection_id: RmrkCollectionId,279 new_issuer: <T::Lookup as StaticLookup>::Source,280 ) -> DispatchResult {281 let sender = ensure_signed(origin)?;282283 let collection = Self::get_nft_collection(Self::unique_collection_id(collection_id)?)?;284 collection.check_is_external()?;285286 let new_issuer = T::Lookup::lookup(new_issuer)?;287288 Self::change_collection_owner(289 Self::unique_collection_id(collection_id)?,290 misc::CollectionType::Regular,291 sender.clone(),292 new_issuer.clone(),293 )?;294295 Self::deposit_event(Event::IssuerChanged {296 old_issuer: sender,297 new_issuer,298 collection_id,299 });300301 Ok(())302 }303304 /// lock collection305 #[transactional]306 #[pallet::weight(<SelfWeightOf<T>>::lock_collection())]307 pub fn lock_collection(308 origin: OriginFor<T>,309 collection_id: RmrkCollectionId,310 ) -> DispatchResult {311 let sender = ensure_signed(origin)?;312 let cross_sender = T::CrossAccountId::from_sub(sender.clone());313314 let collection = Self::get_typed_nft_collection(315 Self::unique_collection_id(collection_id)?,316 misc::CollectionType::Regular,317 )?;318 collection.check_is_external()?;319320 Self::check_collection_owner(&collection, &cross_sender)?;321322 let token_count = collection.total_supply();323324 let mut collection = collection.into_inner();325 collection.limits.token_limit = Some(token_count);326 collection.save()?;327328 Self::deposit_event(Event::CollectionLocked {329 issuer: sender,330 collection_id,331 });332333 Ok(())334 }335336 /// Mints an NFT in the specified collection337 /// Sets metadata and the royalty attribute338 ///339 /// Parameters:340 /// - `collection_id`: The class of the asset to be minted.341 /// - `nft_id`: The nft value of the asset to be minted.342 /// - `recipient`: Receiver of the royalty343 /// - `royalty`: Permillage reward from each trade for the Recipient344 /// - `metadata`: Arbitrary data about an nft, e.g. IPFS hash345 /// - `transferable`: Ability to transfer this NFT346 #[transactional]347 #[pallet::weight(<SelfWeightOf<T>>::mint_nft())]348 pub fn mint_nft(349 origin: OriginFor<T>,350 owner: T::AccountId,351 collection_id: RmrkCollectionId,352 recipient: Option<T::AccountId>,353 royalty_amount: Option<Permill>,354 metadata: RmrkString,355 transferable: bool,356 resources: Option<BoundedVec<RmrkResourceTypes, MaxResourcesOnMint>>357 ) -> DispatchResult {358 let sender = ensure_signed(origin)?;359 let cross_sender = T::CrossAccountId::from_sub(sender.clone());360 let cross_owner = T::CrossAccountId::from_sub(owner.clone());361362 let collection = Self::get_typed_nft_collection(363 Self::unique_collection_id(collection_id)?,364 misc::CollectionType::Regular,365 )?;366 collection.check_is_external()?;367368 let royalty_info = royalty_amount.map(|amount| rmrk_traits::RoyaltyInfo {369 recipient: recipient.unwrap_or_else(|| owner.clone()),370 amount,371 });372373 let nft_id = Self::create_nft(374 &cross_sender,375 &cross_owner,376 &collection,377 [378 Self::rmrk_property(TokenType, &NftType::Regular)?,379 Self::rmrk_property(Transferable, &transferable)?,380 Self::rmrk_property(PendingNftAccept, &false)?,381 Self::rmrk_property(RoyaltyInfo, &royalty_info)?,382 Self::rmrk_property(Metadata, &metadata)?,383 Self::rmrk_property(Equipped, &false)?,384 Self::rmrk_property(ResourceCollection, &None::<CollectionId>)?,385 Self::rmrk_property(ResourcePriorities, &<Vec<u8>>::new())?,386 ]387 .into_iter(),388 )389 .map_err(|err| match err {390 DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),391 err => Self::map_unique_err_to_proxy(err),392 })?;393394 if let Some(resources) = resources {395 for resource in resources {396 Self::resource_add(397 sender.clone(),398 collection.id,399 nft_id,400 resource401 )?;402 }403 }404405 Self::deposit_event(Event::NftMinted {406 owner,407 collection_id,408 nft_id: nft_id.0,409 });410411 Ok(())412 }413414 /// burn nft415 #[transactional]416 #[pallet::weight(<SelfWeightOf<T>>::burn_nft(*max_burns))]417 pub fn burn_nft(418 origin: OriginFor<T>,419 collection_id: RmrkCollectionId,420 nft_id: RmrkNftId,421 max_burns: u32,422 ) -> DispatchResult {423 let sender = ensure_signed(origin)?;424 let cross_sender = T::CrossAccountId::from_sub(sender.clone());425426 let collection = Self::get_typed_nft_collection(427 Self::unique_collection_id(collection_id)?,428 misc::CollectionType::Regular,429 )?;430 collection.check_is_external()?;431432 Self::destroy_nft(433 cross_sender,434 Self::unique_collection_id(collection_id)?,435 nft_id.into(),436 max_burns,437 <Error<T>>::NoPermission,438 )439 .map_err(|err| Self::map_unique_err_to_proxy(err.error))?;440441 Self::deposit_event(Event::NFTBurned {442 owner: sender,443 nft_id,444 });445446 Ok(())447 }448449 /// Transfers a NFT from an Account or NFT A to another Account or NFT B450 ///451 /// Parameters:452 /// - `origin`: sender of the transaction453 /// - `rmrk_collection_id`: collection id of the nft to be transferred454 /// - `rmrk_nft_id`: nft id of the nft to be transferred455 /// - `new_owner`: new owner of the nft which can be either an account or a NFT456 #[transactional]457 #[pallet::weight(<SelfWeightOf<T>>::send())]458 pub fn send(459 origin: OriginFor<T>,460 rmrk_collection_id: RmrkCollectionId,461 rmrk_nft_id: RmrkNftId,462 new_owner: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,463 ) -> DispatchResult {464 let sender = ensure_signed(origin.clone())?;465 let cross_sender = T::CrossAccountId::from_sub(sender.clone());466467 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;468 let nft_id = rmrk_nft_id.into();469470 let collection =471 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;472 collection.check_is_external()?;473474 let token_data =475 <TokenData<T>>::get((collection_id, nft_id)).ok_or(<Error<T>>::NoAvailableNftId)?;476477 let from = token_data.owner;478479 ensure!(480 Self::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Transferable)?,481 <Error<T>>::NonTransferable482 );483484 ensure!(485 !Self::get_nft_property_decoded(486 collection_id,487 nft_id,488 RmrkProperty::PendingNftAccept489 )?,490 <Error<T>>::NoPermission491 );492493 let target_owner;494 let approval_required;495496 match new_owner {497 RmrkAccountIdOrCollectionNftTuple::AccountId(ref account_id) => {498 target_owner = T::CrossAccountId::from_sub(account_id.clone());499 approval_required = false;500 }501 RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(502 target_collection_id,503 target_nft_id,504 ) => {505 let target_collection_id = Self::unique_collection_id(target_collection_id)?;506507 let target_nft_budget = budget::Value::new(NESTING_BUDGET);508509 let target_nft_owner = <PalletStructure<T>>::get_checked_topmost_owner(510 target_collection_id,511 target_nft_id.into(),512 Some((collection_id, nft_id)),513 &target_nft_budget,514 )515 .map_err(Self::map_unique_err_to_proxy)?;516517 approval_required = cross_sender != target_nft_owner;518519 if approval_required {520 target_owner = target_nft_owner;521522 <PalletNft<T>>::set_scoped_token_property(523 collection.id,524 nft_id,525 PropertyScope::Rmrk,526 Self::rmrk_property(PendingNftAccept, &approval_required)?,527 )?;528 } else {529 target_owner = T::CrossTokenAddressMapping::token_to_address(530 target_collection_id,531 target_nft_id.into(),532 );533 }534 }535 }536537 let src_nft_budget = budget::Value::new(NESTING_BUDGET);538539 <PalletNft<T>>::transfer_from(540 &collection,541 &cross_sender,542 &from,543 &target_owner,544 nft_id,545 &src_nft_budget,546 )547 .map_err(Self::map_unique_err_to_proxy)?;548549 Self::deposit_event(Event::NFTSent {550 sender,551 recipient: new_owner,552 collection_id: rmrk_collection_id,553 nft_id: rmrk_nft_id,554 approval_required,555 });556557 Ok(())558 }559560 /// Accepts an NFT sent from another account to self or owned NFT561 ///562 /// Parameters:563 /// - `origin`: sender of the transaction564 /// - `rmrk_collection_id`: collection id of the nft to be accepted565 /// - `rmrk_nft_id`: nft id of the nft to be accepted566 /// - `new_owner`: either origin's account ID or origin-owned NFT, whichever the NFT was567 /// sent to568 #[transactional]569 #[pallet::weight(<SelfWeightOf<T>>::accept_nft())]570 pub fn accept_nft(571 origin: OriginFor<T>,572 rmrk_collection_id: RmrkCollectionId,573 rmrk_nft_id: RmrkNftId,574 new_owner: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,575 ) -> DispatchResult {576 let sender = ensure_signed(origin.clone())?;577 let cross_sender = T::CrossAccountId::from_sub(sender.clone());578579 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;580 let nft_id = rmrk_nft_id.into();581582 let collection =583 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;584 collection.check_is_external()?;585586 let new_cross_owner = match new_owner {587 RmrkAccountIdOrCollectionNftTuple::AccountId(ref account_id) => {588 T::CrossAccountId::from_sub(account_id.clone())589 }590 RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(591 target_collection_id,592 target_nft_id,593 ) => {594 let target_collection_id = Self::unique_collection_id(target_collection_id)?;595596 T::CrossTokenAddressMapping::token_to_address(597 target_collection_id,598 TokenId(target_nft_id),599 )600 }601 };602603 let budget = budget::Value::new(NESTING_BUDGET);604605 <PalletNft<T>>::transfer(606 &collection,607 &cross_sender,608 &new_cross_owner,609 nft_id,610 &budget,611 )612 .map_err(|err| {613 if err == <CommonError<T>>::UserIsNotAllowedToNest.into() {614 <Error<T>>::CannotAcceptNonOwnedNft.into()615 } else {616 Self::map_unique_err_to_proxy(err)617 }618 })?;619620 <PalletNft<T>>::set_scoped_token_property(621 collection.id,622 nft_id,623 PropertyScope::Rmrk,624 Self::rmrk_property(PendingNftAccept, &false)?,625 )?;626627 Self::deposit_event(Event::NFTAccepted {628 sender,629 recipient: new_owner,630 collection_id: rmrk_collection_id,631 nft_id: rmrk_nft_id,632 });633634 Ok(())635 }636637 /// Rejects an NFT sent from another account to self or owned NFT638 ///639 /// Parameters:640 /// - `origin`: sender of the transaction641 /// - `rmrk_collection_id`: collection id of the nft to be accepted642 /// - `rmrk_nft_id`: nft id of the nft to be accepted643 #[transactional]644 #[pallet::weight(<SelfWeightOf<T>>::reject_nft())]645 pub fn reject_nft(646 origin: OriginFor<T>,647 rmrk_collection_id: RmrkCollectionId,648 rmrk_nft_id: RmrkNftId,649 ) -> DispatchResult {650 let sender = ensure_signed(origin)?;651 let cross_sender = T::CrossAccountId::from_sub(sender.clone());652653 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;654 let nft_id = rmrk_nft_id.into();655656 let collection =657 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;658 collection.check_is_external()?;659660 ensure!(661 Self::get_nft_property_decoded(662 collection_id,663 nft_id,664 RmrkProperty::PendingNftAccept665 )?,666 <Error<T>>::NoPermission667 );668669 Self::destroy_nft(670 cross_sender,671 collection_id,672 nft_id,673 NESTING_BUDGET,674 <Error<T>>::CannotRejectNonOwnedNft,675 )676 .map_err(|err| Self::map_unique_err_to_proxy(err.error))?;677678 Self::deposit_event(Event::NFTRejected {679 sender,680 collection_id: rmrk_collection_id,681 nft_id: rmrk_nft_id,682 });683684 Ok(())685 }686687 /// accept the addition of a new resource to an existing NFT688 #[transactional]689 #[pallet::weight(<SelfWeightOf<T>>::accept_resource())]690 pub fn accept_resource(691 origin: OriginFor<T>,692 rmrk_collection_id: RmrkCollectionId,693 rmrk_nft_id: RmrkNftId,694 rmrk_resource_id: RmrkResourceId,695 ) -> DispatchResult {696 let sender = ensure_signed(origin)?;697 let cross_sender = T::CrossAccountId::from_sub(sender);698699 let collection_id = Self::unique_collection_id(rmrk_collection_id)700 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;701 let collection =702 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;703 collection.check_is_external()?;704705 let nft_id = rmrk_nft_id.into();706 let resource_id = rmrk_resource_id.into();707708 let budget = budget::Value::new(NESTING_BUDGET);709710 let nft_owner =711 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)712 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;713714 let resource_collection_id: Option<CollectionId> =715 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)716 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;717718 let resource_collection_id =719 resource_collection_id.ok_or(<Error<T>>::ResourceDoesntExist)?;720721 let is_pending: bool = Self::get_nft_property_decoded(722 resource_collection_id,723 resource_id,724 PendingResourceAccept,725 )726 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;727728 ensure!(is_pending, <Error<T>>::ResourceNotPending);729730 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);731732 <PalletNft<T>>::set_scoped_token_property(733 resource_collection_id,734 rmrk_resource_id.into(),735 PropertyScope::Rmrk,736 Self::rmrk_property(PendingResourceAccept, &false)?,737 )?;738739 Self::deposit_event(Event::<T>::ResourceAccepted {740 nft_id: rmrk_nft_id,741 resource_id: rmrk_resource_id,742 });743744 Ok(())745 }746747 /// accept the removal of a resource of an existing NFT748 #[transactional]749 #[pallet::weight(<SelfWeightOf<T>>::accept_resource_removal())]750 pub fn accept_resource_removal(751 origin: OriginFor<T>,752 rmrk_collection_id: RmrkCollectionId,753 rmrk_nft_id: RmrkNftId,754 rmrk_resource_id: RmrkResourceId,755 ) -> DispatchResult {756 let sender = ensure_signed(origin)?;757 let cross_sender = T::CrossAccountId::from_sub(sender);758759 let collection_id = Self::unique_collection_id(rmrk_collection_id)760 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;761 let collection =762 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;763 collection.check_is_external()?;764765 let nft_id = rmrk_nft_id.into();766 let resource_id = rmrk_resource_id.into();767768 let budget = budget::Value::new(NESTING_BUDGET);769770 let nft_owner =771 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)772 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;773774 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);775776 let resource_collection_id: Option<CollectionId> =777 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)778 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;779780 let resource_collection_id =781 resource_collection_id.ok_or(<Error<T>>::ResourceDoesntExist)?;782783 let is_pending: bool = Self::get_nft_property_decoded(784 resource_collection_id,785 resource_id,786 PendingResourceRemoval,787 )788 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;789790 ensure!(is_pending, <Error<T>>::ResourceNotPending);791792 let resource_collection = Self::get_typed_nft_collection(793 resource_collection_id,794 misc::CollectionType::Resource,795 )?;796797 let resource_data = <TokenData<T>>::get((resource_collection_id, resource_id))798 .ok_or(<Error<T>>::ResourceDoesntExist)?;799800 let resource_owner = resource_data.owner;801802 <PalletNft<T>>::burn(803 &resource_collection,804 &resource_owner,805 rmrk_resource_id.into(),806 )807 .map_err(Self::map_unique_err_to_proxy)?;808809 Self::deposit_event(Event::<T>::ResourceRemovalAccepted {810 nft_id: rmrk_nft_id,811 resource_id: rmrk_resource_id,812 });813814 Ok(())815 }816817 /// set a custom value on an NFT818 #[transactional]819 #[pallet::weight(<SelfWeightOf<T>>::set_property())]820 pub fn set_property(821 origin: OriginFor<T>,822 #[pallet::compact] rmrk_collection_id: RmrkCollectionId,823 maybe_nft_id: Option<RmrkNftId>,824 key: RmrkKeyString,825 value: RmrkValueString,826 ) -> DispatchResult {827 let sender = ensure_signed(origin)?;828 let sender = T::CrossAccountId::from_sub(sender);829830 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;831 let collection =832 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;833 collection.check_is_external()?;834835 let budget = budget::Value::new(NESTING_BUDGET);836837 match maybe_nft_id {838 Some(nft_id) => {839 let token_id: TokenId = nft_id.into();840841 Self::ensure_nft_type(collection_id, token_id, NftType::Regular)?;842 Self::ensure_nft_owner(collection_id, token_id, &sender, &budget)?;843844 <PalletNft<T>>::set_scoped_token_property(845 collection_id,846 token_id,847 PropertyScope::Rmrk,848 Self::rmrk_property(UserProperty(key.as_slice()), &value)?,849 )?;850 }851 None => {852 let collection = Self::get_typed_nft_collection(853 collection_id,854 misc::CollectionType::Regular,855 )?;856857 Self::check_collection_owner(&collection, &sender)?;858859 <PalletCommon<T>>::set_scoped_collection_property(860 collection_id,861 PropertyScope::Rmrk,862 Self::rmrk_property(UserProperty(key.as_slice()), &value)?,863 )?;864 }865 }866867 Self::deposit_event(Event::PropertySet {868 collection_id: rmrk_collection_id,869 maybe_nft_id,870 key,871 value,872 });873874 Ok(())875 }876877 /// set a different order of resource priority878 #[transactional]879 #[pallet::weight(<SelfWeightOf<T>>::set_priority())]880 pub fn set_priority(881 origin: OriginFor<T>,882 rmrk_collection_id: RmrkCollectionId,883 rmrk_nft_id: RmrkNftId,884 priorities: BoundedVec<RmrkResourceId, RmrkMaxPriorities>,885 ) -> DispatchResult {886 let sender = ensure_signed(origin)?;887 let sender = T::CrossAccountId::from_sub(sender);888889 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;890 let nft_id = rmrk_nft_id.into();891892 let collection =893 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;894 collection.check_is_external()?;895896 let budget = budget::Value::new(NESTING_BUDGET);897898 Self::ensure_nft_type(collection_id, nft_id, NftType::Regular)?;899 Self::ensure_nft_owner(collection_id, nft_id, &sender, &budget)?;900901 <PalletNft<T>>::set_scoped_token_property(902 collection_id,903 nft_id,904 PropertyScope::Rmrk,905 Self::rmrk_property(ResourcePriorities, &priorities.into_inner())?,906 )?;907908 Self::deposit_event(Event::<T>::PrioritySet {909 collection_id: rmrk_collection_id,910 nft_id: rmrk_nft_id,911 });912913 Ok(())914 }915916 /// Create basic resource917 #[transactional]918 #[pallet::weight(<SelfWeightOf<T>>::add_basic_resource())]919 pub fn add_basic_resource(920 origin: OriginFor<T>,921 rmrk_collection_id: RmrkCollectionId,922 nft_id: RmrkNftId,923 resource: RmrkBasicResource,924 ) -> DispatchResult {925 let sender = ensure_signed(origin.clone())?;926927 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;928 let collection =929 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;930 collection.check_is_external()?;931932 let resource_id = Self::resource_add(933 sender,934 collection_id,935 nft_id.into(),936 RmrkResourceTypes::Basic(resource),937 )?;938939 Self::deposit_event(Event::ResourceAdded {940 nft_id,941 resource_id,942 });943 Ok(())944 }945946 /// Create composable resource947 #[transactional]948 #[pallet::weight(<SelfWeightOf<T>>::add_composable_resource())]949 pub fn add_composable_resource(950 origin: OriginFor<T>,951 rmrk_collection_id: RmrkCollectionId,952 nft_id: RmrkNftId,953 resource: RmrkComposableResource,954 ) -> DispatchResult {955 let sender = ensure_signed(origin.clone())?;956957 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;958 let collection =959 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;960 collection.check_is_external()?;961962 let resource_id = Self::resource_add(963 sender,964 collection_id,965 nft_id.into(),966 RmrkResourceTypes::Composable(resource)967 )?;968969 Self::deposit_event(Event::ResourceAdded {970 nft_id,971 resource_id,972 });973 Ok(())974 }975976 /// Create slot resource977 #[transactional]978 #[pallet::weight(<SelfWeightOf<T>>::add_slot_resource())]979 pub fn add_slot_resource(980 origin: OriginFor<T>,981 rmrk_collection_id: RmrkCollectionId,982 nft_id: RmrkNftId,983 resource: RmrkSlotResource,984 ) -> DispatchResult {985 let sender = ensure_signed(origin.clone())?;986987 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;988 let collection =989 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;990 collection.check_is_external()?;991992 let resource_id = Self::resource_add(993 sender,994 collection_id,995 nft_id.into(),996 RmrkResourceTypes::Slot(resource),997 )?;998999 Self::deposit_event(Event::ResourceAdded {1000 nft_id,1001 resource_id,1002 });1003 Ok(())1004 }10051006 /// remove resource1007 #[transactional]1008 #[pallet::weight(<SelfWeightOf<T>>::remove_resource())]1009 pub fn remove_resource(1010 origin: OriginFor<T>,1011 rmrk_collection_id: RmrkCollectionId,1012 nft_id: RmrkNftId,1013 resource_id: RmrkResourceId,1014 ) -> DispatchResult {1015 let sender = ensure_signed(origin.clone())?;10161017 let collection_id = Self::unique_collection_id(rmrk_collection_id)?;1018 let collection =1019 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1020 collection.check_is_external()?;10211022 Self::resource_remove(sender, collection_id, nft_id.into(), resource_id.into())?;10231024 Self::deposit_event(Event::ResourceRemoval {1025 nft_id,1026 resource_id,1027 });1028 Ok(())1029 }1030 }1031}10321033impl<T: Config> Pallet<T> {1034 pub fn rmrk_property_key(rmrk_key: RmrkProperty) -> Result<PropertyKey, DispatchError> {1035 let key = rmrk_key.to_key::<T>()?;10361037 let scoped_key = PropertyScope::Rmrk1038 .apply(key)1039 .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)?;10401041 Ok(scoped_key)1042 }10431044 // todo think about renaming these1045 pub fn rmrk_property<E: Encode>(1046 rmrk_key: RmrkProperty,1047 value: &E,1048 ) -> Result<Property, DispatchError> {1049 let key = rmrk_key.to_key::<T>()?;10501051 let value = value1052 .encode()1053 .try_into()1054 .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong)?;10551056 let property = Property { key, value };10571058 Ok(property)1059 }10601061 pub fn decode_property<D: Decode>(vec: PropertyValue) -> Result<D, DispatchError> {1062 vec.decode()1063 .map_err(|_| <Error<T>>::UnableToDecodeRmrkData.into())1064 }10651066 pub fn rebind<L, S>(vec: &BoundedVec<u8, L>) -> Result<BoundedVec<u8, S>, DispatchError>1067 where1068 BoundedVec<u8, S>: TryFrom<Vec<u8>>,1069 {1070 vec.rebind()1071 .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong.into())1072 }10731074 fn init_collection(1075 sender: T::CrossAccountId,1076 data: CreateCollectionData<T::AccountId>,1077 properties: impl Iterator<Item = Property>,1078 ) -> Result<CollectionId, DispatchError> {1079 let collection_id = <PalletNft<T>>::init_collection(sender, data, true);10801081 if let Err(DispatchError::Arithmetic(_)) = &collection_id {1082 return Err(<Error<T>>::NoAvailableCollectionId.into());1083 }10841085 <PalletCommon<T>>::set_scoped_collection_properties(1086 collection_id?,1087 PropertyScope::Rmrk,1088 properties,1089 )?;10901091 collection_id1092 }10931094 pub fn create_nft(1095 sender: &T::CrossAccountId,1096 owner: &T::CrossAccountId,1097 collection: &NonfungibleHandle<T>,1098 properties: impl Iterator<Item = Property>,1099 ) -> Result<TokenId, DispatchError> {1100 let data = CreateNftExData {1101 properties: BoundedVec::default(),1102 owner: owner.clone(),1103 };11041105 let budget = budget::Value::new(NESTING_BUDGET);11061107 <PalletNft<T>>::create_item(collection, sender, data, &budget)?;11081109 let nft_id = <PalletNft<T>>::current_token_id(collection.id);11101111 <PalletNft<T>>::set_scoped_token_properties(1112 collection.id,1113 nft_id,1114 PropertyScope::Rmrk,1115 properties,1116 )?;11171118 Ok(nft_id)1119 }11201121 fn destroy_nft(1122 sender: T::CrossAccountId,1123 collection_id: CollectionId,1124 token_id: TokenId,1125 max_burns: u32,1126 error_if_not_owned: Error<T>,1127 ) -> DispatchResultWithPostInfo {1128 let collection =1129 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;11301131 let token_data =1132 <TokenData<T>>::get((collection_id, token_id)).ok_or(<Error<T>>::NoAvailableNftId)?;11331134 let from = token_data.owner;11351136 let owner_check_budget = budget::Value::new(NESTING_BUDGET);11371138 ensure!(1139 <PalletStructure<T>>::check_indirectly_owned(1140 sender.clone(),1141 collection_id,1142 token_id,1143 None,1144 &owner_check_budget1145 )?,1146 error_if_not_owned,1147 );11481149 let burns_budget = budget::Value::new(max_burns);1150 let breadth_budget = budget::Value::new(max_burns);11511152 <PalletNft<T>>::burn_recursively(1153 &collection,1154 &from,1155 token_id,1156 &burns_budget,1157 &breadth_budget,1158 )1159 }11601161 fn resource_add(1162 sender: T::AccountId,1163 collection_id: CollectionId,1164 nft_id: TokenId,1165 resource: RmrkResourceTypes1166 ) -> Result<RmrkResourceId, DispatchError> {1167 match resource {1168 RmrkResourceTypes::Basic(resource) => {1169 Self::resource_add_helper(1170 sender,1171 collection_id,1172 nft_id,1173 [1174 Self::rmrk_property(TokenType, &NftType::Resource)?,1175 Self::rmrk_property(ResourceType, &misc::ResourceType::Basic)?,1176 Self::rmrk_property(Src, &resource.src)?,1177 Self::rmrk_property(Metadata, &resource.metadata)?,1178 Self::rmrk_property(License, &resource.license)?,1179 Self::rmrk_property(Thumb, &resource.thumb)?,1180 ]1181 .into_iter(),1182 )1183 },1184 RmrkResourceTypes::Composable(resource) => {1185 Self::resource_add_helper(1186 sender,1187 collection_id,1188 nft_id.into(),1189 [1190 Self::rmrk_property(TokenType, &NftType::Resource)?,1191 Self::rmrk_property(ResourceType, &misc::ResourceType::Composable)?,1192 Self::rmrk_property(Parts, &resource.parts)?,1193 Self::rmrk_property(Base, &resource.base)?,1194 Self::rmrk_property(Src, &resource.src)?,1195 Self::rmrk_property(Metadata, &resource.metadata)?,1196 Self::rmrk_property(License, &resource.license)?,1197 Self::rmrk_property(Thumb, &resource.thumb)?,1198 ]1199 .into_iter(),1200 )1201 },1202 RmrkResourceTypes::Slot(resource) => {1203 Self::resource_add_helper(1204 sender,1205 collection_id,1206 nft_id.into(),1207 [1208 Self::rmrk_property(TokenType, &NftType::Resource)?,1209 Self::rmrk_property(ResourceType, &misc::ResourceType::Slot)?,1210 Self::rmrk_property(Base, &resource.base)?,1211 Self::rmrk_property(Src, &resource.src)?,1212 Self::rmrk_property(Metadata, &resource.metadata)?,1213 Self::rmrk_property(Slot, &resource.slot)?,1214 Self::rmrk_property(License, &resource.license)?,1215 Self::rmrk_property(Thumb, &resource.thumb)?,1216 ]1217 .into_iter(),1218 )1219 }1220 }1221 }12221223 fn resource_add_helper(1224 sender: T::AccountId,1225 collection_id: CollectionId,1226 token_id: TokenId,1227 resource_properties: impl Iterator<Item = Property>,1228 ) -> Result<RmrkResourceId, DispatchError> {1229 let collection =1230 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1231 ensure!(collection.owner == sender, Error::<T>::NoPermission);12321233 let sender = T::CrossAccountId::from_sub(sender);1234 let budget = budget::Value::new(NESTING_BUDGET);12351236 let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, token_id, &budget)1237 .map_err(Self::map_unique_err_to_proxy)?;12381239 let pending = sender != nft_owner;12401241 let resource_collection_id: Option<CollectionId> =1242 Self::get_nft_property_decoded(collection_id, token_id, ResourceCollection)?;12431244 let resource_collection_id = match resource_collection_id {1245 Some(id) => id,1246 None => {1247 let resource_collection_id = Self::init_collection(1248 sender.clone(),1249 CreateCollectionData {1250 ..Default::default()1251 },1252 [Self::rmrk_property(1253 CollectionType,1254 &misc::CollectionType::Resource,1255 )?]1256 .into_iter(),1257 )?;12581259 <PalletNft<T>>::set_scoped_token_property(1260 collection_id,1261 token_id,1262 PropertyScope::Rmrk,1263 Self::rmrk_property(ResourceCollection, &Some(resource_collection_id))?,1264 )?;12651266 resource_collection_id1267 }1268 };12691270 let resource_collection =1271 Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?;12721273 // todo probably add extra connections to bases, slots, etc., when RMRK starts to use them12741275 let resource_id = Self::create_nft(1276 &sender,1277 &nft_owner,1278 &resource_collection,1279 resource_properties.chain(1280 [1281 Self::rmrk_property(PendingResourceAccept, &pending)?,1282 Self::rmrk_property(PendingResourceRemoval, &false)?,1283 ]1284 .into_iter(),1285 ),1286 )1287 .map_err(|err| match err {1288 DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),1289 err => Self::map_unique_err_to_proxy(err),1290 })?;12911292 Ok(resource_id.0)1293 }12941295 fn resource_remove(1296 sender: T::AccountId,1297 collection_id: CollectionId,1298 nft_id: TokenId,1299 resource_id: TokenId,1300 ) -> DispatchResult {1301 let collection =1302 Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?;1303 ensure!(collection.owner == sender, Error::<T>::NoPermission);13041305 let resource_collection_id: Option<CollectionId> =1306 Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)?;13071308 let resource_collection_id =1309 resource_collection_id.ok_or(Error::<T>::ResourceDoesntExist)?;13101311 let resource_collection =1312 Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?;1313 ensure!(1314 <PalletNft<T>>::token_exists(&resource_collection, resource_id),1315 Error::<T>::ResourceDoesntExist1316 );13171318 let budget = up_data_structs::budget::Value::new(NESTING_BUDGET);1319 let topmost_owner =1320 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?;13211322 let sender = T::CrossAccountId::from_sub(sender);1323 if topmost_owner == sender {1324 <PalletNft<T>>::burn(&resource_collection, &sender, resource_id)1325 .map_err(Self::map_unique_err_to_proxy)?;1326 } else {1327 <PalletNft<T>>::set_scoped_token_property(1328 resource_collection_id,1329 resource_id,1330 PropertyScope::Rmrk,1331 Self::rmrk_property(PendingResourceRemoval, &true)?,1332 )?;1333 }13341335 Ok(())1336 }13371338 fn change_collection_owner(1339 collection_id: CollectionId,1340 collection_type: misc::CollectionType,1341 sender: T::AccountId,1342 new_owner: T::AccountId,1343 ) -> DispatchResult {1344 let collection = Self::get_typed_nft_collection(collection_id, collection_type)?;1345 Self::check_collection_owner(&collection, &T::CrossAccountId::from_sub(sender))?;13461347 let mut collection = collection.into_inner();13481349 collection.owner = new_owner;1350 collection.save()1351 }13521353 fn check_collection_owner(1354 collection: &NonfungibleHandle<T>,1355 account: &T::CrossAccountId,1356 ) -> DispatchResult {1357 collection1358 .check_is_owner(account)1359 .map_err(Self::map_unique_err_to_proxy)1360 }13611362 pub fn last_collection_idx() -> RmrkCollectionId {1363 <CollectionIndex<T>>::get()1364 }13651366 pub fn unique_collection_id(1367 rmrk_collection_id: RmrkCollectionId,1368 ) -> Result<CollectionId, DispatchError> {1369 <UniqueCollectionId<T>>::try_get(rmrk_collection_id)1370 .map_err(|_| <Error<T>>::CollectionUnknown.into())1371 }13721373 pub fn rmrk_collection_id(1374 unique_collection_id: CollectionId,1375 ) -> Result<RmrkCollectionId, DispatchError> {1376 Self::get_collection_property_decoded(unique_collection_id, RmrkInternalCollectionId)1377 }13781379 pub fn get_nft_collection(1380 collection_id: CollectionId,1381 ) -> Result<NonfungibleHandle<T>, DispatchError> {1382 let collection = <CollectionHandle<T>>::try_get(collection_id)1383 .map_err(|_| <Error<T>>::CollectionUnknown)?;13841385 match collection.mode {1386 CollectionMode::NFT => Ok(NonfungibleHandle::cast(collection)),1387 _ => Err(<Error<T>>::CollectionUnknown.into()),1388 }1389 }13901391 pub fn collection_exists(collection_id: CollectionId) -> bool {1392 <CollectionHandle<T>>::try_get(collection_id).is_ok()1393 }13941395 pub fn get_collection_property(1396 collection_id: CollectionId,1397 key: RmrkProperty,1398 ) -> Result<PropertyValue, DispatchError> {1399 let collection_property = <PalletCommon<T>>::collection_properties(collection_id)1400 .get(&Self::rmrk_property_key(key)?)1401 .ok_or(<Error<T>>::CollectionUnknown)?1402 .clone();14031404 Ok(collection_property)1405 }14061407 pub fn get_collection_property_decoded<V: Decode>(1408 collection_id: CollectionId,1409 key: RmrkProperty,1410 ) -> Result<V, DispatchError> {1411 Self::decode_property(Self::get_collection_property(collection_id, key)?)1412 }14131414 pub fn get_collection_type(1415 collection_id: CollectionId,1416 ) -> Result<misc::CollectionType, DispatchError> {1417 Self::get_collection_property_decoded(collection_id, CollectionType)1418 .map_err(|_| <Error<T>>::CorruptedCollectionType.into())1419 }14201421 pub fn ensure_collection_type(1422 collection_id: CollectionId,1423 collection_type: misc::CollectionType,1424 ) -> DispatchResult {1425 let actual_type = Self::get_collection_type(collection_id)?;1426 ensure!(1427 actual_type == collection_type,1428 <CommonError<T>>::NoPermission1429 );14301431 Ok(())1432 }14331434 pub fn get_typed_nft_collection(1435 collection_id: CollectionId,1436 collection_type: misc::CollectionType,1437 ) -> Result<NonfungibleHandle<T>, DispatchError> {1438 Self::ensure_collection_type(collection_id, collection_type)?;14391440 Self::get_nft_collection(collection_id)1441 }14421443 pub fn get_typed_nft_collection_mapped(1444 rmrk_collection_id: RmrkCollectionId,1445 collection_type: misc::CollectionType,1446 ) -> Result<(NonfungibleHandle<T>, CollectionId), DispatchError> {1447 let unique_collection_id = match collection_type {1448 misc::CollectionType::Regular => Self::unique_collection_id(rmrk_collection_id)?,1449 _ => rmrk_collection_id.into(),1450 };14511452 let collection = Self::get_typed_nft_collection(unique_collection_id, collection_type)?;14531454 Ok((collection, unique_collection_id))1455 }14561457 pub fn get_nft_property(1458 collection_id: CollectionId,1459 nft_id: TokenId,1460 key: RmrkProperty,1461 ) -> Result<PropertyValue, DispatchError> {1462 let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))1463 .get(&Self::rmrk_property_key(key)?)1464 .ok_or(<Error<T>>::NoAvailableNftId)? // todo replace with better error?1465 .clone();14661467 Ok(nft_property)1468 }14691470 pub fn get_nft_property_decoded<V: Decode>(1471 collection_id: CollectionId,1472 nft_id: TokenId,1473 key: RmrkProperty,1474 ) -> Result<V, DispatchError> {1475 Self::decode_property(Self::get_nft_property(collection_id, nft_id, key)?)1476 }14771478 pub fn nft_exists(collection_id: CollectionId, nft_id: TokenId) -> bool {1479 <TokenData<T>>::contains_key((collection_id, nft_id))1480 }14811482 pub fn get_nft_type(1483 collection_id: CollectionId,1484 token_id: TokenId,1485 ) -> Result<NftType, DispatchError> {1486 Self::get_nft_property_decoded(collection_id, token_id, TokenType)1487 .map_err(|_| <Error<T>>::NoAvailableNftId.into())1488 }14891490 pub fn ensure_nft_type(1491 collection_id: CollectionId,1492 token_id: TokenId,1493 nft_type: NftType,1494 ) -> DispatchResult {1495 let actual_type = Self::get_nft_type(collection_id, token_id)?;1496 ensure!(actual_type == nft_type, <Error<T>>::NoPermission);14971498 Ok(())1499 }15001501 pub fn ensure_nft_owner(1502 collection_id: CollectionId,1503 token_id: TokenId,1504 possible_owner: &T::CrossAccountId,1505 nesting_budget: &dyn budget::Budget,1506 ) -> DispatchResult {1507 let is_owned = <PalletStructure<T>>::check_indirectly_owned(1508 possible_owner.clone(),1509 collection_id,1510 token_id,1511 None,1512 nesting_budget,1513 )1514 .map_err(Self::map_unique_err_to_proxy)?;15151516 ensure!(is_owned, <Error<T>>::NoPermission);15171518 Ok(())1519 }15201521 pub fn filter_user_properties<Key, Value, R, Mapper>(1522 collection_id: CollectionId,1523 token_id: Option<TokenId>,1524 filter_keys: Option<Vec<RmrkPropertyKey>>,1525 mapper: Mapper,1526 ) -> Result<Vec<R>, DispatchError>1527 where1528 Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,1529 Value: Decode + Default,1530 Mapper: Fn(Key, Value) -> R,1531 {1532 filter_keys1533 .map(|keys| {1534 let properties = keys1535 .into_iter()1536 .filter_map(|key| {1537 let key: Key = key.try_into().ok()?;15381539 let value = match token_id {1540 Some(token_id) => Self::get_nft_property_decoded(1541 collection_id,1542 token_id,1543 UserProperty(key.as_ref()),1544 ),1545 None => Self::get_collection_property_decoded(1546 collection_id,1547 UserProperty(key.as_ref()),1548 ),1549 }1550 .ok()?;15511552 Some(mapper(key, value))1553 })1554 .collect();15551556 Ok(properties)1557 })1558 .unwrap_or_else(|| {1559 let properties =1560 Self::iterate_user_properties(collection_id, token_id, mapper)?.collect();15611562 Ok(properties)1563 })1564 }15651566 pub fn iterate_user_properties<Key, Value, R, Mapper>(1567 collection_id: CollectionId,1568 token_id: Option<TokenId>,1569 mapper: Mapper,1570 ) -> Result<impl Iterator<Item = R>, DispatchError>1571 where1572 Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,1573 Value: Decode + Default,1574 Mapper: Fn(Key, Value) -> R,1575 {1576 let key_prefix = Self::rmrk_property_key(UserProperty(b""))?;15771578 let properties = match token_id {1579 Some(token_id) => <PalletNft<T>>::token_properties((collection_id, token_id)),1580 None => <PalletCommon<T>>::collection_properties(collection_id),1581 };15821583 let properties = properties.into_iter().filter_map(move |(key, value)| {1584 let key = key.as_slice().strip_prefix(key_prefix.as_slice())?;15851586 let key: Key = key.to_vec().try_into().ok()?;1587 let value: Value = value.decode().ok()?;15881589 Some(mapper(key, value))1590 });15911592 Ok(properties)1593 }15941595 fn map_unique_err_to_proxy(err: DispatchError) -> DispatchError {1596 map_unique_err_to_proxy! {1597 match err {1598 CommonError::NoPermission => NoPermission,1599 CommonError::CollectionTokenLimitExceeded => CollectionFullOrLocked,1600 CommonError::PublicMintingNotAllowed => NoPermission,1601 CommonError::TokenNotFound => NoAvailableNftId,1602 CommonError::ApprovedValueTooLow => NoPermission,1603 CommonError::CantDestroyNotEmptyCollection => CollectionNotEmpty,1604 StructureError::TokenNotFound => NoAvailableNftId,1605 StructureError::OuroborosDetected => CannotSendToDescendentOrSelf,1606 }1607 }1608 }1609}primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -952,6 +952,8 @@
pub const RmrkPartsLimit: u32 = 25;
#[derive(PartialEq)]
pub const RmrkMaxPriorities: u32 = 25;
+ #[derive(PartialEq)]
+ pub const MaxResourcesOnMint: u32 = 100;
}
impl From<RmrkCollectionId> for CollectionId {