--- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] +use codec::{Decode, Encode}; /// A FRAME pallet template with necessary imports /// Feel free to remove or edit this file as needed. @@ -8,13 +9,8 @@ /// For more guidance on Substrate FRAME, see the example pallet /// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs - -use frame_support::{ - dispatch::DispatchResult, decl_module, decl_storage, decl_event, - ensure, -}; -use frame_system::{self as system, ensure_signed }; -use codec::{Encode, Decode}; +use frame_support::{decl_event, decl_module, decl_storage, dispatch::DispatchResult, ensure}; +use frame_system::{self as system, ensure_signed}; use sp_runtime::sp_std::prelude::Vec; #[cfg(test)] @@ -26,391 +22,367 @@ #[derive(Encode, Decode, Default, Clone, PartialEq)] #[cfg_attr(feature = "std", derive(Debug))] pub struct CollectionType { - pub owner: AccountId, - pub next_item_id: u64, - pub custom_data_size: u32, + pub owner: AccountId, + pub next_item_id: u64, + pub custom_data_size: u32, } #[derive(Encode, Decode, Default, Clone, PartialEq)] #[cfg_attr(feature = "std", derive(Debug))] pub struct CollectionAdminsType { - pub admin: AccountId, - pub collection_id: u64, + pub admin: AccountId, + pub collection_id: u64, } #[derive(Encode, Decode, Default, Clone, PartialEq)] #[cfg_attr(feature = "std", derive(Debug))] pub struct NftItemType { - pub collection: u64, - pub owner: AccountId, - pub data: Vec, + pub collection: u64, + pub owner: AccountId, + pub data: Vec, } /// The pallet's configuration trait. pub trait Trait: system::Trait { - // Add other types and constants required to configure this pallet. + // Add other types and constants required to configure this pallet. - /// The overarching event type. - type Event: From> + Into<::Event>; + /// The overarching event type. + type Event: From> + Into<::Event>; } // This pallet's storage items. decl_storage! { - // It is important to update your storage name so that your pallet's - // storage items are isolated from other pallets. - trait Store for Module as Nft { + // It is important to update your storage name so that your pallet's + // storage items are isolated from other pallets. + trait Store for Module as Nft { - /// Next available collection ID - pub NextCollectionID get(fn next_collection_id): u64; + /// Next available collection ID + pub NextCollectionID get(fn next_collection_id): u64; - /// Collection map - pub Collection get(collection): map hasher(identity) u64 => CollectionType; + /// Collection map + pub Collection get(collection): map hasher(identity) u64 => CollectionType; - /// Admins map (collection) - pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec; + /// Admins map (collection) + pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec; - /// Balance owner per collection map - pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64; + /// Balance owner per collection map + pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64; - pub ApprovedList get(approved): map hasher(blake2_128_concat) (u64, u64) => Vec; - pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType; - pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64; - } + pub ApprovedList get(approved): map hasher(blake2_128_concat) (u64, u64) => Vec; + pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType; + pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64; + } } // The pallet's events decl_event!( - pub enum Event where AccountId = ::AccountId { - Created(u32, AccountId), - } + pub enum Event + where + AccountId = ::AccountId, + { + Created(u32, AccountId), + } ); // The pallet's dispatchable functions. decl_module! { - /// The module declaration. - pub struct Module for enum Call where origin: T::Origin { + /// The module declaration. + pub struct Module for enum Call where origin: T::Origin { - // Initializing events - // this is needed only if you are using events in your pallet - fn deposit_event() = default; + // Initializing events + // this is needed only if you are using events in your pallet + fn deposit_event() = default; - // Initializing events - // this is needed only if you are using events in your module - // fn deposit_event() = default; + // Initializing events + // this is needed only if you are using events in your module + // fn deposit_event() = default; - // Create collection of NFT with given parameters - // - // @param customDataSz size of custom data in each collection item - // returns collection ID + // Create collection of NFT with given parameters + // + // @param customDataSz size of custom data in each collection item + // returns collection ID - // Create collection of NFT with given parameters - // - // @param customDataSz size of custom data in each collection item - // returns collection ID - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult { - // Anyone can create a collection - let who = ensure_signed(origin)?; + // Create collection of NFT with given parameters + // + // @param customDataSz size of custom data in each collection item + // returns collection ID + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult { + // Anyone can create a collection + let who = ensure_signed(origin)?; - // Generate next collection ID - let next_id = NextCollectionID::get() - .checked_add(1) - .expect("collection id error"); + // Generate next collection ID + let next_id = NextCollectionID::get() + .checked_add(1) + .expect("collection id error"); - NextCollectionID::put(next_id); + NextCollectionID::put(next_id); - // Create new collection - let new_collection = CollectionType { - owner: who, - next_item_id: next_id, - custom_data_size: custom_data_sz, - }; - - // Add new collection to map - >::insert(next_id, new_collection); + // Create new collection + let new_collection = CollectionType { + owner: who, + next_item_id: next_id, + custom_data_size: custom_data_sz, + }; - Ok(()) - } + // Add new collection to map + >::insert(next_id, new_collection); - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult { + Ok(()) + } - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult { - let owner = >::get(collection_id).owner; - ensure!(sender == owner, "You do not own this collection"); - >::remove(collection_id); + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - Ok(()) - } + let owner = >::get(collection_id).owner; + ensure!(sender == owner, "You do not own this collection"); + >::remove(collection_id); - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult { + Ok(()) + } - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult { - let mut target_collection = >::get(collection_id); - ensure!(sender == target_collection.owner, "You do not own this collection"); + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - target_collection.owner = new_owner; - >::insert(collection_id, target_collection); + let mut target_collection = >::get(collection_id); + ensure!(sender == target_collection.owner, "You do not own this collection"); - Ok(()) - } + target_collection.owner = new_owner; + >::insert(collection_id, target_collection); - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult { + Ok(()) + } - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult { - let target_collection = >::get(collection_id); - let is_owner = sender == target_collection.owner; + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - let no_perm_mes = "You do not have permissions to modify this collection"; - let exists = >::contains_key(collection_id); + let target_collection = >::get(collection_id); + let is_owner = sender == target_collection.owner; - if !is_owner - { - ensure!(exists, no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } - - let mut admin_arr: Vec = Vec::new(); - if exists - { - admin_arr = >::get(collection_id); - ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role"); - } + let no_perm_mes = "You do not have permissions to modify this collection"; + let exists = >::contains_key(collection_id); - admin_arr.push(new_admin_id); - >::insert(collection_id, admin_arr); + if !is_owner + { + ensure!(exists, no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } - Ok(()) - } + let mut admin_arr: Vec = Vec::new(); + if exists + { + admin_arr = >::get(collection_id); + ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role"); + } - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult { + admin_arr.push(new_admin_id); + >::insert(collection_id, admin_arr); - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + Ok(()) + } - let target_collection = >::get(collection_id); - let is_owner = sender == target_collection.owner; + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult { - let no_perm_mes = "You do not have permissions to modify this collection"; - let exists = >::contains_key(collection_id); + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - if !is_owner - { - ensure!(exists, no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } + let target_collection = >::get(collection_id); + let is_owner = sender == target_collection.owner; - if exists - { - let mut admin_arr = >::get(collection_id); - admin_arr.retain(|i| *i != account_id); - >::insert(collection_id, admin_arr); - } + let no_perm_mes = "You do not have permissions to modify this collection"; + let exists = >::contains_key(collection_id); - Ok(()) - } + if !is_owner + { + ensure!(exists, no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn create_item(origin, collection_id: u64, properties: Vec) -> DispatchResult { + if exists + { + let mut admin_arr = >::get(collection_id); + admin_arr.retain(|i| *i != account_id); + >::insert(collection_id, admin_arr); + } - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + Ok(()) + } - let target_collection = >::get(collection_id); - ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large"); - let is_owner = sender == target_collection.owner; - - let no_perm_mes = "You do not have permissions to modify this collection"; - let exists = >::contains_key(collection_id); - - if !is_owner - { - ensure!(exists, no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } - - let new_balance = >::get((collection_id, sender.clone())) + 1; - >::insert((collection_id, sender.clone()), new_balance); - - // Create new item - let new_item = NftItemType { - collection: collection_id, - owner: sender, - data: properties, - }; + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn create_item(origin, collection_id: u64, properties: Vec) -> DispatchResult { - let current_index = ::get(collection_id) + 1; - ::insert(collection_id, current_index); - >::insert((collection_id, current_index), new_item); + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - Ok(()) - } + let target_collection = >::get(collection_id); + ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large"); + let is_owner = sender == target_collection.owner; - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult { + let no_perm_mes = "You do not have permissions to modify this collection"; + let exists = >::contains_key(collection_id); - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + if !is_owner + { + ensure!(exists, no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } - let target_collection = >::get(collection_id); - let is_owner = sender == target_collection.owner; + let new_balance = >::get((collection_id, sender.clone())) + 1; + >::insert((collection_id, sender.clone()), new_balance); - ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); - let item = >::get((collection_id, item_id)); + // Create new item + let new_item = NftItemType { + collection: collection_id, + owner: sender, + data: properties, + }; - if !is_owner - { - // check if item owner - if item.owner != sender - { - let no_perm_mes = "You do not have permissions to modify this collection"; + let current_index = ::get(collection_id) + 1; + ::insert(collection_id, current_index); + >::insert((collection_id, current_index), new_item); - ensure!(>::contains_key(collection_id), no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } - } - >::remove((collection_id, item_id)); + Ok(()) + } - // update balance - let new_balance = >::get((collection_id, item.owner.clone())) - 1; - >::insert((collection_id, item.owner.clone()), new_balance); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult { - Ok(()) - } + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult { + let target_collection = >::get(collection_id); + let is_owner = sender == target_collection.owner; - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); + let item = >::get((collection_id, item_id)); - let target_collection = >::get(collection_id); - let is_owner = sender == target_collection.owner; + if !is_owner + { + // check if item owner + if item.owner != sender + { + let no_perm_mes = "You do not have permissions to modify this collection"; - ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); - let mut item = >::get((collection_id, item_id)); + ensure!(>::contains_key(collection_id), no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } + } + >::remove((collection_id, item_id)); - if !is_owner - { - // check if item owner - if item.owner != sender - { - let no_perm_mes = "You do not have permissions to modify this collection"; + // update balance + let new_balance = >::get((collection_id, item.owner.clone())) - 1; + >::insert((collection_id, item.owner.clone()), new_balance); - ensure!(>::contains_key(collection_id), no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } - } - >::remove((collection_id, item_id)); + Ok(()) + } - // update balance - let balance_old_owner = >::get((collection_id, item.owner.clone())) - 1; - >::insert((collection_id, item.owner.clone()), balance_old_owner); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult { - let balance_new_owner = >::get((collection_id, new_owner.clone())) + 1; - >::insert((collection_id, new_owner.clone()), balance_new_owner); + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - // change owner - item.owner = new_owner; - >::insert((collection_id, item_id), item); + let target_collection = >::get(collection_id); + let is_owner = sender == target_collection.owner; - // reset approved list - let itm: Vec = Vec::new(); - >::insert((collection_id, item_id), itm); + ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); + let mut item = >::get((collection_id, item_id)); - Ok(()) - } + if !is_owner + { + // check if item owner + if item.owner != sender + { + let no_perm_mes = "You do not have permissions to modify this collection"; - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult { + ensure!(>::contains_key(collection_id), no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } + } + >::remove((collection_id, item_id)); - let sender = ensure_signed(origin)?; - ensure!(>::contains_key(collection_id), "This collection does not exist"); + // update balance + let balance_old_owner = >::get((collection_id, item.owner.clone())) - 1; + >::insert((collection_id, item.owner.clone()), balance_old_owner); - let target_collection = >::get(collection_id); - let is_owner = sender == target_collection.owner; + let balance_new_owner = >::get((collection_id, new_owner.clone())) + 1; + >::insert((collection_id, new_owner.clone()), balance_new_owner); - ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); - let item = >::get((collection_id, item_id)); + // change owner + item.owner = new_owner; + >::insert((collection_id, item_id), item); - if !is_owner - { - // check if item owner - if item.owner != sender - { - let no_perm_mes = "You do not have permissions to modify this collection"; + // reset approved list + let itm: Vec = Vec::new(); + >::insert((collection_id, item_id), itm); - ensure!(>::contains_key(collection_id), no_perm_mes); - ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - } - } + Ok(()) + } - let list_exists = >::contains_key((collection_id, item_id)); - if list_exists { - - let mut list = >::get((collection_id, item_id)); - let item_contains = list.contains(&approved.clone()); + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult { - if !item_contains { - list.push(approved.clone()); - } - } else { - - let mut itm = Vec::new(); - itm.push(approved.clone()); - >::insert((collection_id, item_id), itm); - } + let sender = ensure_signed(origin)?; + ensure!(>::contains_key(collection_id), "This collection does not exist"); - Ok(()) - } + let target_collection = >::get(collection_id); + let is_owner = sender == target_collection.owner; - #[weight = frame_support::weights::SimpleDispatchInfo::default()] - pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult { + ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); + let item = >::get((collection_id, item_id)); - // let sender = ensure_signed(origin)?; - // ensure!(>::contains_key(collection_id), "This collection does not exist"); + if !is_owner + { + // check if item owner + if item.owner != sender + { + let no_perm_mes = "You do not have permissions to modify this collection"; - // let target_collection = >::get(collection_id); - // let is_owner = sender == target_collection.owner; + ensure!(>::contains_key(collection_id), no_perm_mes); + ensure!(>::get(collection_id).contains(&sender), no_perm_mes); + } + } - // ensure!(>::contains_key((collection_id, item_id)), "Item does not exists"); - // let mut item = >::get((collection_id, item_id)); + let list_exists = >::contains_key((collection_id, item_id)); + if list_exists { - // if !is_owner - // { - // let no_perm_mes = "You do not have permissions to modify this collection"; + let mut list = >::get((collection_id, item_id)); + let item_contains = list.contains(&approved.clone()); - // // check if item owner - // if item.owner != sender - // { - // ensure!(>::contains_key(collection_id), no_perm_mes); - // ensure!(>::get(collection_id).contains(&sender), no_perm_mes); - // } + if !item_contains { + list.push(approved.clone()); + } + } else { - // ensure!(>::contains_key((collection_id, item_id)), no_perm_mes); - // let list_itm = >::get((collection_id, item_id)); - // ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes); + let mut itm = Vec::new(); + itm.push(approved.clone()); + >::insert((collection_id, item_id), itm); + } - // } + Ok(()) + } + #[weight = frame_support::weights::SimpleDispatchInfo::default()] + pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult { - let no_perm_mes = "You do not have permissions to modify this collection"; - ensure!(>::contains_key((collection_id, item_id)), no_perm_mes); - let list_itm = >::get((collection_id, item_id)); - ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes); + let no_perm_mes = "You do not have permissions to modify this collection"; + ensure!(>::contains_key((collection_id, item_id)), no_perm_mes); + let list_itm = >::get((collection_id, item_id)); + ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes); - Self::transfer(origin, collection_id, item_id, new_owner); + Self::transfer(origin, collection_id, item_id, new_owner)?; - Ok(()) - } - } -} \ No newline at end of file + Ok(()) + } + } +} --- a/pallets/nft/src/mock.rs +++ b/pallets/nft/src/mock.rs @@ -1,15 +1,17 @@ // Creating mock runtime here use crate::{Module, Trait}; +use frame_support::{impl_outer_origin, parameter_types, weights::Weight}; +use frame_system as system; use sp_core::H256; -use frame_support::{impl_outer_origin, parameter_types, weights::Weight}; use sp_runtime::{ - traits::{BlakeTwo256, IdentityLookup}, testing::Header, Perbill, + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, + Perbill, }; -use frame_system as system; impl_outer_origin! { - pub enum Origin for Test {} + pub enum Origin for Test {} } // For testing the pallet, we construct most of a mock runtime. This means @@ -18,39 +20,42 @@ #[derive(Clone, Eq, PartialEq)] pub struct Test; parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: Weight = 1024; - pub const MaximumBlockLength: u32 = 2 * 1024; - pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); + pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: Weight = 1024; + pub const MaximumBlockLength: u32 = 2 * 1024; + pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); } impl system::Trait for Test { - type Origin = Origin; - type Call = (); - type Index = u64; - type BlockNumber = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = u64; - type Lookup = IdentityLookup; - type Header = Header; - type Event = (); - type BlockHashCount = BlockHashCount; - type MaximumBlockWeight = MaximumBlockWeight; - type MaximumBlockLength = MaximumBlockLength; - type AvailableBlockRatio = AvailableBlockRatio; - type Version = (); - type ModuleToIndex = (); - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type Origin = Origin; + type Call = (); + type Index = u64; + type BlockNumber = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = (); + type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; + type MaximumBlockLength = MaximumBlockLength; + type AvailableBlockRatio = AvailableBlockRatio; + type Version = (); + type ModuleToIndex = (); + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); } impl Trait for Test { - type Event = (); + type Event = (); } pub type TemplateModule = Module; // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> sp_io::TestExternalities { - system::GenesisConfig::default().build_storage::().unwrap().into() + system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into() } --- a/pallets/nft/src/tests.rs +++ b/pallets/nft/src/tests.rs @@ -1,6 +1,6 @@ // Tests to be written here -use crate::{ mock::*}; -use frame_support::{assert_ok, assert_noop}; +use crate::mock::*; +use frame_support::{assert_noop, assert_ok}; #[test] fn create_collection_test() { @@ -14,69 +14,83 @@ #[test] fn change_collection_owner() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); assert_ok!(TemplateModule::create_collection(origin1.clone(), size)); - assert_ok!(TemplateModule::change_collection_owner(origin1.clone(), 1, 2)); + assert_ok!(TemplateModule::change_collection_owner( + origin1.clone(), + 1, + 2 + )); assert_eq!(TemplateModule::collection(1).owner, 2); - }); + }); } #[test] fn destroy_collection() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); assert_ok!(TemplateModule::create_collection(origin1.clone(), size)); assert_ok!(TemplateModule::destroy_collection(origin1.clone(), 1)); - }); + }); } #[test] fn create_item() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); assert_ok!(TemplateModule::create_collection(origin1.clone(), size)); assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2)); - assert_ok!(TemplateModule::create_item(origin2.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin2.clone(), + 1, + [1, 1, 1].to_vec() + )); // check balance (collection with id = 1, user id = 2) assert_eq!(TemplateModule::balance_count((1, 2)), 1); - }); + }); } #[test] fn burn_item() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); assert_ok!(TemplateModule::create_collection(origin1.clone(), size)); assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2)); - assert_ok!(TemplateModule::create_item(origin2.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin2.clone(), + 1, + [1, 1, 1].to_vec() + )); // check balance (collection with id = 1, user id = 2) assert_eq!(TemplateModule::balance_count((1, 2)), 1); // burn item assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1)); - assert_noop!(TemplateModule::burn_item(origin1.clone(), 1, 1), "Item does not exists"); + assert_noop!( + TemplateModule::burn_item(origin1.clone(), 1, 1), + "Item does not exists" + ); assert_eq!(TemplateModule::balance_count((1, 1)), 0); - }); + }); } - #[test] fn add_collection_admin() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -96,12 +110,12 @@ assert_eq!(TemplateModule::admin_list_collection(1).contains(&2), true); assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), true); - }); + }); } #[test] fn remove_collection_admin() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -123,14 +137,18 @@ assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), true); // remove admin - assert_ok!(TemplateModule::remove_collection_admin(origin2.clone(), 1, 3)); + assert_ok!(TemplateModule::remove_collection_admin( + origin2.clone(), + 1, + 3 + )); assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), false); - }); + }); } #[test] fn balance_of() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -148,17 +166,21 @@ assert_eq!(TemplateModule::balance_count((1, 1)), 0); // create item - assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec())); - + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 1, 1].to_vec() + )); + // check balance (collection with id = 1, user id = 2) assert_eq!(TemplateModule::balance_count((1, 1)), 1); - assert_eq!(TemplateModule::item_id((1,1)).owner, 1); - }); + assert_eq!(TemplateModule::item_id((1, 1)).owner, 1); + }); } #[test] fn transfer() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -173,21 +195,25 @@ assert_eq!(TemplateModule::collection(3).owner, 3); // create item - assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 1, 1].to_vec() + )); // transfer assert_ok!(TemplateModule::transfer(origin1.clone(), 1, 1, 2)); - assert_eq!(TemplateModule::item_id((1,1)).owner, 2); + assert_eq!(TemplateModule::item_id((1, 1)).owner, 2); // balance_of check assert_eq!(TemplateModule::balance_count((1, 1)), 0); assert_eq!(TemplateModule::balance_count((1, 2)), 1); - }); + }); } #[test] fn approve() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -202,18 +228,21 @@ assert_eq!(TemplateModule::collection(3).owner, 3); // create item - assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 1, 1].to_vec() + )); // approve assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1)); - assert_eq!(TemplateModule::approved((1,1)).contains(&2), true); - - }); + assert_eq!(TemplateModule::approved((1, 1)).contains(&2), true); + }); } #[test] fn get_approved() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -228,18 +257,21 @@ assert_eq!(TemplateModule::collection(3).owner, 3); // create item - assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 1, 1].to_vec() + )); // approve assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1)); - assert_eq!(TemplateModule::approved((1,1)).contains(&2), true); - - }); + assert_eq!(TemplateModule::approved((1, 1)).contains(&2), true); + }); } #[test] fn transfer_from() { - new_test_ext().execute_with(|| { + new_test_ext().execute_with(|| { let size = 1024; let origin1 = Origin::signed(1); let origin2 = Origin::signed(2); @@ -254,11 +286,14 @@ assert_eq!(TemplateModule::collection(3).owner, 3); // create item - assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec())); + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 1, 1].to_vec() + )); // approve assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1)); assert_ok!(TemplateModule::transfer_from(origin1.clone(), 1, 1, 2)); - - }); -} \ No newline at end of file + }); +}