difftreelog
style reformat code
in: master
19 files changed
client/rpc/src/lib.rsdiffbeforeafterboth--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -30,8 +30,7 @@
// RMRK
use rmrk_rpc::RmrkApi as RmrkRuntimeApi;
use up_data_structs::{
- RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName,
- RmrkResourceId,
+ RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName, RmrkResourceId,
};
pub use rmrk_unique_rpc::RmrkApi;
node/cli/src/service.rsdiffbeforeafterboth--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -364,7 +364,8 @@
RmrkBaseInfo<AccountId>,
RmrkPartType,
RmrkTheme,
- >*/ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ >*/
+ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ sp_api::Metadata<Block>
+ sp_offchain::OffchainWorkerApi<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>,
@@ -656,7 +657,8 @@
RmrkBaseInfo<AccountId>,
RmrkPartType,
RmrkTheme,
- >*/ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ >*/
+ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ sp_api::Metadata<Block>
+ sp_offchain::OffchainWorkerApi<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>
@@ -800,7 +802,8 @@
RmrkBaseInfo<AccountId>,
RmrkPartType,
RmrkTheme,
- >*/ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ >*/
+ + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+ sp_api::Metadata<Block>
+ sp_offchain::OffchainWorkerApi<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -501,7 +501,6 @@
PhantomType<(
TokenData<T::CrossAccountId>,
RpcCollection<T::AccountId>,
-
// RMRK
RmrkCollectionInfo<T::AccountId>,
RmrkInstanceInfo<T::AccountId>,
@@ -526,19 +525,34 @@
if !v.offchain_schema.is_empty() {
props.push(Property {
key: b"_old_offchainSchema".to_vec().try_into().unwrap(),
- value: v.offchain_schema.clone().into_inner().try_into().expect("offchain schema too big"),
+ value: v
+ .offchain_schema
+ .clone()
+ .into_inner()
+ .try_into()
+ .expect("offchain schema too big"),
});
}
if !v.variable_on_chain_schema.is_empty() {
props.push(Property {
key: b"_old_variableOnChainSchema".to_vec().try_into().unwrap(),
- value: v.variable_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),
+ value: v
+ .variable_on_chain_schema
+ .clone()
+ .into_inner()
+ .try_into()
+ .expect("offchain schema too big"),
});
}
if !v.const_on_chain_schema.is_empty() {
props.push(Property {
key: b"_old_constOnChainSchema".to_vec().try_into().unwrap(),
- value: v.const_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),
+ value: v
+ .const_on_chain_schema
+ .clone()
+ .into_inner()
+ .try_into()
+ .expect("offchain schema too big"),
});
}
props.push(Property {
@@ -546,13 +560,17 @@
value: match v.schema_version {
SchemaVersion::ImageURL => b"ImageUrl".as_slice(),
SchemaVersion::Unique => b"Unique".as_slice(),
- }.to_vec().try_into().unwrap(),
+ }
+ .to_vec()
+ .try_into()
+ .unwrap(),
});
Self::set_scoped_collection_properties(
id,
PropertyScope::None,
props.into_iter(),
- ).expect("existing data larger than properties");
+ )
+ .expect("existing data larger than properties");
let mut new = CollectionVersion2::from(v.clone());
new.permissions.access = Some(v.access);
new.permissions.mint_mode = Some(v.mint_mode);
@@ -644,18 +662,12 @@
let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)
.into_iter()
- .map(|(key, permission)| PropertyKeyPermission {
- key,
- permission,
- })
+ .map(|(key, permission)| PropertyKeyPermission { key, permission })
.collect();
let properties = <CollectionProperties<T>>::get(collection)
.into_iter()
- .map(|(key, value)| Property {
- key,
- value,
- })
+ .map(|(key, value)| Property { key, value })
.collect();
let permissions = CollectionPermissions {
@@ -751,7 +763,9 @@
.unwrap_or_else(|| Ok(CollectionLimits::default()))?,
permissions: data
.permissions
- .map(|permissions| Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions))
+ .map(|permissions| {
+ Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions)
+ })
.unwrap_or_else(|| Ok(CollectionPermissions::default()))?,
};
@@ -1005,10 +1019,7 @@
.unwrap_or_else(|| {
properties
.into_iter()
- .map(|(key, value)| Property {
- key,
- value,
- })
+ .map(|(key, value)| Property { key, value })
.collect()
});
@@ -1037,10 +1048,7 @@
.unwrap_or_else(|| {
permissions
.into_iter()
- .map(|(key, permission)| PropertyKeyPermission {
- key,
- permission,
- })
+ .map(|(key, permission)| PropertyKeyPermission { key, permission })
.collect()
});
@@ -1276,17 +1284,9 @@
budget: &dyn Budget,
) -> DispatchResult;
- fn nest(
- &self,
- under: TokenId,
- to_nest: (CollectionId, TokenId)
- );
+ fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId));
- fn unnest(
- &self,
- under: TokenId,
- to_nest: (CollectionId, TokenId)
- );
+ fn unnest(&self, under: TokenId, to_nest: (CollectionId, TokenId));
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
fn collection_tokens(&self) -> Vec<TokenId>;
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -298,17 +298,9 @@
fail!(<Error<T>>::FungibleDisallowsNesting)
}
- fn nest(
- &self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}
- fn unnest(
- &self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}
fn collection_tokens(&self) -> Vec<TokenId> {
vec![TokenId::default()]
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -184,11 +184,7 @@
if balance == 0 {
<Balance<T>>::remove((collection.id, owner));
- <PalletStructure<T>>::unnest_if_nested(
- owner,
- collection.id,
- TokenId::default()
- );
+ <PalletStructure<T>>::unnest_if_nested(owner, collection.id, TokenId::default());
} else {
<Balance<T>>::insert((collection.id, owner), balance);
}
@@ -249,18 +245,14 @@
to,
collection.id,
TokenId::default(),
- nesting_budget
+ nesting_budget,
)?;
if let Some(balance_to) = balance_to {
// from != to
if balance_from == 0 {
<Balance<T>>::remove((collection.id, from));
- <PalletStructure<T>>::unnest_if_nested(
- from,
- collection.id,
- TokenId::default()
- );
+ <PalletStructure<T>>::unnest_if_nested(from, collection.id, TokenId::default());
} else {
<Balance<T>>::insert((collection.id, from), balance_from);
}
@@ -333,7 +325,11 @@
<TotalSupply<T>>::insert(collection.id, total_supply);
for (user, amount) in balances {
<Balance<T>>::insert((collection.id, &user), amount);
- <PalletStructure<T>>::nest_if_sent_to_token_unchecked(&user, collection.id, TokenId::default());
+ <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
+ &user,
+ collection.id,
+ TokenId::default(),
+ );
<PalletEvm<T>>::deposit_log(
ERC20Events::Transfer {
from: H160::default(),
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -22,7 +22,7 @@
PropertyKeyPermission, PropertyValue,
};
use pallet_common::{
- CommonCollectionOperations, CommonWeightInfo, with_weight, weights::WeightInfo as _
+ CommonCollectionOperations, CommonWeightInfo, with_weight, weights::WeightInfo as _,
};
use sp_runtime::DispatchError;
use sp_std::vec::Vec;
@@ -353,19 +353,11 @@
<Pallet<T>>::check_nesting(self, sender, from, under, budget)
}
- fn nest(
- &self,
- under: TokenId,
- to_nest: (CollectionId, TokenId)
- ) {
+ fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId)) {
<Pallet<T>>::nest((self.id, under), to_nest);
}
- fn unnest(
- &self,
- under: TokenId,
- to_unnest: (CollectionId, TokenId)
- ) {
+ fn unnest(&self, under: TokenId, to_unnest: (CollectionId, TokenId)) {
<Pallet<T>>::unnest((self.id, under), to_unnest);
}
@@ -415,10 +407,7 @@
.unwrap_or_else(|| {
properties
.into_iter()
- .map(|(key, value)| Property {
- key,
- value,
- })
+ .map(|(key, value)| Property { key, value })
.collect()
})
}
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -426,11 +426,10 @@
Ok(a)
}
-fn has_token_permission<T: Config>(
- collection_id: CollectionId,
- key: &PropertyKey,
-) -> bool {
- if let Ok(token_property_permissions) = CollectionPropertyPermissions::<T>::try_get(collection_id) {
+fn has_token_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> bool {
+ if let Ok(token_property_permissions) =
+ CollectionPropertyPermissions::<T>::try_get(collection_id)
+ {
return token_property_permissions.contains_key(key);
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -164,31 +164,44 @@
fn on_runtime_upgrade() -> Weight {
if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {
let mut had_consts = BTreeSet::new();
- <TokenData<T>>::translate::<ItemDataVersion1<T::CrossAccountId>, _>(|(collection, token), v| {
- let mut props = vec![];
- if !v.const_data.is_empty() {
- props.push(Property {
- key: b"_old_constData".to_vec().try_into().unwrap(),
- value: v.const_data.clone().into_inner().try_into().expect("const too long"),
- });
- had_consts.insert(collection);
- }
- if !v.variable_data.is_empty() {
- props.push(Property {
- key: b"_old_variableData".to_vec().try_into().unwrap(),
- value: v.variable_data.clone().into_inner().try_into().expect("variable too long"),
- })
- }
- if !props.is_empty() {
- Self::set_scoped_token_properties(
- collection,
- token,
- PropertyScope::None,
- props.into_iter(),
- ).expect("existing token data exceeds property storage");
- }
- Some(<ItemDataVersion2<T::CrossAccountId>>::from(v))
- });
+ <TokenData<T>>::translate::<ItemDataVersion1<T::CrossAccountId>, _>(
+ |(collection, token), v| {
+ let mut props = vec![];
+ if !v.const_data.is_empty() {
+ props.push(Property {
+ key: b"_old_constData".to_vec().try_into().unwrap(),
+ value: v
+ .const_data
+ .clone()
+ .into_inner()
+ .try_into()
+ .expect("const too long"),
+ });
+ had_consts.insert(collection);
+ }
+ if !v.variable_data.is_empty() {
+ props.push(Property {
+ key: b"_old_variableData".to_vec().try_into().unwrap(),
+ value: v
+ .variable_data
+ .clone()
+ .into_inner()
+ .try_into()
+ .expect("variable too long"),
+ })
+ }
+ if !props.is_empty() {
+ Self::set_scoped_token_properties(
+ collection,
+ token,
+ PropertyScope::None,
+ props.into_iter(),
+ )
+ .expect("existing token data exceeds property storage");
+ }
+ Some(<ItemDataVersion2<T::CrossAccountId>>::from(v))
+ },
+ );
for collection in had_consts {
<PalletCommon<T>>::set_property_permission_unchecked(
collection,
@@ -199,8 +212,9 @@
collection_admin: true,
token_owner: false,
},
- }
- ).expect("failed to configure permission");
+ },
+ )
+ .expect("failed to configure permission");
}
}
@@ -263,7 +277,7 @@
collection_id: CollectionId,
token_id: TokenId,
scope: PropertyScope,
- properties: impl Iterator<Item=Property>,
+ properties: impl Iterator<Item = Property>,
) -> DispatchResult {
TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
stored_properties.try_scoped_set_from_iter(scope, properties)
@@ -347,11 +361,7 @@
<AccountBalance<T>>::insert((collection.id, token_data.owner.clone()), balance);
}
- <PalletStructure<T>>::unnest_if_nested(
- &token_data.owner,
- collection.id,
- token
- );
+ <PalletStructure<T>>::unnest_if_nested(&token_data.owner, collection.id, token);
<Owned<T>>::remove((collection.id, &token_data.owner, token));
<TokensBurnt<T>>::insert(collection.id, burnt);
@@ -589,16 +599,12 @@
to,
collection.id,
token,
- nesting_budget
+ nesting_budget,
)?;
// =========
- <PalletStructure<T>>::unnest_if_nested(
- from,
- collection.id,
- token
- );
+ <PalletStructure<T>>::unnest_if_nested(from, collection.id, token);
<TokenData<T>>::insert(
(collection.id, token),
@@ -709,7 +715,11 @@
},
);
- <PalletStructure<T>>::nest_if_sent_to_token_unchecked(&data.owner, collection.id, TokenId(token));
+ <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
+ &data.owner,
+ collection.id,
+ TokenId(token),
+ );
if let Err(e) = Self::set_token_properties(
collection,
@@ -958,31 +968,24 @@
Ok(())
}
- fn nest(
- under: (CollectionId, TokenId),
- to_nest: (CollectionId, TokenId),
- ) {
- <TokenChildren<T>>::insert(
- (under.0, under.1, (to_nest.0, to_nest.1)),
- true
- );
+ fn nest(under: (CollectionId, TokenId), to_nest: (CollectionId, TokenId)) {
+ <TokenChildren<T>>::insert((under.0, under.1, (to_nest.0, to_nest.1)), true);
}
- fn unnest(
- under: (CollectionId, TokenId),
- to_unnest: (CollectionId, TokenId),
- ) {
- <TokenChildren<T>>::remove(
- (under.0, under.1, to_unnest)
- );
+ fn unnest(under: (CollectionId, TokenId), to_unnest: (CollectionId, TokenId)) {
+ <TokenChildren<T>>::remove((under.0, under.1, to_unnest));
}
fn collection_has_tokens(collection_id: CollectionId) -> bool {
- <TokenData<T>>::iter_prefix((collection_id,)).next().is_some()
+ <TokenData<T>>::iter_prefix((collection_id,))
+ .next()
+ .is_some()
}
fn token_has_children(collection_id: CollectionId, token_id: TokenId) -> bool {
- <TokenChildren<T>>::iter_prefix((collection_id, token_id)).next().is_some()
+ <TokenChildren<T>>::iter_prefix((collection_id, token_id))
+ .next()
+ .is_some()
}
/// Delegated to `create_multiple_items`
pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -21,7 +21,9 @@
use sp_runtime::{DispatchError, Permill, traits::StaticLookup};
use sp_std::vec::Vec;
use up_data_structs::*;
-use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};
+use pallet_common::{
+ Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations,
+};
use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle, TokenData};
use pallet_evm::account::CrossAccountId;
use core::convert::AsRef;
@@ -38,18 +40,17 @@
#[frame_support::pallet]
pub mod pallet {
- use super::*;
- use pallet_evm::account;
+ use super::*;
+ use pallet_evm::account;
#[pallet::config]
- pub trait Config: frame_system::Config
- + pallet_common::Config
- + pallet_nonfungible::Config
- + account::Config {
+ pub trait Config:
+ frame_system::Config + pallet_common::Config + pallet_nonfungible::Config + account::Config
+ {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
- #[pallet::storage]
+ #[pallet::storage]
#[pallet::getter(fn collection_index)]
pub type CollectionIndex<T: Config> = StorageValue<_, RmrkCollectionId, ValueQuery>;
@@ -60,33 +61,33 @@
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
- CollectionCreated {
+ CollectionCreated {
issuer: T::AccountId,
collection_id: RmrkCollectionId,
},
- CollectionDestroyed {
+ CollectionDestroyed {
issuer: T::AccountId,
collection_id: RmrkCollectionId,
},
- IssuerChanged {
+ IssuerChanged {
old_issuer: T::AccountId,
new_issuer: T::AccountId,
collection_id: RmrkCollectionId,
},
- CollectionLocked {
+ CollectionLocked {
issuer: T::AccountId,
collection_id: RmrkCollectionId,
},
- NftMinted {
+ NftMinted {
owner: T::AccountId,
collection_id: RmrkCollectionId,
nft_id: RmrkNftId,
},
- NFTBurned {
+ NFTBurned {
owner: T::AccountId,
nft_id: RmrkNftId,
},
- PropertySet {
+ PropertySet {
collection_id: RmrkCollectionId,
maybe_nft_id: Option<RmrkNftId>,
key: RmrkKeyString,
@@ -96,24 +97,24 @@
#[pallet::error]
pub enum Error<T> {
- /* Unique-specific events */
- CorruptedCollectionType,
- NftTypeEncodeError,
- RmrkPropertyKeyIsTooLong,
- RmrkPropertyValueIsTooLong,
+ /* Unique-specific events */
+ CorruptedCollectionType,
+ NftTypeEncodeError,
+ RmrkPropertyKeyIsTooLong,
+ RmrkPropertyValueIsTooLong,
- /* RMRK compatible events */
- CollectionNotEmpty,
- NoAvailableCollectionId,
- NoAvailableNftId,
- CollectionUnknown,
- NoPermission,
- CollectionFullOrLocked,
+ /* RMRK compatible events */
+ CollectionNotEmpty,
+ NoAvailableCollectionId,
+ NoAvailableNftId,
+ CollectionUnknown,
+ NoPermission,
+ CollectionFullOrLocked,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn create_collection(
origin: OriginFor<T>,
@@ -121,127 +122,141 @@
max: Option<u32>,
symbol: RmrkCollectionSymbol,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
+ let sender = ensure_signed(origin)?;
- let limits = CollectionLimits {
- owner_can_transfer: Some(false),
- token_limit: max,
- ..Default::default()
- };
+ let limits = CollectionLimits {
+ owner_can_transfer: Some(false),
+ token_limit: max,
+ ..Default::default()
+ };
- let data = CreateCollectionData {
- limits: Some(limits),
- token_prefix: symbol.into_inner()
- .try_into()
- .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
- ..Default::default()
- };
+ let data = CreateCollectionData {
+ limits: Some(limits),
+ token_prefix: symbol
+ .into_inner()
+ .try_into()
+ .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
+ ..Default::default()
+ };
- let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);
+ let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);
- if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {
- return Err(<Error<T>>::NoAvailableCollectionId.into());
- }
+ if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {
+ return Err(<Error<T>>::NoAvailableCollectionId.into());
+ }
- let collection_id = collection_id_res?;
+ let collection_id = collection_id_res?;
- <PalletCommon<T>>::set_scoped_collection_properties(
- collection_id,
- PropertyScope::Rmrk,
- [
- Self::rmrk_property(Metadata, &metadata)?,
- Self::rmrk_property(CollectionType, &misc::CollectionType::Regular)?,
- ].into_iter()
- )?;
+ <PalletCommon<T>>::set_scoped_collection_properties(
+ collection_id,
+ PropertyScope::Rmrk,
+ [
+ Self::rmrk_property(Metadata, &metadata)?,
+ Self::rmrk_property(CollectionType, &misc::CollectionType::Regular)?,
+ ]
+ .into_iter(),
+ )?;
- <CollectionIndex<T>>::mutate(|n| *n += 1);
+ <CollectionIndex<T>>::mutate(|n| *n += 1);
- Self::deposit_event(Event::CollectionCreated {
- issuer: sender,
- collection_id: collection_id.0
- });
+ Self::deposit_event(Event::CollectionCreated {
+ issuer: sender,
+ collection_id: collection_id.0,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn destroy_collection(
origin: OriginFor<T>,
collection_id: RmrkCollectionId,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
- let cross_sender = T::CrossAccountId::from_sub(sender.clone());
+ let sender = ensure_signed(origin)?;
+ let cross_sender = T::CrossAccountId::from_sub(sender.clone());
- let unique_collection_id = collection_id.into();
+ let unique_collection_id = collection_id.into();
- let collection = Self::get_typed_nft_collection(unique_collection_id, misc::CollectionType::Regular)?;
+ let collection = Self::get_typed_nft_collection(
+ unique_collection_id,
+ misc::CollectionType::Regular,
+ )?;
- ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
+ ensure!(
+ collection.total_supply() == 0,
+ <Error<T>>::CollectionNotEmpty
+ );
- <PalletNft<T>>::destroy_collection(collection, &cross_sender)
- .map_err(Self::map_common_err_to_proxy)?;
+ <PalletNft<T>>::destroy_collection(collection, &cross_sender)
+ .map_err(Self::map_common_err_to_proxy)?;
- Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });
+ Self::deposit_event(Event::CollectionDestroyed {
+ issuer: sender,
+ collection_id,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn change_collection_issuer(
origin: OriginFor<T>,
collection_id: RmrkCollectionId,
new_issuer: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
+ let sender = ensure_signed(origin)?;
- let new_issuer = T::Lookup::lookup(new_issuer)?;
+ let new_issuer = T::Lookup::lookup(new_issuer)?;
- Self::change_collection_owner(
- collection_id.into(),
- misc::CollectionType::Regular,
- sender.clone(),
- new_issuer.clone()
- )?;
+ Self::change_collection_owner(
+ collection_id.into(),
+ misc::CollectionType::Regular,
+ sender.clone(),
+ new_issuer.clone(),
+ )?;
- Self::deposit_event(Event::IssuerChanged {
+ Self::deposit_event(Event::IssuerChanged {
old_issuer: sender,
new_issuer,
collection_id,
});
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn lock_collection(
origin: OriginFor<T>,
collection_id: RmrkCollectionId,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
- let cross_sender = T::CrossAccountId::from_sub(sender.clone());
+ let sender = ensure_signed(origin)?;
+ let cross_sender = T::CrossAccountId::from_sub(sender.clone());
- let collection = Self::get_typed_nft_collection(
- collection_id.into(),
- misc::CollectionType::Regular
- )?;
+ let collection = Self::get_typed_nft_collection(
+ collection_id.into(),
+ misc::CollectionType::Regular,
+ )?;
- Self::check_collection_owner(&collection, &cross_sender)?;
+ Self::check_collection_owner(&collection, &cross_sender)?;
- let token_count = collection.total_supply();
+ let token_count = collection.total_supply();
- let mut collection = collection.into_inner();
- collection.limits.token_limit = Some(token_count);
- collection.save()?;
+ let mut collection = collection.into_inner();
+ collection.limits.token_limit = Some(token_count);
+ collection.save()?;
- Self::deposit_event(Event::CollectionLocked { issuer: sender, collection_id });
+ Self::deposit_event(Event::CollectionLocked {
+ issuer: sender,
+ collection_id,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn mint_nft(
origin: OriginFor<T>,
@@ -251,47 +266,49 @@
royalty_amount: Option<Permill>,
metadata: RmrkString,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
- let sender = T::CrossAccountId::from_sub(sender);
- let cross_owner = T::CrossAccountId::from_sub(owner.clone());
+ let sender = ensure_signed(origin)?;
+ let sender = T::CrossAccountId::from_sub(sender);
+ let cross_owner = T::CrossAccountId::from_sub(owner.clone());
- let royalty_info = royalty_amount.map(|amount| rmrk::RoyaltyInfo {
- recipient: recipient.unwrap_or_else(|| owner.clone()),
- amount
- });
+ let royalty_info = royalty_amount.map(|amount| rmrk::RoyaltyInfo {
+ recipient: recipient.unwrap_or_else(|| owner.clone()),
+ amount,
+ });
- let collection = Self::get_typed_nft_collection(
- collection_id.into(),
- misc::CollectionType::Regular,
- )?;
+ let collection = Self::get_typed_nft_collection(
+ collection_id.into(),
+ misc::CollectionType::Regular,
+ )?;
- let nft_id = Self::create_nft(
- &sender,
- &cross_owner,
- &collection,
- NftType::Regular,
- [
- Self::rmrk_property(RoyaltyInfo, &royalty_info)?,
- Self::rmrk_property(Metadata, &metadata)?,
- Self::rmrk_property(Equipped, &false)?,
- Self::rmrk_property(ResourceCollection, &None::<CollectionId>)?,
- Self::rmrk_property(ResourcePriorities, &<Vec<u8>>::new())?,
- ].into_iter()
- ).map_err(|err| match err {
- DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),
- err => Self::map_common_err_to_proxy(err)
- })?;
+ let nft_id = Self::create_nft(
+ &sender,
+ &cross_owner,
+ &collection,
+ NftType::Regular,
+ [
+ Self::rmrk_property(RoyaltyInfo, &royalty_info)?,
+ Self::rmrk_property(Metadata, &metadata)?,
+ Self::rmrk_property(Equipped, &false)?,
+ Self::rmrk_property(ResourceCollection, &None::<CollectionId>)?,
+ Self::rmrk_property(ResourcePriorities, &<Vec<u8>>::new())?,
+ ]
+ .into_iter(),
+ )
+ .map_err(|err| match err {
+ DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),
+ err => Self::map_common_err_to_proxy(err),
+ })?;
- Self::deposit_event(Event::NftMinted {
- owner,
- collection_id,
- nft_id: nft_id.0
- });
+ Self::deposit_event(Event::NftMinted {
+ owner,
+ collection_id,
+ nft_id: nft_id.0,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn burn_nft(
origin: OriginFor<T>,
@@ -299,21 +316,24 @@
nft_id: RmrkNftId,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
- let cross_sender = T::CrossAccountId::from_sub(sender.clone());
+ let cross_sender = T::CrossAccountId::from_sub(sender.clone());
- Self::destroy_nft(
- cross_sender,
- collection_id.into(),
- misc::CollectionType::Regular,
- nft_id.into()
- )?;
+ Self::destroy_nft(
+ cross_sender,
+ collection_id.into(),
+ misc::CollectionType::Regular,
+ nft_id.into(),
+ )?;
- Self::deposit_event(Event::NFTBurned { owner: sender, nft_id });
+ Self::deposit_event(Event::NFTBurned {
+ owner: sender,
+ nft_id,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[transactional]
pub fn set_property(
origin: OriginFor<T>,
@@ -322,323 +342,346 @@
key: RmrkKeyString,
value: RmrkValueString,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
- let sender = T::CrossAccountId::from_sub(sender);
+ let sender = ensure_signed(origin)?;
+ let sender = T::CrossAccountId::from_sub(sender);
- let collection_id: CollectionId = rmrk_collection_id.into();
+ let collection_id: CollectionId = rmrk_collection_id.into();
- match maybe_nft_id {
- Some(nft_id) => {
- let token_id: TokenId = nft_id.into();
+ match maybe_nft_id {
+ Some(nft_id) => {
+ let token_id: TokenId = nft_id.into();
- Self::ensure_nft_owner(collection_id, token_id, &sender)?;
- Self::ensure_nft_type(collection_id, token_id, NftType::Regular)?;
+ Self::ensure_nft_owner(collection_id, token_id, &sender)?;
+ Self::ensure_nft_type(collection_id, token_id, NftType::Regular)?;
- <PalletNft<T>>::set_scoped_token_property(
- collection_id,
- token_id,
- PropertyScope::Rmrk,
- Self::rmrk_property(UserProperty(key.as_slice()), &value)?
- )?;
- },
- None => {
- let collection = Self::get_typed_nft_collection(
- collection_id,
- misc::CollectionType::Regular
- )?;
+ <PalletNft<T>>::set_scoped_token_property(
+ collection_id,
+ token_id,
+ PropertyScope::Rmrk,
+ Self::rmrk_property(UserProperty(key.as_slice()), &value)?,
+ )?;
+ }
+ None => {
+ let collection = Self::get_typed_nft_collection(
+ collection_id,
+ misc::CollectionType::Regular,
+ )?;
- Self::check_collection_owner(&collection, &sender)?;
+ Self::check_collection_owner(&collection, &sender)?;
- <PalletCommon<T>>::set_scoped_collection_property(
- collection_id,
- PropertyScope::Rmrk,
- Self::rmrk_property(UserProperty(key.as_slice()), &value)?
- )?;
- }
- }
+ <PalletCommon<T>>::set_scoped_collection_property(
+ collection_id,
+ PropertyScope::Rmrk,
+ Self::rmrk_property(UserProperty(key.as_slice()), &value)?,
+ )?;
+ }
+ }
- Self::deposit_event(
- Event::PropertySet {
- collection_id: rmrk_collection_id,
- maybe_nft_id,
- key,
- value
- }
- );
+ Self::deposit_event(Event::PropertySet {
+ collection_id: rmrk_collection_id,
+ maybe_nft_id,
+ key,
+ value,
+ });
- Ok(())
- }
+ Ok(())
+ }
}
}
impl<T: Config> Pallet<T> {
- pub fn rmrk_property_key(rmrk_key: RmrkProperty) -> Result<PropertyKey, DispatchError> {
- let key = rmrk_key.to_key::<T>()?;
+ pub fn rmrk_property_key(rmrk_key: RmrkProperty) -> Result<PropertyKey, DispatchError> {
+ let key = rmrk_key.to_key::<T>()?;
- let scoped_key = PropertyScope::Rmrk.apply(key)
- .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)?;
+ let scoped_key = PropertyScope::Rmrk
+ .apply(key)
+ .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)?;
- Ok(scoped_key)
- }
+ Ok(scoped_key)
+ }
- pub fn rmrk_property<E: Encode>(rmrk_key: RmrkProperty, value: &E) -> Result<Property, DispatchError> {
- let key = rmrk_key.to_key::<T>()?;
+ pub fn rmrk_property<E: Encode>(
+ rmrk_key: RmrkProperty,
+ value: &E,
+ ) -> Result<Property, DispatchError> {
+ let key = rmrk_key.to_key::<T>()?;
- let value = value.encode()
- .try_into()
- .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong)?;
+ let value = value
+ .encode()
+ .try_into()
+ .map_err(|_| <Error<T>>::RmrkPropertyValueIsTooLong)?;
- let property = Property {
- key,
- value,
- };
+ let property = Property { key, value };
- Ok(property)
- }
+ Ok(property)
+ }
- pub fn create_nft(
- sender: &T::CrossAccountId,
- owner: &T::CrossAccountId,
- collection: &NonfungibleHandle<T>,
- nft_type: NftType,
- properties: impl Iterator<Item=Property>
- ) -> Result<TokenId, DispatchError> {
- todo!("store nft type");
- let data = CreateNftExData {
- properties: BoundedVec::default(),
- owner: owner.clone(),
- };
+ pub fn create_nft(
+ sender: &T::CrossAccountId,
+ owner: &T::CrossAccountId,
+ collection: &NonfungibleHandle<T>,
+ nft_type: NftType,
+ properties: impl Iterator<Item = Property>,
+ ) -> Result<TokenId, DispatchError> {
+ todo!("store nft type");
+ let data = CreateNftExData {
+ properties: BoundedVec::default(),
+ owner: owner.clone(),
+ };
- let budget = budget::Value::new(2);
+ let budget = budget::Value::new(2);
- <PalletNft<T>>::create_item(
- collection,
- sender,
- data,
- &budget,
- )?;
+ <PalletNft<T>>::create_item(collection, sender, data, &budget)?;
- let nft_id = <PalletNft<T>>::current_token_id(collection.id);
+ let nft_id = <PalletNft<T>>::current_token_id(collection.id);
- <PalletNft<T>>::set_scoped_token_properties(
- collection.id,
- nft_id,
- PropertyScope::Rmrk,
- properties
- )?;
+ <PalletNft<T>>::set_scoped_token_properties(
+ collection.id,
+ nft_id,
+ PropertyScope::Rmrk,
+ properties,
+ )?;
- Ok(nft_id)
- }
+ Ok(nft_id)
+ }
- fn destroy_nft(
- sender: T::CrossAccountId,
- collection_id: CollectionId,
- collection_type: misc::CollectionType,
- token_id: TokenId
- ) -> DispatchResult {
- let collection = Self::get_typed_nft_collection(
- collection_id,
- collection_type
- )?;
+ fn destroy_nft(
+ sender: T::CrossAccountId,
+ collection_id: CollectionId,
+ collection_type: misc::CollectionType,
+ token_id: TokenId,
+ ) -> DispatchResult {
+ let collection = Self::get_typed_nft_collection(collection_id, collection_type)?;
- <PalletNft<T>>::burn(&collection, &sender, token_id)
- .map_err(Self::map_common_err_to_proxy)?;
+ <PalletNft<T>>::burn(&collection, &sender, token_id)
+ .map_err(Self::map_common_err_to_proxy)?;
- Ok(())
- }
+ Ok(())
+ }
- fn change_collection_owner(
- collection_id: CollectionId,
- collection_type: misc::CollectionType,
- sender: T::AccountId,
- new_owner: T::AccountId,
- ) -> DispatchResult {
- let collection = Self::get_typed_nft_collection(
- collection_id,
- collection_type
- )?;
- Self::check_collection_owner(&collection, &T::CrossAccountId::from_sub(sender))?;
+ fn change_collection_owner(
+ collection_id: CollectionId,
+ collection_type: misc::CollectionType,
+ sender: T::AccountId,
+ new_owner: T::AccountId,
+ ) -> DispatchResult {
+ let collection = Self::get_typed_nft_collection(collection_id, collection_type)?;
+ Self::check_collection_owner(&collection, &T::CrossAccountId::from_sub(sender))?;
- let mut collection = collection.into_inner();
+ let mut collection = collection.into_inner();
- collection.owner = new_owner;
- collection.save()
- }
+ collection.owner = new_owner;
+ collection.save()
+ }
- fn check_collection_owner(collection: &NonfungibleHandle<T>, account: &T::CrossAccountId) -> DispatchResult {
- collection.check_is_owner(account)
- .map_err(Self::map_common_err_to_proxy)
- }
+ fn check_collection_owner(
+ collection: &NonfungibleHandle<T>,
+ account: &T::CrossAccountId,
+ ) -> DispatchResult {
+ collection
+ .check_is_owner(account)
+ .map_err(Self::map_common_err_to_proxy)
+ }
- pub fn last_collection_idx() -> RmrkCollectionId {
- <CollectionIndex<T>>::get()
- }
+ pub fn last_collection_idx() -> RmrkCollectionId {
+ <CollectionIndex<T>>::get()
+ }
- pub fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {
- let collection = <CollectionHandle<T>>::try_get(collection_id)
- .map_err(|_| <Error<T>>::CollectionUnknown)?;
+ pub fn get_nft_collection(
+ collection_id: CollectionId,
+ ) -> Result<NonfungibleHandle<T>, DispatchError> {
+ let collection = <CollectionHandle<T>>::try_get(collection_id)
+ .map_err(|_| <Error<T>>::CollectionUnknown)?;
- match collection.mode {
- CollectionMode::NFT => Ok(NonfungibleHandle::cast(collection)),
- _ => Err(<Error<T>>::CollectionUnknown.into())
- }
- }
+ match collection.mode {
+ CollectionMode::NFT => Ok(NonfungibleHandle::cast(collection)),
+ _ => Err(<Error<T>>::CollectionUnknown.into()),
+ }
+ }
- pub fn collection_exists(collection_id: CollectionId) -> bool {
- <CollectionHandle<T>>::try_get(collection_id).is_ok()
- }
+ pub fn collection_exists(collection_id: CollectionId) -> bool {
+ <CollectionHandle<T>>::try_get(collection_id).is_ok()
+ }
- pub fn nft_exists(collection_id: CollectionId, nft_id: TokenId) -> bool {
- <TokenData<T>>::contains_key((collection_id, nft_id))
- }
+ pub fn nft_exists(collection_id: CollectionId, nft_id: TokenId) -> bool {
+ <TokenData<T>>::contains_key((collection_id, nft_id))
+ }
- pub fn get_collection_property(collection_id: CollectionId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {
- let collection_property = <PalletCommon<T>>::collection_properties(collection_id)
- .get(&Self::rmrk_property_key(key)?)
- .ok_or(<Error<T>>::CollectionUnknown)?
- .clone();
+ pub fn get_collection_property(
+ collection_id: CollectionId,
+ key: RmrkProperty,
+ ) -> Result<PropertyValue, DispatchError> {
+ let collection_property = <PalletCommon<T>>::collection_properties(collection_id)
+ .get(&Self::rmrk_property_key(key)?)
+ .ok_or(<Error<T>>::CollectionUnknown)?
+ .clone();
- Ok(collection_property)
- }
+ Ok(collection_property)
+ }
- pub fn get_collection_type(collection_id: CollectionId) -> Result<misc::CollectionType, DispatchError> {
- let value = Self::get_collection_property(collection_id, CollectionType)?;
+ pub fn get_collection_type(
+ collection_id: CollectionId,
+ ) -> Result<misc::CollectionType, DispatchError> {
+ let value = Self::get_collection_property(collection_id, CollectionType)?;
- let mut value = value.as_slice();
+ let mut value = value.as_slice();
- misc::CollectionType::decode(&mut value)
- .map_err(|_| <Error<T>>::CorruptedCollectionType.into())
- }
+ misc::CollectionType::decode(&mut value)
+ .map_err(|_| <Error<T>>::CorruptedCollectionType.into())
+ }
- pub fn ensure_collection_type(collection_id: CollectionId, collection_type: misc::CollectionType) -> DispatchResult {
- let actual_type = Self::get_collection_type(collection_id)?;
- ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);
+ pub fn ensure_collection_type(
+ collection_id: CollectionId,
+ collection_type: misc::CollectionType,
+ ) -> DispatchResult {
+ let actual_type = Self::get_collection_type(collection_id)?;
+ ensure!(
+ actual_type == collection_type,
+ <CommonError<T>>::NoPermission
+ );
- Ok(())
- }
+ Ok(())
+ }
- pub fn get_nft_property(collection_id: CollectionId, nft_id: TokenId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {
- let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))
- .get(&Self::rmrk_property_key(key)?)
- .ok_or(<Error<T>>::NoAvailableNftId)?
- .clone();
+ pub fn get_nft_property(
+ collection_id: CollectionId,
+ nft_id: TokenId,
+ key: RmrkProperty,
+ ) -> Result<PropertyValue, DispatchError> {
+ let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))
+ .get(&Self::rmrk_property_key(key)?)
+ .ok_or(<Error<T>>::NoAvailableNftId)?
+ .clone();
- Ok(nft_property)
- }
+ Ok(nft_property)
+ }
- pub fn get_nft_type(_collection_id: CollectionId, _token_id: TokenId) -> Result<NftType, DispatchError> {
- todo!("should get it from properties?")
- }
+ pub fn get_nft_type(
+ _collection_id: CollectionId,
+ _token_id: TokenId,
+ ) -> Result<NftType, DispatchError> {
+ todo!("should get it from properties?")
+ }
- pub fn ensure_nft_type(collection_id: CollectionId, token_id: TokenId, nft_type: NftType) -> DispatchResult {
- let actual_type = Self::get_nft_type(collection_id, token_id)?;
- ensure!(actual_type == nft_type, <Error<T>>::NoPermission);
+ pub fn ensure_nft_type(
+ collection_id: CollectionId,
+ token_id: TokenId,
+ nft_type: NftType,
+ ) -> DispatchResult {
+ let actual_type = Self::get_nft_type(collection_id, token_id)?;
+ ensure!(actual_type == nft_type, <Error<T>>::NoPermission);
- Ok(())
- }
+ Ok(())
+ }
- pub fn ensure_nft_owner(
- collection_id: CollectionId,
- token_id: TokenId,
- possible_owner: &T::CrossAccountId
- ) -> DispatchResult {
- let token_data = <TokenData<T>>::get((collection_id, token_id))
- .ok_or(<Error<T>>::NoAvailableNftId)?;
+ pub fn ensure_nft_owner(
+ collection_id: CollectionId,
+ token_id: TokenId,
+ possible_owner: &T::CrossAccountId,
+ ) -> DispatchResult {
+ let token_data =
+ <TokenData<T>>::get((collection_id, token_id)).ok_or(<Error<T>>::NoAvailableNftId)?;
- ensure!(token_data.owner == *possible_owner, <Error<T>>::NoPermission);
+ ensure!(
+ token_data.owner == *possible_owner,
+ <Error<T>>::NoPermission
+ );
- Ok(())
- }
+ Ok(())
+ }
- pub fn filter_user_properties<Key, Value, R, Mapper>(
- collection_id: CollectionId,
- token_id: Option<TokenId>,
- filter_keys: Option<Vec<RmrkPropertyKey>>,
- mapper: Mapper,
- ) -> Result<Vec<R>, DispatchError>
- where
- Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,
- Value: Decode + Default,
- Mapper: Fn(Key, Value) -> R
- {
- filter_keys.map(|keys| {
- let properties = keys.into_iter()
- .filter_map(|key| {
- let key: Key = key.try_into().ok()?;
+ pub fn filter_user_properties<Key, Value, R, Mapper>(
+ collection_id: CollectionId,
+ token_id: Option<TokenId>,
+ filter_keys: Option<Vec<RmrkPropertyKey>>,
+ mapper: Mapper,
+ ) -> Result<Vec<R>, DispatchError>
+ where
+ Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,
+ Value: Decode + Default,
+ Mapper: Fn(Key, Value) -> R,
+ {
+ filter_keys
+ .map(|keys| {
+ let properties = keys
+ .into_iter()
+ .filter_map(|key| {
+ let key: Key = key.try_into().ok()?;
- let value = match token_id {
- Some(token_id) => Self::get_nft_property(
- collection_id,
- token_id,
- UserProperty(key.as_ref())
- ),
- None => Self::get_collection_property(
- collection_id,
- UserProperty(key.as_ref())
- )
- }.ok()?.decode_or_default();
+ let value = match token_id {
+ Some(token_id) => Self::get_nft_property(
+ collection_id,
+ token_id,
+ UserProperty(key.as_ref()),
+ ),
+ None => Self::get_collection_property(
+ collection_id,
+ UserProperty(key.as_ref()),
+ ),
+ }
+ .ok()?
+ .decode_or_default();
- Some(mapper(key, value))
- })
- .collect();
+ Some(mapper(key, value))
+ })
+ .collect();
- Ok(properties)
- }).unwrap_or_else(|| {
- let properties = Self::iterate_user_properties(collection_id, token_id, mapper)?
- .collect();
+ Ok(properties)
+ })
+ .unwrap_or_else(|| {
+ let properties =
+ Self::iterate_user_properties(collection_id, token_id, mapper)?.collect();
- Ok(properties)
- })
- }
+ Ok(properties)
+ })
+ }
- pub fn iterate_user_properties<Key, Value, R, Mapper>(
- collection_id: CollectionId,
- token_id: Option<TokenId>,
- mapper: Mapper,
- ) -> Result<impl Iterator<Item=R>, DispatchError>
- where
- Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,
- Value: Decode + Default,
- Mapper: Fn(Key, Value) -> R
- {
- let key_prefix = Self::rmrk_property_key(UserProperty(b""))?;
+ pub fn iterate_user_properties<Key, Value, R, Mapper>(
+ collection_id: CollectionId,
+ token_id: Option<TokenId>,
+ mapper: Mapper,
+ ) -> Result<impl Iterator<Item = R>, DispatchError>
+ where
+ Key: TryFrom<RmrkPropertyKey> + AsRef<[u8]>,
+ Value: Decode + Default,
+ Mapper: Fn(Key, Value) -> R,
+ {
+ let key_prefix = Self::rmrk_property_key(UserProperty(b""))?;
- let properties = match token_id {
- Some(token_id) => <PalletNft<T>>::token_properties((collection_id, token_id)),
- None => <PalletCommon<T>>::collection_properties(collection_id)
- };
+ let properties = match token_id {
+ Some(token_id) => <PalletNft<T>>::token_properties((collection_id, token_id)),
+ None => <PalletCommon<T>>::collection_properties(collection_id),
+ };
- let properties = properties
- .into_iter()
- .filter_map(move |(key, value)| {
- let key = key.as_slice().strip_prefix(key_prefix.as_slice())?;
+ let properties = properties.into_iter().filter_map(move |(key, value)| {
+ let key = key.as_slice().strip_prefix(key_prefix.as_slice())?;
- let key: Key = key.to_vec().try_into().ok()?;
- let value: Value = value.decode_or_default();
+ let key: Key = key.to_vec().try_into().ok()?;
+ let value: Value = value.decode_or_default();
- Some(mapper(key, value))
- });
+ Some(mapper(key, value))
+ });
- Ok(properties)
- }
+ Ok(properties)
+ }
- pub fn get_typed_nft_collection(
- collection_id: CollectionId,
- collection_type: misc::CollectionType
- ) -> Result<NonfungibleHandle<T>, DispatchError> {
- Self::ensure_collection_type(collection_id, collection_type)?;
+ pub fn get_typed_nft_collection(
+ collection_id: CollectionId,
+ collection_type: misc::CollectionType,
+ ) -> Result<NonfungibleHandle<T>, DispatchError> {
+ Self::ensure_collection_type(collection_id, collection_type)?;
- Self::get_nft_collection(collection_id)
- }
+ Self::get_nft_collection(collection_id)
+ }
- fn map_common_err_to_proxy(err: DispatchError) -> DispatchError {
- map_common_err_to_proxy! {
- match err {
- NoPermission => NoPermission,
- CollectionTokenLimitExceeded => CollectionFullOrLocked,
- PublicMintingNotAllowed => NoPermission,
- TokenNotFound => NoAvailableNftId
- }
- }
- }
+ fn map_common_err_to_proxy(err: DispatchError) -> DispatchError {
+ map_common_err_to_proxy! {
+ match err {
+ NoPermission => NoPermission,
+ CollectionTokenLimitExceeded => CollectionFullOrLocked,
+ PublicMintingNotAllowed => NoPermission,
+ TokenNotFound => NoAvailableNftId
+ }
+ }
+ }
}
pallets/proxy-rmrk-core/src/misc.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/misc.rs
+++ b/pallets/proxy-rmrk-core/src/misc.rs
@@ -15,41 +15,42 @@
}
pub trait RmrkDecode<T: Decode + Default, S> {
- fn decode_or_default(&self) -> T;
+ fn decode_or_default(&self) -> T;
}
impl<T: Decode + Default, S> RmrkDecode<T, S> for BoundedVec<u8, S> {
- fn decode_or_default(&self) -> T {
- let mut value = self.as_slice();
+ fn decode_or_default(&self) -> T {
+ let mut value = self.as_slice();
- T::decode(&mut value).unwrap_or_default()
- }
+ T::decode(&mut value).unwrap_or_default()
+ }
}
pub trait RmrkRebind<T, S> {
- fn rebind(&self) -> BoundedVec<u8, S>;
+ fn rebind(&self) -> BoundedVec<u8, S>;
}
-impl<T, S> RmrkRebind<T, S> for BoundedVec<u8, T> where BoundedVec<u8, S>: TryFrom<Vec<u8>> {
- fn rebind(&self) -> BoundedVec<u8, S> {
- BoundedVec::<u8, S>::try_from(
- self.clone().into_inner()
- ).unwrap_or_default()
- }
+impl<T, S> RmrkRebind<T, S> for BoundedVec<u8, T>
+where
+ BoundedVec<u8, S>: TryFrom<Vec<u8>>,
+{
+ fn rebind(&self) -> BoundedVec<u8, S> {
+ BoundedVec::<u8, S>::try_from(self.clone().into_inner()).unwrap_or_default()
+ }
}
#[derive(Encode, Decode, PartialEq, Eq)]
pub enum CollectionType {
- Regular,
- Resource,
- Base,
+ Regular,
+ Resource,
+ Base,
}
#[derive(Encode, Decode, PartialEq, Eq)]
pub enum NftType {
- Regular,
- Resource,
- FixedPart,
- SlotPart,
- Theme
+ Regular,
+ Resource,
+ FixedPart,
+ SlotPart,
+ Theme,
}
pallets/proxy-rmrk-core/src/property.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/property.rs
+++ b/pallets/proxy-rmrk-core/src/property.rs
@@ -2,38 +2,38 @@
use core::convert::AsRef;
pub enum RmrkProperty<'r> {
- Metadata,
- CollectionType,
- RoyaltyInfo,
- Equipped,
- ResourceCollection,
- ResourcePriorities,
- ResourceType,
- PendingResourceAccept,
- PendingResourceRemoval,
- Parts,
- Base,
- Src,
- Slot,
- License,
- Thumb,
- EquippedNft,
- BaseType,
- ExternalPartId,
- EquippableList,
- ZIndex,
- ThemeName,
- ThemeInherit,
- UserProperty(&'r [u8]),
+ Metadata,
+ CollectionType,
+ RoyaltyInfo,
+ Equipped,
+ ResourceCollection,
+ ResourcePriorities,
+ ResourceType,
+ PendingResourceAccept,
+ PendingResourceRemoval,
+ Parts,
+ Base,
+ Src,
+ Slot,
+ License,
+ Thumb,
+ EquippedNft,
+ BaseType,
+ ExternalPartId,
+ EquippableList,
+ ZIndex,
+ ThemeName,
+ ThemeInherit,
+ UserProperty(&'r [u8]),
}
impl<'r> RmrkProperty<'r> {
- pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {
- fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {
- container.as_ref()
- }
+ pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {
+ fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {
+ container.as_ref()
+ }
- macro_rules! key {
+ macro_rules! key {
($($component:expr),+) => {
PropertyKey::try_from([$(key!(@ &$component)),+].concat())
.map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)
@@ -44,30 +44,30 @@
};
}
- match self {
- Self::Metadata => key!("metadata"),
- Self::CollectionType => key!("collection-type"),
- Self::RoyaltyInfo => key!("royalty-info"),
- Self::Equipped => key!("equipped"),
- Self::ResourceCollection => key!("resource-collection"),
- Self::ResourcePriorities => key!("resource-priorities"),
- Self::ResourceType => key!("resource-type"),
- Self::PendingResourceAccept => key!("pending-accept"),
- Self::PendingResourceRemoval => key!("pending-removal"),
- Self::Parts => key!("parts"),
- Self::Base => key!("base"),
- Self::Src => key!("src"),
- Self::Slot => key!("slot"),
- Self::License => key!("license"),
- Self::Thumb => key!("thumb"),
- Self::EquippedNft => key!("equipped-nft"),
- Self::BaseType => key!("base-type"),
- Self::ExternalPartId => key!("ext-part-id"),
- Self::EquippableList => key!("equippable-list"),
- Self::ZIndex => key!("z-index"),
- Self::ThemeName => key!("theme-name"),
- Self::ThemeInherit => key!("theme-inherit"),
- Self::UserProperty(name) => key!("userprop-", name),
- }
- }
+ match self {
+ Self::Metadata => key!("metadata"),
+ Self::CollectionType => key!("collection-type"),
+ Self::RoyaltyInfo => key!("royalty-info"),
+ Self::Equipped => key!("equipped"),
+ Self::ResourceCollection => key!("resource-collection"),
+ Self::ResourcePriorities => key!("resource-priorities"),
+ Self::ResourceType => key!("resource-type"),
+ Self::PendingResourceAccept => key!("pending-accept"),
+ Self::PendingResourceRemoval => key!("pending-removal"),
+ Self::Parts => key!("parts"),
+ Self::Base => key!("base"),
+ Self::Src => key!("src"),
+ Self::Slot => key!("slot"),
+ Self::License => key!("license"),
+ Self::Thumb => key!("thumb"),
+ Self::EquippedNft => key!("equipped-nft"),
+ Self::BaseType => key!("base-type"),
+ Self::ExternalPartId => key!("ext-part-id"),
+ Self::EquippableList => key!("equippable-list"),
+ Self::ZIndex => key!("z-index"),
+ Self::ThemeName => key!("theme-name"),
+ Self::ThemeInherit => key!("theme-inherit"),
+ Self::UserProperty(name) => key!("userprop-", name),
+ }
+ }
}
pallets/proxy-rmrk-equip/src/lib.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-equip/src/lib.rs
+++ b/pallets/proxy-rmrk-equip/src/lib.rs
@@ -21,7 +21,11 @@
use sp_runtime::DispatchError;
use up_data_structs::*;
use pallet_common::{Pallet as PalletCommon, Error as CommonError};
-use pallet_rmrk_core::{Pallet as PalletCore, misc::{self, *}, property::RmrkProperty::*};
+use pallet_rmrk_core::{
+ Pallet as PalletCore,
+ misc::{self, *},
+ property::RmrkProperty::*,
+};
use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};
use pallet_evm::account::CrossAccountId;
@@ -29,212 +33,206 @@
#[frame_support::pallet]
pub mod pallet {
- use super::*;
+ use super::*;
#[pallet::config]
- pub trait Config: frame_system::Config
- + pallet_rmrk_core::Config {
+ pub trait Config: frame_system::Config + pallet_rmrk_core::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
- #[pallet::storage]
+ #[pallet::storage]
#[pallet::getter(fn internal_part_id)]
- pub type InernalPartId<T: Config> = StorageDoubleMap<
- _,
- Twox64Concat,
- CollectionId,
- Twox64Concat,
- RmrkPartId,
- TokenId
- >;
+ pub type InernalPartId<T: Config> =
+ StorageDoubleMap<_, Twox64Concat, CollectionId, Twox64Concat, RmrkPartId, TokenId>;
- #[pallet::storage]
+ #[pallet::storage]
#[pallet::getter(fn base_has_default_theme)]
- pub type BaseHasDefaultTheme<T: Config> = StorageMap<
- _,
- Twox64Concat,
- CollectionId,
- bool,
- ValueQuery
- >;
+ pub type BaseHasDefaultTheme<T: Config> =
+ StorageMap<_, Twox64Concat, CollectionId, bool, ValueQuery>;
- #[pallet::pallet]
+ #[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
- BaseCreated {
+ BaseCreated {
issuer: T::AccountId,
base_id: RmrkBaseId,
},
- }
+ }
- #[pallet::error]
+ #[pallet::error]
pub enum Error<T> {
- PermissionError,
- NoAvailableBaseId,
- NoAvailablePartId,
- BaseDoesntExist,
- NeedsDefaultThemeFirst,
- }
+ PermissionError,
+ NoAvailableBaseId,
+ NoAvailablePartId,
+ BaseDoesntExist,
+ NeedsDefaultThemeFirst,
+ }
- #[pallet::call]
+ #[pallet::call]
impl<T: Config> Pallet<T> {
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
- #[transactional]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[transactional]
pub fn create_base(
origin: OriginFor<T>,
base_type: RmrkString,
symbol: RmrkString,
parts: BoundedVec<RmrkPartType, RmrkPartsLimit>,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
- let cross_sender = T::CrossAccountId::from_sub(sender.clone());
+ let sender = ensure_signed(origin)?;
+ let cross_sender = T::CrossAccountId::from_sub(sender.clone());
- let data = CreateCollectionData {
- limits: None,
- token_prefix: symbol.into_inner()
- .try_into()
- .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
- ..Default::default()
- };
+ let data = CreateCollectionData {
+ limits: None,
+ token_prefix: symbol
+ .into_inner()
+ .try_into()
+ .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,
+ ..Default::default()
+ };
- let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);
+ let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);
- if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {
- return Err(<Error<T>>::NoAvailableBaseId.into());
- }
+ if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {
+ return Err(<Error<T>>::NoAvailableBaseId.into());
+ }
- let collection_id = collection_id_res?;
+ let collection_id = collection_id_res?;
- <PalletCommon<T>>::set_scoped_collection_properties(
- collection_id,
- PropertyScope::Rmrk,
- [
- <PalletCore<T>>::rmrk_property(CollectionType, &misc::CollectionType::Base)?,
- <PalletCore<T>>::rmrk_property(BaseType, &base_type)?,
- ].into_iter()
- )?;
+ <PalletCommon<T>>::set_scoped_collection_properties(
+ collection_id,
+ PropertyScope::Rmrk,
+ [
+ <PalletCore<T>>::rmrk_property(CollectionType, &misc::CollectionType::Base)?,
+ <PalletCore<T>>::rmrk_property(BaseType, &base_type)?,
+ ]
+ .into_iter(),
+ )?;
- let collection = <PalletCore<T>>::get_nft_collection(collection_id)?;
+ let collection = <PalletCore<T>>::get_nft_collection(collection_id)?;
- for part in parts {
- let part_id = part.id();
- let part_token_id = Self::create_part(
- &cross_sender,
- &collection,
- part
- )?;
+ for part in parts {
+ let part_id = part.id();
+ let part_token_id = Self::create_part(&cross_sender, &collection, part)?;
- <InernalPartId<T>>::insert(collection_id, part_id, part_token_id);
+ <InernalPartId<T>>::insert(collection_id, part_id, part_token_id);
- <PalletNft<T>>::set_scoped_token_property(
- collection_id,
- part_token_id,
- PropertyScope::Rmrk,
- <PalletCore<T>>::rmrk_property(ExternalPartId, &part_id)?
- )?;
- }
+ <PalletNft<T>>::set_scoped_token_property(
+ collection_id,
+ part_token_id,
+ PropertyScope::Rmrk,
+ <PalletCore<T>>::rmrk_property(ExternalPartId, &part_id)?,
+ )?;
+ }
- Self::deposit_event(Event::BaseCreated { issuer: sender, base_id: collection_id.0 });
+ Self::deposit_event(Event::BaseCreated {
+ issuer: sender,
+ base_id: collection_id.0,
+ });
- Ok(())
- }
+ Ok(())
+ }
- #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
- #[transactional]
+ #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
+ #[transactional]
pub fn theme_add(
origin: OriginFor<T>,
base_id: RmrkBaseId,
theme: RmrkTheme,
) -> DispatchResult {
- let sender = ensure_signed(origin)?;
+ let sender = ensure_signed(origin)?;
- let sender = T::CrossAccountId::from_sub(sender);
- let owner = &sender;
+ let sender = T::CrossAccountId::from_sub(sender);
+ let owner = &sender;
- let collection_id: CollectionId = base_id.into();
+ let collection_id: CollectionId = base_id.into();
- let collection = <PalletCore<T>>::get_typed_nft_collection(
- collection_id,
- misc::CollectionType::Base
- ).map_err(|_| <Error<T>>::BaseDoesntExist)?;
+ let collection = <PalletCore<T>>::get_typed_nft_collection(
+ collection_id,
+ misc::CollectionType::Base,
+ )
+ .map_err(|_| <Error<T>>::BaseDoesntExist)?;
- if theme.name.as_slice() == b"default" {
- <BaseHasDefaultTheme<T>>::insert(collection_id, true);
- } else if !Self::base_has_default_theme(collection_id) {
- return Err(<Error<T>>::NeedsDefaultThemeFirst.into());
- }
+ if theme.name.as_slice() == b"default" {
+ <BaseHasDefaultTheme<T>>::insert(collection_id, true);
+ } else if !Self::base_has_default_theme(collection_id) {
+ return Err(<Error<T>>::NeedsDefaultThemeFirst.into());
+ }
- let token_id = <PalletCore<T>>::create_nft(
- &sender,
- owner,
- &collection,
- NftType::Theme,
- [
- <PalletCore<T>>::rmrk_property(ThemeName, &theme.name)?,
- <PalletCore<T>>::rmrk_property(ThemeInherit, &theme.inherit)?
- ].into_iter()
- ).map_err(|_| <Error<T>>::PermissionError)?;
+ let token_id = <PalletCore<T>>::create_nft(
+ &sender,
+ owner,
+ &collection,
+ NftType::Theme,
+ [
+ <PalletCore<T>>::rmrk_property(ThemeName, &theme.name)?,
+ <PalletCore<T>>::rmrk_property(ThemeInherit, &theme.inherit)?,
+ ]
+ .into_iter(),
+ )
+ .map_err(|_| <Error<T>>::PermissionError)?;
- for property in theme.properties {
- <PalletNft<T>>::set_scoped_token_property(
- collection_id,
- token_id,
- PropertyScope::Rmrk,
- <PalletCore<T>>::rmrk_property(
- UserProperty(property.key.as_slice()),
- &property.value
- )?
- )?;
- }
+ for property in theme.properties {
+ <PalletNft<T>>::set_scoped_token_property(
+ collection_id,
+ token_id,
+ PropertyScope::Rmrk,
+ <PalletCore<T>>::rmrk_property(
+ UserProperty(property.key.as_slice()),
+ &property.value,
+ )?,
+ )?;
+ }
- Ok(())
- }
- }
+ Ok(())
+ }
+ }
}
impl<T: Config> Pallet<T> {
- fn create_part(
- sender: &T::CrossAccountId,
- collection: &NonfungibleHandle<T>,
- part: RmrkPartType
- ) -> Result<TokenId, DispatchError> {
- let owner = sender;
+ fn create_part(
+ sender: &T::CrossAccountId,
+ collection: &NonfungibleHandle<T>,
+ part: RmrkPartType,
+ ) -> Result<TokenId, DispatchError> {
+ let owner = sender;
- let src = part.src();
- let z_index = part.z_index();
+ let src = part.src();
+ let z_index = part.z_index();
- let nft_type = match part {
- RmrkPartType::FixedPart(_) => NftType::FixedPart,
- RmrkPartType::SlotPart(_) => NftType::SlotPart,
- };
+ let nft_type = match part {
+ RmrkPartType::FixedPart(_) => NftType::FixedPart,
+ RmrkPartType::SlotPart(_) => NftType::SlotPart,
+ };
- let token_id = <PalletCore<T>>::create_nft(
- sender,
- owner,
- collection,
- nft_type,
- [
- <PalletCore<T>>::rmrk_property(Src, &src)?,
- <PalletCore<T>>::rmrk_property(ZIndex, &z_index)?
- ].into_iter()
- ).map_err(|err| match err {
- DispatchError::Arithmetic(_) => <Error<T>>::NoAvailablePartId.into(),
- err => err
- })?;
+ let token_id = <PalletCore<T>>::create_nft(
+ sender,
+ owner,
+ collection,
+ nft_type,
+ [
+ <PalletCore<T>>::rmrk_property(Src, &src)?,
+ <PalletCore<T>>::rmrk_property(ZIndex, &z_index)?,
+ ]
+ .into_iter(),
+ )
+ .map_err(|err| match err {
+ DispatchError::Arithmetic(_) => <Error<T>>::NoAvailablePartId.into(),
+ err => err,
+ })?;
- if let RmrkPartType::SlotPart(part) = part {
- <PalletNft<T>>::set_scoped_token_property(
- collection.id,
- token_id,
- PropertyScope::Rmrk,
- <PalletCore<T>>::rmrk_property(EquippableList, &part.equippable)?
- )?;
- }
+ if let RmrkPartType::SlotPart(part) = part {
+ <PalletNft<T>>::set_scoped_token_property(
+ collection.id,
+ token_id,
+ PropertyScope::Rmrk,
+ <PalletCore<T>>::rmrk_property(EquippableList, &part.equippable)?,
+ )?;
+ }
- Ok(token_id)
- }
+ Ok(token_id)
+ }
}
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -313,17 +313,9 @@
fail!(<Error<T>>::RefungibleDisallowsNesting)
}
- fn nest(
- &self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}
- fn unnest(
- &self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
<Owned<T>>::iter_prefix((self.id, account))
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -22,9 +22,7 @@
CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,
};
use pallet_evm::account::CrossAccountId;
-use pallet_common::{
- Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,
-};
+use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon};
use pallet_structure::Pallet as PalletStructure;
use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
@@ -230,7 +228,9 @@
}
fn collection_has_tokens(collection_id: CollectionId) -> bool {
- <TokenData<T>>::iter_prefix((collection_id,)).next().is_some()
+ <TokenData<T>>::iter_prefix((collection_id,))
+ .next()
+ .is_some()
}
pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {
@@ -388,18 +388,14 @@
to,
collection.id,
token,
- nesting_budget
+ nesting_budget,
)?;
if let Some(balance_to) = balance_to {
// from != to
if balance_from == 0 {
<Balance<T>>::remove((collection.id, token, from));
- <PalletStructure<T>>::unnest_if_nested(
- from,
- collection.id,
- token
- );
+ <PalletStructure<T>>::unnest_if_nested(from, collection.id, token);
} else {
<Balance<T>>::insert((collection.id, token, from), balance_from);
}
@@ -497,7 +493,6 @@
for (i, token) in data.iter().enumerate() {
let token_id = TokenId(first_token_id + i as u32 + 1);
for (to, _) in token.users.iter() {
-
<PalletStructure<T>>::check_nesting(
sender.clone(),
to,
@@ -531,7 +526,11 @@
}
<Balance<T>>::insert((collection.id, token_id, &user), amount);
<Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);
- <PalletStructure<T>>::nest_if_sent_to_token_unchecked(&user, collection.id, TokenId(token_id));
+ <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
+ &user,
+ collection.id,
+ TokenId(token_id),
+ );
// TODO: ERC20 transfer event
<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(
pallets/structure/src/lib.rsdiffbeforeafterboth--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -189,17 +189,11 @@
under: &T::CrossAccountId,
collection_id: CollectionId,
token_id: TokenId,
- nesting_budget: &dyn Budget
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
- Self::try_exec_if_owner_is_valid_nft(
- under,
- |d, parent_id| d.check_nesting(
- from,
- (collection_id, token_id),
- parent_id,
- nesting_budget
- )
- )
+ Self::try_exec_if_owner_is_valid_nft(under, |d, parent_id| {
+ d.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)
+ })
}
pub fn nest_if_sent_to_token(
@@ -207,69 +201,51 @@
under: &T::CrossAccountId,
collection_id: CollectionId,
token_id: TokenId,
- nesting_budget: &dyn Budget
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
- Self::try_exec_if_owner_is_valid_nft(
- under,
- |d, parent_id| {
- d.check_nesting(
- from,
- (collection_id, token_id),
- parent_id,
- nesting_budget
- )?;
+ Self::try_exec_if_owner_is_valid_nft(under, |d, parent_id| {
+ d.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)?;
- d.nest(parent_id, (collection_id, token_id));
+ d.nest(parent_id, (collection_id, token_id));
- Ok(())
- }
- )
+ Ok(())
+ })
}
pub fn nest_if_sent_to_token_unchecked(
owner: &T::CrossAccountId,
collection_id: CollectionId,
- token_id: TokenId
+ token_id: TokenId,
) {
- Self::exec_if_owner_is_valid_nft(
- owner,
- |d, parent_id| d.nest(
- parent_id,
- (collection_id, token_id)
- )
- );
+ Self::exec_if_owner_is_valid_nft(owner, |d, parent_id| {
+ d.nest(parent_id, (collection_id, token_id))
+ });
}
pub fn unnest_if_nested(
owner: &T::CrossAccountId,
collection_id: CollectionId,
- token_id: TokenId
+ token_id: TokenId,
) {
- Self::exec_if_owner_is_valid_nft(
- owner,
- |d, parent_id| d.unnest(
- parent_id,
- (collection_id, token_id)
- )
- );
+ Self::exec_if_owner_is_valid_nft(owner, |d, parent_id| {
+ d.unnest(parent_id, (collection_id, token_id))
+ });
}
fn exec_if_owner_is_valid_nft(
account: &T::CrossAccountId,
- action: impl FnOnce(&dyn CommonCollectionOperations<T>, TokenId)
+ action: impl FnOnce(&dyn CommonCollectionOperations<T>, TokenId),
) {
- Self::try_exec_if_owner_is_valid_nft(
- account,
- |d, id| {
- action(d, id);
- Ok(())
- }
- ).unwrap();
+ Self::try_exec_if_owner_is_valid_nft(account, |d, id| {
+ action(d, id);
+ Ok(())
+ })
+ .unwrap();
}
fn try_exec_if_owner_is_valid_nft(
account: &T::CrossAccountId,
- action: impl FnOnce(&dyn CommonCollectionOperations<T>, TokenId) -> DispatchResult
+ action: impl FnOnce(&dyn CommonCollectionOperations<T>, TokenId) -> DispatchResult,
) -> DispatchResult {
let account = T::CrossTokenAddressMapping::address_to_token(account);
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -37,11 +37,10 @@
use frame_system::{self as system, ensure_signed};
use sp_runtime::{sp_std::prelude::Vec};
use up_data_structs::{
- MAX_COLLECTION_NAME_LENGTH,
- MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, AccessMode, CreateItemData,
- CollectionLimits, CollectionPermissions, CollectionId, CollectionMode, TokenId, SponsorshipState,
- CreateCollectionData, CreateItemExData, budget, Property, PropertyKey,
- PropertyKeyPermission,
+ MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
+ AccessMode, CreateItemData, CollectionLimits, CollectionPermissions, CollectionId,
+ CollectionMode, TokenId, SponsorshipState, CreateCollectionData, CreateItemExData, budget,
+ Property, PropertyKey, PropertyKeyPermission,
};
use pallet_evm::account::CrossAccountId;
use pallet_common::{
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -49,7 +49,8 @@
},
NftChild as RmrkNftChild, AccountIdOrCollectionNftTuple as RmrkAccountIdOrCollectionNftTuple,
FixedPart as RmrkFixedPart, SlotPart as RmrkSlotPart, EquippableList as RmrkEquippableList,
- BasicResource as RmrkBasicResource, ComposableResource as RmrkComposableResource, SlotResource as RmrkSlotResource,
+ BasicResource as RmrkBasicResource, ComposableResource as RmrkComposableResource,
+ SlotResource as RmrkSlotResource,
};
mod bounded;
@@ -361,8 +362,7 @@
pub type CollectionPropertiesPermissionsVec =
BoundedVec<PropertyKeyPermission, ConstU32<MAX_PROPERTIES_PER_ITEM>>;
-pub type CollectionPropertiesVec =
- BoundedVec<Property, ConstU32<MAX_PROPERTIES_PER_ITEM>>;
+pub type CollectionPropertiesVec = BoundedVec<Property, ConstU32<MAX_PROPERTIES_PER_ITEM>>;
/// All fields are wrapped in `Option`s, where None means chain default
// When adding/removing fields from this struct - don't forget to also update clamp_limits
@@ -790,8 +790,8 @@
>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
- self.0.into_iter()
- }
+ self.0.into_iter()
+ }
}
impl<Value> TrySetProperty for PropertiesMap<Value> {
@@ -853,8 +853,8 @@
type IntoIter = <PropertiesMap<PropertyValue> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
- self.map.into_iter()
- }
+ self.map.into_iter()
+ }
}
impl TrySetProperty for Properties {
@@ -932,11 +932,7 @@
pub type RmrkCollectionInfo<AccountId> =
CollectionInfo<RmrkString, RmrkCollectionSymbol, AccountId>;
pub type RmrkInstanceInfo<AccountId> = NftInfo<AccountId, Permill, RmrkString>;
-pub type RmrkResourceInfo = ResourceInfo<
- RmrkBoundedResource,
- RmrkString,
- RmrkBoundedParts,
->;
+pub type RmrkResourceInfo = ResourceInfo<RmrkBoundedResource, RmrkString, RmrkBoundedParts>;
pub type RmrkPropertyInfo = PropertyInfo<RmrkKeyString, RmrkValueString>;
pub type RmrkBaseInfo<AccountId> = BaseInfo<AccountId, RmrkString>;
pub type RmrkPartType =
primitives/data-structs/src/rmrk.rsdiffbeforeafterboth--- a/primitives/data-structs/src/rmrk.rs
+++ b/primitives/data-structs/src/rmrk.rs
@@ -20,37 +20,37 @@
#[cfg(feature = "std")]
mod serialize {
- use core::convert::AsRef;
- use serde::ser::{self, Serialize};
+ use core::convert::AsRef;
+ use serde::ser::{self, Serialize};
- pub mod vec {
- use super::*;
+ pub mod vec {
+ use super::*;
- pub fn serialize<D, V, C>(value: &C, serializer: D) -> Result<D::Ok, D::Error>
- where
- D: ser::Serializer,
- V: Serialize,
- C: AsRef<[V]>,
- {
- value.as_ref().serialize(serializer)
- }
- }
+ pub fn serialize<D, V, C>(value: &C, serializer: D) -> Result<D::Ok, D::Error>
+ where
+ D: ser::Serializer,
+ V: Serialize,
+ C: AsRef<[V]>,
+ {
+ value.as_ref().serialize(serializer)
+ }
+ }
- pub mod opt_vec {
- use super::*;
+ pub mod opt_vec {
+ use super::*;
- pub fn serialize<D, V, C>(value: &Option<C>, serializer: D) -> Result<D::Ok, D::Error>
- where
- D: ser::Serializer,
- V: Serialize,
- C: AsRef<[V]>,
- {
- match value {
- Some(value) => super::vec::serialize(value, serializer),
- None => serializer.serialize_none()
- }
- }
- }
+ pub fn serialize<D, V, C>(value: &Option<C>, serializer: D) -> Result<D::Ok, D::Error>
+ where
+ D: ser::Serializer,
+ V: Serialize,
+ C: AsRef<[V]>,
+ {
+ match value {
+ Some(value) => super::vec::serialize(value, serializer),
+ None => serializer.serialize_none(),
+ }
+ }
+ }
}
/// Collection info.
@@ -58,13 +58,11 @@
#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
AccountId: Serialize,
BoundedString: AsRef<[u8]>,
BoundedSymbol: AsRef<[u8]>
- "#
- )
+ "#)
)]
pub struct CollectionInfo<BoundedString, BoundedSymbol, AccountId> {
/// Current bidder and bid price.
@@ -91,9 +89,9 @@
#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
pub struct RoyaltyInfo<AccountId, RoyaltyAmount> {
/// Recipient (AccountId) of the royalty
- pub recipient: AccountId,
+ pub recipient: AccountId,
/// Amount (Permill) of the royalty
- pub amount: RoyaltyAmount,
+ pub amount: RoyaltyAmount,
}
/// Nft info.
@@ -101,13 +99,11 @@
#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
AccountId: Serialize,
RoyaltyAmount: Serialize,
BoundedString: AsRef<[u8]>
- "#
- )
+ "#)
)]
pub struct NftInfo<AccountId, RoyaltyAmount, BoundedString> {
/// The owner of the NFT, can be either an Account or a tuple (CollectionId, NftId)
@@ -129,22 +125,19 @@
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct NftChild {
pub collection_id: CollectionId,
- pub nft_id: NftId
+ pub nft_id: NftId,
}
#[cfg_attr(feature = "std", derive(Serialize))]
#[derive(Encode, Decode, PartialEq, TypeInfo)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedKey: AsRef<[u8]>,
BoundedValue: AsRef<[u8]>
- "#
- )
+ "#)
)]
-pub struct PropertyInfo<BoundedKey, BoundedValue>
-{
+pub struct PropertyInfo<BoundedKey, BoundedValue> {
/// Key of the property
#[cfg_attr(feature = "std", serde(with = "serialize::vec"))]
pub key: BoundedKey,
@@ -156,10 +149,7 @@
#[derive(Encode, Decode, Default, Eq, PartialEq, Clone, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize))]
-#[cfg_attr(
- feature = "std",
- serde(bound = "BoundedString: AsRef<[u8]>")
-)]
+#[cfg_attr(feature = "std", serde(bound = "BoundedString: AsRef<[u8]>"))]
pub struct BasicResource<BoundedString> {
/// If the resource is Media, the base property is absent. Media src should be a URI like an
/// IPFS hash.
@@ -188,12 +178,10 @@
#[cfg_attr(feature = "std", derive(Serialize))]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedString: AsRef<[u8]>,
BoundedParts: AsRef<[PartId]>
- "#
- )
+ "#)
)]
pub struct ComposableResource<BoundedString, BoundedParts> {
/// If a resource is composed, it will have an array of parts that compose it
@@ -235,10 +223,7 @@
#[derive(Encode, Decode, Eq, PartialEq, Clone, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize))]
-#[cfg_attr(
- feature = "std",
- serde(bound = "BoundedString: AsRef<[u8]>")
-)]
+#[cfg_attr(feature = "std", serde(bound = "BoundedString: AsRef<[u8]>"))]
pub struct SlotResource<BoundedString> {
/// A Base is uniquely identified by the combination of the word `base`, its minting block
/// number, and user provided symbol during Base creation, glued by dashes `-`, e.g.
@@ -279,14 +264,12 @@
#[cfg_attr(feature = "std", derive(Serialize))]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedString: AsRef<[u8]>,
BoundedParts: AsRef<[PartId]>
- "#
- )
+ "#)
)]
-#[derivative(Default(bound=""))]
+#[derivative(Default(bound = ""))]
pub enum ResourceTypes<BoundedString: Default, BoundedParts> {
#[derivative(Default)]
Basic(BasicResource<BoundedString>),
@@ -294,18 +277,15 @@
Slot(SlotResource<BoundedString>),
}
-
#[derive(Encode, Decode, Eq, PartialEq, Clone, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize))]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedResource: AsRef<[u8]>,
BoundedString: AsRef<[u8]>,
BoundedParts: AsRef<[PartId]>
- "#
- )
+ "#)
)]
pub struct ResourceInfo<BoundedResource, BoundedString: Default, BoundedParts> {
/// id is a 5-character string of reasonable uniqueness.
@@ -328,12 +308,10 @@
#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
AccountId: Serialize,
BoundedString: AsRef<[u8]>
- "#
- )
+ "#)
)]
pub struct BaseInfo<AccountId, BoundedString> {
/// Original creator of the Base
@@ -350,10 +328,7 @@
#[cfg_attr(feature = "std", derive(Serialize))]
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, Eq, MaxEncodedLen)]
-#[cfg_attr(
- feature = "std",
- serde(bound = "BoundedString: AsRef<[u8]>")
-)]
+#[cfg_attr(feature = "std", serde(bound = "BoundedString: AsRef<[u8]>"))]
pub struct FixedPart<BoundedString> {
pub id: PartId,
pub z: ZIndex,
@@ -368,29 +343,24 @@
feature = "std",
serde(bound = "BoundedCollectionList: AsRef<[CollectionId]>")
)]
-#[derivative(Default(bound=""))]
+#[derivative(Default(bound = ""))]
pub enum EquippableList<BoundedCollectionList> {
All,
#[derivative(Default)]
Empty,
- Custom(
- #[cfg_attr(feature = "std", serde(with = "serialize::vec"))]
- BoundedCollectionList
- ),
+ Custom(#[cfg_attr(feature = "std", serde(with = "serialize::vec"))] BoundedCollectionList),
}
#[cfg_attr(feature = "std", derive(Serialize))]
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, Eq, MaxEncodedLen)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedString: AsRef<[u8]>,
BoundedCollectionList: AsRef<[CollectionId]>
- "#
- )
+ "#)
)]
pub struct SlotPart<BoundedString, BoundedCollectionList> {
pub id: PartId,
@@ -406,12 +376,10 @@
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq, Eq, MaxEncodedLen)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedString: AsRef<[u8]>,
BoundedCollectionList: AsRef<[CollectionId]>
- "#
- )
+ "#)
)]
pub enum PartType<BoundedString, BoundedCollectionList> {
FixedPart(FixedPart<BoundedString>),
@@ -422,21 +390,21 @@
pub fn id(&self) -> PartId {
match self {
Self::FixedPart(part) => part.id,
- Self::SlotPart(part) => part.id
+ Self::SlotPart(part) => part.id,
}
}
pub fn src(&self) -> &BoundedString {
match self {
Self::FixedPart(part) => &part.src,
- Self::SlotPart(part) => &part.src
+ Self::SlotPart(part) => &part.src,
}
}
pub fn z_index(&self) -> ZIndex {
match self {
Self::FixedPart(part) => part.z,
- Self::SlotPart(part) => part.z
+ Self::SlotPart(part) => part.z,
}
}
}
@@ -445,12 +413,10 @@
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq)]
#[cfg_attr(
feature = "std",
- serde(
- bound = r#"
+ serde(bound = r#"
BoundedString: AsRef<[u8]>,
PropertyList: AsRef<[ThemeProperty<BoundedString>]>,
- "#
- )
+ "#)
)]
pub struct Theme<BoundedString, PropertyList> {
/// Name of the theme
@@ -466,10 +432,7 @@
#[cfg_attr(feature = "std", derive(Eq, Serialize))]
#[derive(Encode, Decode, Debug, TypeInfo, Clone, PartialEq)]
-#[cfg_attr(
- feature = "std",
- serde(bound = "BoundedString: AsRef<[u8]>")
-)]
+#[cfg_attr(feature = "std", serde(bound = "BoundedString: AsRef<[u8]>"))]
pub struct ThemeProperty<BoundedString> {
/// Key of the property
#[cfg_attr(feature = "std", serde(with = "serialize::vec"))]
runtime/tests/src/tests.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// Tests to be written here18use crate::{Test, TestCrossAccountId, CollectionCreationPrice, Origin, Unique, new_test_ext};19use up_data_structs::{20 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,21 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, TokenId,22 MAX_TOKEN_OWNERSHIP, CreateCollectionData, CollectionMode,23 AccessMode, CollectionPermissions, PropertyKeyPermission, PropertyPermission,24 Property, CollectionPropertiesVec, CollectionPropertiesPermissionsVec,25};26use frame_support::{assert_noop, assert_ok, assert_err};27use sp_std::convert::TryInto;28use pallet_evm::account::CrossAccountId;29use pallet_common::Error as CommonError;30use pallet_unique::Error as UniqueError;3132fn add_balance(user: u64, value: u64) {33 const DONOR_USER: u64 = 999;34 assert_ok!(<pallet_balances::Pallet<Test>>::set_balance(35 Origin::root(),36 DONOR_USER,37 value,38 039 ));40 assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(41 Origin::root(),42 DONOR_USER,43 user,44 value45 ));46}4748fn default_nft_data() -> CreateNftData {49 CreateNftData {50 properties: vec![51 Property {52 key: b"test-prop".to_vec().try_into().unwrap(),53 value: b"test-nft-prop".to_vec().try_into().unwrap(),54 },55 ].try_into().unwrap(),56 }57}5859fn default_fungible_data() -> CreateFungibleData {60 CreateFungibleData { value: 5 }61}6263fn default_re_fungible_data() -> CreateReFungibleData {64 CreateReFungibleData {65 const_data: vec![1, 2, 3].try_into().unwrap(),66 pieces: 1023,67 }68}6970fn create_test_collection_for_owner(71 mode: &CollectionMode,72 owner: u64,73 id: CollectionId,74) -> CollectionId {75 add_balance(owner, CollectionCreationPrice::get() as u64 + 1);7677 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();78 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();79 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();80 let token_property_permissions: CollectionPropertiesPermissionsVec = vec![81 PropertyKeyPermission {82 key: b"test-prop".to_vec().try_into().unwrap(),83 permission: PropertyPermission {84 mutable: true,85 collection_admin: false,86 token_owner: true,87 },88 },89 ].try_into().unwrap();90 let properties: CollectionPropertiesVec = vec![91 Property {92 key: b"test-collection-prop".to_vec().try_into().unwrap(),93 value: b"test-collection-value".to_vec().try_into().unwrap(),94 }95 ].try_into().unwrap();9697 let data: CreateCollectionData<u64> = CreateCollectionData {98 name: col_name1.try_into().unwrap(),99 description: col_desc1.try_into().unwrap(),100 token_prefix: token_prefix1.try_into().unwrap(),101 mode: mode.clone(),102 token_property_permissions: token_property_permissions.clone(),103 properties: properties.clone(),104 ..Default::default()105 };106107 let origin1 = Origin::signed(owner);108 assert_ok!(Unique::create_collection_ex(origin1, data));109110 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();111 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();112 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();113 assert_eq!(114 <pallet_common::CollectionById<Test>>::get(id)115 .unwrap()116 .owner,117 owner118 );119 assert_eq!(120 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,121 saved_col_name122 );123 assert_eq!(124 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,125 *mode126 );127 assert_eq!(128 <pallet_common::CollectionById<Test>>::get(id)129 .unwrap()130 .description,131 saved_description132 );133 assert_eq!(134 <pallet_common::CollectionById<Test>>::get(id)135 .unwrap()136 .token_prefix,137 saved_prefix138 );139 assert_eq!(140 get_collection_property_permissions(id).as_slice(),141 token_property_permissions.as_slice()142 );143 assert_eq!(144 get_collection_properties(id).as_slice(),145 properties.as_slice()146 );147 id148}149150fn get_collection_property_permissions(collection_id: CollectionId) -> Vec<PropertyKeyPermission> {151 <pallet_common::Pallet<Test>>::property_permissions(collection_id)152 .into_iter()153 .map(|(key, permission)| PropertyKeyPermission {154 key,155 permission,156 })157 .collect()158}159160fn get_collection_properties(collection_id: CollectionId) -> Vec<Property> {161 <pallet_common::Pallet<Test>>::collection_properties(collection_id)162 .into_iter()163 .map(|(key, value)| Property {164 key,165 value,166 })167 .collect()168}169170fn get_token_properties(collection_id: CollectionId, token_id: TokenId) -> Vec<Property> {171 <pallet_nonfungible::Pallet<Test>>::token_properties((collection_id, token_id))172 .into_iter()173 .map(|(key, value)| Property {174 key,175 value,176 })177 .collect()178}179180fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {181 create_test_collection_for_owner(&mode, 1, id)182}183184fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {185 let origin1 = Origin::signed(1);186 assert_ok!(Unique::create_item(187 origin1,188 collection_id,189 account(1),190 data.clone()191 ));192}193194fn account(sub: u64) -> TestCrossAccountId {195 TestCrossAccountId::from_sub(sub)196}197198// Use cases tests region199// #region200201#[test]202fn check_not_sufficient_founds() {203 new_test_ext().execute_with(|| {204 let acc: u64 = 1;205 <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();206207 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();208 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();209 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();210211 let data: CreateCollectionData<<Test as frame_system::Config>::AccountId> =212 CreateCollectionData {213 name: name.try_into().unwrap(),214 description: description.try_into().unwrap(),215 token_prefix: token_prefix.try_into().unwrap(),216 mode: CollectionMode::NFT,217 ..Default::default()218 };219220 let result = Unique::create_collection_ex(Origin::signed(acc), data);221 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);222 });223}224225#[test]226fn create_fungible_collection_fails_with_large_decimal_numbers() {227 new_test_ext().execute_with(|| {228 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();229 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();230 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();231232 let data: CreateCollectionData<u64> = CreateCollectionData {233 name: col_name1.try_into().unwrap(),234 description: col_desc1.try_into().unwrap(),235 token_prefix: token_prefix1.try_into().unwrap(),236 mode: CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1),237 ..Default::default()238 };239240 let origin1 = Origin::signed(1);241 assert_noop!(242 Unique::create_collection_ex(origin1, data),243 UniqueError::<Test>::CollectionDecimalPointLimitExceeded244 );245 });246}247248#[test]249fn create_nft_item() {250 new_test_ext().execute_with(|| {251 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));252253 let data = default_nft_data();254 create_test_item(collection_id, &data.clone().into());255256 assert_eq!(257 get_token_properties(collection_id, TokenId(1)).as_slice(),258 data.properties.as_slice(),259 );260 });261}262263// Use cases tests region264// #region265#[test]266fn create_nft_multiple_items() {267 new_test_ext().execute_with(|| {268 create_test_collection(&CollectionMode::NFT, CollectionId(1));269270 let origin1 = Origin::signed(1);271272 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];273274 assert_ok!(Unique::create_multiple_items(275 origin1,276 CollectionId(1),277 account(1),278 items_data279 .clone()280 .into_iter()281 .map(|d| { d.into() })282 .collect()283 ));284 for (index, data) in items_data.into_iter().enumerate() {285 assert_eq!(286 get_token_properties(CollectionId(1), TokenId(index as u32 + 1)).as_slice(),287 data.properties.as_slice()288 );289 }290 });291}292293#[test]294fn create_refungible_item() {295 new_test_ext().execute_with(|| {296 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));297298 let data = default_re_fungible_data();299 create_test_item(collection_id, &data.clone().into());300 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));301 let balance =302 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));303 assert_eq!(item.const_data, data.const_data.into_inner());304 assert_eq!(balance, 1023);305 });306}307308#[test]309fn create_multiple_refungible_items() {310 new_test_ext().execute_with(|| {311 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));312313 let origin1 = Origin::signed(1);314315 let items_data = vec![316 default_re_fungible_data(),317 default_re_fungible_data(),318 default_re_fungible_data(),319 ];320321 assert_ok!(Unique::create_multiple_items(322 origin1,323 CollectionId(1),324 account(1),325 items_data326 .clone()327 .into_iter()328 .map(|d| { d.into() })329 .collect()330 ));331 for (index, data) in items_data.into_iter().enumerate() {332 let item = <pallet_refungible::TokenData<Test>>::get((333 CollectionId(1),334 TokenId((index + 1) as u32),335 ));336 let balance =337 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));338 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());339 assert_eq!(balance, 1023);340 }341 });342}343344#[test]345fn create_fungible_item() {346 new_test_ext().execute_with(|| {347 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));348349 let data = default_fungible_data();350 create_test_item(collection_id, &data.into());351352 assert_eq!(353 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),354 5355 );356 });357}358359//#[test]360// fn create_multiple_fungible_items() {361// new_test_ext().execute_with(|| {362// default_limits();363364// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));365366// let origin1 = Origin::signed(1);367368// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];369370// assert_ok!(Unique::create_multiple_items(371// origin1.clone(),372// 1,373// 1,374// items_data.clone().into_iter().map(|d| { d.into() }).collect()375// ));376377// for (index, _) in items_data.iter().enumerate() {378// assert_eq!(Unique::fungible_item_id(1, (index + 1) as TokenId).value, 5);379// }380// assert_eq!(Unique::balance_count(1, 1), 3000);381// assert_eq!(Unique::address_tokens(1, 1), [1, 2, 3]);382// });383// }384385#[test]386fn transfer_fungible_item() {387 new_test_ext().execute_with(|| {388 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));389390 let origin1 = Origin::signed(1);391 let origin2 = Origin::signed(2);392393 let data = default_fungible_data();394 create_test_item(collection_id, &data.into());395396 assert_eq!(397 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),398 5399 );400401 // change owner scenario402 assert_ok!(Unique::transfer(403 origin1,404 account(2),405 CollectionId(1),406 TokenId(0),407 5408 ));409 assert_eq!(410 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),411 0412 );413414 // split item scenario415 assert_ok!(Unique::transfer(416 origin2.clone(),417 account(3),418 CollectionId(1),419 TokenId(0),420 3421 ));422423 // split item and new owner has account scenario424 assert_ok!(Unique::transfer(425 origin2,426 account(3),427 CollectionId(1),428 TokenId(0),429 1430 ));431 assert_eq!(432 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),433 1434 );435 assert_eq!(436 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),437 4438 );439 });440}441442#[test]443fn transfer_refungible_item() {444 new_test_ext().execute_with(|| {445 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));446447 // Create RFT 1 in 1023 pieces for account 1448 let data = default_re_fungible_data();449 create_test_item(collection_id, &data.clone().into());450 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));451 assert_eq!(item.const_data, data.const_data.into_inner());452 assert_eq!(453 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),454 1455 );456 assert_eq!(457 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),458 1023459 );460 assert_eq!(461 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),462 true463 );464465 // Account 1 transfers all 1023 pieces of RFT 1 to account 2466 let origin1 = Origin::signed(1);467 let origin2 = Origin::signed(2);468 assert_ok!(Unique::transfer(469 origin1,470 account(2),471 CollectionId(1),472 TokenId(1),473 1023474 ));475 assert_eq!(476 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),477 1023478 );479 assert_eq!(480 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),481 0482 );483 assert_eq!(484 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),485 1486 );487 assert_eq!(488 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),489 false490 );491 assert_eq!(492 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),493 true494 );495496 // Account 2 transfers 500 pieces of RFT 1 to account 3497 assert_ok!(Unique::transfer(498 origin2.clone(),499 account(3),500 CollectionId(1),501 TokenId(1),502 500503 ));504 assert_eq!(505 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),506 523507 );508 assert_eq!(509 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),510 500511 );512 assert_eq!(513 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),514 1515 );516 assert_eq!(517 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),518 1519 );520 assert_eq!(521 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),522 true523 );524 assert_eq!(525 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),526 true527 );528529 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance530 assert_ok!(Unique::transfer(531 origin2,532 account(3),533 CollectionId(1),534 TokenId(1),535 200536 ));537 assert_eq!(538 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),539 323540 );541 assert_eq!(542 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),543 700544 );545 assert_eq!(546 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),547 1548 );549 assert_eq!(550 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),551 1552 );553 assert_eq!(554 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),555 true556 );557 assert_eq!(558 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),559 true560 );561 });562}563564#[test]565fn transfer_nft_item() {566 new_test_ext().execute_with(|| {567 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));568569 let data = default_nft_data();570 create_test_item(collection_id, &data.into());571 assert_eq!(572 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),573 1574 );575 assert_eq!(576 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),577 true578 );579580 let origin1 = Origin::signed(1);581 // default scenario582 assert_ok!(Unique::transfer(583 origin1,584 account(2),585 CollectionId(1),586 TokenId(1),587 1588 ));589 assert_eq!(590 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),591 0592 );593 assert_eq!(594 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),595 1596 );597 assert_eq!(598 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),599 false600 );601 assert_eq!(602 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),603 true604 );605 });606}607608#[test]609fn transfer_nft_item_wrong_value() {610 new_test_ext().execute_with(|| {611 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));612613 let data = default_nft_data();614 create_test_item(collection_id, &data.into());615 assert_eq!(616 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),617 1618 );619 assert_eq!(620 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),621 true622 );623624 let origin1 = Origin::signed(1);625626 assert_noop!(627 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)628 .map_err(|e| e.error),629 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount630 );631 });632}633634#[test]635fn transfer_nft_item_zero_value() {636 new_test_ext().execute_with(|| {637 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));638639 let data = default_nft_data();640 create_test_item(collection_id, &data.into());641 assert_eq!(642 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),643 1644 );645 assert_eq!(646 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),647 true648 );649650 let origin1 = Origin::signed(1);651652 // Transferring 0 amount works on NFT...653 assert_ok!(Unique::transfer(654 origin1,655 account(2),656 CollectionId(1),657 TokenId(1),658 0659 ));660 // ... and results in no transfer661 assert_eq!(662 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),663 1664 );665 assert_eq!(666 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),667 true668 );669 });670}671672#[test]673fn nft_approve_and_transfer_from() {674 new_test_ext().execute_with(|| {675 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));676677 let data = default_nft_data();678 create_test_item(collection_id, &data.into());679680 let origin1 = Origin::signed(1);681 let origin2 = Origin::signed(2);682683 assert_eq!(684 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),685 1686 );687 assert_eq!(688 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),689 true690 );691692 // neg transfer_from693 assert_noop!(694 Unique::transfer_from(695 origin2.clone(),696 account(1),697 account(2),698 CollectionId(1),699 TokenId(1),700 1701 )702 .map_err(|e| e.error),703 CommonError::<Test>::ApprovedValueTooLow704 );705706 // do approve707 assert_ok!(Unique::approve(708 origin1,709 account(2),710 CollectionId(1),711 TokenId(1),712 1713 ));714 assert_eq!(715 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),716 account(2)717 );718719 assert_ok!(Unique::transfer_from(720 origin2,721 account(1),722 account(3),723 CollectionId(1),724 TokenId(1),725 1726 ));727 assert!(728 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()729 );730 });731}732733#[test]734fn nft_approve_and_transfer_from_allow_list() {735 new_test_ext().execute_with(|| {736 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));737738 let origin1 = Origin::signed(1);739 let origin2 = Origin::signed(2);740741 // Create NFT 1 for account 1742 let data = default_nft_data();743 create_test_item(collection_id, &data.clone().into());744 assert_eq!(745 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),746 1747 );748 assert_eq!(749 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),750 true751 );752753 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list754 assert_ok!(Unique::set_collection_permissions(755 origin1.clone(),756 CollectionId(1),757 CollectionPermissions {758 mint_mode: Some(true),759 access: Some(AccessMode::AllowList),760 nesting: None,761 }762 ));763 assert_ok!(Unique::add_to_allow_list(764 origin1.clone(),765 CollectionId(1),766 account(1)767 ));768 assert_ok!(Unique::add_to_allow_list(769 origin1.clone(),770 CollectionId(1),771 account(2)772 ));773 assert_ok!(Unique::add_to_allow_list(774 origin1.clone(),775 CollectionId(1),776 account(3)777 ));778779 // Account 1 approves account 2 for NFT 1780 assert_ok!(Unique::approve(781 origin1.clone(),782 account(2),783 CollectionId(1),784 TokenId(1),785 1786 ));787 assert_eq!(788 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),789 account(2)790 );791792 // Account 2 transfers NFT 1 from account 1 to account 3793 assert_ok!(Unique::transfer_from(794 origin2,795 account(1),796 account(3),797 CollectionId(1),798 TokenId(1),799 1800 ));801 assert!(802 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()803 );804 });805}806807#[test]808fn refungible_approve_and_transfer_from() {809 new_test_ext().execute_with(|| {810 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));811812 let origin1 = Origin::signed(1);813 let origin2 = Origin::signed(2);814815 // Create RFT 1 in 1023 pieces for account 1816 let data = default_re_fungible_data();817 create_test_item(collection_id, &data.into());818819 assert_eq!(820 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),821 1822 );823 assert_eq!(824 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),825 1023826 );827 assert_eq!(828 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),829 true830 );831832 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list833 assert_ok!(Unique::set_collection_permissions(834 origin1.clone(),835 CollectionId(1),836 CollectionPermissions {837 mint_mode: Some(true),838 access: Some(AccessMode::AllowList),839 nesting: None,840 }841 ));842 assert_ok!(Unique::add_to_allow_list(843 origin1.clone(),844 CollectionId(1),845 account(1)846 ));847 assert_ok!(Unique::add_to_allow_list(848 origin1.clone(),849 CollectionId(1),850 account(2)851 ));852 assert_ok!(Unique::add_to_allow_list(853 origin1.clone(),854 CollectionId(1),855 account(3)856 ));857858 // Account 1 approves account 2 for 1023 pieces of RFT 1859 assert_ok!(Unique::approve(860 origin1,861 account(2),862 CollectionId(1),863 TokenId(1),864 1023865 ));866 assert_eq!(867 <pallet_refungible::Allowance<Test>>::get((868 CollectionId(1),869 TokenId(1),870 account(1),871 account(2)872 )),873 1023874 );875876 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3877 assert_ok!(Unique::transfer_from(878 origin2,879 account(1),880 account(3),881 CollectionId(1),882 TokenId(1),883 100884 ));885 assert_eq!(886 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),887 1888 );889 assert_eq!(890 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),891 1892 );893 assert_eq!(894 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),895 923896 );897 assert_eq!(898 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),899 100900 );901 assert_eq!(902 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),903 true904 );905 assert_eq!(906 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),907 true908 );909 assert_eq!(910 <pallet_refungible::Allowance<Test>>::get((911 CollectionId(1),912 TokenId(1),913 account(1),914 account(2)915 )),916 923917 );918 });919}920921#[test]922fn fungible_approve_and_transfer_from() {923 new_test_ext().execute_with(|| {924 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));925926 let data = default_fungible_data();927 create_test_item(collection_id, &data.into());928929 let origin1 = Origin::signed(1);930 let origin2 = Origin::signed(2);931932 assert_ok!(Unique::set_collection_permissions(933 origin1.clone(),934 CollectionId(1),935 CollectionPermissions {936 mint_mode: Some(true),937 access: Some(AccessMode::AllowList),938 nesting: None,939 }940 ));941 assert_ok!(Unique::add_to_allow_list(942 origin1.clone(),943 CollectionId(1),944 account(1)945 ));946 assert_ok!(Unique::add_to_allow_list(947 origin1.clone(),948 CollectionId(1),949 account(2)950 ));951 assert_ok!(Unique::add_to_allow_list(952 origin1.clone(),953 CollectionId(1),954 account(3)955 ));956957 // do approve958 assert_ok!(Unique::approve(959 origin1.clone(),960 account(2),961 CollectionId(1),962 TokenId(0),963 5964 ));965 assert_eq!(966 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),967 5968 );969 assert_ok!(Unique::approve(970 origin1,971 account(3),972 CollectionId(1),973 TokenId(0),974 5975 ));976 assert_eq!(977 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),978 5979 );980 assert_eq!(981 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),982 5983 );984985 assert_ok!(Unique::transfer_from(986 origin2.clone(),987 account(1),988 account(3),989 CollectionId(1),990 TokenId(0),991 4992 ));993994 assert_eq!(995 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),996 1997 );998999 assert_noop!(1000 Unique::transfer_from(1001 origin2,1002 account(1),1003 account(3),1004 CollectionId(1),1005 TokenId(0),1006 41007 )1008 .map_err(|e| e.error),1009 CommonError::<Test>::ApprovedValueTooLow1010 );1011 });1012}10131014#[test]1015fn change_collection_owner() {1016 new_test_ext().execute_with(|| {1017 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10181019 let origin1 = Origin::signed(1);1020 assert_ok!(Unique::change_collection_owner(origin1, collection_id, 2));1021 assert_eq!(1022 <pallet_common::CollectionById<Test>>::get(collection_id)1023 .unwrap()1024 .owner,1025 21026 );1027 });1028}10291030#[test]1031fn destroy_collection() {1032 new_test_ext().execute_with(|| {1033 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10341035 let origin1 = Origin::signed(1);1036 assert_ok!(Unique::destroy_collection(origin1, collection_id));1037 });1038}10391040#[test]1041fn burn_nft_item() {1042 new_test_ext().execute_with(|| {1043 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10441045 let origin1 = Origin::signed(1);10461047 let data = default_nft_data();1048 create_test_item(collection_id, &data.into());10491050 // check balance (collection with id = 1, user id = 1)1051 assert_eq!(1052 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1053 11054 );10551056 // burn item1057 assert_ok!(Unique::burn_item(1058 origin1.clone(),1059 collection_id,1060 TokenId(1),1061 11062 ));1063 assert_eq!(1064 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1065 01066 );1067 });1068}10691070#[test]1071fn burn_same_nft_item_twice() {1072 new_test_ext().execute_with(|| {1073 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10741075 let origin1 = Origin::signed(1);10761077 let data = default_nft_data();1078 create_test_item(collection_id, &data.into());10791080 // check balance (collection with id = 1, user id = 1)1081 assert_eq!(1082 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1083 11084 );10851086 // burn item1087 assert_ok!(Unique::burn_item(1088 origin1.clone(),1089 collection_id,1090 TokenId(1),1091 11092 ));10931094 // burn item again1095 assert_noop!(1096 Unique::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1097 CommonError::<Test>::TokenNotFound1098 );10991100 assert_eq!(1101 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1102 01103 );1104 });1105}11061107#[test]1108fn burn_fungible_item() {1109 new_test_ext().execute_with(|| {1110 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11111112 let origin1 = Origin::signed(1);1113 assert_ok!(Unique::add_collection_admin(1114 origin1.clone(),1115 collection_id,1116 account(2)1117 ));11181119 let data = default_fungible_data();1120 create_test_item(collection_id, &data.into());11211122 // check balance (collection with id = 1, user id = 1)1123 assert_eq!(1124 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1125 51126 );11271128 // burn item1129 assert_ok!(Unique::burn_item(1130 origin1.clone(),1131 CollectionId(1),1132 TokenId(0),1133 51134 ));1135 assert_noop!(1136 Unique::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1137 CommonError::<Test>::TokenValueTooLow1138 );11391140 assert_eq!(1141 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1142 01143 );1144 });1145}11461147#[test]1148fn burn_fungible_item_with_token_id() {1149 new_test_ext().execute_with(|| {1150 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11511152 let origin1 = Origin::signed(1);1153 assert_ok!(Unique::add_collection_admin(1154 origin1.clone(),1155 collection_id,1156 account(2)1157 ));11581159 let data = default_fungible_data();1160 create_test_item(collection_id, &data.into());11611162 // check balance (collection with id = 1, user id = 1)1163 assert_eq!(1164 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1165 51166 );11671168 // Try to burn item using Token ID1169 assert_noop!(1170 Unique::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1171 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1172 );1173 });1174}1175#[test]1176fn burn_refungible_item() {1177 new_test_ext().execute_with(|| {1178 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1179 let origin1 = Origin::signed(1);11801181 assert_ok!(Unique::set_collection_permissions(1182 origin1.clone(),1183 collection_id,1184 CollectionPermissions {1185 mint_mode: Some(true),1186 access: Some(AccessMode::AllowList),1187 nesting: None,1188 }1189 ));1190 assert_ok!(Unique::add_to_allow_list(1191 origin1.clone(),1192 collection_id,1193 account(1)1194 ));11951196 assert_ok!(Unique::add_collection_admin(1197 origin1.clone(),1198 collection_id,1199 account(2)1200 ));12011202 let data = default_re_fungible_data();1203 create_test_item(collection_id, &data.into());12041205 // check balance (collection with id = 1, user id = 2)1206 assert_eq!(1207 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1208 11209 );1210 assert_eq!(1211 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1212 10231213 );12141215 // burn item1216 assert_ok!(Unique::burn_item(1217 origin1.clone(),1218 collection_id,1219 TokenId(1),1220 10231221 ));1222 assert_noop!(1223 Unique::burn_item(origin1, collection_id, TokenId(1), 1023).map_err(|e| e.error),1224 CommonError::<Test>::TokenValueTooLow1225 );12261227 assert_eq!(1228 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1229 01230 );1231 });1232}12331234#[test]1235fn add_collection_admin() {1236 new_test_ext().execute_with(|| {1237 let collection1_id =1238 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1239 let origin1 = Origin::signed(1);12401241 // Add collection admins1242 assert_ok!(Unique::add_collection_admin(1243 origin1.clone(),1244 collection1_id,1245 account(2)1246 ));1247 assert_ok!(Unique::add_collection_admin(1248 origin1,1249 collection1_id,1250 account(3)1251 ));12521253 // Owner is not an admin by default1254 assert_eq!(1255 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1256 false1257 );1258 assert!(<pallet_common::IsAdmin<Test>>::get((1259 CollectionId(1),1260 account(2)1261 )));1262 assert!(<pallet_common::IsAdmin<Test>>::get((1263 CollectionId(1),1264 account(3)1265 )));1266 });1267}12681269#[test]1270fn remove_collection_admin() {1271 new_test_ext().execute_with(|| {1272 let collection1_id =1273 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1274 let origin1 = Origin::signed(1);1275 let origin2 = Origin::signed(2);12761277 // Add collection admins 2 and 31278 assert_ok!(Unique::add_collection_admin(1279 origin1.clone(),1280 collection1_id,1281 account(2)1282 ));1283 assert_ok!(Unique::add_collection_admin(1284 origin1,1285 collection1_id,1286 account(3)1287 ));12881289 assert!(<pallet_common::IsAdmin<Test>>::get((1290 CollectionId(1),1291 account(2)1292 )));1293 assert!(<pallet_common::IsAdmin<Test>>::get((1294 CollectionId(1),1295 account(3)1296 )));12971298 // remove admin 31299 assert_ok!(Unique::remove_collection_admin(1300 origin2,1301 CollectionId(1),1302 account(3)1303 ));13041305 // 2 is still admin, 3 is not an admin anymore1306 assert!(<pallet_common::IsAdmin<Test>>::get((1307 CollectionId(1),1308 account(2)1309 )));1310 assert_eq!(1311 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1312 false1313 );1314 });1315}13161317#[test]1318fn balance_of() {1319 new_test_ext().execute_with(|| {1320 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1321 let fungible_collection_id =1322 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1323 let re_fungible_collection_id =1324 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));13251326 // check balance before1327 assert_eq!(1328 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1329 01330 );1331 assert_eq!(1332 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1333 01334 );1335 assert_eq!(1336 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1337 01338 );13391340 let nft_data = default_nft_data();1341 create_test_item(nft_collection_id, &nft_data.into());13421343 let fungible_data = default_fungible_data();1344 create_test_item(fungible_collection_id, &fungible_data.into());13451346 let re_fungible_data = default_re_fungible_data();1347 create_test_item(re_fungible_collection_id, &re_fungible_data.into());13481349 // check balance (collection with id = 1, user id = 1)1350 assert_eq!(1351 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1352 11353 );1354 assert_eq!(1355 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1356 51357 );1358 assert_eq!(1359 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1360 11361 );13621363 assert_eq!(1364 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1365 true1366 );1367 assert_eq!(1368 <pallet_refungible::Owned<Test>>::get((1369 re_fungible_collection_id,1370 account(1),1371 TokenId(1)1372 )),1373 true1374 );1375 });1376}13771378#[test]1379fn approve() {1380 new_test_ext().execute_with(|| {1381 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13821383 let data = default_nft_data();1384 create_test_item(collection_id, &data.into());13851386 let origin1 = Origin::signed(1);13871388 // approve1389 assert_ok!(Unique::approve(1390 origin1,1391 account(2),1392 CollectionId(1),1393 TokenId(1),1394 11395 ));1396 assert_eq!(1397 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1398 account(2)1399 );1400 });1401}14021403#[test]1404fn transfer_from() {1405 new_test_ext().execute_with(|| {1406 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1407 let origin1 = Origin::signed(1);1408 let origin2 = Origin::signed(2);14091410 let data = default_nft_data();1411 create_test_item(collection_id, &data.into());14121413 // approve1414 assert_ok!(Unique::approve(1415 origin1.clone(),1416 account(2),1417 CollectionId(1),1418 TokenId(1),1419 11420 ));1421 assert_eq!(1422 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1423 account(2)1424 );14251426 assert_ok!(Unique::set_collection_permissions(1427 origin1.clone(),1428 CollectionId(1),1429 CollectionPermissions {1430 mint_mode: Some(true),1431 access: Some(AccessMode::AllowList),1432 nesting: None,1433 }1434 ));1435 assert_ok!(Unique::add_to_allow_list(1436 origin1.clone(),1437 CollectionId(1),1438 account(1)1439 ));1440 assert_ok!(Unique::add_to_allow_list(1441 origin1.clone(),1442 CollectionId(1),1443 account(2)1444 ));1445 assert_ok!(Unique::add_to_allow_list(1446 origin1,1447 CollectionId(1),1448 account(3)1449 ));14501451 assert_ok!(Unique::transfer_from(1452 origin2,1453 account(1),1454 account(2),1455 CollectionId(1),1456 TokenId(1),1457 11458 ));14591460 // after transfer1461 assert_eq!(1462 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1463 01464 );1465 assert_eq!(1466 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1467 11468 );1469 });1470}14711472// #endregion14731474// Coverage tests region1475// #region14761477#[test]1478fn owner_can_add_address_to_allow_list() {1479 new_test_ext().execute_with(|| {1480 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14811482 let origin1 = Origin::signed(1);1483 assert_ok!(Unique::add_to_allow_list(1484 origin1,1485 collection_id,1486 account(2)1487 ));1488 assert!(<pallet_common::Allowlist<Test>>::get((1489 collection_id,1490 account(2)1491 )));1492 });1493}14941495#[test]1496fn admin_can_add_address_to_allow_list() {1497 new_test_ext().execute_with(|| {1498 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1499 let origin1 = Origin::signed(1);1500 let origin2 = Origin::signed(2);15011502 assert_ok!(Unique::add_collection_admin(1503 origin1,1504 collection_id,1505 account(2)1506 ));1507 assert_ok!(Unique::add_to_allow_list(1508 origin2,1509 collection_id,1510 account(3)1511 ));1512 assert!(<pallet_common::Allowlist<Test>>::get((1513 collection_id,1514 account(3)1515 )));1516 });1517}15181519#[test]1520fn nonprivileged_user_cannot_add_address_to_allow_list() {1521 new_test_ext().execute_with(|| {1522 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15231524 let origin2 = Origin::signed(2);1525 assert_noop!(1526 Unique::add_to_allow_list(origin2, collection_id, account(3)),1527 CommonError::<Test>::NoPermission1528 );1529 });1530}15311532#[test]1533fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1534 new_test_ext().execute_with(|| {1535 let origin1 = Origin::signed(1);15361537 assert_noop!(1538 Unique::add_to_allow_list(origin1, CollectionId(1), account(2)),1539 CommonError::<Test>::CollectionNotFound1540 );1541 });1542}15431544#[test]1545fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1546 new_test_ext().execute_with(|| {1547 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15481549 let origin1 = Origin::signed(1);1550 assert_ok!(Unique::destroy_collection(origin1.clone(), collection_id));1551 assert_noop!(1552 Unique::add_to_allow_list(origin1, collection_id, account(2)),1553 CommonError::<Test>::CollectionNotFound1554 );1555 });1556}15571558// If address is already added to allow list, nothing happens1559#[test]1560fn address_is_already_added_to_allow_list() {1561 new_test_ext().execute_with(|| {1562 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1563 let origin1 = Origin::signed(1);15641565 assert_ok!(Unique::add_to_allow_list(1566 origin1.clone(),1567 collection_id,1568 account(2)1569 ));1570 assert_ok!(Unique::add_to_allow_list(1571 origin1,1572 collection_id,1573 account(2)1574 ));1575 assert!(<pallet_common::Allowlist<Test>>::get((1576 collection_id,1577 account(2)1578 )));1579 });1580}15811582#[test]1583fn owner_can_remove_address_from_allow_list() {1584 new_test_ext().execute_with(|| {1585 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15861587 let origin1 = Origin::signed(1);1588 assert_ok!(Unique::add_to_allow_list(1589 origin1.clone(),1590 collection_id,1591 account(2)1592 ));1593 assert_ok!(Unique::remove_from_allow_list(1594 origin1,1595 collection_id,1596 account(2)1597 ));1598 assert_eq!(1599 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1600 false1601 );1602 });1603}16041605#[test]1606fn admin_can_remove_address_from_allow_list() {1607 new_test_ext().execute_with(|| {1608 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1609 let origin1 = Origin::signed(1);1610 let origin2 = Origin::signed(2);16111612 // Owner adds admin1613 assert_ok!(Unique::add_collection_admin(1614 origin1.clone(),1615 collection_id,1616 account(2)1617 ));16181619 // Owner adds address 3 to allow list1620 assert_ok!(Unique::add_to_allow_list(1621 origin1,1622 collection_id,1623 account(3)1624 ));16251626 // Admin removes address 3 from allow list1627 assert_ok!(Unique::remove_from_allow_list(1628 origin2,1629 collection_id,1630 account(3)1631 ));1632 assert_eq!(1633 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1634 false1635 );1636 });1637}16381639#[test]1640fn nonprivileged_user_cannot_remove_address_from_allow_list() {1641 new_test_ext().execute_with(|| {1642 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1643 let origin1 = Origin::signed(1);1644 let origin2 = Origin::signed(2);16451646 assert_ok!(Unique::add_to_allow_list(1647 origin1,1648 collection_id,1649 account(2)1650 ));1651 assert_noop!(1652 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1653 CommonError::<Test>::NoPermission1654 );1655 assert!(<pallet_common::Allowlist<Test>>::get((1656 collection_id,1657 account(2)1658 )));1659 });1660}16611662#[test]1663fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1664 new_test_ext().execute_with(|| {1665 let origin1 = Origin::signed(1);16661667 assert_noop!(1668 Unique::remove_from_allow_list(origin1, CollectionId(1), account(2)),1669 CommonError::<Test>::CollectionNotFound1670 );1671 });1672}16731674#[test]1675fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1676 new_test_ext().execute_with(|| {1677 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1678 let origin1 = Origin::signed(1);1679 let origin2 = Origin::signed(2);16801681 // Add account 2 to allow list1682 assert_ok!(Unique::add_to_allow_list(1683 origin1.clone(),1684 collection_id,1685 account(2)1686 ));16871688 // Account 2 is in collection allow-list1689 assert!(<pallet_common::Allowlist<Test>>::get((1690 collection_id,1691 account(2)1692 )));16931694 // Destroy collection1695 assert_ok!(Unique::destroy_collection(origin1, collection_id));16961697 // Attempt to remove account 2 from collection allow-list => error1698 assert_noop!(1699 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1700 CommonError::<Test>::CollectionNotFound1701 );17021703 // Account 2 is not found in collection allow-list anyway1704 assert_eq!(1705 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1706 false1707 );1708 });1709}17101711// If address is already removed from allow list, nothing happens1712#[test]1713fn address_is_already_removed_from_allow_list() {1714 new_test_ext().execute_with(|| {1715 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1716 let origin1 = Origin::signed(1);17171718 assert_ok!(Unique::add_to_allow_list(1719 origin1.clone(),1720 collection_id,1721 account(2)1722 ));1723 assert_ok!(Unique::remove_from_allow_list(1724 origin1.clone(),1725 collection_id,1726 account(2)1727 ));1728 assert_eq!(1729 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1730 false1731 );1732 assert_ok!(Unique::remove_from_allow_list(1733 origin1,1734 collection_id,1735 account(2)1736 ));1737 assert_eq!(1738 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1739 false1740 );1741 });1742}17431744// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1745#[test]1746fn allow_list_test_1() {1747 new_test_ext().execute_with(|| {1748 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17491750 let origin1 = Origin::signed(1);17511752 let data = default_nft_data();1753 create_test_item(collection_id, &data.into());17541755 assert_ok!(Unique::set_collection_permissions(1756 origin1.clone(),1757 collection_id,1758 CollectionPermissions {1759 mint_mode: None,1760 access: Some(AccessMode::AllowList),1761 nesting: None,1762 }1763 ));1764 assert_ok!(Unique::add_to_allow_list(1765 origin1.clone(),1766 collection_id,1767 account(2)1768 ));17691770 assert_noop!(1771 Unique::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1772 .map_err(|e| e.error),1773 CommonError::<Test>::AddressNotInAllowlist1774 );1775 });1776}17771778#[test]1779fn allow_list_test_2() {1780 new_test_ext().execute_with(|| {1781 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1782 let origin1 = Origin::signed(1);17831784 let data = default_nft_data();1785 create_test_item(collection_id, &data.into());17861787 assert_ok!(Unique::set_collection_permissions(1788 origin1.clone(),1789 collection_id,1790 CollectionPermissions {1791 mint_mode: None,1792 access: Some(AccessMode::AllowList),1793 nesting: None,1794 }1795 ));1796 assert_ok!(Unique::add_to_allow_list(1797 origin1.clone(),1798 collection_id,1799 account(1)1800 ));1801 assert_ok!(Unique::add_to_allow_list(1802 origin1.clone(),1803 collection_id,1804 account(2)1805 ));18061807 // do approve1808 assert_ok!(Unique::approve(1809 origin1.clone(),1810 account(1),1811 collection_id,1812 TokenId(1),1813 11814 ));1815 assert_eq!(1816 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1817 account(1)1818 );18191820 assert_ok!(Unique::remove_from_allow_list(1821 origin1.clone(),1822 collection_id,1823 account(1)1824 ));18251826 assert_noop!(1827 Unique::transfer_from(1828 origin1,1829 account(1),1830 account(3),1831 CollectionId(1),1832 TokenId(1),1833 11834 )1835 .map_err(|e| e.error),1836 CommonError::<Test>::AddressNotInAllowlist1837 );1838 });1839}18401841// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1842#[test]1843fn allow_list_test_3() {1844 new_test_ext().execute_with(|| {1845 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18461847 let origin1 = Origin::signed(1);18481849 let data = default_nft_data();1850 create_test_item(collection_id, &data.into());18511852 assert_ok!(Unique::set_collection_permissions(1853 origin1.clone(),1854 collection_id,1855 CollectionPermissions {1856 mint_mode: None,1857 access: Some(AccessMode::AllowList),1858 nesting: None,1859 }1860 ));1861 assert_ok!(Unique::add_to_allow_list(1862 origin1.clone(),1863 collection_id,1864 account(1)1865 ));18661867 assert_noop!(1868 Unique::transfer(origin1, account(3), collection_id, TokenId(1), 1)1869 .map_err(|e| e.error),1870 CommonError::<Test>::AddressNotInAllowlist1871 );1872 });1873}18741875#[test]1876fn allow_list_test_4() {1877 new_test_ext().execute_with(|| {1878 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18791880 let origin1 = Origin::signed(1);18811882 let data = default_nft_data();1883 create_test_item(collection_id, &data.into());18841885 assert_ok!(Unique::set_collection_permissions(1886 origin1.clone(),1887 collection_id,1888 CollectionPermissions {1889 mint_mode: None,1890 access: Some(AccessMode::AllowList),1891 nesting: None,1892 }1893 ));1894 assert_ok!(Unique::add_to_allow_list(1895 origin1.clone(),1896 collection_id,1897 account(1)1898 ));1899 assert_ok!(Unique::add_to_allow_list(1900 origin1.clone(),1901 collection_id,1902 account(2)1903 ));19041905 // do approve1906 assert_ok!(Unique::approve(1907 origin1.clone(),1908 account(1),1909 collection_id,1910 TokenId(1),1911 11912 ));1913 assert_eq!(1914 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1915 account(1)1916 );19171918 assert_ok!(Unique::remove_from_allow_list(1919 origin1.clone(),1920 collection_id,1921 account(2)1922 ));19231924 assert_noop!(1925 Unique::transfer_from(1926 origin1,1927 account(1),1928 account(3),1929 collection_id,1930 TokenId(1),1931 11932 )1933 .map_err(|e| e.error),1934 CommonError::<Test>::AddressNotInAllowlist1935 );1936 });1937}19381939// If Public Access mode is set to AllowList, tokens can’t be destroyed by a non-allowlisted address (even if it owned them before enabling AllowList mode)1940#[test]1941fn allow_list_test_5() {1942 new_test_ext().execute_with(|| {1943 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19441945 let origin1 = Origin::signed(1);19461947 let data = default_nft_data();1948 create_test_item(collection_id, &data.into());19491950 assert_ok!(Unique::set_collection_permissions(1951 origin1.clone(),1952 collection_id,1953 CollectionPermissions {1954 mint_mode: None,1955 access: Some(AccessMode::AllowList),1956 nesting: None,1957 }1958 ));1959 assert_noop!(1960 Unique::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1).map_err(|e| e.error),1961 CommonError::<Test>::AddressNotInAllowlist1962 );1963 });1964}19651966// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1967#[test]1968fn allow_list_test_6() {1969 new_test_ext().execute_with(|| {1970 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19711972 let origin1 = Origin::signed(1);19731974 let data = default_nft_data();1975 create_test_item(collection_id, &data.into());19761977 assert_ok!(Unique::set_collection_permissions(1978 origin1.clone(),1979 collection_id,1980 CollectionPermissions {1981 mint_mode: None,1982 access: Some(AccessMode::AllowList),1983 nesting: None,1984 }1985 ));19861987 // do approve1988 assert_noop!(1989 Unique::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1990 .map_err(|e| e.error),1991 CommonError::<Test>::AddressNotInAllowlist1992 );1993 });1994}19951996// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1997// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1998#[test]1999fn allow_list_test_7() {2000 new_test_ext().execute_with(|| {2001 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20022003 let data = default_nft_data();2004 create_test_item(collection_id, &data.into());20052006 let origin1 = Origin::signed(1);20072008 assert_ok!(Unique::set_collection_permissions(2009 origin1.clone(),2010 collection_id,2011 CollectionPermissions {2012 mint_mode: None,2013 access: Some(AccessMode::AllowList),2014 nesting: None,2015 }2016 ));2017 assert_ok!(Unique::add_to_allow_list(2018 origin1.clone(),2019 collection_id,2020 account(1)2021 ));2022 assert_ok!(Unique::add_to_allow_list(2023 origin1.clone(),2024 collection_id,2025 account(2)2026 ));20272028 assert_ok!(Unique::transfer(2029 origin1,2030 account(2),2031 CollectionId(1),2032 TokenId(1),2033 12034 ));2035 });2036}20372038#[test]2039fn allow_list_test_8() {2040 new_test_ext().execute_with(|| {2041 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20422043 // Create NFT for account 12044 let data = default_nft_data();2045 create_test_item(collection_id, &data.into());20462047 let origin1 = Origin::signed(1);20482049 // Toggle Allow List mode and add accounts 1 and 22050 assert_ok!(Unique::set_collection_permissions(2051 origin1.clone(),2052 collection_id,2053 CollectionPermissions {2054 mint_mode: None,2055 access: Some(AccessMode::AllowList),2056 nesting: None,2057 }2058 ));2059 assert_ok!(Unique::add_to_allow_list(2060 origin1.clone(),2061 collection_id,2062 account(1)2063 ));2064 assert_ok!(Unique::add_to_allow_list(2065 origin1.clone(),2066 collection_id,2067 account(2)2068 ));20692070 // Sself-approve account 1 for NFT 12071 assert_ok!(Unique::approve(2072 origin1.clone(),2073 account(1),2074 CollectionId(1),2075 TokenId(1),2076 12077 ));2078 assert_eq!(2079 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2080 account(1)2081 );20822083 // Transfer from 1 to 22084 assert_ok!(Unique::transfer_from(2085 origin1,2086 account(1),2087 account(2),2088 CollectionId(1),2089 TokenId(1),2090 12091 ));2092 });2093}20942095// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.2096#[test]2097fn allow_list_test_9() {2098 new_test_ext().execute_with(|| {2099 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2100 let origin1 = Origin::signed(1);21012102 assert_ok!(Unique::set_collection_permissions(2103 origin1.clone(),2104 collection_id,2105 CollectionPermissions {2106 mint_mode: Some(false),2107 access: Some(AccessMode::AllowList),2108 nesting: None,2109 }2110 ));21112112 let data = default_nft_data();2113 create_test_item(collection_id, &data.into());2114 });2115}21162117// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2118#[test]2119fn allow_list_test_10() {2120 new_test_ext().execute_with(|| {2121 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21222123 let origin1 = Origin::signed(1);2124 let origin2 = Origin::signed(2);21252126 assert_ok!(Unique::set_collection_permissions(2127 origin1.clone(),2128 collection_id,2129 CollectionPermissions {2130 mint_mode: Some(false),2131 access: Some(AccessMode::AllowList),2132 nesting: None,2133 }2134 ));21352136 assert_ok!(Unique::add_collection_admin(2137 origin1,2138 collection_id,2139 account(2)2140 ));21412142 assert_ok!(Unique::create_item(2143 origin2,2144 collection_id,2145 account(2),2146 default_nft_data().into()2147 ));2148 });2149}21502151// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and allow listed address.2152#[test]2153fn allow_list_test_11() {2154 new_test_ext().execute_with(|| {2155 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21562157 let origin1 = Origin::signed(1);2158 let origin2 = Origin::signed(2);21592160 assert_ok!(Unique::set_collection_permissions(2161 origin1.clone(),2162 collection_id,2163 CollectionPermissions {2164 mint_mode: Some(false),2165 access: Some(AccessMode::AllowList),2166 nesting: None,2167 }2168 ));2169 assert_ok!(Unique::add_to_allow_list(2170 origin1,2171 collection_id,2172 account(2)2173 ));21742175 assert_noop!(2176 Unique::create_item(2177 origin2,2178 CollectionId(1),2179 account(2),2180 default_nft_data().into()2181 )2182 .map_err(|e| e.error),2183 CommonError::<Test>::PublicMintingNotAllowed2184 );2185 });2186}21872188// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-allow listed address.2189#[test]2190fn allow_list_test_12() {2191 new_test_ext().execute_with(|| {2192 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21932194 let origin1 = Origin::signed(1);2195 let origin2 = Origin::signed(2);21962197 assert_ok!(Unique::set_collection_permissions(2198 origin1.clone(),2199 collection_id,2200 CollectionPermissions {2201 mint_mode: Some(false),2202 access: Some(AccessMode::AllowList),2203 nesting: None,2204 }2205 ));22062207 assert_noop!(2208 Unique::create_item(2209 origin2,2210 CollectionId(1),2211 account(2),2212 default_nft_data().into()2213 )2214 .map_err(|e| e.error),2215 CommonError::<Test>::PublicMintingNotAllowed2216 );2217 });2218}22192220// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2221#[test]2222fn allow_list_test_13() {2223 new_test_ext().execute_with(|| {2224 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22252226 let origin1 = Origin::signed(1);22272228 assert_ok!(Unique::set_collection_permissions(2229 origin1.clone(),2230 collection_id,2231 CollectionPermissions {2232 mint_mode: Some(true),2233 access: Some(AccessMode::AllowList),2234 nesting: None,2235 }2236 ));22372238 let data = default_nft_data();2239 create_test_item(collection_id, &data.into());2240 });2241}22422243// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2244#[test]2245fn allow_list_test_14() {2246 new_test_ext().execute_with(|| {2247 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22482249 let origin1 = Origin::signed(1);2250 let origin2 = Origin::signed(2);22512252 assert_ok!(Unique::set_collection_permissions(2253 origin1.clone(),2254 collection_id,2255 CollectionPermissions {2256 mint_mode: Some(true),2257 access: Some(AccessMode::AllowList),2258 nesting: None,2259 }2260 ));22612262 assert_ok!(Unique::add_collection_admin(2263 origin1,2264 collection_id,2265 account(2)2266 ));22672268 assert_ok!(Unique::create_item(2269 origin2,2270 collection_id,2271 account(2),2272 default_nft_data().into()2273 ));2274 });2275}22762277// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-allow listed address.2278#[test]2279fn allow_list_test_15() {2280 new_test_ext().execute_with(|| {2281 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22822283 let origin1 = Origin::signed(1);2284 let origin2 = Origin::signed(2);22852286 assert_ok!(Unique::set_collection_permissions(2287 origin1.clone(),2288 collection_id,2289 CollectionPermissions {2290 mint_mode: Some(true),2291 access: Some(AccessMode::AllowList),2292 nesting: None,2293 }2294 ));22952296 assert_noop!(2297 Unique::create_item(2298 origin2,2299 collection_id,2300 account(2),2301 default_nft_data().into()2302 )2303 .map_err(|e| e.error),2304 CommonError::<Test>::AddressNotInAllowlist2305 );2306 });2307}23082309// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by non-privileged and allow listed address.2310#[test]2311fn allow_list_test_16() {2312 new_test_ext().execute_with(|| {2313 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23142315 let origin1 = Origin::signed(1);2316 let origin2 = Origin::signed(2);23172318 assert_ok!(Unique::set_collection_permissions(2319 origin1.clone(),2320 collection_id,2321 CollectionPermissions {2322 mint_mode: Some(true),2323 access: Some(AccessMode::AllowList),2324 nesting: None,2325 }2326 ));2327 assert_ok!(Unique::add_to_allow_list(2328 origin1,2329 collection_id,2330 account(2)2331 ));23322333 assert_ok!(Unique::create_item(2334 origin2,2335 collection_id,2336 account(2),2337 default_nft_data().into()2338 ));2339 });2340}23412342// Total number of collections. Positive test2343#[test]2344fn total_number_collections_bound() {2345 new_test_ext().execute_with(|| {2346 create_test_collection(&CollectionMode::NFT, CollectionId(1));2347 });2348}23492350#[test]2351fn create_max_collections() {2352 new_test_ext().execute_with(|| {2353 for i in 1..COLLECTION_NUMBER_LIMIT {2354 create_test_collection(&CollectionMode::NFT, CollectionId(i));2355 }2356 });2357}23582359// Total number of collections. Negative test2360#[test]2361fn total_number_collections_bound_neg() {2362 new_test_ext().execute_with(|| {2363 let origin1 = Origin::signed(1);23642365 for i in 1..=COLLECTION_NUMBER_LIMIT {2366 create_test_collection(&CollectionMode::NFT, CollectionId(i));2367 }23682369 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2370 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2371 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23722373 let data: CreateCollectionData<u64> = CreateCollectionData {2374 name: col_name1.try_into().unwrap(),2375 description: col_desc1.try_into().unwrap(),2376 token_prefix: token_prefix1.try_into().unwrap(),2377 mode: CollectionMode::NFT,2378 ..Default::default()2379 };23802381 // 11-th collection in chain. Expects error2382 assert_noop!(2383 Unique::create_collection_ex(origin1, data),2384 CommonError::<Test>::TotalCollectionsLimitExceeded2385 );2386 });2387}23882389// Owned tokens by a single address. Positive test2390#[test]2391fn owned_tokens_bound() {2392 new_test_ext().execute_with(|| {2393 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23942395 let data = default_nft_data();2396 create_test_item(collection_id, &data.clone().into());2397 create_test_item(collection_id, &data.into());2398 });2399}24002401// Owned tokens by a single address. Negotive test2402#[test]2403fn owned_tokens_bound_neg() {2404 new_test_ext().execute_with(|| {2405 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24062407 let origin1 = Origin::signed(1);24082409 for _ in 1..=MAX_TOKEN_OWNERSHIP {2410 let data = default_nft_data();2411 create_test_item(collection_id, &data.clone().into());2412 }24132414 let data = default_nft_data();2415 assert_noop!(2416 Unique::create_item(origin1, CollectionId(1), account(1), data.into())2417 .map_err(|e| e.error),2418 CommonError::<Test>::AccountTokenLimitExceeded2419 );2420 });2421}24222423// Number of collection admins. Positive test2424#[test]2425fn collection_admins_bound() {2426 new_test_ext().execute_with(|| {2427 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24282429 let origin1 = Origin::signed(1);24302431 assert_ok!(Unique::add_collection_admin(2432 origin1.clone(),2433 collection_id,2434 account(2)2435 ));2436 assert_ok!(Unique::add_collection_admin(2437 origin1,2438 collection_id,2439 account(3)2440 ));2441 });2442}24432444// Number of collection admins. Negotive test2445#[test]2446fn collection_admins_bound_neg() {2447 new_test_ext().execute_with(|| {2448 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24492450 let origin1 = Origin::signed(1);24512452 for i in 0..COLLECTION_ADMINS_LIMIT {2453 assert_ok!(Unique::add_collection_admin(2454 origin1.clone(),2455 collection_id,2456 account((2 + i).into())2457 ));2458 }2459 assert_noop!(2460 Unique::add_collection_admin(2461 origin1,2462 collection_id,2463 account((3 + COLLECTION_ADMINS_LIMIT).into())2464 ),2465 CommonError::<Test>::CollectionAdminCountExceeded2466 );2467 });2468}2469// #endregion24702471#[test]2472fn collection_transfer_flag_works() {2473 new_test_ext().execute_with(|| {2474 let origin1 = Origin::signed(1);24752476 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2477 assert_ok!(Unique::set_transfers_enabled_flag(2478 origin1,2479 collection_id,2480 true2481 ));24822483 let data = default_nft_data();2484 create_test_item(collection_id, &data.into());2485 assert_eq!(2486 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2487 12488 );2489 assert_eq!(2490 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2491 true2492 );24932494 let origin1 = Origin::signed(1);24952496 // default scenario2497 assert_ok!(Unique::transfer(2498 origin1,2499 account(2),2500 collection_id,2501 TokenId(1),2502 12503 ));2504 assert_eq!(2505 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2506 false2507 );2508 assert_eq!(2509 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2510 true2511 );2512 assert_eq!(2513 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2514 02515 );2516 assert_eq!(2517 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2518 12519 );2520 });2521}25222523#[test]2524fn collection_transfer_flag_works_neg() {2525 new_test_ext().execute_with(|| {2526 let origin1 = Origin::signed(1);25272528 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2529 assert_ok!(Unique::set_transfers_enabled_flag(2530 origin1,2531 collection_id,2532 false2533 ));25342535 let data = default_nft_data();2536 create_test_item(collection_id, &data.into());2537 assert_eq!(2538 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2539 12540 );2541 assert_eq!(2542 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2543 true2544 );25452546 let origin1 = Origin::signed(1);25472548 // default scenario2549 assert_noop!(2550 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2551 .map_err(|e| e.error),2552 CommonError::<Test>::TransferNotAllowed2553 );2554 assert_eq!(2555 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2556 12557 );2558 assert_eq!(2559 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2560 02561 );2562 assert_eq!(2563 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2564 true2565 );2566 assert_eq!(2567 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2568 false2569 );2570 });2571}25722573#[test]2574fn collection_sponsoring() {2575 new_test_ext().execute_with(|| {2576 // default_limits();2577 let user1 = 1_u64;2578 let user2 = 777_u64;2579 let origin1 = Origin::signed(user1);2580 let origin2 = Origin::signed(user2);2581 let account2 = account(user2);25822583 let collection_id =2584 create_test_collection_for_owner(&CollectionMode::NFT, user1, CollectionId(1));2585 assert_ok!(Unique::set_collection_sponsor(2586 origin1.clone(),2587 collection_id,2588 user12589 ));2590 assert_ok!(Unique::confirm_sponsorship(origin1.clone(), collection_id));25912592 // Expect error while have no permissions2593 assert!(Unique::create_item(2594 origin2.clone(),2595 collection_id,2596 account2.clone(),2597 default_nft_data().into()2598 )2599 .is_err());26002601 assert_ok!(Unique::set_collection_permissions(2602 origin1.clone(),2603 collection_id,2604 CollectionPermissions {2605 mint_mode: Some(true),2606 access: Some(AccessMode::AllowList),2607 nesting: None,2608 }2609 ));2610 assert_ok!(Unique::add_to_allow_list(2611 origin1.clone(),2612 collection_id,2613 account2.clone()2614 ));26152616 assert_ok!(Unique::create_item(2617 origin2,2618 collection_id,2619 account2,2620 default_nft_data().into()2621 ));2622 });2623}