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, AccessMode, CollectionPermissions,23 PropertyKeyPermission, PropertyPermission, Property, CollectionPropertiesVec,24 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![Property {51 key: b"test-prop".to_vec().try_into().unwrap(),52 value: b"test-nft-prop".to_vec().try_into().unwrap(),53 }]54 .try_into()55 .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 =81 vec![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()90 .unwrap();91 let properties: CollectionPropertiesVec = vec![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()96 .unwrap();9798 let data: CreateCollectionData<u64> = CreateCollectionData {99 name: col_name1.try_into().unwrap(),100 description: col_desc1.try_into().unwrap(),101 token_prefix: token_prefix1.try_into().unwrap(),102 mode: mode.clone(),103 token_property_permissions: token_property_permissions.clone(),104 properties: properties.clone(),105 ..Default::default()106 };107108 let origin1 = Origin::signed(owner);109 assert_ok!(Unique::create_collection_ex(origin1, data));110111 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();112 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();113 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();114 assert_eq!(115 <pallet_common::CollectionById<Test>>::get(id)116 .unwrap()117 .owner,118 owner119 );120 assert_eq!(121 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,122 saved_col_name123 );124 assert_eq!(125 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,126 *mode127 );128 assert_eq!(129 <pallet_common::CollectionById<Test>>::get(id)130 .unwrap()131 .description,132 saved_description133 );134 assert_eq!(135 <pallet_common::CollectionById<Test>>::get(id)136 .unwrap()137 .token_prefix,138 saved_prefix139 );140 assert_eq!(141 get_collection_property_permissions(id).as_slice(),142 token_property_permissions.as_slice()143 );144 assert_eq!(145 get_collection_properties(id).as_slice(),146 properties.as_slice()147 );148 id149}150151fn get_collection_property_permissions(collection_id: CollectionId) -> Vec<PropertyKeyPermission> {152 <pallet_common::Pallet<Test>>::property_permissions(collection_id)153 .into_iter()154 .map(|(key, permission)| PropertyKeyPermission { key, permission })155 .collect()156}157158fn get_collection_properties(collection_id: CollectionId) -> Vec<Property> {159 <pallet_common::Pallet<Test>>::collection_properties(collection_id)160 .into_iter()161 .map(|(key, value)| Property { key, value })162 .collect()163}164165fn get_token_properties(collection_id: CollectionId, token_id: TokenId) -> Vec<Property> {166 <pallet_nonfungible::Pallet<Test>>::token_properties((collection_id, token_id))167 .into_iter()168 .map(|(key, value)| Property { key, value })169 .collect()170}171172fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {173 create_test_collection_for_owner(&mode, 1, id)174}175176fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {177 let origin1 = Origin::signed(1);178 assert_ok!(Unique::create_item(179 origin1,180 collection_id,181 account(1),182 data.clone()183 ));184}185186fn account(sub: u64) -> TestCrossAccountId {187 TestCrossAccountId::from_sub(sub)188}189190// Use cases tests region191// #region192193#[test]194fn check_not_sufficient_founds() {195 new_test_ext().execute_with(|| {196 let acc: u64 = 1;197 <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();198199 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();200 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();201 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();202203 let data: CreateCollectionData<<Test as frame_system::Config>::AccountId> =204 CreateCollectionData {205 name: name.try_into().unwrap(),206 description: description.try_into().unwrap(),207 token_prefix: token_prefix.try_into().unwrap(),208 mode: CollectionMode::NFT,209 ..Default::default()210 };211212 let result = Unique::create_collection_ex(Origin::signed(acc), data);213 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);214 });215}216217#[test]218fn create_fungible_collection_fails_with_large_decimal_numbers() {219 new_test_ext().execute_with(|| {220 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();221 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();222 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();223224 let data: CreateCollectionData<u64> = CreateCollectionData {225 name: col_name1.try_into().unwrap(),226 description: col_desc1.try_into().unwrap(),227 token_prefix: token_prefix1.try_into().unwrap(),228 mode: CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1),229 ..Default::default()230 };231232 let origin1 = Origin::signed(1);233 assert_noop!(234 Unique::create_collection_ex(origin1, data),235 UniqueError::<Test>::CollectionDecimalPointLimitExceeded236 );237 });238}239240#[test]241fn create_nft_item() {242 new_test_ext().execute_with(|| {243 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));244245 let data = default_nft_data();246 create_test_item(collection_id, &data.clone().into());247248 assert_eq!(249 get_token_properties(collection_id, TokenId(1)).as_slice(),250 data.properties.as_slice(),251 );252 });253}254255// Use cases tests region256// #region257#[test]258fn create_nft_multiple_items() {259 new_test_ext().execute_with(|| {260 create_test_collection(&CollectionMode::NFT, CollectionId(1));261262 let origin1 = Origin::signed(1);263264 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];265266 assert_ok!(Unique::create_multiple_items(267 origin1,268 CollectionId(1),269 account(1),270 items_data271 .clone()272 .into_iter()273 .map(|d| { d.into() })274 .collect()275 ));276 for (index, data) in items_data.into_iter().enumerate() {277 assert_eq!(278 get_token_properties(CollectionId(1), TokenId(index as u32 + 1)).as_slice(),279 data.properties.as_slice()280 );281 }282 });283}284285#[test]286fn create_refungible_item() {287 new_test_ext().execute_with(|| {288 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));289290 let data = default_re_fungible_data();291 create_test_item(collection_id, &data.clone().into());292 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));293 let balance =294 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));295 assert_eq!(item.const_data, data.const_data.into_inner());296 assert_eq!(balance, 1023);297 });298}299300#[test]301fn create_multiple_refungible_items() {302 new_test_ext().execute_with(|| {303 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));304305 let origin1 = Origin::signed(1);306307 let items_data = vec![308 default_re_fungible_data(),309 default_re_fungible_data(),310 default_re_fungible_data(),311 ];312313 assert_ok!(Unique::create_multiple_items(314 origin1,315 CollectionId(1),316 account(1),317 items_data318 .clone()319 .into_iter()320 .map(|d| { d.into() })321 .collect()322 ));323 for (index, data) in items_data.into_iter().enumerate() {324 let item = <pallet_refungible::TokenData<Test>>::get((325 CollectionId(1),326 TokenId((index + 1) as u32),327 ));328 let balance =329 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));330 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());331 assert_eq!(balance, 1023);332 }333 });334}335336#[test]337fn create_fungible_item() {338 new_test_ext().execute_with(|| {339 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));340341 let data = default_fungible_data();342 create_test_item(collection_id, &data.into());343344 assert_eq!(345 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),346 5347 );348 });349}350351//#[test]352// fn create_multiple_fungible_items() {353// new_test_ext().execute_with(|| {354// default_limits();355356// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));357358// let origin1 = Origin::signed(1);359360// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];361362// assert_ok!(Unique::create_multiple_items(363// origin1.clone(),364// 1,365// 1,366// items_data.clone().into_iter().map(|d| { d.into() }).collect()367// ));368369// for (index, _) in items_data.iter().enumerate() {370// assert_eq!(Unique::fungible_item_id(1, (index + 1) as TokenId).value, 5);371// }372// assert_eq!(Unique::balance_count(1, 1), 3000);373// assert_eq!(Unique::address_tokens(1, 1), [1, 2, 3]);374// });375// }376377#[test]378fn transfer_fungible_item() {379 new_test_ext().execute_with(|| {380 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));381382 let origin1 = Origin::signed(1);383 let origin2 = Origin::signed(2);384385 let data = default_fungible_data();386 create_test_item(collection_id, &data.into());387388 assert_eq!(389 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),390 5391 );392393 // change owner scenario394 assert_ok!(Unique::transfer(395 origin1,396 account(2),397 CollectionId(1),398 TokenId(0),399 5400 ));401 assert_eq!(402 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),403 0404 );405406 // split item scenario407 assert_ok!(Unique::transfer(408 origin2.clone(),409 account(3),410 CollectionId(1),411 TokenId(0),412 3413 ));414415 // split item and new owner has account scenario416 assert_ok!(Unique::transfer(417 origin2,418 account(3),419 CollectionId(1),420 TokenId(0),421 1422 ));423 assert_eq!(424 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),425 1426 );427 assert_eq!(428 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),429 4430 );431 });432}433434#[test]435fn transfer_refungible_item() {436 new_test_ext().execute_with(|| {437 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));438439 // Create RFT 1 in 1023 pieces for account 1440 let data = default_re_fungible_data();441 create_test_item(collection_id, &data.clone().into());442 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));443 assert_eq!(item.const_data, data.const_data.into_inner());444 assert_eq!(445 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),446 1447 );448 assert_eq!(449 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),450 1023451 );452 assert_eq!(453 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),454 true455 );456457 // Account 1 transfers all 1023 pieces of RFT 1 to account 2458 let origin1 = Origin::signed(1);459 let origin2 = Origin::signed(2);460 assert_ok!(Unique::transfer(461 origin1,462 account(2),463 CollectionId(1),464 TokenId(1),465 1023466 ));467 assert_eq!(468 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),469 1023470 );471 assert_eq!(472 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),473 0474 );475 assert_eq!(476 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),477 1478 );479 assert_eq!(480 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),481 false482 );483 assert_eq!(484 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),485 true486 );487488 // Account 2 transfers 500 pieces of RFT 1 to account 3489 assert_ok!(Unique::transfer(490 origin2.clone(),491 account(3),492 CollectionId(1),493 TokenId(1),494 500495 ));496 assert_eq!(497 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),498 523499 );500 assert_eq!(501 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),502 500503 );504 assert_eq!(505 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),506 1507 );508 assert_eq!(509 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),510 1511 );512 assert_eq!(513 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),514 true515 );516 assert_eq!(517 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),518 true519 );520521 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance522 assert_ok!(Unique::transfer(523 origin2,524 account(3),525 CollectionId(1),526 TokenId(1),527 200528 ));529 assert_eq!(530 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),531 323532 );533 assert_eq!(534 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),535 700536 );537 assert_eq!(538 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),539 1540 );541 assert_eq!(542 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),543 1544 );545 assert_eq!(546 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),547 true548 );549 assert_eq!(550 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),551 true552 );553 });554}555556#[test]557fn transfer_nft_item() {558 new_test_ext().execute_with(|| {559 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));560561 let data = default_nft_data();562 create_test_item(collection_id, &data.into());563 assert_eq!(564 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),565 1566 );567 assert_eq!(568 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),569 true570 );571572 let origin1 = Origin::signed(1);573 // default scenario574 assert_ok!(Unique::transfer(575 origin1,576 account(2),577 CollectionId(1),578 TokenId(1),579 1580 ));581 assert_eq!(582 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),583 0584 );585 assert_eq!(586 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),587 1588 );589 assert_eq!(590 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),591 false592 );593 assert_eq!(594 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),595 true596 );597 });598}599600#[test]601fn transfer_nft_item_wrong_value() {602 new_test_ext().execute_with(|| {603 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));604605 let data = default_nft_data();606 create_test_item(collection_id, &data.into());607 assert_eq!(608 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),609 1610 );611 assert_eq!(612 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),613 true614 );615616 let origin1 = Origin::signed(1);617618 assert_noop!(619 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)620 .map_err(|e| e.error),621 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount622 );623 });624}625626#[test]627fn transfer_nft_item_zero_value() {628 new_test_ext().execute_with(|| {629 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));630631 let data = default_nft_data();632 create_test_item(collection_id, &data.into());633 assert_eq!(634 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),635 1636 );637 assert_eq!(638 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),639 true640 );641642 let origin1 = Origin::signed(1);643644 // Transferring 0 amount works on NFT...645 assert_ok!(Unique::transfer(646 origin1,647 account(2),648 CollectionId(1),649 TokenId(1),650 0651 ));652 // ... and results in no transfer653 assert_eq!(654 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),655 1656 );657 assert_eq!(658 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),659 true660 );661 });662}663664#[test]665fn nft_approve_and_transfer_from() {666 new_test_ext().execute_with(|| {667 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));668669 let data = default_nft_data();670 create_test_item(collection_id, &data.into());671672 let origin1 = Origin::signed(1);673 let origin2 = Origin::signed(2);674675 assert_eq!(676 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),677 1678 );679 assert_eq!(680 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),681 true682 );683684 // neg transfer_from685 assert_noop!(686 Unique::transfer_from(687 origin2.clone(),688 account(1),689 account(2),690 CollectionId(1),691 TokenId(1),692 1693 )694 .map_err(|e| e.error),695 CommonError::<Test>::ApprovedValueTooLow696 );697698 // do approve699 assert_ok!(Unique::approve(700 origin1,701 account(2),702 CollectionId(1),703 TokenId(1),704 1705 ));706 assert_eq!(707 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),708 account(2)709 );710711 assert_ok!(Unique::transfer_from(712 origin2,713 account(1),714 account(3),715 CollectionId(1),716 TokenId(1),717 1718 ));719 assert!(720 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()721 );722 });723}724725#[test]726fn nft_approve_and_transfer_from_allow_list() {727 new_test_ext().execute_with(|| {728 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));729730 let origin1 = Origin::signed(1);731 let origin2 = Origin::signed(2);732733 // Create NFT 1 for account 1734 let data = default_nft_data();735 create_test_item(collection_id, &data.clone().into());736 assert_eq!(737 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),738 1739 );740 assert_eq!(741 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),742 true743 );744745 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list746 assert_ok!(Unique::set_collection_permissions(747 origin1.clone(),748 CollectionId(1),749 CollectionPermissions {750 mint_mode: Some(true),751 access: Some(AccessMode::AllowList),752 nesting: None,753 }754 ));755 assert_ok!(Unique::add_to_allow_list(756 origin1.clone(),757 CollectionId(1),758 account(1)759 ));760 assert_ok!(Unique::add_to_allow_list(761 origin1.clone(),762 CollectionId(1),763 account(2)764 ));765 assert_ok!(Unique::add_to_allow_list(766 origin1.clone(),767 CollectionId(1),768 account(3)769 ));770771 // Account 1 approves account 2 for NFT 1772 assert_ok!(Unique::approve(773 origin1.clone(),774 account(2),775 CollectionId(1),776 TokenId(1),777 1778 ));779 assert_eq!(780 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),781 account(2)782 );783784 // Account 2 transfers NFT 1 from account 1 to account 3785 assert_ok!(Unique::transfer_from(786 origin2,787 account(1),788 account(3),789 CollectionId(1),790 TokenId(1),791 1792 ));793 assert!(794 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()795 );796 });797}798799#[test]800fn refungible_approve_and_transfer_from() {801 new_test_ext().execute_with(|| {802 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));803804 let origin1 = Origin::signed(1);805 let origin2 = Origin::signed(2);806807 // Create RFT 1 in 1023 pieces for account 1808 let data = default_re_fungible_data();809 create_test_item(collection_id, &data.into());810811 assert_eq!(812 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),813 1814 );815 assert_eq!(816 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),817 1023818 );819 assert_eq!(820 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),821 true822 );823824 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list825 assert_ok!(Unique::set_collection_permissions(826 origin1.clone(),827 CollectionId(1),828 CollectionPermissions {829 mint_mode: Some(true),830 access: Some(AccessMode::AllowList),831 nesting: None,832 }833 ));834 assert_ok!(Unique::add_to_allow_list(835 origin1.clone(),836 CollectionId(1),837 account(1)838 ));839 assert_ok!(Unique::add_to_allow_list(840 origin1.clone(),841 CollectionId(1),842 account(2)843 ));844 assert_ok!(Unique::add_to_allow_list(845 origin1.clone(),846 CollectionId(1),847 account(3)848 ));849850 // Account 1 approves account 2 for 1023 pieces of RFT 1851 assert_ok!(Unique::approve(852 origin1,853 account(2),854 CollectionId(1),855 TokenId(1),856 1023857 ));858 assert_eq!(859 <pallet_refungible::Allowance<Test>>::get((860 CollectionId(1),861 TokenId(1),862 account(1),863 account(2)864 )),865 1023866 );867868 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3869 assert_ok!(Unique::transfer_from(870 origin2,871 account(1),872 account(3),873 CollectionId(1),874 TokenId(1),875 100876 ));877 assert_eq!(878 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),879 1880 );881 assert_eq!(882 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),883 1884 );885 assert_eq!(886 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),887 923888 );889 assert_eq!(890 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),891 100892 );893 assert_eq!(894 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),895 true896 );897 assert_eq!(898 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),899 true900 );901 assert_eq!(902 <pallet_refungible::Allowance<Test>>::get((903 CollectionId(1),904 TokenId(1),905 account(1),906 account(2)907 )),908 923909 );910 });911}912913#[test]914fn fungible_approve_and_transfer_from() {915 new_test_ext().execute_with(|| {916 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));917918 let data = default_fungible_data();919 create_test_item(collection_id, &data.into());920921 let origin1 = Origin::signed(1);922 let origin2 = Origin::signed(2);923924 assert_ok!(Unique::set_collection_permissions(925 origin1.clone(),926 CollectionId(1),927 CollectionPermissions {928 mint_mode: Some(true),929 access: Some(AccessMode::AllowList),930 nesting: None,931 }932 ));933 assert_ok!(Unique::add_to_allow_list(934 origin1.clone(),935 CollectionId(1),936 account(1)937 ));938 assert_ok!(Unique::add_to_allow_list(939 origin1.clone(),940 CollectionId(1),941 account(2)942 ));943 assert_ok!(Unique::add_to_allow_list(944 origin1.clone(),945 CollectionId(1),946 account(3)947 ));948949 // do approve950 assert_ok!(Unique::approve(951 origin1.clone(),952 account(2),953 CollectionId(1),954 TokenId(0),955 5956 ));957 assert_eq!(958 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),959 5960 );961 assert_ok!(Unique::approve(962 origin1,963 account(3),964 CollectionId(1),965 TokenId(0),966 5967 ));968 assert_eq!(969 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),970 5971 );972 assert_eq!(973 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),974 5975 );976977 assert_ok!(Unique::transfer_from(978 origin2.clone(),979 account(1),980 account(3),981 CollectionId(1),982 TokenId(0),983 4984 ));985986 assert_eq!(987 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),988 1989 );990991 assert_noop!(992 Unique::transfer_from(993 origin2,994 account(1),995 account(3),996 CollectionId(1),997 TokenId(0),998 4999 )1000 .map_err(|e| e.error),1001 CommonError::<Test>::ApprovedValueTooLow1002 );1003 });1004}10051006#[test]1007fn change_collection_owner() {1008 new_test_ext().execute_with(|| {1009 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10101011 let origin1 = Origin::signed(1);1012 assert_ok!(Unique::change_collection_owner(origin1, collection_id, 2));1013 assert_eq!(1014 <pallet_common::CollectionById<Test>>::get(collection_id)1015 .unwrap()1016 .owner,1017 21018 );1019 });1020}10211022#[test]1023fn destroy_collection() {1024 new_test_ext().execute_with(|| {1025 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10261027 let origin1 = Origin::signed(1);1028 assert_ok!(Unique::destroy_collection(origin1, collection_id));1029 });1030}10311032#[test]1033fn burn_nft_item() {1034 new_test_ext().execute_with(|| {1035 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10361037 let origin1 = Origin::signed(1);10381039 let data = default_nft_data();1040 create_test_item(collection_id, &data.into());10411042 // check balance (collection with id = 1, user id = 1)1043 assert_eq!(1044 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1045 11046 );10471048 // burn item1049 assert_ok!(Unique::burn_item(1050 origin1.clone(),1051 collection_id,1052 TokenId(1),1053 11054 ));1055 assert_eq!(1056 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1057 01058 );1059 });1060}10611062#[test]1063fn burn_same_nft_item_twice() {1064 new_test_ext().execute_with(|| {1065 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10661067 let origin1 = Origin::signed(1);10681069 let data = default_nft_data();1070 create_test_item(collection_id, &data.into());10711072 // check balance (collection with id = 1, user id = 1)1073 assert_eq!(1074 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1075 11076 );10771078 // burn item1079 assert_ok!(Unique::burn_item(1080 origin1.clone(),1081 collection_id,1082 TokenId(1),1083 11084 ));10851086 // burn item again1087 assert_noop!(1088 Unique::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1089 CommonError::<Test>::TokenNotFound1090 );10911092 assert_eq!(1093 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1094 01095 );1096 });1097}10981099#[test]1100fn burn_fungible_item() {1101 new_test_ext().execute_with(|| {1102 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11031104 let origin1 = Origin::signed(1);1105 assert_ok!(Unique::add_collection_admin(1106 origin1.clone(),1107 collection_id,1108 account(2)1109 ));11101111 let data = default_fungible_data();1112 create_test_item(collection_id, &data.into());11131114 // check balance (collection with id = 1, user id = 1)1115 assert_eq!(1116 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1117 51118 );11191120 // burn item1121 assert_ok!(Unique::burn_item(1122 origin1.clone(),1123 CollectionId(1),1124 TokenId(0),1125 51126 ));1127 assert_noop!(1128 Unique::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1129 CommonError::<Test>::TokenValueTooLow1130 );11311132 assert_eq!(1133 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1134 01135 );1136 });1137}11381139#[test]1140fn burn_fungible_item_with_token_id() {1141 new_test_ext().execute_with(|| {1142 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11431144 let origin1 = Origin::signed(1);1145 assert_ok!(Unique::add_collection_admin(1146 origin1.clone(),1147 collection_id,1148 account(2)1149 ));11501151 let data = default_fungible_data();1152 create_test_item(collection_id, &data.into());11531154 // check balance (collection with id = 1, user id = 1)1155 assert_eq!(1156 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1157 51158 );11591160 // Try to burn item using Token ID1161 assert_noop!(1162 Unique::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1163 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1164 );1165 });1166}1167#[test]1168fn burn_refungible_item() {1169 new_test_ext().execute_with(|| {1170 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1171 let origin1 = Origin::signed(1);11721173 assert_ok!(Unique::set_collection_permissions(1174 origin1.clone(),1175 collection_id,1176 CollectionPermissions {1177 mint_mode: Some(true),1178 access: Some(AccessMode::AllowList),1179 nesting: None,1180 }1181 ));1182 assert_ok!(Unique::add_to_allow_list(1183 origin1.clone(),1184 collection_id,1185 account(1)1186 ));11871188 assert_ok!(Unique::add_collection_admin(1189 origin1.clone(),1190 collection_id,1191 account(2)1192 ));11931194 let data = default_re_fungible_data();1195 create_test_item(collection_id, &data.into());11961197 // check balance (collection with id = 1, user id = 2)1198 assert_eq!(1199 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1200 11201 );1202 assert_eq!(1203 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1204 10231205 );12061207 // burn item1208 assert_ok!(Unique::burn_item(1209 origin1.clone(),1210 collection_id,1211 TokenId(1),1212 10231213 ));1214 assert_noop!(1215 Unique::burn_item(origin1, collection_id, TokenId(1), 1023).map_err(|e| e.error),1216 CommonError::<Test>::TokenValueTooLow1217 );12181219 assert_eq!(1220 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1221 01222 );1223 });1224}12251226#[test]1227fn add_collection_admin() {1228 new_test_ext().execute_with(|| {1229 let collection1_id =1230 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1231 let origin1 = Origin::signed(1);12321233 // Add collection admins1234 assert_ok!(Unique::add_collection_admin(1235 origin1.clone(),1236 collection1_id,1237 account(2)1238 ));1239 assert_ok!(Unique::add_collection_admin(1240 origin1,1241 collection1_id,1242 account(3)1243 ));12441245 // Owner is not an admin by default1246 assert_eq!(1247 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1248 false1249 );1250 assert!(<pallet_common::IsAdmin<Test>>::get((1251 CollectionId(1),1252 account(2)1253 )));1254 assert!(<pallet_common::IsAdmin<Test>>::get((1255 CollectionId(1),1256 account(3)1257 )));1258 });1259}12601261#[test]1262fn remove_collection_admin() {1263 new_test_ext().execute_with(|| {1264 let collection1_id =1265 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1266 let origin1 = Origin::signed(1);1267 let origin2 = Origin::signed(2);12681269 // Add collection admins 2 and 31270 assert_ok!(Unique::add_collection_admin(1271 origin1.clone(),1272 collection1_id,1273 account(2)1274 ));1275 assert_ok!(Unique::add_collection_admin(1276 origin1,1277 collection1_id,1278 account(3)1279 ));12801281 assert!(<pallet_common::IsAdmin<Test>>::get((1282 CollectionId(1),1283 account(2)1284 )));1285 assert!(<pallet_common::IsAdmin<Test>>::get((1286 CollectionId(1),1287 account(3)1288 )));12891290 // remove admin 31291 assert_ok!(Unique::remove_collection_admin(1292 origin2,1293 CollectionId(1),1294 account(3)1295 ));12961297 // 2 is still admin, 3 is not an admin anymore1298 assert!(<pallet_common::IsAdmin<Test>>::get((1299 CollectionId(1),1300 account(2)1301 )));1302 assert_eq!(1303 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1304 false1305 );1306 });1307}13081309#[test]1310fn balance_of() {1311 new_test_ext().execute_with(|| {1312 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1313 let fungible_collection_id =1314 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1315 let re_fungible_collection_id =1316 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));13171318 // check balance before1319 assert_eq!(1320 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1321 01322 );1323 assert_eq!(1324 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1325 01326 );1327 assert_eq!(1328 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1329 01330 );13311332 let nft_data = default_nft_data();1333 create_test_item(nft_collection_id, &nft_data.into());13341335 let fungible_data = default_fungible_data();1336 create_test_item(fungible_collection_id, &fungible_data.into());13371338 let re_fungible_data = default_re_fungible_data();1339 create_test_item(re_fungible_collection_id, &re_fungible_data.into());13401341 // check balance (collection with id = 1, user id = 1)1342 assert_eq!(1343 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1344 11345 );1346 assert_eq!(1347 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1348 51349 );1350 assert_eq!(1351 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1352 11353 );13541355 assert_eq!(1356 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1357 true1358 );1359 assert_eq!(1360 <pallet_refungible::Owned<Test>>::get((1361 re_fungible_collection_id,1362 account(1),1363 TokenId(1)1364 )),1365 true1366 );1367 });1368}13691370#[test]1371fn approve() {1372 new_test_ext().execute_with(|| {1373 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13741375 let data = default_nft_data();1376 create_test_item(collection_id, &data.into());13771378 let origin1 = Origin::signed(1);13791380 // approve1381 assert_ok!(Unique::approve(1382 origin1,1383 account(2),1384 CollectionId(1),1385 TokenId(1),1386 11387 ));1388 assert_eq!(1389 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1390 account(2)1391 );1392 });1393}13941395#[test]1396fn transfer_from() {1397 new_test_ext().execute_with(|| {1398 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1399 let origin1 = Origin::signed(1);1400 let origin2 = Origin::signed(2);14011402 let data = default_nft_data();1403 create_test_item(collection_id, &data.into());14041405 // approve1406 assert_ok!(Unique::approve(1407 origin1.clone(),1408 account(2),1409 CollectionId(1),1410 TokenId(1),1411 11412 ));1413 assert_eq!(1414 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1415 account(2)1416 );14171418 assert_ok!(Unique::set_collection_permissions(1419 origin1.clone(),1420 CollectionId(1),1421 CollectionPermissions {1422 mint_mode: Some(true),1423 access: Some(AccessMode::AllowList),1424 nesting: None,1425 }1426 ));1427 assert_ok!(Unique::add_to_allow_list(1428 origin1.clone(),1429 CollectionId(1),1430 account(1)1431 ));1432 assert_ok!(Unique::add_to_allow_list(1433 origin1.clone(),1434 CollectionId(1),1435 account(2)1436 ));1437 assert_ok!(Unique::add_to_allow_list(1438 origin1,1439 CollectionId(1),1440 account(3)1441 ));14421443 assert_ok!(Unique::transfer_from(1444 origin2,1445 account(1),1446 account(2),1447 CollectionId(1),1448 TokenId(1),1449 11450 ));14511452 // after transfer1453 assert_eq!(1454 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1455 01456 );1457 assert_eq!(1458 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1459 11460 );1461 });1462}14631464// #endregion14651466// Coverage tests region1467// #region14681469#[test]1470fn owner_can_add_address_to_allow_list() {1471 new_test_ext().execute_with(|| {1472 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14731474 let origin1 = Origin::signed(1);1475 assert_ok!(Unique::add_to_allow_list(1476 origin1,1477 collection_id,1478 account(2)1479 ));1480 assert!(<pallet_common::Allowlist<Test>>::get((1481 collection_id,1482 account(2)1483 )));1484 });1485}14861487#[test]1488fn admin_can_add_address_to_allow_list() {1489 new_test_ext().execute_with(|| {1490 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1491 let origin1 = Origin::signed(1);1492 let origin2 = Origin::signed(2);14931494 assert_ok!(Unique::add_collection_admin(1495 origin1,1496 collection_id,1497 account(2)1498 ));1499 assert_ok!(Unique::add_to_allow_list(1500 origin2,1501 collection_id,1502 account(3)1503 ));1504 assert!(<pallet_common::Allowlist<Test>>::get((1505 collection_id,1506 account(3)1507 )));1508 });1509}15101511#[test]1512fn nonprivileged_user_cannot_add_address_to_allow_list() {1513 new_test_ext().execute_with(|| {1514 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15151516 let origin2 = Origin::signed(2);1517 assert_noop!(1518 Unique::add_to_allow_list(origin2, collection_id, account(3)),1519 CommonError::<Test>::NoPermission1520 );1521 });1522}15231524#[test]1525fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1526 new_test_ext().execute_with(|| {1527 let origin1 = Origin::signed(1);15281529 assert_noop!(1530 Unique::add_to_allow_list(origin1, CollectionId(1), account(2)),1531 CommonError::<Test>::CollectionNotFound1532 );1533 });1534}15351536#[test]1537fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1538 new_test_ext().execute_with(|| {1539 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15401541 let origin1 = Origin::signed(1);1542 assert_ok!(Unique::destroy_collection(origin1.clone(), collection_id));1543 assert_noop!(1544 Unique::add_to_allow_list(origin1, collection_id, account(2)),1545 CommonError::<Test>::CollectionNotFound1546 );1547 });1548}15491550// If address is already added to allow list, nothing happens1551#[test]1552fn address_is_already_added_to_allow_list() {1553 new_test_ext().execute_with(|| {1554 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1555 let origin1 = Origin::signed(1);15561557 assert_ok!(Unique::add_to_allow_list(1558 origin1.clone(),1559 collection_id,1560 account(2)1561 ));1562 assert_ok!(Unique::add_to_allow_list(1563 origin1,1564 collection_id,1565 account(2)1566 ));1567 assert!(<pallet_common::Allowlist<Test>>::get((1568 collection_id,1569 account(2)1570 )));1571 });1572}15731574#[test]1575fn owner_can_remove_address_from_allow_list() {1576 new_test_ext().execute_with(|| {1577 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15781579 let origin1 = Origin::signed(1);1580 assert_ok!(Unique::add_to_allow_list(1581 origin1.clone(),1582 collection_id,1583 account(2)1584 ));1585 assert_ok!(Unique::remove_from_allow_list(1586 origin1,1587 collection_id,1588 account(2)1589 ));1590 assert_eq!(1591 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1592 false1593 );1594 });1595}15961597#[test]1598fn admin_can_remove_address_from_allow_list() {1599 new_test_ext().execute_with(|| {1600 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1601 let origin1 = Origin::signed(1);1602 let origin2 = Origin::signed(2);16031604 // Owner adds admin1605 assert_ok!(Unique::add_collection_admin(1606 origin1.clone(),1607 collection_id,1608 account(2)1609 ));16101611 // Owner adds address 3 to allow list1612 assert_ok!(Unique::add_to_allow_list(1613 origin1,1614 collection_id,1615 account(3)1616 ));16171618 // Admin removes address 3 from allow list1619 assert_ok!(Unique::remove_from_allow_list(1620 origin2,1621 collection_id,1622 account(3)1623 ));1624 assert_eq!(1625 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1626 false1627 );1628 });1629}16301631#[test]1632fn nonprivileged_user_cannot_remove_address_from_allow_list() {1633 new_test_ext().execute_with(|| {1634 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1635 let origin1 = Origin::signed(1);1636 let origin2 = Origin::signed(2);16371638 assert_ok!(Unique::add_to_allow_list(1639 origin1,1640 collection_id,1641 account(2)1642 ));1643 assert_noop!(1644 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1645 CommonError::<Test>::NoPermission1646 );1647 assert!(<pallet_common::Allowlist<Test>>::get((1648 collection_id,1649 account(2)1650 )));1651 });1652}16531654#[test]1655fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1656 new_test_ext().execute_with(|| {1657 let origin1 = Origin::signed(1);16581659 assert_noop!(1660 Unique::remove_from_allow_list(origin1, CollectionId(1), account(2)),1661 CommonError::<Test>::CollectionNotFound1662 );1663 });1664}16651666#[test]1667fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1668 new_test_ext().execute_with(|| {1669 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1670 let origin1 = Origin::signed(1);1671 let origin2 = Origin::signed(2);16721673 // Add account 2 to allow list1674 assert_ok!(Unique::add_to_allow_list(1675 origin1.clone(),1676 collection_id,1677 account(2)1678 ));16791680 // Account 2 is in collection allow-list1681 assert!(<pallet_common::Allowlist<Test>>::get((1682 collection_id,1683 account(2)1684 )));16851686 // Destroy collection1687 assert_ok!(Unique::destroy_collection(origin1, collection_id));16881689 // Attempt to remove account 2 from collection allow-list => error1690 assert_noop!(1691 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1692 CommonError::<Test>::CollectionNotFound1693 );16941695 // Account 2 is not found in collection allow-list anyway1696 assert_eq!(1697 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1698 false1699 );1700 });1701}17021703// If address is already removed from allow list, nothing happens1704#[test]1705fn address_is_already_removed_from_allow_list() {1706 new_test_ext().execute_with(|| {1707 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1708 let origin1 = Origin::signed(1);17091710 assert_ok!(Unique::add_to_allow_list(1711 origin1.clone(),1712 collection_id,1713 account(2)1714 ));1715 assert_ok!(Unique::remove_from_allow_list(1716 origin1.clone(),1717 collection_id,1718 account(2)1719 ));1720 assert_eq!(1721 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1722 false1723 );1724 assert_ok!(Unique::remove_from_allow_list(1725 origin1,1726 collection_id,1727 account(2)1728 ));1729 assert_eq!(1730 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1731 false1732 );1733 });1734}17351736// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1737#[test]1738fn allow_list_test_1() {1739 new_test_ext().execute_with(|| {1740 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17411742 let origin1 = Origin::signed(1);17431744 let data = default_nft_data();1745 create_test_item(collection_id, &data.into());17461747 assert_ok!(Unique::set_collection_permissions(1748 origin1.clone(),1749 collection_id,1750 CollectionPermissions {1751 mint_mode: None,1752 access: Some(AccessMode::AllowList),1753 nesting: None,1754 }1755 ));1756 assert_ok!(Unique::add_to_allow_list(1757 origin1.clone(),1758 collection_id,1759 account(2)1760 ));17611762 assert_noop!(1763 Unique::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1764 .map_err(|e| e.error),1765 CommonError::<Test>::AddressNotInAllowlist1766 );1767 });1768}17691770#[test]1771fn allow_list_test_2() {1772 new_test_ext().execute_with(|| {1773 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1774 let origin1 = Origin::signed(1);17751776 let data = default_nft_data();1777 create_test_item(collection_id, &data.into());17781779 assert_ok!(Unique::set_collection_permissions(1780 origin1.clone(),1781 collection_id,1782 CollectionPermissions {1783 mint_mode: None,1784 access: Some(AccessMode::AllowList),1785 nesting: None,1786 }1787 ));1788 assert_ok!(Unique::add_to_allow_list(1789 origin1.clone(),1790 collection_id,1791 account(1)1792 ));1793 assert_ok!(Unique::add_to_allow_list(1794 origin1.clone(),1795 collection_id,1796 account(2)1797 ));17981799 // do approve1800 assert_ok!(Unique::approve(1801 origin1.clone(),1802 account(1),1803 collection_id,1804 TokenId(1),1805 11806 ));1807 assert_eq!(1808 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1809 account(1)1810 );18111812 assert_ok!(Unique::remove_from_allow_list(1813 origin1.clone(),1814 collection_id,1815 account(1)1816 ));18171818 assert_noop!(1819 Unique::transfer_from(1820 origin1,1821 account(1),1822 account(3),1823 CollectionId(1),1824 TokenId(1),1825 11826 )1827 .map_err(|e| e.error),1828 CommonError::<Test>::AddressNotInAllowlist1829 );1830 });1831}18321833// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1834#[test]1835fn allow_list_test_3() {1836 new_test_ext().execute_with(|| {1837 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18381839 let origin1 = Origin::signed(1);18401841 let data = default_nft_data();1842 create_test_item(collection_id, &data.into());18431844 assert_ok!(Unique::set_collection_permissions(1845 origin1.clone(),1846 collection_id,1847 CollectionPermissions {1848 mint_mode: None,1849 access: Some(AccessMode::AllowList),1850 nesting: None,1851 }1852 ));1853 assert_ok!(Unique::add_to_allow_list(1854 origin1.clone(),1855 collection_id,1856 account(1)1857 ));18581859 assert_noop!(1860 Unique::transfer(origin1, account(3), collection_id, TokenId(1), 1)1861 .map_err(|e| e.error),1862 CommonError::<Test>::AddressNotInAllowlist1863 );1864 });1865}18661867#[test]1868fn allow_list_test_4() {1869 new_test_ext().execute_with(|| {1870 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18711872 let origin1 = Origin::signed(1);18731874 let data = default_nft_data();1875 create_test_item(collection_id, &data.into());18761877 assert_ok!(Unique::set_collection_permissions(1878 origin1.clone(),1879 collection_id,1880 CollectionPermissions {1881 mint_mode: None,1882 access: Some(AccessMode::AllowList),1883 nesting: None,1884 }1885 ));1886 assert_ok!(Unique::add_to_allow_list(1887 origin1.clone(),1888 collection_id,1889 account(1)1890 ));1891 assert_ok!(Unique::add_to_allow_list(1892 origin1.clone(),1893 collection_id,1894 account(2)1895 ));18961897 // do approve1898 assert_ok!(Unique::approve(1899 origin1.clone(),1900 account(1),1901 collection_id,1902 TokenId(1),1903 11904 ));1905 assert_eq!(1906 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1907 account(1)1908 );19091910 assert_ok!(Unique::remove_from_allow_list(1911 origin1.clone(),1912 collection_id,1913 account(2)1914 ));19151916 assert_noop!(1917 Unique::transfer_from(1918 origin1,1919 account(1),1920 account(3),1921 collection_id,1922 TokenId(1),1923 11924 )1925 .map_err(|e| e.error),1926 CommonError::<Test>::AddressNotInAllowlist1927 );1928 });1929}19301931// 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)1932#[test]1933fn allow_list_test_5() {1934 new_test_ext().execute_with(|| {1935 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19361937 let origin1 = Origin::signed(1);19381939 let data = default_nft_data();1940 create_test_item(collection_id, &data.into());19411942 assert_ok!(Unique::set_collection_permissions(1943 origin1.clone(),1944 collection_id,1945 CollectionPermissions {1946 mint_mode: None,1947 access: Some(AccessMode::AllowList),1948 nesting: None,1949 }1950 ));1951 assert_noop!(1952 Unique::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1).map_err(|e| e.error),1953 CommonError::<Test>::AddressNotInAllowlist1954 );1955 });1956}19571958// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1959#[test]1960fn allow_list_test_6() {1961 new_test_ext().execute_with(|| {1962 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19631964 let origin1 = Origin::signed(1);19651966 let data = default_nft_data();1967 create_test_item(collection_id, &data.into());19681969 assert_ok!(Unique::set_collection_permissions(1970 origin1.clone(),1971 collection_id,1972 CollectionPermissions {1973 mint_mode: None,1974 access: Some(AccessMode::AllowList),1975 nesting: None,1976 }1977 ));19781979 // do approve1980 assert_noop!(1981 Unique::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1982 .map_err(|e| e.error),1983 CommonError::<Test>::AddressNotInAllowlist1984 );1985 });1986}19871988// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1989// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1990#[test]1991fn allow_list_test_7() {1992 new_test_ext().execute_with(|| {1993 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19941995 let data = default_nft_data();1996 create_test_item(collection_id, &data.into());19971998 let origin1 = Origin::signed(1);19992000 assert_ok!(Unique::set_collection_permissions(2001 origin1.clone(),2002 collection_id,2003 CollectionPermissions {2004 mint_mode: None,2005 access: Some(AccessMode::AllowList),2006 nesting: None,2007 }2008 ));2009 assert_ok!(Unique::add_to_allow_list(2010 origin1.clone(),2011 collection_id,2012 account(1)2013 ));2014 assert_ok!(Unique::add_to_allow_list(2015 origin1.clone(),2016 collection_id,2017 account(2)2018 ));20192020 assert_ok!(Unique::transfer(2021 origin1,2022 account(2),2023 CollectionId(1),2024 TokenId(1),2025 12026 ));2027 });2028}20292030#[test]2031fn allow_list_test_8() {2032 new_test_ext().execute_with(|| {2033 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20342035 // Create NFT for account 12036 let data = default_nft_data();2037 create_test_item(collection_id, &data.into());20382039 let origin1 = Origin::signed(1);20402041 // Toggle Allow List mode and add accounts 1 and 22042 assert_ok!(Unique::set_collection_permissions(2043 origin1.clone(),2044 collection_id,2045 CollectionPermissions {2046 mint_mode: None,2047 access: Some(AccessMode::AllowList),2048 nesting: None,2049 }2050 ));2051 assert_ok!(Unique::add_to_allow_list(2052 origin1.clone(),2053 collection_id,2054 account(1)2055 ));2056 assert_ok!(Unique::add_to_allow_list(2057 origin1.clone(),2058 collection_id,2059 account(2)2060 ));20612062 // Sself-approve account 1 for NFT 12063 assert_ok!(Unique::approve(2064 origin1.clone(),2065 account(1),2066 CollectionId(1),2067 TokenId(1),2068 12069 ));2070 assert_eq!(2071 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2072 account(1)2073 );20742075 // Transfer from 1 to 22076 assert_ok!(Unique::transfer_from(2077 origin1,2078 account(1),2079 account(2),2080 CollectionId(1),2081 TokenId(1),2082 12083 ));2084 });2085}20862087// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.2088#[test]2089fn allow_list_test_9() {2090 new_test_ext().execute_with(|| {2091 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2092 let origin1 = Origin::signed(1);20932094 assert_ok!(Unique::set_collection_permissions(2095 origin1.clone(),2096 collection_id,2097 CollectionPermissions {2098 mint_mode: Some(false),2099 access: Some(AccessMode::AllowList),2100 nesting: None,2101 }2102 ));21032104 let data = default_nft_data();2105 create_test_item(collection_id, &data.into());2106 });2107}21082109// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2110#[test]2111fn allow_list_test_10() {2112 new_test_ext().execute_with(|| {2113 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21142115 let origin1 = Origin::signed(1);2116 let origin2 = Origin::signed(2);21172118 assert_ok!(Unique::set_collection_permissions(2119 origin1.clone(),2120 collection_id,2121 CollectionPermissions {2122 mint_mode: Some(false),2123 access: Some(AccessMode::AllowList),2124 nesting: None,2125 }2126 ));21272128 assert_ok!(Unique::add_collection_admin(2129 origin1,2130 collection_id,2131 account(2)2132 ));21332134 assert_ok!(Unique::create_item(2135 origin2,2136 collection_id,2137 account(2),2138 default_nft_data().into()2139 ));2140 });2141}21422143// 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.2144#[test]2145fn allow_list_test_11() {2146 new_test_ext().execute_with(|| {2147 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21482149 let origin1 = Origin::signed(1);2150 let origin2 = Origin::signed(2);21512152 assert_ok!(Unique::set_collection_permissions(2153 origin1.clone(),2154 collection_id,2155 CollectionPermissions {2156 mint_mode: Some(false),2157 access: Some(AccessMode::AllowList),2158 nesting: None,2159 }2160 ));2161 assert_ok!(Unique::add_to_allow_list(2162 origin1,2163 collection_id,2164 account(2)2165 ));21662167 assert_noop!(2168 Unique::create_item(2169 origin2,2170 CollectionId(1),2171 account(2),2172 default_nft_data().into()2173 )2174 .map_err(|e| e.error),2175 CommonError::<Test>::PublicMintingNotAllowed2176 );2177 });2178}21792180// 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.2181#[test]2182fn allow_list_test_12() {2183 new_test_ext().execute_with(|| {2184 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21852186 let origin1 = Origin::signed(1);2187 let origin2 = Origin::signed(2);21882189 assert_ok!(Unique::set_collection_permissions(2190 origin1.clone(),2191 collection_id,2192 CollectionPermissions {2193 mint_mode: Some(false),2194 access: Some(AccessMode::AllowList),2195 nesting: None,2196 }2197 ));21982199 assert_noop!(2200 Unique::create_item(2201 origin2,2202 CollectionId(1),2203 account(2),2204 default_nft_data().into()2205 )2206 .map_err(|e| e.error),2207 CommonError::<Test>::PublicMintingNotAllowed2208 );2209 });2210}22112212// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2213#[test]2214fn allow_list_test_13() {2215 new_test_ext().execute_with(|| {2216 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22172218 let origin1 = Origin::signed(1);22192220 assert_ok!(Unique::set_collection_permissions(2221 origin1.clone(),2222 collection_id,2223 CollectionPermissions {2224 mint_mode: Some(true),2225 access: Some(AccessMode::AllowList),2226 nesting: None,2227 }2228 ));22292230 let data = default_nft_data();2231 create_test_item(collection_id, &data.into());2232 });2233}22342235// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2236#[test]2237fn allow_list_test_14() {2238 new_test_ext().execute_with(|| {2239 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22402241 let origin1 = Origin::signed(1);2242 let origin2 = Origin::signed(2);22432244 assert_ok!(Unique::set_collection_permissions(2245 origin1.clone(),2246 collection_id,2247 CollectionPermissions {2248 mint_mode: Some(true),2249 access: Some(AccessMode::AllowList),2250 nesting: None,2251 }2252 ));22532254 assert_ok!(Unique::add_collection_admin(2255 origin1,2256 collection_id,2257 account(2)2258 ));22592260 assert_ok!(Unique::create_item(2261 origin2,2262 collection_id,2263 account(2),2264 default_nft_data().into()2265 ));2266 });2267}22682269// 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.2270#[test]2271fn allow_list_test_15() {2272 new_test_ext().execute_with(|| {2273 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22742275 let origin1 = Origin::signed(1);2276 let origin2 = Origin::signed(2);22772278 assert_ok!(Unique::set_collection_permissions(2279 origin1.clone(),2280 collection_id,2281 CollectionPermissions {2282 mint_mode: Some(true),2283 access: Some(AccessMode::AllowList),2284 nesting: None,2285 }2286 ));22872288 assert_noop!(2289 Unique::create_item(2290 origin2,2291 collection_id,2292 account(2),2293 default_nft_data().into()2294 )2295 .map_err(|e| e.error),2296 CommonError::<Test>::AddressNotInAllowlist2297 );2298 });2299}23002301// 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.2302#[test]2303fn allow_list_test_16() {2304 new_test_ext().execute_with(|| {2305 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23062307 let origin1 = Origin::signed(1);2308 let origin2 = Origin::signed(2);23092310 assert_ok!(Unique::set_collection_permissions(2311 origin1.clone(),2312 collection_id,2313 CollectionPermissions {2314 mint_mode: Some(true),2315 access: Some(AccessMode::AllowList),2316 nesting: None,2317 }2318 ));2319 assert_ok!(Unique::add_to_allow_list(2320 origin1,2321 collection_id,2322 account(2)2323 ));23242325 assert_ok!(Unique::create_item(2326 origin2,2327 collection_id,2328 account(2),2329 default_nft_data().into()2330 ));2331 });2332}23332334// Total number of collections. Positive test2335#[test]2336fn total_number_collections_bound() {2337 new_test_ext().execute_with(|| {2338 create_test_collection(&CollectionMode::NFT, CollectionId(1));2339 });2340}23412342#[test]2343fn create_max_collections() {2344 new_test_ext().execute_with(|| {2345 for i in 1..COLLECTION_NUMBER_LIMIT {2346 create_test_collection(&CollectionMode::NFT, CollectionId(i));2347 }2348 });2349}23502351// Total number of collections. Negative test2352#[test]2353fn total_number_collections_bound_neg() {2354 new_test_ext().execute_with(|| {2355 let origin1 = Origin::signed(1);23562357 for i in 1..=COLLECTION_NUMBER_LIMIT {2358 create_test_collection(&CollectionMode::NFT, CollectionId(i));2359 }23602361 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2362 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2363 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23642365 let data: CreateCollectionData<u64> = CreateCollectionData {2366 name: col_name1.try_into().unwrap(),2367 description: col_desc1.try_into().unwrap(),2368 token_prefix: token_prefix1.try_into().unwrap(),2369 mode: CollectionMode::NFT,2370 ..Default::default()2371 };23722373 // 11-th collection in chain. Expects error2374 assert_noop!(2375 Unique::create_collection_ex(origin1, data),2376 CommonError::<Test>::TotalCollectionsLimitExceeded2377 );2378 });2379}23802381// Owned tokens by a single address. Positive test2382#[test]2383fn owned_tokens_bound() {2384 new_test_ext().execute_with(|| {2385 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23862387 let data = default_nft_data();2388 create_test_item(collection_id, &data.clone().into());2389 create_test_item(collection_id, &data.into());2390 });2391}23922393// Owned tokens by a single address. Negotive test2394#[test]2395fn owned_tokens_bound_neg() {2396 new_test_ext().execute_with(|| {2397 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23982399 let origin1 = Origin::signed(1);24002401 for _ in 1..=MAX_TOKEN_OWNERSHIP {2402 let data = default_nft_data();2403 create_test_item(collection_id, &data.clone().into());2404 }24052406 let data = default_nft_data();2407 assert_noop!(2408 Unique::create_item(origin1, CollectionId(1), account(1), data.into())2409 .map_err(|e| e.error),2410 CommonError::<Test>::AccountTokenLimitExceeded2411 );2412 });2413}24142415// Number of collection admins. Positive test2416#[test]2417fn collection_admins_bound() {2418 new_test_ext().execute_with(|| {2419 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24202421 let origin1 = Origin::signed(1);24222423 assert_ok!(Unique::add_collection_admin(2424 origin1.clone(),2425 collection_id,2426 account(2)2427 ));2428 assert_ok!(Unique::add_collection_admin(2429 origin1,2430 collection_id,2431 account(3)2432 ));2433 });2434}24352436// Number of collection admins. Negotive test2437#[test]2438fn collection_admins_bound_neg() {2439 new_test_ext().execute_with(|| {2440 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24412442 let origin1 = Origin::signed(1);24432444 for i in 0..COLLECTION_ADMINS_LIMIT {2445 assert_ok!(Unique::add_collection_admin(2446 origin1.clone(),2447 collection_id,2448 account((2 + i).into())2449 ));2450 }2451 assert_noop!(2452 Unique::add_collection_admin(2453 origin1,2454 collection_id,2455 account((3 + COLLECTION_ADMINS_LIMIT).into())2456 ),2457 CommonError::<Test>::CollectionAdminCountExceeded2458 );2459 });2460}2461// #endregion24622463#[test]2464fn collection_transfer_flag_works() {2465 new_test_ext().execute_with(|| {2466 let origin1 = Origin::signed(1);24672468 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2469 assert_ok!(Unique::set_transfers_enabled_flag(2470 origin1,2471 collection_id,2472 true2473 ));24742475 let data = default_nft_data();2476 create_test_item(collection_id, &data.into());2477 assert_eq!(2478 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2479 12480 );2481 assert_eq!(2482 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2483 true2484 );24852486 let origin1 = Origin::signed(1);24872488 // default scenario2489 assert_ok!(Unique::transfer(2490 origin1,2491 account(2),2492 collection_id,2493 TokenId(1),2494 12495 ));2496 assert_eq!(2497 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2498 false2499 );2500 assert_eq!(2501 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2502 true2503 );2504 assert_eq!(2505 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2506 02507 );2508 assert_eq!(2509 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2510 12511 );2512 });2513}25142515#[test]2516fn collection_transfer_flag_works_neg() {2517 new_test_ext().execute_with(|| {2518 let origin1 = Origin::signed(1);25192520 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2521 assert_ok!(Unique::set_transfers_enabled_flag(2522 origin1,2523 collection_id,2524 false2525 ));25262527 let data = default_nft_data();2528 create_test_item(collection_id, &data.into());2529 assert_eq!(2530 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2531 12532 );2533 assert_eq!(2534 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2535 true2536 );25372538 let origin1 = Origin::signed(1);25392540 // default scenario2541 assert_noop!(2542 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2543 .map_err(|e| e.error),2544 CommonError::<Test>::TransferNotAllowed2545 );2546 assert_eq!(2547 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2548 12549 );2550 assert_eq!(2551 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2552 02553 );2554 assert_eq!(2555 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2556 true2557 );2558 assert_eq!(2559 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2560 false2561 );2562 });2563}25642565#[test]2566fn collection_sponsoring() {2567 new_test_ext().execute_with(|| {2568 // default_limits();2569 let user1 = 1_u64;2570 let user2 = 777_u64;2571 let origin1 = Origin::signed(user1);2572 let origin2 = Origin::signed(user2);2573 let account2 = account(user2);25742575 let collection_id =2576 create_test_collection_for_owner(&CollectionMode::NFT, user1, CollectionId(1));2577 assert_ok!(Unique::set_collection_sponsor(2578 origin1.clone(),2579 collection_id,2580 user12581 ));2582 assert_ok!(Unique::confirm_sponsorship(origin1.clone(), collection_id));25832584 // Expect error while have no permissions2585 assert!(Unique::create_item(2586 origin2.clone(),2587 collection_id,2588 account2.clone(),2589 default_nft_data().into()2590 )2591 .is_err());25922593 assert_ok!(Unique::set_collection_permissions(2594 origin1.clone(),2595 collection_id,2596 CollectionPermissions {2597 mint_mode: Some(true),2598 access: Some(AccessMode::AllowList),2599 nesting: None,2600 }2601 ));2602 assert_ok!(Unique::add_to_allow_list(2603 origin1.clone(),2604 collection_id,2605 account2.clone()2606 ));26072608 assert_ok!(Unique::create_item(2609 origin2,2610 collection_id,2611 account2,2612 default_nft_data().into()2613 ));2614 });2615}